Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
providenz committed Oct 19, 2024
2 parents 418ebc7 + c1305bb commit 49af27d
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 88 deletions.
73 changes: 0 additions & 73 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

7 changes: 6 additions & 1 deletion apps/cron/src/commands/appendix1.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,15 @@ async function cleanOrphanAppendix1() {
});

cursor = orphansChunk[orphansChunk.length - 1].id;
const orphansChunkIds = orphansChunk.map(form => form.id);

await prisma.form.updateMany({
where: { id: { in: orphansChunk.map(form => form.id) } },
where: { id: { in: orphansChunkIds } },
data: { isDeleted: true }
});

for (const id of orphansChunkIds) {
await deleteBsd({ id }, { user: { auth: AuthType.BEARER } } as any);
}
}
}
3 changes: 2 additions & 1 deletion back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"reindex-bsd": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/reindexBsd.ts $bsdId",
"purge-pdf-token": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/purgePdfAccessToken.ts",
"storeAllWebhookSettings.ts": "tsx --tsconfig tsconfig.lib.json ./src/webhooks/commands/storeAllWebhookSettings.ts",
"generate-bsds-templates": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/bsdTemplates/generateBsdTemplates.ts"
"generate-bsds-templates": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/bsdTemplates/generateBsdTemplates.ts",
"reindex-deleted-orphan-appendix1": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/reindexDeletedOrphanAppendix1.ts"
},
"engines": {
"node": "^20"
Expand Down
2 changes: 1 addition & 1 deletion back/src/bsda/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export function computeLatestRevision(
return revisionRequests.reduce((latestRevision, currentRevision) => {
if (
!latestRevision ||
currentRevision.updatedAt > latestRevision.updatedAt
currentRevision.createdAt > latestRevision.createdAt
) {
return currentRevision;
}
Expand Down
2 changes: 1 addition & 1 deletion back/src/bsdasris/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export function computeLatestRevision(
return revisionRequests.reduce((latestRevision, currentRevision) => {
if (
!latestRevision ||
currentRevision.updatedAt > latestRevision.updatedAt
currentRevision.createdAt > latestRevision.createdAt
) {
return currentRevision;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ const UPDATE_COMPANY = gql`
}
`;

const UPDATE_COMPANY_BSDASRI_TAKEOVER = gql`
mutation UpdateCompany(
$id: String!
$allowBsdasriTakeOverWithoutSignature: Boolean
) {
updateCompany(
id: $id
allowBsdasriTakeOverWithoutSignature: $allowBsdasriTakeOverWithoutSignature
) {
id
}
}
`;

describe("mutation updateCompany", () => {
afterEach(async () => {
await resetDatabase();
Expand Down Expand Up @@ -101,6 +116,33 @@ describe("mutation updateCompany", () => {
expect(updatedCompany).toMatchObject(variables);
});

it("should update a company allowBsdasriTakeOverWithoutSignature when cotactEMail is an contactEmail string", async () => {
// bugfix: user were not able to update allowBsdasriTakeOverWithoutSignature when contactEmail was en empty string
const { user, company } = await userWithCompanyFactory("ADMIN", {
contactEmail: ""
});

const { mutate } = makeClient({ ...user, auth: AuthType.Session });

const variables = {
id: company.id,

allowBsdasriTakeOverWithoutSignature: true
};
const { data } = await mutate<Pick<Mutation, "updateCompany">>(
UPDATE_COMPANY_BSDASRI_TAKEOVER,
{
variables
}
);
expect(data.updateCompany.id).toEqual(company.id);

const updatedCompany = await prisma.company.findUnique({
where: { id: company.id }
});
expect(updatedCompany).toMatchObject(variables);
});

it("should fail to update a company with crematorium companyType", async () => {
const { user, company } = await userWithCompanyFactory("ADMIN");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,100 @@ describe("{ mutation { updateworkerCertification } }", () => {
});
expect(updated.certificationNumber).toEqual(update.certificationNumber);
});

it("should reset Worker certification when subsection 3 is false", async () => {
const certification = {
hasSubSectionFour: true,
hasSubSectionThree: true,
certificationNumber: "AAA",
validityLimit: new Date().toISOString(),
organisation: "AFNOR Certification"
};

const createdCertification = await prisma.workerCertification.create({
data: certification
});
const { user, company } = await userWithCompanyFactory("ADMIN");
await prisma.company.update({
data: {
workerCertification: { connect: { id: createdCertification.id } }
},
where: { id: company.id }
});

const update = {
hasSubSectionThree: false
};

const mutation = `
mutation {
updateWorkerCertification(
input: {
id: "${createdCertification.id}"
hasSubSectionThree:false
}
) { hasSubSectionThree }
}`;
const { mutate } = makeClient({ ...user, auth: AuthType.Session });

const { data } = await mutate<Pick<Mutation, "updateWorkerCertification">>(
mutation
);

// check returned value
expect(data.updateWorkerCertification).toEqual(update);

// check record was modified in db
const { id, ...updated } =
await prisma.workerCertification.findUniqueOrThrow({
where: { id: createdCertification.id }
});
expect(updated.hasSubSectionThree).toEqual(false);
expect(updated.certificationNumber).toEqual("");
expect(updated.organisation).toEqual("");
expect(updated.validityLimit).toEqual(null);
});

it("should not reset Worker certification when input does not contain subsection 3 field", async () => {
const certification = {
hasSubSectionFour: true,
hasSubSectionThree: true,
certificationNumber: "AAA",
validityLimit: new Date().toISOString(),
organisation: "AFNOR Certification"
};

const createdCertification = await prisma.workerCertification.create({
data: certification
});
const { user, company } = await userWithCompanyFactory("ADMIN");
await prisma.company.update({
data: {
workerCertification: { connect: { id: createdCertification.id } }
},
where: { id: company.id }
});

const mutation = `
mutation {
updateWorkerCertification(
input: {
id: "${createdCertification.id}"
organisation: "QUALIBAT"
}
) { hasSubSectionThree }
}`;
const { mutate } = makeClient({ ...user, auth: AuthType.Session });

await mutate<Pick<Mutation, "updateWorkerCertification">>(mutation);

// check record was modified in db
const { id, ...updated } =
await prisma.workerCertification.findUniqueOrThrow({
where: { id: createdCertification.id }
});
expect(updated.hasSubSectionThree).toEqual(true);
expect(updated.certificationNumber).toEqual("AAA");
expect(updated.organisation).toEqual("QUALIBAT");
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { applyAuthStrategies, AuthType } from "../../../auth";
import { removeEmptyKeys } from "../../../common/converter";
import { checkIsAuthenticated } from "../../../common/permissions";
import { MutationUpdateWorkerCertificationArgs } from "../../../generated/graphql/types";
import {
MutationUpdateWorkerCertificationArgs,
UpdateWorkerCertificationInput
} from "../../../generated/graphql/types";
import { prisma } from "@td/prisma";
import { GraphQLContext } from "../../../types";
import { getWorkerCertificationOrNotFound } from "../../database";
import { checkCanReadUpdateDeleteWorkerCertification } from "../../permissions";
import { workerCertificationSchema } from "./createWorkerCertification";

const cleanupCertificate = (
input: Omit<UpdateWorkerCertificationInput, "id">
) => {
return {
...removeEmptyKeys(input),
certificationNumber: "",
organisation: "",
validityLimit: null
};
};

export async function updateWorkerCertification(
_,
{ input }: MutationUpdateWorkerCertificationArgs,
Expand All @@ -16,13 +30,15 @@ export async function updateWorkerCertification(
applyAuthStrategies(context, [AuthType.Session]);
const user = checkIsAuthenticated(context);
const { id, ...data } = input;

const hasNoSubSectionThree = input.hasSubSectionThree === false;
const certification = await getWorkerCertificationOrNotFound({ id });
await checkCanReadUpdateDeleteWorkerCertification(user, certification);
await workerCertificationSchema.validate(input);

return prisma.workerCertification.update({
data: removeEmptyKeys(data),
data: hasNoSubSectionThree
? cleanupCertificate(data)
: removeEmptyKeys(data),
where: { id: certification.id }
});
}
2 changes: 1 addition & 1 deletion back/src/companies/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const rawCompanySchema = z.object({
gerepId: z.string().nullish(),
codeNaf: z.string().nullish(),
givenName: z.string().nullish(),
contactEmail: z.string().email().nullish(),
contactEmail: z.string().email().nullish().or(z.literal("")), // accept empty strings
contactPhone: z.string().nullish(),
contact: z.string().nullish(),
website: z
Expand Down
2 changes: 1 addition & 1 deletion back/src/forms/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ export function expandFormFromElastic(form: FormForElastic): GraphQLForm {
(latestRevision, currentRevision) => {
if (
!latestRevision ||
currentRevision.updatedAt > latestRevision.updatedAt
currentRevision.createdAt > latestRevision.createdAt
) {
return currentRevision;
}
Expand Down
49 changes: 49 additions & 0 deletions back/src/scripts/bin/reindexDeletedOrphanAppendix1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { prisma } from "@td/prisma";
import { AuthType, EmitterType, Prisma } from "@prisma/client";
import { logger } from "@td/logger";
import { deleteBsd } from "../../common/elastic";
import { closeQueues } from "../../queue/producers";

async function exitScript() {
logger.info("Done reindexAllInBulk script, exiting");
await prisma.$disconnect();
await closeQueues();
process.exit(0);
}

// ensure deleted orphans appendix1 are removed from ES
(async function () {
const CHUNK_SIZE = 200;

const where: Prisma.FormWhereInput = {
emitterType: EmitterType.APPENDIX1_PRODUCER,
isDeleted: true,
groupedIn: { none: {} }
};
const orphansCount = await prisma.form.count({ where });

let cursor: string | null = null;

for (let i = 0; i < orphansCount; i += CHUNK_SIZE) {
console.log(`Chunk ${i + 1}`);

const orphans = await prisma.form.findMany({
where,
orderBy: { id: "asc" },
take: CHUNK_SIZE,
select: { id: true },
...(cursor ? { skip: 1, cursor: { id: cursor } } : {})
});

cursor = orphans[orphans.length - 1].id;

const orphansIds = orphans.map(form => form.id);

for (const id of orphansIds) {
console.log(`Removing ${id}`);
await deleteBsd({ id }, { user: { auth: AuthType.BEARER } } as any);
}
}

await exitScript();
})();
Loading

0 comments on commit 49af27d

Please sign in to comment.