Skip to content

Commit

Permalink
Merge pull request #3721 from MTES-MCT/TRA-15340-appendix1-no-index
Browse files Browse the repository at this point in the history
Corrige l'indexation des annexes 1 orphelines et draft
  • Loading branch information
providenz authored Nov 12, 2024
2 parents 9d4c2b7 + c3c6ed4 commit 99bb25c
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 74 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ et le projet suit un schéma de versionning inspiré de [Calendar Versioning](ht
- Le champ "Numéro de notification" est obligatoire lorsque la destination ultérieure renseignée est étrangère [PR 3719](https://github.com/MTES-MCT/trackdechets/pull/3719)
- La présence d'une quantité reçue est requise pour passer du statut SENT à ACCEPTED via la mutation markAsReceived [PR 3720](https://github.com/MTES-MCT/trackdechets/pull/3720)

#### :bug: Corrections de bugs

- Corrige l'indexation des annexes 1 orphelines et draft [PR 3721](https://github.com/MTES-MCT/trackdechets/pull/3721)

#### :house: Interne

- Modification de la query controlBsds et fermeture de la query bsds aux comptes gouvernementaux [PR 3270](https://github.com/MTES-MCT/trackdechets/pull/3270)
Expand Down
9 changes: 7 additions & 2 deletions back/src/bsds/indexation/reindexBsdHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { BsdasriForElasticInclude, indexBsdasri } from "../../bsdasris/elastic";
import { getBsffForElastic, indexBsff } from "../../bsffs/elastic";
import { getBsvhuForElastic, indexBsvhu } from "../../bsvhu/elastic";
import { getBspaohForElastic, indexBspaoh } from "../../bspaoh/elastic";
import { getFormForElastic, indexForm } from "../../forms/elastic";
import {
getFormForElastic,
indexForm,
isBsddNotIndexable
} from "../../forms/elastic";
import { deleteBsd } from "../../common/elastic";
import { getReadonlyBsdaRepository } from "../../bsda/repository";
import { getReadonlyBsvhuRepository } from "../../bsvhu/repository";
Expand Down Expand Up @@ -84,7 +88,8 @@ export async function reindex(bsdId, exitFn) {

if (bsdId.startsWith("BSD-") || bsdId.startsWith("TD-")) {
const formForElastic = await getFormForElastic({ readableId: bsdId });
if (formForElastic.isDeleted) {

if (isBsddNotIndexable(formForElastic)) {
await deleteBsd({ id: formForElastic.id });
} else {
await indexForm(formForElastic);
Expand Down
115 changes: 113 additions & 2 deletions back/src/forms/__tests__/elastic.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
Company,
EmptyReturnADR,
Status,
WasteAcceptationStatus
WasteAcceptationStatus,
EmitterType
} from "@prisma/client";
import { resetDatabase } from "../../../integration-tests/helper";
import { prisma } from "@td/prisma";
Expand Down Expand Up @@ -60,11 +61,15 @@ describe("getSiretsByTab", () => {
const user = await userFactory();
const form = await formFactory({
ownerId: user.id,
opt: { status: Status.SEALED, emitterType: "APPENDIX1_PRODUCER" }
opt: {
status: Status.SEALED,
emitterType: EmitterType.APPENDIX1_PRODUCER
}
});
const fullForm = await getFullForm(form);
const transporter = getFirstTransporterSync(fullForm);
const { isToCollectFor, isForActionFor } = getSiretsByTab(fullForm);

expect(isForActionFor).toContain(form.emitterCompanySiret);
expect(isToCollectFor).toContain(transporter!.transporterCompanySiret);
});
Expand Down Expand Up @@ -695,3 +700,109 @@ describe("toBsdElastic > companies Names & OrgIds", () => {
);
});
});

describe("toBsdElastic > APPENDIX1", () => {
afterEach(resetDatabase);
test.each([Status.DRAFT, Status.SEALED, Status.SENT])(
"orphan APPENDIX1_PRODUCER : status %p should not appear in dashboard",
async status => {
const user = await userFactory();
const form = await formFactory({
ownerId: user.id,
opt: { status: status, emitterType: EmitterType.APPENDIX1_PRODUCER }
});
const formForElastic = await getFormForElastic(form);

const elasticBsd = toBsdElastic(formForElastic);

expect(elasticBsd.isDraftFor).toEqual([]);
expect(elasticBsd.isForActionFor).toEqual([]);
expect(elasticBsd.isFollowFor).toEqual([]);
expect(elasticBsd.isArchivedFor).toEqual([]);
expect(elasticBsd.isToCollectFor).toEqual([]);
expect(elasticBsd.isCollectedFor).toEqual([]);
expect(elasticBsd.isInRevisionFor).toEqual([]);
expect(elasticBsd.isReturnFor).toEqual([]);
expect(elasticBsd.isRevisedFor).toEqual([]);
}
);

test.each([
Status.SEALED,
Status.SENT,
Status.RECEIVED,
Status.RECEIVED,
Status.RECEIVED,
Status.PROCESSED
])(
"grouped APPENDIX1_PRODUCER : status %p should appear in dashboard",
async status => {
const user = await userFactory();
const appendix1 = await formFactory({
ownerId: user.id,
opt: {
status: status,
emitterType: EmitterType.APPENDIX1_PRODUCER
}
});
// top level bsdd
await formFactory({
ownerId: user.id,
opt: {
status: Status.SENT,
wasteDetailsCode: "16 06 01*",

emitterType: EmitterType.APPENDIX1,
grouping: { create: { initialFormId: appendix1.id, quantity: 0 } }
}
});
const formForElastic = await getFormForElastic(appendix1);

const elasticBsd = toBsdElastic(formForElastic);
const displayedFor = [
...elasticBsd.isDraftFor,
...elasticBsd.isForActionFor,
...elasticBsd.isFollowFor,
...elasticBsd.isFollowFor,
...elasticBsd.isToCollectFor,
...elasticBsd.isCollectedFor,
...elasticBsd.isArchivedFor
].filter(Boolean);
expect(displayedFor.length).toBeTruthy();
}
);

test("grouped APPENDIX1_PRODUCER : DRAFT should appear in dashboard", async () => {
const user = await userFactory();
const appendix1 = await formFactory({
ownerId: user.id,
opt: {
status: Status.DRAFT,
emitterType: EmitterType.APPENDIX1_PRODUCER
}
});
// top level bsdd
await formFactory({
ownerId: user.id,
opt: {
status: Status.SENT,
wasteDetailsCode: "16 06 01*",

emitterType: EmitterType.APPENDIX1,
grouping: { create: { initialFormId: appendix1.id, quantity: 0 } }
}
});
const formForElastic = await getFormForElastic(appendix1);

const elasticBsd = toBsdElastic(formForElastic);
expect(elasticBsd.isDraftFor).toEqual([]);
expect(elasticBsd.isForActionFor).toEqual([]);
expect(elasticBsd.isFollowFor).toEqual([]);
expect(elasticBsd.isArchivedFor).toEqual([]);
expect(elasticBsd.isToCollectFor).toEqual([]);
expect(elasticBsd.isCollectedFor).toEqual([]);
expect(elasticBsd.isInRevisionFor).toEqual([]);
expect(elasticBsd.isReturnFor).toEqual([]);
expect(elasticBsd.isRevisedFor).toEqual([]);
});
});
44 changes: 38 additions & 6 deletions back/src/forms/elastic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
FormWithRevisionRequestsInclude,
FormWithForwarding,
FormWithForwardingInclude,
FormWithForwardedInWithTransporters
FormWithForwardedInWithTransporters,
FormWithAppendix1GroupingInfo,
FormWithAppendix1GroupingInfoInclude
} from "./types";
import { GraphQLContext } from "../types";
import { getRegistryFields } from "./registry";
Expand All @@ -31,14 +33,16 @@ export type FormForElastic = Form &
FormWithForwardedInWithTransporters &
FormWithIntermediaries &
FormWithForwarding &
FormWithRevisionRequests;
FormWithRevisionRequests &
FormWithAppendix1GroupingInfo;

export const FormForElasticInclude = {
...FormWithForwardedInWithTransportersInclude,
...FormWithForwardingInclude,
...FormWithTransportersInclude,
...FormWithIntermediariesInclude,
...FormWithRevisionRequestsInclude
...FormWithRevisionRequestsInclude,
...FormWithAppendix1GroupingInfoInclude // provided to tell apart orphans appendix1 from others
};

export async function getFormForElastic(
Expand All @@ -50,8 +54,32 @@ export async function getFormForElastic(
});
}

/**
*
* Utility to know if BSDD indexing should be skipped
* sould not be indexed:
* deleted forms
* forwarding forms
* appendix form in draft state or not linked to another top level form through formGroupement
*/
export function isBsddNotIndexable(form: FormForElastic): boolean {
if (form.isDeleted || !!form.forwarding) {
return true;
}

if (form.emitterType === "APPENDIX1_PRODUCER") {
if (form.status === "DRAFT") {
return true;
}

return !form.groupedIn?.length; // no relation
}
return false;
}

/**
* Convert a BSD from the forms table to Elastic Search's BSD model.
* NB: some BSDDs should not appear on user dashboard, see `isBsddNotIndexable`
*/
export function toBsdElastic(form: FormForElastic): BsdElastic {
const siretsByTab = getSiretsByTab(form);
Expand Down Expand Up @@ -137,9 +165,10 @@ export function toBsdElastic(form: FormForElastic): BsdElastic {
: null,
destinationOperationDate: form.processedAt?.getTime(),
...getFormReturnOrgIds(form),
...(form.forwarding

...(isBsddNotIndexable(form)
? {
// do not display BSD suite in dashboard
// do not display in dashboard : BSDDs suite, deleted BSDs, orphans appendix1
isDraftFor: [],
isForActionFor: [],
isFollowFor: [],
Expand All @@ -149,6 +178,7 @@ export function toBsdElastic(form: FormForElastic): BsdElastic {
isInRevisionFor: []
}
: siretsByTab),

...getFormRevisionOrgIds(form),
revisionRequests: form.bsddRevisionRequests,
sirets: Object.values(siretsByTab).flat(),
Expand Down Expand Up @@ -204,6 +234,7 @@ export async function indexForm(
if (form.isDeleted) {
return toBsdElastic(form);
}

if (form.forwardedIn) {
// index next BSD asynchronously
indexBsd(
Expand All @@ -212,7 +243,8 @@ export async function indexForm(
intermediaries: [],
forwardedIn: null,
forwarding: form,
bsddRevisionRequests: []
bsddRevisionRequests: [],
groupedIn: []
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ describe("Test appendixForms", () => {
});

expect(appendixForms.length).toBe(2);
expect(appendixForms[0].id).toBe(awaitingGroupForm.id);
expect(appendixForms[1].id).toBe(initialForm2.id);

expect(
appendixForms.map(bsd => bsd.id).sort((a, b) => a.localeCompare(b))
).toEqual(
[awaitingGroupForm.id, initialForm2.id].sort((a, b) => a.localeCompare(b))
);
});

it("should not return appendixForms data", async () => {
Expand Down Expand Up @@ -322,10 +326,17 @@ describe("Test appendixForms", () => {
});

expect(appendixForms.length).toBe(2);
expect(appendixForms[0].id).toBe(awaitingGroupForm.id);
expect(appendixForms[0].quantityGrouped).toBe(0);
expect(appendixForms[1].id).toBe(partiallyGrouped.id);
expect(appendixForms[1].quantityGrouped).toBe(3);

expect(
appendixForms.map(bsd => bsd.id).sort((a, b) => a.localeCompare(b))
).toEqual(
[awaitingGroupForm.id, partiallyGrouped.id].sort((a, b) =>
a.localeCompare(b)
)
);
expect(appendixForms.map(bsd => bsd.quantityGrouped).sort()).toEqual([
0, 3
]);
});

it("new + legacy > should return candidates", async () => {
Expand Down Expand Up @@ -447,12 +458,17 @@ describe("Test appendixForms", () => {
});

expect(appendixForms.length).toBe(3);
expect(appendixForms[0].id).toBe(awaitingGroupForm.id);
expect(appendixForms[0].quantityGrouped).toBe(0);
expect(appendixForms[1].id).toBe(awaitingGroupForm2.id);
expect(appendixForms[1].quantityGrouped).toBe(0);
expect(appendixForms[2].id).toBe(partiallyGrouped.id);
expect(appendixForms[2].quantityGrouped).toBe(3);

expect(
appendixForms.map(bsd => bsd.id).sort((a, b) => a.localeCompare(b))
).toEqual(
[awaitingGroupForm.id, awaitingGroupForm2.id, partiallyGrouped.id].sort(
(a, b) => a.localeCompare(b)
)
);
expect(appendixForms.map(bsd => bsd.quantityGrouped).sort()).toEqual([
0, 0, 3
]);
});
});
});
9 changes: 9 additions & 0 deletions back/src/forms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ export type FormWithRevisionRequests = Prisma.FormGetPayload<{
include: typeof FormWithRevisionRequestsInclude;
}>;

export const FormWithAppendix1GroupingInfoInclude =
Prisma.validator<Prisma.FormInclude>()({
groupedIn: { select: { id: true } }
});

export type FormWithAppendix1GroupingInfo = Prisma.FormGetPayload<{
include: typeof FormWithAppendix1GroupingInfoInclude;
}>;

/**
* A Prisma Form with linked objects
* ***********************************
Expand Down
49 changes: 0 additions & 49 deletions back/src/scripts/bin/reindexDeletedOrphanAppendix1.ts

This file was deleted.

Loading

0 comments on commit 99bb25c

Please sign in to comment.