diff --git a/back/src/forms/resolvers/mutations/__tests__/createForm.integration.ts b/back/src/forms/resolvers/mutations/__tests__/createForm.integration.ts index 73d39ea786..65f2b78708 100644 --- a/back/src/forms/resolvers/mutations/__tests__/createForm.integration.ts +++ b/back/src/forms/resolvers/mutations/__tests__/createForm.integration.ts @@ -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>( - 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>( - 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); - }); }); }); diff --git a/back/src/forms/resolvers/mutations/__tests__/updateForm.integration.ts b/back/src/forms/resolvers/mutations/__tests__/updateForm.integration.ts index 611d81fa4c..eb7693f148 100644 --- a/back/src/forms/resolvers/mutations/__tests__/updateForm.integration.ts +++ b/back/src/forms/resolvers/mutations/__tests__/updateForm.integration.ts @@ -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: "annexe1@test.com", + 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, + 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: "annexe1@test.com", + 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, + 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", diff --git a/back/src/forms/validation.ts b/back/src/forms/validation.ts index 71c8225b3d..e79975082a 100644 --- a/back/src/forms/validation.ts +++ b/back/src/forms/validation.ts @@ -313,26 +313,6 @@ const emitterSchemaFn: FactorySchemaOf = ({ .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", @@ -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();