Skip to content

Commit

Permalink
Merge branch 'dev' into tra-15098
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitguigal authored Nov 27, 2024
2 parents aafde0d + fecb7b8 commit 3ffef8b
Show file tree
Hide file tree
Showing 38 changed files with 1,570 additions and 384 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ et le projet suit un schéma de versionning inspiré de [Calendar Versioning](ht

# [2024.12.1] 17/12/2024


#### :nail_care: Améliorations

- Permettre au transporteur étranger d'avoir les mêmes droits qu'un transporteur FR concernant la révision sur une Annexe 1 [PR 3770](https://github.com/MTES-MCT/trackdechets/pull/3770)

#### :bug: Corrections de bugs

- Ne pas doubler les quantités restantes à regrouper lorsqu'on modifie un bordereau de groupement [PR 3760](https://github.com/MTES-MCT/trackdechets/pull/3760)


# [2024.11.1] 19/11/2024

#### :rocket: Nouvelles fonctionnalités
Expand Down
190 changes: 188 additions & 2 deletions apps/cron/src/commands/__tests__/onboarding.helpers.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import {
import { prisma } from "@td/prisma";
import {
associateUserToCompany,
endOfDay,
inXDays,
Mutation,
MutationDeleteCompanyArgs
MutationDeleteCompanyArgs,
todayAtMidnight,
toddMMYYYY
} from "back";
import { resetDatabase } from "libs/back/tests-integration";
import makeClient from "back/src/__tests__/testClient";
Expand All @@ -20,6 +24,7 @@ import {
userFactory,
userWithCompanyFactory
} from "back/src/__tests__/factories";
import { registryDelegationFactory } from "back/src/registryDelegation/__tests__/factories";
import { bsdaFactory } from "back/src/bsda/__tests__/factories";
import { bsdasriFactory } from "back/src/bsdasris/__tests__/factories";
import {
Expand All @@ -32,7 +37,8 @@ import {
getPendingMembershipRequestsAndAssociatedSubscribers,
getRecentlyRegisteredProducers,
getRecentlyRegisteredProfesionals,
getRecentlyRegisteredUsersWithNoCompanyNorMembershipRequest
getRecentlyRegisteredUsersWithNoCompanyNorMembershipRequest,
getExpiringRegistryDelegationWarningMailPayloads
} from "../onboarding.helpers";
import { xDaysAgo } from "../helpers";

Expand All @@ -42,6 +48,10 @@ const TWO_DAYS_AGO = xDaysAgo(TODAY, 2);
const THREE_DAYS_AGO = xDaysAgo(TODAY, 3);
const FOUR_DAYS_AGO = xDaysAgo(TODAY, 4);

const NOW = todayAtMidnight();
const YESTERDAY = xDaysAgo(NOW, 1);
const NOW_PLUS_SEVEN_DAYS = endOfDay(inXDays(NOW, 7));

export const DELETE_COMPANY = `
mutation DeleteCompany($id: ID!) {
deleteCompany(id: $id) {
Expand Down Expand Up @@ -2061,3 +2071,179 @@ describe("addPendingApprovalsCompanyAdmins", () => {
expect(wrappedRequests[0].approvals.length).toBe(0);
});
});

describe("getExpiringRegistryDelegationWarningMailPayloads", () => {
afterEach(resetDatabase);

it("should return nothing if no expiring delegation", async () => {
// Given

// When
const mails = await getExpiringRegistryDelegationWarningMailPayloads();

// Then
expect(mails.length).toBe(0);
});

it("should return a warning email to expired delegation's involved users", async () => {
// Given
const {
delegation,
delegatorUser,
delegateUser,
delegatorCompany,
delegateCompany
} = await registryDelegationFactory({
startDate: YESTERDAY,
endDate: NOW_PLUS_SEVEN_DAYS
});

// When
const mails = await getExpiringRegistryDelegationWarningMailPayloads();

// Then
expect(mails.length).toBe(1);
const sortFn = (a, b) => a.email.localeCompare(b.email);
expect(mails[0].to?.sort(sortFn)).toMatchObject(
[
{ email: delegatorUser.email, name: delegatorUser.name },
{ email: delegateUser.email, name: delegateUser.name }
].sort(sortFn)
);
expect(mails[0].subject).toBe(
`Expiration prochaine de la délégation entre l'établissement ${delegatorCompany.orgId} et l'établissement ${delegateCompany.orgId}`
);
expect(mails[0].body).toBe(`<p>
La plateforme Trackdéchets vous informe que la délégation accordée par
l'établissement ${delegatorCompany.name} (${
delegatorCompany.orgId
}) à l'établissement
${delegateCompany.name} (${
delegateCompany.orgId
}), effective depuis le ${toddMMYYYY(delegation.startDate).replace(
/\//g,
"&#x2F;"
)},
arrivera à expiration dans 7 jours, soit le ${toddMMYYYY(
delegation.endDate!
).replace(/\//g, "&#x2F;")}.
</p>
<p>
Pour en savoir plus sur les délégations et découvrir comment prolonger cette
période, nous vous invitons à consulter cet
<a
href="https://faq.trackdechets.fr/inscription-et-gestion-de-compte/gerer-son-compte/modifier-les-informations-de-son-compte#visualiser-lensemble-des-collaborateurs-ayant-acces-a-mon-etablissement"
>
article de notre FAQ </a
>.
</p>
`);
});

it("should send a warning email to expired delegation's involved users - multiple delegations involved", async () => {
// Given
const {
delegation: delegation1,
delegatorUser: delegatorUser1,
delegateUser: delegateUser1,
delegatorCompany: delegatorCompany1,
delegateCompany: delegateCompany1
} = await registryDelegationFactory({
startDate: YESTERDAY,
endDate: NOW_PLUS_SEVEN_DAYS
});

const {
delegation: delegation2,
delegatorUser: delegatorUser2,
delegateUser: delegateUser2,
delegatorCompany: delegatorCompany2,
delegateCompany: delegateCompany2
} = await registryDelegationFactory({
startDate: YESTERDAY,
endDate: NOW_PLUS_SEVEN_DAYS
});

// When
const mails = await getExpiringRegistryDelegationWarningMailPayloads();

// Then
expect(mails.length).toBe(2);
const sortFn = (a, b) => a.email.localeCompare(b.email);

// Mail 1
expect(mails[0].to?.sort(sortFn)).toMatchObject(
[
{ email: delegatorUser1.email, name: delegatorUser1.name },
{ email: delegateUser1.email, name: delegateUser1.name }
].sort(sortFn)
);
expect(mails[0].subject).toBe(
`Expiration prochaine de la délégation entre l'établissement ${delegatorCompany1.orgId} et l'établissement ${delegateCompany1.orgId}`
);
expect(mails[0].body).toBe(`<p>
La plateforme Trackdéchets vous informe que la délégation accordée par
l'établissement ${delegatorCompany1.name} (${
delegatorCompany1.orgId
}) à l'établissement
${delegateCompany1.name} (${
delegateCompany1.orgId
}), effective depuis le ${toddMMYYYY(delegation1.startDate).replace(
/\//g,
"&#x2F;"
)},
arrivera à expiration dans 7 jours, soit le ${toddMMYYYY(
delegation1.endDate!
).replace(/\//g, "&#x2F;")}.
</p>
<p>
Pour en savoir plus sur les délégations et découvrir comment prolonger cette
période, nous vous invitons à consulter cet
<a
href="https://faq.trackdechets.fr/inscription-et-gestion-de-compte/gerer-son-compte/modifier-les-informations-de-son-compte#visualiser-lensemble-des-collaborateurs-ayant-acces-a-mon-etablissement"
>
article de notre FAQ </a
>.
</p>
`);

// Mail 2
expect(mails[1].to?.sort(sortFn)).toMatchObject(
[
{ email: delegatorUser2.email, name: delegatorUser2.name },
{ email: delegateUser2.email, name: delegateUser2.name }
].sort(sortFn)
);
expect(mails[1].subject).toBe(
`Expiration prochaine de la délégation entre l'établissement ${delegatorCompany2.orgId} et l'établissement ${delegateCompany2.orgId}`
);
expect(mails[1].body).toBe(`<p>
La plateforme Trackdéchets vous informe que la délégation accordée par
l'établissement ${delegatorCompany2.name} (${
delegatorCompany2.orgId
}) à l'établissement
${delegateCompany2.name} (${
delegateCompany2.orgId
}), effective depuis le ${toddMMYYYY(delegation2.startDate).replace(
/\//g,
"&#x2F;"
)},
arrivera à expiration dans 7 jours, soit le ${toddMMYYYY(
delegation2.endDate!
).replace(/\//g, "&#x2F;")}.
</p>
<p>
Pour en savoir plus sur les délégations et découvrir comment prolonger cette
période, nous vous invitons à consulter cet
<a
href="https://faq.trackdechets.fr/inscription-et-gestion-de-compte/gerer-son-compte/modifier-les-informations-de-son-compte#visualiser-lensemble-des-collaborateurs-ayant-acces-a-mon-etablissement"
>
article de notre FAQ </a
>.
</p>
`);
});
});
59 changes: 57 additions & 2 deletions apps/cron/src/commands/onboarding.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import {
getCompaniesAndSubscribersByCompanyOrgIds,
formatDate,
sendMail,
UserNotification
UserNotification,
getDelegationNotifiableUsers,
getRegistryDelegationsExpiringInDays,
toddMMYYYY
} from "back";
import { prisma } from "@td/prisma";
import {
Expand All @@ -28,7 +31,9 @@ import {
pendingMembershipRequestEmail,
profesionalsSecondOnboardingEmail,
producersSecondOnboardingEmail,
pendingRevisionRequestEmail
pendingRevisionRequestEmail,
expiringRegistryDelegationWarning,
Mail
} from "@td/mail";
import { xDaysAgo } from "./helpers";

Expand Down Expand Up @@ -569,3 +574,53 @@ export const sendPendingRevisionRequestEmail = async (

await prisma.$disconnect();
};

export const getExpiringRegistryDelegationWarningMailPayloads = async () => {
const expiringDelegations = await getRegistryDelegationsExpiringInDays(7);

if (!expiringDelegations.length) return [];

const payloads: Mail[] = await Promise.all(
expiringDelegations
.map(async delegation => {
const users = await getDelegationNotifiableUsers(delegation);

if (!users.length) return undefined;

const variables = {
startDate: toddMMYYYY(delegation.startDate),
endDate: toddMMYYYY(delegation.endDate!),
delegator: delegation.delegator,
delegate: delegation.delegate
};

const payload = renderMail(expiringRegistryDelegationWarning, {
variables,
to: users.map(user => ({
email: user.email,
name: user.name
}))
});

return payload;
})
.filter(Boolean) as unknown as Mail[]
);

return payloads;
};

/**
* If a delegation expires in X days, warn involved companies' users.
*
* If delegation isn't even X days long, skip.
*/
export const sendExpiringRegistryDelegationWarning = async () => {
const payloads = await getExpiringRegistryDelegationWarningMailPayloads();

if (!payloads?.length) return;

await Promise.all(payloads?.map(async p => await sendMail(p)));

await prisma.$disconnect();
};
9 changes: 9 additions & 0 deletions apps/cron/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as cron from "cron";
import cronValidator from "cron-validate";
import { cleanUpIsReturnForTab, initSentry } from "back";
import {
sendExpiringRegistryDelegationWarning,
sendMembershipRequestDetailsEmail,
sendPendingMembershipRequestDetailsEmail,
sendPendingMembershipRequestEmail,
Expand Down Expand Up @@ -67,6 +68,14 @@ if (CRON_ONBOARDING_SCHEDULE) {
await sendPendingRevisionRequestEmail();
},
timeZone: TZ
}),
// delegations about to expire
new cron.CronJob({
cronTime: CRON_ONBOARDING_SCHEDULE,
onTick: async () => {
await sendExpiringRegistryDelegationWarning();
},
timeZone: TZ
})
];
}
Expand Down
3 changes: 2 additions & 1 deletion back/src/__tests__/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export const userInCompany = async (
companyAssociations: {
create: {
company: { connect: { id: companyId } },
role: role
role: role,
...getDefaultNotifications(role)
}
}
});
Expand Down
11 changes: 10 additions & 1 deletion back/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
hashToken,
xDaysAgo,
randomNbrChain,
removeSpecialCharsExceptHyphens
removeSpecialCharsExceptHyphens,
inXDays
} from "../utils";

test("getUid returns a unique identifier of fixed length", () => {
Expand Down Expand Up @@ -59,6 +60,14 @@ describe("xDaysAgo", () => {
});
});

describe("inXDays", () => {
it("should return a date in x days", () => {
const someDate = new Date("2019-10-03T10:00:00.000Z");
const threeDaysLater = inXDays(someDate, 3);
expect(threeDaysLater).toEqual(new Date("2019-10-06T00:00:00.000Z"));
});
});

describe("randomNbrChain", () => {
it("should generate random chain of numbers", async () => {
// Given
Expand Down
3 changes: 2 additions & 1 deletion back/src/companies/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export function toGqlCompanyPrivate(company: Company): CompanyPrivate {
signatureCodeRenewal: false,
bsdRefusal: false,
bsdaFinalDestinationUpdate: false,
revisionRequest: false
revisionRequest: false,
registryDelegation: false
}
};
}
4 changes: 4 additions & 0 deletions back/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ export { getBsdaFromActivityEvents } from "./activity-events/bsda";
export { getBsddFromActivityEvents } from "./activity-events/bsdd";
export { cleanUpIsReturnForTab } from "./common/elasticHelpers";
export { UserNotification } from "./users/notifications";
export {
getDelegationNotifiableUsers,
getRegistryDelegationsExpiringInDays
} from "./registryDelegation/resolvers/utils";
Loading

0 comments on commit 3ffef8b

Please sign in to comment.