Skip to content

Commit

Permalink
Merge pull request #218 from IABTechLab/jyg-Fix-participant-types-get…
Browse files Browse the repository at this point in the history
…-deleted-issue

Fix participant types get deleted issue
  • Loading branch information
jingyi-gao-ttd authored Oct 24, 2023
2 parents e9d17d3 + 4dfe702 commit bbeae2f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/api/routers/participantsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ParticipantRequest,
sendNewParticipantEmail,
sendParticipantApprovedEmail,
updateParticipantAndTypes,
UpdateSharingTypes,
} from '../services/participantsService';
import {
Expand Down Expand Up @@ -133,15 +134,7 @@ export function createParticipantsRouter() {
assignClientRoleToUser(kcAdminClient, user.email, 'api-participant-member')
)
);
await Participant.query().upsertGraph(
{
id: participant!.id!,
...data,
},
{
relate: true,
}
);
await updateParticipantAndTypes(participant!, data);
await sendParticipantApprovedEmail(users);
await updateAuditTrailToProceed(auditTrail.id);
return res.sendStatus(200);
Expand Down
31 changes: 31 additions & 0 deletions src/api/services/participantsService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { NextFunction, Request, Response } from 'express';
import { TransactionOrKnex } from 'objection';
import { z } from 'zod';

import {
Participant,
ParticipantApprovalPartial,
ParticipantCreationPartial,
ParticipantDTO,
ParticipantStatus,
Expand Down Expand Up @@ -104,6 +106,35 @@ export const deleteSharingParticipants = async (
);
};

const updateParticipantAssociatedRequestTypes = async (
participant: Participant,
participantApprovalPartial: z.infer<typeof ParticipantApprovalPartial>,
trx: TransactionOrKnex
) => {
const approvedTypeMappings = participantApprovalPartial.types.map((pt) => ({
participantId: participant.id,
participantTypeId: pt.id,
}));
await participant.$relatedQuery('types', trx).unrelate();
await trx('participantsToTypes').insert(approvedTypeMappings);
};

export const updateParticipantAndTypes = async (
participant: Participant,
participantApprovalPartial: z.infer<typeof ParticipantApprovalPartial> & {
status: ParticipantStatus;
}
) => {
const trx = await Participant.startTransaction();
await participant.$query(trx).patch({
name: participantApprovalPartial.name,
siteId: participantApprovalPartial.siteId,
status: participantApprovalPartial.status,
});
await updateParticipantAssociatedRequestTypes(participant, participantApprovalPartial, trx);
await trx.commit();
};

export const UpdateSharingTypes = async (
participantSiteId: number,
types: ClientType[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('participantsToTypes', (table) => {
table.dropForeign('participantTypeId');
table.foreign('participantTypeId').references('participantTypes.id').onDelete('NO ACTION');
});

await knex.schema.alterTable('approvers', (table) => {
table.dropForeign('participantTypeId');
table.foreign('participantTypeId').references('participantTypes.id').onDelete('NO ACTION');
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('participantsToTypes', (table) => {
table.dropForeign('participantTypeId');
table.foreign('participantTypeId').references('participantTypes.id').onDelete('CASCADE');
});

await knex.schema.alterTable('approvers', (table) => {
table.dropForeign('participantTypeId');
table.foreign('participantTypeId').references('participantTypes.id').onDelete('CASCADE');
});
}

0 comments on commit bbeae2f

Please sign in to comment.