Skip to content

Commit

Permalink
Validate when grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
Riron committed Jun 4, 2024
1 parent cf90dd2 commit 41f1f25
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2368,74 +2368,5 @@ describe("Mutation.createForm", () => {
})
]);
});

it("should not allow to create a APPENDIX1_PRODUCER form with an emitter that is not registered", async () => {
const destination = await userWithCompanyFactory(UserRole.MEMBER);
const { mutate } = makeClient(destination.user);
const { errors } = await mutate<Pick<Mutation, "createForm">>(
CREATE_FORM,
{
variables: {
createFormInput: {
recipient: {
company: {
siret: destination.company.siret
}
},
emitter: {
type: "APPENDIX1_PRODUCER",
company: {
siret: "42172923700022"
}
}
}
}
}
);
expect(errors).toEqual([
expect.objectContaining({
message:
"L'émetteur doit être inscrit sur Trackdéchets pour apparaitre sur une annexe 1 sans éco-organisme",
extensions: {
code: "BAD_USER_INPUT"
}
})
]);
});

it("should allow to create a APPENDIX1_PRODUCER form with an emitter that is not registered if there is an eco organisme", async () => {
const destination = await userWithCompanyFactory(UserRole.MEMBER);
const ecoOrganisme = await userWithCompanyFactory("MEMBER", {
companyTypes: {
set: ["ECO_ORGANISME"]
}
});
const { mutate } = makeClient(destination.user);
const { errors } = await mutate<Pick<Mutation, "createForm">>(
CREATE_FORM,
{
variables: {
createFormInput: {
ecoOrganisme: {
name: ecoOrganisme.company.name,
siret: ecoOrganisme.company.siret
},
recipient: {
company: {
siret: destination.company.siret
}
},
emitter: {
type: "APPENDIX1_PRODUCER",
company: {
siret: "42172923700022"
}
}
}
}
}
);
expect(errors.length).toEqual(0);
});
});
});
108 changes: 108 additions & 0 deletions back/src/forms/resolvers/mutations/__tests__/updateForm.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2684,6 +2684,114 @@ describe("Mutation.updateForm", () => {
);
});

it("should not allow to group an APPENDIX1_PRODUCER form with an emitter that is not registered", async () => {
const { user, company } = await userWithCompanyFactory("MEMBER");
const { mutate } = makeClient(user);

const appendix1_1 = await prisma.form.create({
data: {
readableId: getReadableId(),
status: Status.DRAFT,
emitterType: EmitterType.APPENDIX1_PRODUCER,
emitterCompanySiret: "42172923700022",
emitterCompanyName: "UnregisteredProducerCompany",
emitterCompanyAddress: "rue de l'annexe",
emitterCompanyContact: "Contact",
emitterCompanyPhone: "01 01 01 01 01",
emitterCompanyMail: "[email protected]",
wasteDetailsCode: "16 06 01*",
owner: { connect: { id: user.id } }
}
});

// Group with appendix1_1
const form = await formFactory({
ownerId: user.id,
opt: {
status: Status.SEALED,
wasteDetailsCode: "16 06 01*",
emitterCompanySiret: company.siret,
emitterType: EmitterType.APPENDIX1
}
});

// Update container
const { errors } = await mutate<
Pick<Mutation, "updateForm">,
MutationUpdateFormArgs
>(UPDATE_FORM, {
variables: {
updateFormInput: {
id: form.id,
grouping: [{ form: { id: appendix1_1.id }, quantity: 0 }]
}
}
});

expect(errors).toEqual([
expect.objectContaining({
message: `L'émetteur du bordereau d'annexe 1 ${appendix1_1.id} n'est pas inscrit sur Trackdéchets. Il est impossible de joindre cette annexe à un bordereau chapeau sans éco-organisme.`,
extensions: {
code: "BAD_USER_INPUT"
}
})
]);
});

