Skip to content

Commit 219d64b

Browse files
authored
fix: allow empty affiliations array in PATCH project affiliations (#3946)
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
1 parent e76f62c commit 219d64b

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

backend/src/api/public/v1/members/project-affiliations/patchProjectAffiliation.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const paramsSchema = z.object({
2626
projectId: z.uuid(),
2727
})
2828

29-
const bodySchema = z.object({
30-
affiliations: z
31-
.array(
29+
const bodySchema = z
30+
.object({
31+
affiliations: z.array(
3232
z
3333
.object({
3434
organizationId: z.uuid(),
@@ -38,10 +38,13 @@ const bodySchema = z.object({
3838
.refine((a) => a.dateEnd == null || a.dateEnd >= a.dateStart, {
3939
message: 'dateEnd must be greater than or equal to dateStart',
4040
}),
41-
)
42-
.min(1),
43-
verifiedBy: z.string().max(255),
44-
})
41+
),
42+
verifiedBy: z.string().max(255).optional(),
43+
})
44+
.refine((b) => b.affiliations.length === 0 || b.verifiedBy != null, {
45+
message: 'verifiedBy is required when affiliations is non-empty',
46+
path: ['verifiedBy'],
47+
})
4548

4649
export async function patchProjectAffiliation(req: Request, res: Response): Promise<void> {
4750
const { memberId, projectId } = validateOrThrow(paramsSchema, req.params)
@@ -75,17 +78,19 @@ export async function patchProjectAffiliation(req: Request, res: Response): Prom
7578
await qx.tx(async (tx) => {
7679
await deleteAllMemberSegmentAffiliationsForProject(tx, memberId, projectId)
7780

78-
await insertMemberSegmentAffiliations(
79-
tx,
80-
memberId,
81-
projectId,
82-
affiliations.map((a) => ({
83-
organizationId: a.organizationId,
84-
dateStart: a.dateStart.toISOString(),
85-
dateEnd: a.dateEnd?.toISOString() ?? null,
86-
verifiedBy,
87-
})),
88-
)
81+
if (affiliations.length > 0) {
82+
await insertMemberSegmentAffiliations(
83+
tx,
84+
memberId,
85+
projectId,
86+
affiliations.map((a) => ({
87+
organizationId: a.organizationId,
88+
dateStart: a.dateStart.toISOString(),
89+
dateEnd: a.dateEnd?.toISOString() ?? null,
90+
verifiedBy: verifiedBy!,
91+
})),
92+
)
93+
}
8994

9095
const oldOrgIds = existingAffiliations.map((a) => a.organizationId)
9196
const newOrgIds = affiliations.map((a) => a.organizationId)

0 commit comments

Comments
 (0)