Skip to content

Commit

Permalink
Apply plates number validation on transporter mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
providenz committed Feb 9, 2025
1 parent 9b256e1 commit 023c307
Show file tree
Hide file tree
Showing 18 changed files with 548 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,93 @@ describe("Mutation.Bsda.create", () => {
]);
});

it("should fail creating the form if plate numbers are invalid", async () => {
const { user, company } = await userWithCompanyFactory("MEMBER");
const { company: destinationCompany } = await userWithCompanyFactory(
"MEMBER"
);
const { company: transporterCompany } = await userWithCompanyFactory(
"MEMBER"
);
const worker = await companyFactory();

const input: BsdaInput = {
type: "OTHER_COLLECTIONS",
emitter: {
isPrivateIndividual: false,
company: {
siret: company.siret,
name: "The crusher",
address: "Rue de la carcasse",
contact: "Centre amiante",
phone: "0101010101",
mail: "[email protected]"
}
},
worker: {
company: {
siret: worker.siret,
name: "worker",
address: "address",
contact: "contactEmail",
phone: "contactPhone",
mail: "[email protected]"
}
},
waste: {
code: "06 07 01*",
adr: "ADR",
pop: true,
consistence: "SOLIDE",
familyCode: "Code famille",
materialName: "A material",
sealNumbers: ["1", "2"]
},
packagings: [{ quantity: 1, type: "PALETTE_FILME" }],
weight: { isEstimate: true, value: 1.2 },
destination: {
cap: "A cap",
plannedOperationCode: "D 9",
company: {
siret: destinationCompany.siret,
name: "destination",
address: "address",
contact: "contactEmail",
phone: "contactPhone",
mail: "[email protected]"
}
},
transporter: {
company: {
siret: transporterCompany.siret,
name: "The Transporter",
address: "Rue du bsda",
contact: "Un transporter",
phone: "0101010101",
mail: "[email protected]"
},
transport: { plates: ["SD"] }
}
};

const { mutate } = makeClient(user);
const { errors } = await mutate<Pick<Mutation, "createBsda">>(CREATE_BSDA, {
variables: {
input
}
});

expect(errors).toEqual([
expect.objectContaining({
message:
"Le numéro d'immatriculation doit faire entre 4 et 12 caractères",
extensions: expect.objectContaining({
code: ErrorCode.BAD_USER_INPUT
})
})
]);
});

it("should fail creating the form if a required field like the waste code is missing", async () => {
const { user, company } = await userWithCompanyFactory("MEMBER");
const { company: destinationCompany } = await userWithCompanyFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,47 @@ describe("Mutation.createBsdaTransporter", () => {
]);
});

it("should throw error if plate is invalid", async () => {
const user = await userFactory();
const { mutate } = makeClient(user);
const transporter = await companyFactory({
companyTypes: ["TRANSPORTER"],
transporterReceipt: {
create: {
department: "13",
receiptNumber: "MON-RECEPISSE",
validityLimit: new Date("2024-01-01")
}
}
});

const { errors } = await mutate<
Pick<Mutation, "createBsdaTransporter">,
MutationCreateBsdaTransporterArgs
>(CREATE_BSDA_TRANSPORTER, {
variables: {
input: {
company: {
siret: transporter.siret,
name: transporter.name,
address: transporter.address,
contact: transporter.contact
},
transport: {
mode: "ROAD",
plates: ["A"]
}
}
}
});
expect(errors).toEqual([
expect.objectContaining({
message:
"Le numéro d'immatriculation doit faire entre 4 et 12 caractères"
})
]);
});

