Skip to content

Commit

Permalink
refactor(api): rework flows for feasibility PDF and dematerialized
Browse files Browse the repository at this point in the history
- Reworked the logic for handling feasibility PDF and dematerialized flows to ensure data integrity.
- Updated the create and update operations to occur at the appropriate moments in the process.
- Ensured that existing data is preserved and correctly associated with the feasibility records.
  • Loading branch information
ThomasDos committed Aug 15, 2024
1 parent 220364f commit 863266a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FeasibilityFormat } from "@prisma/client";
import { prismaClient } from "../../../../prisma/client";
import {
CandidacyAuditLogUserInfo,
Expand Down Expand Up @@ -26,18 +27,14 @@ export const confirmTrainingFormByCandidate = async ({
);
}

const candidacy = await updateCandidacyStatus({
candidacyId,
status: "PARCOURS_CONFIRME",
});

const candidacyCertification = await getCertificationByCandidacyId({
candidacyId,
});

const feasibilityFormat = candidacyCertification?.feasibilityFormat;
const feasibilityFormat =
candidacyCertification?.feasibilityFormat as FeasibilityFormat;

await prismaClient.$transaction([
const [_, __, feasibility] = await prismaClient.$transaction([
prismaClient.candidacy.update({
where: {
id: candidacyId,
Expand All @@ -64,6 +61,24 @@ export const confirmTrainingFormByCandidate = async ({
}),
]);

if (feasibilityFormat === "DEMATERIALIZED") {
await prismaClient.feasibility.update({
where: {
id: feasibility.id,
},
data: {
decision: "PENDING",
dematerializedFeasibilityFile: {
create: {},
},
},
});
}

const candidacy = await updateCandidacyStatus({
candidacyId,
status: "PARCOURS_CONFIRME",
});
await logCandidacyAuditEvent({
candidacyId,
eventType: "TRAINING_FORM_CONFIRMED",
Expand Down
134 changes: 76 additions & 58 deletions packages/reva-api/modules/feasibility/feasibility.features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ export const createFeasibility = async ({
where: {
candidacyId,
isActive: true,
feasibilityUploadedPdf: { isNot: null },
feasibilityFormat: "UPLOADED_PDF",
},
});

const isFeasibilityEditableDecision = ["INCOMPLETE", "DRAFT"];

if (
existingFeasibilityUploadedPdf &&
existingFeasibilityUploadedPdf.decision !== "INCOMPLETE"
!isFeasibilityEditableDecision.includes(
existingFeasibilityUploadedPdf.decision,
)
) {
throw new Error(
"Un dossier de faisabilité actif existe déjà pour cette candidature",
Expand Down Expand Up @@ -166,68 +170,82 @@ export const createFeasibility = async ({
);
}

if (existingFeasibilityUploadedPdf) {
await prismaClient.feasibility.update({
where: { id: existingFeasibilityUploadedPdf.id },
data: { isActive: false },
});
}
const feasibilityUploadedPdfData = {
feasibilityFile: {
create: {
id: feasibilityFileInstance.id,
mimeType: feasibilityFile.mimetype,
name: feasibilityFile.filename,
path: `${candidacyId}/${feasibilityFileInstance.id}`,
},
},
IDFile: {
create: {
id: IDFileInstance.id,
mimeType: IDFile.mimetype,
name: IDFile.filename,
path: `${candidacyId}/${IDFileInstance.id}`,
},
},
documentaryProofFile: documentaryProofFile
? {
create: {
id: documentaryProofFileInstance?.id,
mimeType: documentaryProofFile.mimetype,
name: documentaryProofFile.filename,
path: `${candidacyId}/${documentaryProofFileInstance?.id}`,
},
}
: undefined,
certificateOfAttendanceFile: certificateOfAttendanceFile
? {
create: {
id: certificateOfAttendanceFileInstance?.id,
mimeType: certificateOfAttendanceFile.mimetype,
name: certificateOfAttendanceFile.filename,
path: `${candidacyId}/${certificateOfAttendanceFileInstance?.id}`,
},
}
: undefined,
} as Prisma.FeasibilityUploadedPdfCreateInput;

const feasibilityData = {
decision: "PENDING",
isActive: true,
candidacy: { connect: { id: candidacyId } },
certificationAuthority: { connect: { id: certificationAuthorityId } },
feasibilityFileSentAt: new Date(),
feasibilityFormat: "UPLOADED_PDF",
} as Prisma.FeasibilityCreateInput;

let feasibility;

await prismaClient.feasibility.updateMany({
where: { candidacyId },
data: { isActive: false },
});

const [_, feasibility] = await prismaClient.$transaction([
prismaClient.feasibility.updateMany({
where: { candidacyId },
data: { isActive: false },
}),
prismaClient.feasibility.create({
if (existingFeasibilityUploadedPdf?.decision === "DRAFT") {
feasibility = await prismaClient.feasibility.update({
where: { id: existingFeasibilityUploadedPdf.id },
data: {
decision: "PENDING",
candidacy: { connect: { id: candidacyId } },
certificationAuthority: { connect: { id: certificationAuthorityId } },
feasibilityFileSentAt: new Date(),
feasibilityFormat: "UPLOADED_PDF",
...feasibilityData,
feasibilityUploadedPdf: {
create: {
feasibilityFile: {
create: {
id: feasibilityFileInstance.id,
mimeType: feasibilityFile.mimetype,
name: feasibilityFile.filename,
path: `${candidacyId}/${feasibilityFileInstance.id}`,
},
},
IDFile: {
create: {
id: IDFileInstance.id,
mimeType: IDFile.mimetype,
name: IDFile.filename,
path: `${candidacyId}/${IDFileInstance.id}`,
},
},
documentaryProofFile: documentaryProofFile
? {
create: {
id: documentaryProofFileInstance?.id,
mimeType: documentaryProofFile.mimetype,
name: documentaryProofFile.filename,
path: `${candidacyId}/${documentaryProofFileInstance?.id}`,
},
}
: undefined,
certificateOfAttendanceFile: certificateOfAttendanceFile
? {
create: {
id: certificateOfAttendanceFileInstance?.id,
mimeType: certificateOfAttendanceFile.mimetype,
name: certificateOfAttendanceFile.filename,
path: `${candidacyId}/${certificateOfAttendanceFileInstance?.id}`,
},
}
: undefined,
upsert: {
create: feasibilityUploadedPdfData,
update: feasibilityUploadedPdfData,
},
},
},
}),
]);
});
} else {
feasibility = await prismaClient.feasibility.create({
data: {
...feasibilityData,
feasibilityUploadedPdf: { create: feasibilityUploadedPdfData },
},
});
}

await updateCandidacyStatus({
candidacyId,
Expand Down

0 comments on commit 863266a

Please sign in to comment.