Skip to content

Commit

Permalink
Merge branch 'tra-14824' into tra-15092
Browse files Browse the repository at this point in the history
  • Loading branch information
GaelFerrand committed Oct 11, 2024
2 parents dbf2798 + 888bb6b commit b75c020
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,80 @@ describe("mutation createRegistryDelegation", () => {
});
});

it("testing email content - no end date", async () => {
// Given
const delegate = await companyFactory({ givenName: "Some given name" });
const { user: delegatorAdmin, company: delegator } =
await userWithCompanyFactory();

// Email
jest.mock("../../../../mailer/mailing");
(sendMail as jest.Mock).mockImplementation(() => Promise.resolve());

// When
const { errors, delegation } = await createDelegation(delegatorAdmin, {
delegateOrgId: delegate.orgId,
delegatorOrgId: delegator.orgId
});

// Then
expect(errors).toBeUndefined();
expect(sendMail as jest.Mock).toHaveBeenCalledTimes(1);
expect(delegation).not.toBeUndefined();

if (!delegation) return;

const formattedStartDate = toddMMYYYY(delegation.startDate).replace(
/\//g,
"/"
);
expect(sendMail as jest.Mock).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.stringContaining(`effective à partir du ${formattedStartDate}
<span>pour une durée illimitée.</span>`),
subject: `Émission d'une demande de délégation de l'établissement ${delegator.name} (${delegator.siret})`
})
);
});

it("testing email content - with end date", async () => {
// Given
const delegate = await companyFactory({ givenName: "Some given name" });
const { user: delegatorAdmin, company: delegator } =
await userWithCompanyFactory();
const endDate = new Date("2050-10-10");

// Email
jest.mock("../../../../mailer/mailing");
(sendMail as jest.Mock).mockImplementation(() => Promise.resolve());

// When
const { errors, delegation } = await createDelegation(delegatorAdmin, {
delegateOrgId: delegate.orgId,
delegatorOrgId: delegator.orgId,
endDate: endDate.toISOString() as any
});

// Then
expect(errors).toBeUndefined();
expect(sendMail as jest.Mock).toHaveBeenCalledTimes(1);

if (!delegation) return;

const formattedStartDate = toddMMYYYY(delegation.startDate).replace(
/\//g,
"&#x2F;"
);
const formattedEndDate = toddMMYYYY(endDate).replace(/\//g, "&#x2F;");
expect(sendMail as jest.Mock).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.stringContaining(`effective à partir du ${formattedStartDate}
<span>et jusqu'au ${formattedEndDate}.</span>`),
subject: `Émission d'une demande de délégation de l'établissement ${delegator.name} (${delegator.siret})`
})
);
});

describe("authentication & roles", () => {
it("user must be authenticated", async () => {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const sendRegistryDelegationCreationEmail = async (
const payload = renderMail(registryDelegationCreation, {
variables: {
startDate: toddMMYYYY(delegation.startDate),
endDate: delegation.endDate ? toddMMYYYY(delegation.endDate) : undefined,
delegator,
delegate
},
Expand Down
20 changes: 4 additions & 16 deletions back/src/registryDelegation/resolvers/subResolvers/status.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import {
RegistryDelegation,
RegistryDelegationResolvers,
RegistryDelegationStatus
RegistryDelegationResolvers
} from "../../../generated/graphql/types";
import { getDelegationStatus } from "../utils";

/**
* For frontend's convenience, we compute the status here
*/
const getStatus = (delegation: RegistryDelegation) => {
const NOW = new Date();

const { isRevoked, startDate, endDate } = delegation;

if (isRevoked) return "CLOSED" as RegistryDelegationStatus;

if (startDate > NOW) return "INCOMING" as RegistryDelegationStatus;

if (startDate <= NOW && (!endDate || endDate > NOW))
return "ONGOING" as RegistryDelegationStatus;

return "CLOSED" as RegistryDelegationStatus;
};
const getStatus = (delegation: RegistryDelegation) =>
getDelegationStatus(delegation);

export const statusResolver: RegistryDelegationResolvers["status"] =
delegation => getStatus(delegation);
19 changes: 19 additions & 0 deletions back/src/registryDelegation/resolvers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { prisma } from "@td/prisma";
import { UserInputError } from "../../common/errors";
import { getRegistryDelegationRepository } from "../repository";
import { Company, Prisma } from "@prisma/client";
import {
RegistryDelegation,
RegistryDelegationStatus
} from "../../generated/graphql/types";

export const findDelegateAndDelegatorOrThrow = async (
delegateOrgId: string,
Expand Down Expand Up @@ -74,3 +78,18 @@ export const findDelegateOrDelegatorOrThrow = async (

return { delegator, delegate };
};

export const getDelegationStatus = (delegation: RegistryDelegation) => {
const NOW = new Date();

const { isRevoked, startDate, endDate } = delegation;

if (isRevoked) return "CLOSED" as RegistryDelegationStatus;

if (startDate > NOW) return "INCOMING" as RegistryDelegationStatus;

if (startDate <= NOW && (!endDate || endDate > NOW))
return "ONGOING" as RegistryDelegationStatus;

return "CLOSED" as RegistryDelegationStatus;
};
1 change: 1 addition & 0 deletions libs/back/mail/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export const registryDelegationCreation: MailTemplate<{
startDate: string;
delegator: Company;
delegate: Company;
endDate?: string;
}> = {
subject: ({ delegator }) =>
`Émission d'une demande de délégation de l'établissement ${delegator.name} (${delegator.siret})`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<p>
La plateforme Trackdéchets vous informe qu'une délégation a été accordée par
l'établissement {{delegator.name}} ({{delegator.siret}}) à l'établissement
{{delegate.name}} ({{delegate.siret}}), effective à partir du {{startDate}}.
Cette délégation permet à l'établissement {{delegate.name}}
{{delegate.name}} ({{delegate.siret}}), effective à partir du {{startDate}}
{{#endDate}}
<span>et jusqu'au {{endDate}}.</span>
{{/endDate}} {{^endDate}}
<span>pour une durée illimitée.</span>
{{/endDate}} Cette délégation permet à l'établissement {{delegate.name}}
({{delegate.siret}}) de déclarer des registres de déchets règlementaires au
nom de l'établissement {{delegator.name}} ({{delegator.siret}}).
</p>
Expand Down

0 comments on commit b75c020

Please sign in to comment.