it("should create a BSDA transporter", async () => {
const user = await userFactory();
const { mutate } = makeClient(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,37 @@ describe("Mutation.updateBsda", () => {
expect(updatedBsda.wasteMaterialName).toEqual("new name");
});

it("should fail if plate numbers are invalid ", async () => {
const { company, user } = await userWithCompanyFactory(UserRole.ADMIN);
const bsda = await bsdaFactory({
userId: user.id,
opt: {
isDraft: true,
status: "INITIAL",
emitterCompanySiret: company.siret
}
});

const { mutate } = makeClient(user);
const { errors } = await mutate<
Pick<Mutation, "updateBsda">,
MutationUpdateBsdaArgs
>(UPDATE_BSDA, {
variables: {
id: bsda.id,
input: {
transporter: { transport: { plates: ["AZ"] } }
}
}
});
expect(errors).toEqual([
expect.objectContaining({
message:
"Le numéro d'immatriculation doit faire entre 4 et 12 caractères"
})
]);
});

it("should disallow unauthenticated user from updating a bsda", async () => {
const { mutate } = makeClient();
const { errors } = await mutate<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,39 @@ describe("Mutation.updateBsdaTransporter", () => {
]);
});

it("should throw error if plate is invalid", async () => {
const user = await userFactory();
const { mutate } = makeClient(user);
const transporter = await companyFactory({ companyTypes: ["TRANSPORTER"] });
const bsdaTransporter = await prisma.bsdaTransporter.create({
data: {
number: 0,
transporterCompanySiret: transporter.siret,
transporterCompanyName: transporter.name,
transporterTransportMode: "ROAD"
}
});
const { errors } = await mutate<
Pick<Mutation, "updateBsdaTransporter">,
MutationUpdateBsdaTransporterArgs
>(UPDATE_BSDA_TRANSPORTER, {
variables: {
id: bsdaTransporter.id,
input: {
transport: {
plates: ["A"]
}
}
}
});
expect(errors).toEqual([
expect.objectContaining({
message:
"Le numéro d'immatriculation doit faire entre 4 et 12 caractères"
})
]);
});

it("should not be possible to update a transporter that has already signed", async () => {
const emitter = await userWithCompanyFactory("MEMBER");
const transporter = await companyFactory({ companyTypes: ["TRANSPORTER"] });
Expand Down
11 changes: 9 additions & 2 deletions back/src/bsda/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ import {
rawTransporterSchema,
siretSchema
} from "../../common/validation/zod/schema";
import { validateMultiTransporterPlates } from "../../common/validation/zod/refinement";
import {
validateMultiTransporterPlates,
validateTransporterPlates
} from "../../common/validation/zod/refinement";

const ZodBsdaPackagingEnum = z.enum([
"BIG_BAG",
Expand Down Expand Up @@ -337,6 +340,10 @@ export type ParsedZodBsdaTransporter = z.output<
typeof rawBsdaTransporterSchema
>;

export const transformedBsdaTransporterSchema = rawBsdaTransporterSchema
const refinedBsdaTransporter = rawBsdaTransporterSchema.superRefine(
validateTransporterPlates
);

export const transformedBsdaTransporterSchema = refinedBsdaTransporter
.transform(updateTransporterRecepisse)
.transform(sirenifyBsdaTransporter);
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,78 @@ describe("Mutation.createDasri validation scenarii", () => {
]);
});

it("should fail creating the form if plate humbers are invalid", async () => {
const { user, company } = await userWithCompanyFactory("MEMBER");
const transporterCompany = await companyFactory();

const input = {
waste: { adr: "xyz 33", code: "18 01 03*" },
emitter: {
company: {
name: "hopital blanc",
siret: company.siret,
contact: "jean durand",
phone: "06 18 76 02 00",

address: "avenue de la mer"
},
emission: {
weight: { value: 23.2, isEstimate: false },

packagings: [
{
type: "BOITE_CARTON",
volume: 22,
quantity: 3
}
]
}
},

transporter: {
company: {
mail: "[email protected]",
name: "El transporter",
siret: transporterCompany.siret,
contact: "Jason Statham",
phone: "06 18 76 02 00",
address: "avenue de la mer"
},
transport: {
plates: ["AB"],
weight: { value: 22, isEstimate: false },
packagings: [
{
type: "BOITE_CARTON",
volume: 22,
quantity: 3
}
]
}
},
...(await getDestinationCompanyInfo())
};

const { mutate } = makeClient(user);
const { errors } = await mutate<Pick<Mutation, "createBsdasri">>(
CREATE_DASRI,
{
variables: {
input
}
}
);
expect(errors).toEqual([
expect.objectContaining({
message:
"Le numéro d'immatriculation doit faire entre 4 et 12 caractères",
extensions: expect.objectContaining({
code: ErrorCode.BAD_USER_INPUT
})
})
]);
});

it("create a dasri without transport quantity and type", async () => {
const { user, company } = await userWithCompanyFactory("MEMBER");
const transporterCompany = await companyFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,35 @@ describe("Mutation.createBsff", () => {
})
]);
});

it("should not be possible to set invalid transporter plates", async () => {
const emitter = await userWithCompanyFactory(UserRole.ADMIN);
const transporter = await userWithCompanyFactory(UserRole.ADMIN, {
companyTypes: ["TRANSPORTER"]
});
await transporterReceiptFactory({
company: transporter.company
});
const destination = await userWithCompanyFactory(UserRole.ADMIN);
const { mutate } = makeClient(emitter.user);

const input = createInput(emitter, transporter, destination);
input.transporter["transport"] = { plates: ["XY"] };

const { errors } = await mutate<
Pick<Mutation, "createBsff">,
MutationCreateBsffArgs
>(CREATE_BSFF, {
variables: {
input
}
});

expect(errors).toEqual([
expect.objectContaining({
message:
"Le numéro d'immatriculation doit faire entre 4 et 12 caractères"
})
]);
});
});
Loading

0 comments on commit 023c307

Please sign in to comment.