Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRA 14421] brouillons BSVHU non accessibles si pas auteur #3650

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ et le projet suit un schéma de versionning inspiré de [Calendar Versioning](ht
#### :rocket: Nouvelles fonctionnalités

- Ajout d'Eco-organisme sur BSVHU [PR 3619](https://github.com/MTES-MCT/trackdechets/pull/3619)
- Ajout des profils Négociant et Courtier sur BSVHU [PR 3645](https://github.com/MTES-MCT/trackdechets/pull/3645)

#### :bug: Corrections de bugs

Expand Down
7 changes: 4 additions & 3 deletions back/src/bsda/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,10 @@ export async function checkCanDelete(user: User, bsda: BsdaWithTransporters) {
? [bsda.emitterCompanySiret]
: [];

const errorMsg = bsda.isDraft
? "Vous n'êtes pas autorisé à supprimer ce bordereau."
: "Seuls les bordereaux en brouillon ou n'ayant pas encore été signés peuvent être supprimés";
const errorMsg =
bsda.status === BsdaStatus.INITIAL
? "Vous n'êtes pas autorisé à supprimer ce bordereau."
: "Seuls les bordereaux en brouillon ou n'ayant pas encore été signés peuvent être supprimés";
return checkUserPermissions(
user,
authorizedOrgIds,
Expand Down
2 changes: 1 addition & 1 deletion back/src/bsda/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ export function toIncomingWaste(bsda: RegistryBsda): Required<IncomingWaste> {
traderRecepisseNumber: null,
brokerCompanyName: bsda.brokerCompanyName,
brokerCompanySiret: bsda.brokerCompanySiret,
brokerRecepisseNumber: null,
brokerRecepisseNumber: bsda.brokerRecepisseNumber,
emitterCompanyMail: bsda.emitterCompanyMail,
...getOperationData(bsda),
nextDestinationProcessingOperation:
Expand Down
3 changes: 1 addition & 2 deletions back/src/bsda/repository/bsda/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ export function buildCreateBsda(deps: RepositoryFnDeps): CreateBsdaFn {
where: { id: bsda.id },
data: {
...(canAccessDraftOrgIds.length ? { canAccessDraftOrgIds } : {}),
...(transportersOrgIds.length ? { transportersOrgIds } : {}),
transportersOrgIds
...(transportersOrgIds.length ? { transportersOrgIds } : {})
},
include: {
grouping: { select: { id: true } },
Expand Down
72 changes: 72 additions & 0 deletions back/src/bsda/validation/recipify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { getTransporterCompanyOrgId } from "@td/constants";
import { getSealedFields } from "./rules";
import { ParsedZodBsda } from "./schema";
import { BsdaValidationContext, ZodBsdaTransformer } from "./types";
import { CompanyRole } from "../../common/validation/zod/schema";
import { buildRecipify, RecipifyInputAccessor } from "../../companies/recipify";

const recipifyBsdaAccessors = (
bsd: ParsedZodBsda,
// Tranformations should not be run on sealed fields
sealedFields: string[]
): RecipifyInputAccessor<ParsedZodBsda>[] => [
...(bsd.transporters ?? []).map(
(_, idx) =>
({
role: CompanyRole.Transporter,
skip: !!bsd.transporters![idx].transporterTransportSignatureDate,
orgIdGetter: () => {
const orgId = getTransporterCompanyOrgId({
transporterCompanySiret:
bsd.transporters![idx].transporterCompanySiret ?? null,
transporterCompanyVatNumber:
bsd.transporters![idx].transporterCompanyVatNumber ?? null
});
return orgId ?? null;
},
setter: async (bsda: ParsedZodBsda, receipt) => {
const transporter = bsda.transporters![idx];
if (transporter.transporterRecepisseIsExempted) {
transporter.transporterRecepisseNumber = null;
transporter.transporterRecepisseValidityLimit = null;
transporter.transporterRecepisseDepartment = null;
} else {
transporter.transporterRecepisseNumber =
receipt?.receiptNumber ?? null;
transporter.transporterRecepisseValidityLimit =
receipt?.validityLimit ?? null;
transporter.transporterRecepisseDepartment =
receipt?.department ?? null;
}
}
} as RecipifyInputAccessor<ParsedZodBsda>)
),
{
role: CompanyRole.Broker,
skip: sealedFields.includes("brokerRecepisseNumber"),
orgIdGetter: () => {
return bsd.brokerCompanySiret ?? null;
},
setter: async (bsda: ParsedZodBsda, receipt) => {
if (!bsda.brokerRecepisseNumber && receipt?.receiptNumber) {
bsda.brokerRecepisseNumber = receipt.receiptNumber;
}
if (!bsda.brokerRecepisseValidityLimit && receipt?.validityLimit) {
bsda.brokerRecepisseValidityLimit = receipt.validityLimit;
}
if (!bsda.brokerRecepisseDepartment && receipt?.department) {
bsda.brokerRecepisseDepartment = receipt.department;
}
}
}
];

export const recipifyBsda: (
context: BsdaValidationContext
) => ZodBsdaTransformer = context => {
return async bsda => {
const sealedFields = await getSealedFields(bsda, context);
const accessors = recipifyBsdaAccessors(bsda, sealedFields);
return buildRecipify(accessors, bsda);
};
};
3 changes: 1 addition & 2 deletions back/src/bsda/validation/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,7 @@ export const bsdaEditionRules: BsdaEditionRules = {
sealed: { from: "OPERATION" }
},
brokerRecepisseValidityLimit: {
readableFieldName:
"la date de validité de la certification de l'entreprise de travaux",
readableFieldName: "la date de validité du récépissé du courtier",
sealed: { from: "OPERATION" }
},
wasteCode: {
Expand Down
4 changes: 2 additions & 2 deletions back/src/bsda/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ import {
updateTransporterRecepisse
} from "./transformers";
import { sirenifyBsda, sirenifyBsdaTransporter } from "./sirenify";
import { updateTransportersRecepisse } from "../../common/validation/zod/transformers";
import {
CompanyRole,
foreignVatNumberSchema,
rawTransporterSchema,
siretSchema
} from "../../common/validation/zod/schema";
import { recipifyBsda } from "./recipify";

const ZodBsdaPackagingEnum = z.enum([
"BIG_BAG",
Expand Down Expand Up @@ -292,7 +292,7 @@ export const contextualSchemaAsync = (context: BsdaValidationContext) => {
// `enableCompletionTransformers=false`;
transformedSyncSchema
.transform(sirenifyBsda(context))
.transform(updateTransportersRecepisse)
.transform(recipifyBsda(context))
.transform(fillWasteConsistenceWhenForwarding)
: transformedSyncSchema;

Expand Down
14 changes: 8 additions & 6 deletions back/src/bsds/indexation/__tests__/bulkIndexBsds.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ describe("indexAllBsdTypeSync", () => {
});

it("should index BSVHUs synchronously", async () => {
const user = await userFactory();
const bsvhus = await Promise.all([
bsvhuFactory({}),
bsvhuFactory({}),
bsvhuFactory({})
bsvhuFactory({ userId: user.id }),
bsvhuFactory({ userId: user.id }),
bsvhuFactory({ userId: user.id })
]);
await indexAllBsdTypeSync({
bsdName: "bsvhu",
Expand Down Expand Up @@ -407,10 +408,11 @@ describe("indexAllBsdTypeConcurrently", () => {
});

it("should index BSVHUs using the index queue", async () => {
const user = await userFactory();
const bsvhus = await Promise.all([
bsvhuFactory({}),
bsvhuFactory({}),
bsvhuFactory({})
bsvhuFactory({ userId: user.id }),
bsvhuFactory({ userId: user.id }),
bsvhuFactory({ userId: user.id })
]);
const jobs = await indexAllBsdTypeConcurrentJobs({
bsdName: "bsvhu",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ describe("Query.bsds.vhus base workflow", () => {
destination = await userWithCompanyFactory(UserRole.ADMIN, {
companyTypes: {
set: ["WASTE_VEHICLES"]
},
wasteVehiclesTypes: {
set: ["BROYEUR", "DEMOLISSEUR"]
}
});
});
Expand Down Expand Up @@ -200,7 +203,7 @@ describe("Query.bsds.vhus base workflow", () => {
expect.objectContaining({ node: { id: vhuId } })
]);
});
it("draft vhu should be isDraftFor transporter", async () => {
it("draft vhu should not be isDraftFor transporter", async () => {
const { query } = makeClient(transporter.user);
const { data } = await query<Pick<Query, "bsds">, QueryBsdsArgs>(
GET_BSDS,
Expand All @@ -213,11 +216,9 @@ describe("Query.bsds.vhus base workflow", () => {
}
);

expect(data.bsds.edges).toEqual([
expect.objectContaining({ node: { id: vhuId } })
]);
expect(data.bsds.edges).toEqual([]);
});
it("draft vhu should be isDraftFor destination", async () => {
it("draft vhu should not be isDraftFor destination", async () => {
const { query } = makeClient(destination.user);
const { data } = await query<Pick<Query, "bsds">, QueryBsdsArgs>(
GET_BSDS,
Expand All @@ -230,9 +231,7 @@ describe("Query.bsds.vhus base workflow", () => {
}
);

expect(data.bsds.edges).toEqual([
expect.objectContaining({ node: { id: vhuId } })
]);
expect(data.bsds.edges).toEqual([]);
});
});

Expand Down
54 changes: 54 additions & 0 deletions back/src/bsffs/validation/bsff/recipify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getTransporterCompanyOrgId } from "@td/constants";
import { ParsedZodBsff } from "./schema";
import { BsffValidationContext, ZodBsffTransformer } from "./types";
import { CompanyRole } from "../../../common/validation/zod/schema";
import {
buildRecipify,
RecipifyInputAccessor
} from "../../../companies/recipify";

const recipifyBsffAccessors = (
bsd: ParsedZodBsff
): RecipifyInputAccessor<ParsedZodBsff>[] => [
...(bsd.transporters ?? []).map(
(_, idx) =>
({
role: CompanyRole.Transporter,
skip: !!bsd.transporters![idx].transporterTransportSignatureDate,
orgIdGetter: () => {
const orgId = getTransporterCompanyOrgId({
transporterCompanySiret:
bsd.transporters![idx].transporterCompanySiret ?? null,
transporterCompanyVatNumber:
bsd.transporters![idx].transporterCompanyVatNumber ?? null
});
return orgId ?? null;
},
setter: async (bsff: ParsedZodBsff, receipt) => {
const transporter = bsff.transporters![idx];
if (transporter.transporterRecepisseIsExempted) {
transporter.transporterRecepisseNumber = null;
transporter.transporterRecepisseValidityLimit = null;
transporter.transporterRecepisseDepartment = null;
} else {
transporter.transporterRecepisseNumber =
receipt?.receiptNumber ?? null;
transporter.transporterRecepisseValidityLimit =
receipt?.validityLimit ?? null;
transporter.transporterRecepisseDepartment =
receipt?.department ?? null;
}
}
} as RecipifyInputAccessor<ParsedZodBsff>)
)
];

export const recipifyBsff: (
context: BsffValidationContext
) => ZodBsffTransformer = () => {
return async bsff => {
// const sealedFields = await getSealedFields(bsda, context);
const accessors = recipifyBsffAccessors(bsff);
return buildRecipify(accessors, bsff);
};
};
4 changes: 2 additions & 2 deletions back/src/bsffs/validation/bsff/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import {
checkAndSetPreviousPackagings,
updateTransporterRecepisse
} from "./transformers";
import { updateTransportersRecepisse } from "../../../common/validation/zod/transformers";
import {
CompanyRole,
rawTransporterSchema,
siretSchema
} from "../../../common/validation/zod/schema";
import { recipifyBsff } from "./recipify";

export const ZodWasteCodeEnum = z
.enum(BSFF_WASTE_CODES, {
Expand Down Expand Up @@ -195,7 +195,7 @@ export const contextualBsffSchemaAsync = (context: BsffValidationContext) => {
return refinedBsffSchema
.superRefine(checkCompanies)
.transform(sirenifyBsff(context))
.transform(updateTransportersRecepisse)
.transform(recipifyBsff(context))
.superRefine(
// run le check sur les champs requis après les transformations
// au cas où des transformations auto-complète certains champs
Expand Down
16 changes: 15 additions & 1 deletion back/src/bsvhu/__tests__/bsvhuEdition.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BsvhuBrokerInput,
BsvhuDestinationInput,
BsvhuDestinationType,
BsvhuEcoOrganismeInput,
Expand All @@ -9,6 +10,7 @@ import {
BsvhuOperationInput,
BsvhuRecepisseInput,
BsvhuReceptionInput,
BsvhuTraderInput,
BsvhuTransporterInput,
BsvhuTransportInput,
BsvhuWeightInput,
Expand Down Expand Up @@ -102,6 +104,16 @@ describe("edition", () => {
name: "yyy"
};

const broker: Required<BsvhuBrokerInput> = {
company,
recepisse
};

const trader: Required<BsvhuTraderInput> = {
company,
recepisse
};

const input: Required<BsvhuInput> = {
emitter,
wasteCode: "",
Expand All @@ -112,7 +124,9 @@ describe("edition", () => {
transporter,
destination,
intermediaries: [company],
ecoOrganisme
ecoOrganisme,
broker,
trader
};
const flatInput = graphQlInputToZodBsvhu(input);
for (const key of Object.keys(flatInput)) {
Expand Down
13 changes: 12 additions & 1 deletion back/src/bsvhu/__tests__/elastic.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe("toBsdElastic > companies Names & OrgIds", () => {
let intermediary1: Company;
let intermediary2: Company;
let ecoOrganisme: Company;
let broker: Company;
let trader: Company;

beforeAll(async () => {
// Given
Expand All @@ -45,7 +47,8 @@ describe("toBsdElastic > companies Names & OrgIds", () => {
intermediary1 = await companyFactory({ name: "Intermediaire 1" });
intermediary2 = await companyFactory({ name: "Intermediaire 2" });
ecoOrganisme = await companyFactory({ name: "Eco organisme" });

broker = await companyFactory({ name: "Broker" });
trader = await companyFactory({ name: "Trader" });
bsvhu = await bsvhuFactory({
opt: {
emitterCompanyName: emitter.name,
Expand All @@ -57,6 +60,10 @@ describe("toBsdElastic > companies Names & OrgIds", () => {
destinationCompanySiret: destination.siret,
ecoOrganismeName: ecoOrganisme.name,
ecoOrganismeSiret: ecoOrganisme.siret,
brokerCompanySiret: broker.siret,
brokerCompanyName: broker.name,
traderCompanySiret: trader.siret,
traderCompanyName: trader.name,
intermediaries: {
createMany: {
data: [
Expand All @@ -80,6 +87,8 @@ describe("toBsdElastic > companies Names & OrgIds", () => {
expect(elasticBsvhu.companyNames).toContain(intermediary1.name);
expect(elasticBsvhu.companyNames).toContain(intermediary2.name);
expect(elasticBsvhu.companyNames).toContain(ecoOrganisme.name);
expect(elasticBsvhu.companyNames).toContain(broker.name);
expect(elasticBsvhu.companyNames).toContain(trader.name);
});

test("companyOrgIds > should contain the orgIds of ALL BSVHU companies", async () => {
Expand All @@ -90,5 +99,7 @@ describe("toBsdElastic > companies Names & OrgIds", () => {
expect(elasticBsvhu.companyOrgIds).toContain(intermediary1.siret);
expect(elasticBsvhu.companyOrgIds).toContain(intermediary2.siret);
expect(elasticBsvhu.companyOrgIds).toContain(ecoOrganisme.siret);
expect(elasticBsvhu.companyOrgIds).toContain(broker.siret);
expect(elasticBsvhu.companyOrgIds).toContain(trader.siret);
});
});
Loading
Loading