it("should allow to group an APPENDIX1_PRODUCER form with an emitter that is not registered if the container has an eco organisme", async () => {
const { user, company } = await userWithCompanyFactory("MEMBER");
const ecoOrganisme = await userWithCompanyFactory("MEMBER", {
companyTypes: {
set: ["ECO_ORGANISME"]
}
});
const { mutate } = makeClient(user);

const appendix1_1 = await prisma.form.create({
data: {
readableId: getReadableId(),
status: Status.DRAFT,
emitterType: EmitterType.APPENDIX1_PRODUCER,
emitterCompanySiret: "42172923700022",
emitterCompanyName: "UnregisteredProducerCompany",
emitterCompanyAddress: "rue de l'annexe",
emitterCompanyContact: "Contact",
emitterCompanyPhone: "01 01 01 01 01",
emitterCompanyMail: "[email protected]",
wasteDetailsCode: "16 06 01*",
ecoOrganismeName: ecoOrganisme.company.name,
ecoOrganismeSiret: ecoOrganisme.company.siret,
owner: { connect: { id: user.id } }
}
});

// Group with appendix1_1
const form = await formFactory({
ownerId: user.id,
opt: {
status: Status.SEALED,
wasteDetailsCode: "16 06 01*",
emitterCompanySiret: company.siret,
emitterType: EmitterType.APPENDIX1
}
});

// Update container
const { data } = await mutate<
Pick<Mutation, "updateForm">,
MutationUpdateFormArgs
>(UPDATE_FORM, {
variables: {
updateFormInput: {
id: form.id,
grouping: [{ form: { id: appendix1_1.id }, quantity: 0 }]
}
}
});

expect(data.updateForm.grouping).toHaveLength(1);
});

it(
"should be possible to update a form where transport is ROAD and wasteDetailsQuantity is > 40T " +
"if it was created before (<=) process.env.MAX_WEIGHT_BY_ROAD_VALIDATE_AFTER",
Expand Down
36 changes: 16 additions & 20 deletions back/src/forms/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,26 +313,6 @@ const emitterSchemaFn: FactorySchemaOf<FormValidationContext, Emitter> = ({
.label("Émetteur")
.when("emitterIsForeignShip", siretConditions.isForeignShip)
.when("emitterIsPrivateIndividual", siretConditions.isPrivateIndividual)
.test(
"emitter-is-registered-for-appendix1-producer",
"L'émetteur doit être inscrit sur Trackdéchets pour apparaitre sur une annexe 1 sans éco-organisme",
async (value, testContext) => {
const rootValue = testContext.parent;
if (
!value ||
rootValue.emitterType !== EmitterType.APPENDIX1_PRODUCER ||
rootValue.ecoOrganismeSiret
) {
return true;
}

const company = await prisma.company.findFirst({
where: { orgId: value },
select: { id: true }
});
return company != null;
}
)
.test(
"is-not-eco-organisme",
"L'émetteur ne peut pas être un éco-organisme. Merci de bien vouloir renseigner l'émetteur effectif de ce déchet (ex: déchetterie, producteur, TTR...). Un autre champ dédié existe et doit être utilisé pour viser l'éco-organisme concerné : https://faq.trackdechets.fr/dechets-dangereux-classiques/les-eco-organismes-sur-trackdechets#ou-etre-vise-en-tant-queco-organisme",
Expand Down Expand Up @@ -2044,6 +2024,22 @@ export async function validateAppendix1Groupement(
};
});

for (const initialForm of initialForms) {
if (initialForm.ecoOrganismeSiret || !initialForm.emitterCompanySiret) {
continue;
}

const company = await prisma.company.findFirst({
where: { orgId: initialForm.emitterCompanySiret },
select: { id: true }
});
if (!company) {
throw new UserInputError(
`L'émetteur du bordereau d'annexe 1 ${initialForm.id} n'est pas inscrit sur Trackdéchets. Il est impossible de joindre cette annexe à un bordereau chapeau sans éco-organisme.`
);
}
}

// Once the first appendix has been signed by the transporter,
// you have maximum 5 calendar days to add and sign new appendix.
const currentDate = new Date();
Expand Down

0 comments on commit 41f1f25

Please sign in to comment.