Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(signature-collection): Admin create lists fix #16491

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions libs/clients/signature-collection/src/clientConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,41 @@
}
}
},
"/Admin/Medmaelalisti": {
"post": {
"tags": ["Admin"],
"summary": "Bætir við nýjum meðmælalistum fyrir þegar stofnað framboð",
"requestBody": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MedmaelalistarRequestDTO"
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": { "$ref": "#/components/schemas/MedmaelalistiDTO" }
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
},
"/Admin/Medmaelalisti/{ID}": {
"delete": {
"tags": ["Admin"],
Expand Down Expand Up @@ -928,7 +963,9 @@
},
"post": {
"tags": ["Medmaelalistar"],
"summary": "Bætir við nýjum meðmælalistum fyrir þegar stofnað framboð",
"requestBody": {
"description": "",
"content": {
"application/json": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,51 @@ export class SignatureCollectionAdminClientService {
throw new Error('Collection id input wrong')
}

const candidates = await this.getApiWithAuth(
this.candidateApi,
auth,
).frambodGet({
sofnunID: parseInt(collectionId),
})
juni-haukur marked this conversation as resolved.
Show resolved Hide resolved

const adminApi = await this.getApiWithAuth(this.adminApi, auth)

const filteredAreas = areas
? collectionAreas.filter((area) =>
areas.flatMap((a) => a.areaId).includes(area.id),
)
: collectionAreas

const candidacy = await this.getApiWithAuth(
this.adminApi,
auth,
).adminFrambodPost({
frambodRequestDTO: {
sofnunID: parseInt(id),
kennitala: owner.nationalId,
simi: owner.phone,
netfang: owner.email,
medmaelalistar: filteredAreas.map((area) => ({
svaediID: parseInt(area.id),
listiNafn: `${owner.name} - ${area.name}`,
})),
},
})
let candidacy = candidates.find((c) => c.kennitala === owner.nationalId)

// If no candidacy exists, create one
if (!candidacy) {
candidacy = await adminApi.adminFrambodPost({
frambodRequestDTO: {
sofnunID: parseInt(id),
kennitala: owner.nationalId,
simi: owner.phone,
netfang: owner.email,
medmaelalistar: filteredAreas.map((area) => ({
svaediID: parseInt(area.id),
listiNafn: `${owner.name} - ${area.name}`,
})),
},
})
juni-haukur marked this conversation as resolved.
Show resolved Hide resolved
}
// Candidacy exists, add area
else {
await adminApi.adminMedmaelalistiPost({
medmaelalistarRequestDTO: {
frambodID: candidacy.id,
medmaelalistar: filteredAreas.map((area) => ({
svaediID: parseInt(area.id),
listiNafn: `${owner.name} - ${area.name}`,
})),
},
})
juni-haukur marked this conversation as resolved.
Show resolved Hide resolved
}

return {
slug: getSlug(
candidacy.id ?? '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const Constituency = ({
(list) => list.area.name === constituencyName,
)

const areaId = collection.areas.find((a) => a.name === constituencyName)?.id

const countCandidatesLists = (allLists: SignatureCollectionList[]) => {
return allLists?.reduce((acc: any, list: SignatureCollectionList) => {
acc[list.candidate.id] = (acc[list.candidate.id] || 0) + 1
Expand Down Expand Up @@ -126,7 +128,10 @@ export const Constituency = ({
constituencyLists.length}
</Text>
{allowedToProcess && constituencyLists?.length > 0 && (
<CreateCollection collectionId={collection?.id} />
<CreateCollection
collectionId={collection?.id}
areaId={areaId}
/>
)}
</Box>
<Stack space={3}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ const Lists = ({ allowedToProcess }: { allowedToProcess: boolean }) => {
{lists?.length > 0 &&
allowedToProcess &&
collectionStatus === CollectionStatus.InInitialReview && (
<CreateCollection collectionId={collection?.id} />
<CreateCollection
collectionId={collection?.id}
areaId={undefined}
/>
)}
</Box>
</GridColumn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ import { useCandidateLookupLazyQuery } from './candidateLookup.generated'
import { setReason } from './utils'
import { useCreateCollectionMutation } from './createCollection.generated'
import { m } from '../../lib/messages'
import { useParams, useRevalidator } from 'react-router-dom'
juni-haukur marked this conversation as resolved.
Show resolved Hide resolved

const CreateCollection = ({ collectionId }: { collectionId: string }) => {
const CreateCollection = ({
collectionId,
areaId,
}: {
collectionId: string
areaId: string | undefined
}) => {
juni-haukur marked this conversation as resolved.
Show resolved Hide resolved
const { formatMessage } = useLocale()
const { control } = useForm()
const { revalidate } = useRevalidator()
const { constituencyName } = useParams() as {
constituencyName: string | undefined
}

const [modalIsOpen, setModalIsOpen] = useState(false)
const [nationalIdInput, setNationalIdInput] = useState('')
Expand All @@ -42,8 +53,12 @@ const CreateCollection = ({ collectionId }: { collectionId: string }) => {
phone: '',
email: '',
},
areas: areaId ? [{ areaId }] : null,
},
},
onCompleted: () => {
revalidate()
},
})

const createNewCollection = async () => {
Expand Down Expand Up @@ -154,6 +169,14 @@ const CreateCollection = ({ collectionId }: { collectionId: string }) => {
readOnly
value={name}
/>
{areaId && (
<Input
name="candidateArea"
label={formatMessage(m.signatureListsConstituencyTitle)}
readOnly
value={constituencyName}
/>
)}
</Stack>
{!canCreate && (
<Box marginTop={3}>
Expand Down