Skip to content

Commit

Permalink
fix: delete interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Nov 21, 2023
1 parent 6c3c196 commit cbe0c1f
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 144 deletions.
2 changes: 2 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ fileignoreconfig:
checksum: bd05dd6b68f7fe2ca36ab1180a0449878cf78f4e048d2e5979b3dbe5fba1db53
- filename: packages/backend/src/_migrations/_init-db/1603812391580-pr-env-create-database.ts
checksum: 586e3b26481d71256a1b7626956aaf9e68211dfbda527dd862a2aad075b5cc04
- filename: packages/backend/src/interactions/services/InteractionsDeletor.service.spec.ts
checksum: b66659fea19859e55afaf0ed47519f0804857e486b2ecc6b01d5ae96f1a86a0b
- filename: yarn.lock
checksum: 4ac842f95592d194e65b208fdc4b9a78a0bb6470ad2d4950626c2ffcec831a33
version: "1.0"
2 changes: 1 addition & 1 deletion _scripts/dev/local-node.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16.18-bullseye
FROM node:18.20.0-bullseye

USER root

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { In, IsNull } from "typeorm";
import { getDateForMonthInterval } from "../../../stats/services";
import { FranceRegion } from "../../../util/territoires";
import {
Structure,
Usager,
UserStructureAuthenticated,
} from "../../../_common/model";
import { Usager, UserStructureAuthenticated } from "../../../_common/model";
import {
INTERACTION_OK_LIST,
Interactions,
} from "../../../_common/model/interaction";
import { InteractionsTable } from "../../entities";
import { myDataSource } from "../_postgres";
import { InteractionType } from "@domifa/common";
import { userUsagerLoginRepository } from "../user-usager";
import { differenceInMinutes } from "date-fns";

export const interactionRepository = myDataSource
.getRepository<Interactions>(InteractionsTable)
Expand Down Expand Up @@ -117,15 +111,12 @@ async function countPendingInteraction({

async function countPendingInteractionsIn({
usager,
structure,
}: {
usager: Pick<Usager, "uuid" | "options" | "decision">;
structure: Pick<Structure, "portailUsager">;
}): Promise<{
courrierIn: number;
recommandeIn: number;
colisIn: number;
dateInteraction: Date | null;
}> {
// NOTE: cette requête ne renvoit pas de résultats pour les usagers de cette structure qui n'ont pas d'interaction
const results: {
Expand All @@ -145,55 +136,11 @@ async function countPendingInteractionsIn({
)
.where({ usagerUUID: usager.uuid, interactionOutUUID: IsNull() })
.getRawOne();

const lastInteractionDate = await interactionRepository.findOne({
where: {
usagerUUID: usager.uuid,
type: In(INTERACTION_OK_LIST),
},
select: {
dateInteraction: true,
},
order: { dateInteraction: "DESC" },
});

const lastInteractions = {
return {
courrierIn: parseInt(results.courrierIn, 10),
recommandeIn: parseInt(results.recommandeIn, 10),
colisIn: parseInt(results.colisIn, 10),
// Si aucune interaction, on récupère la date de début du statut actuel (domicilié, radié, etc)
dateInteraction:
lastInteractionDate?.dateInteraction ?? usager.decision.dateDebut,
};

// Si le portail est activé, on récupère la date de dernière connexion
if (
structure.portailUsager.usagerLoginUpdateLastInteraction &&
usager.options.portailUsagerEnabled
) {
const lastUserUsagerLogin = await userUsagerLoginRepository.findOne({
where: {
usagerUUID: usager.uuid,
},
select: {
createdAt: true,
},
order: { createdAt: "DESC" },
});

if (lastUserUsagerLogin?.createdAt) {
if (
differenceInMinutes(
lastInteractions.dateInteraction,
lastUserUsagerLogin.createdAt
) > 0
) {
lastInteractions.dateInteraction = lastUserUsagerLogin.createdAt;
}
}
}

return lastInteractions;
}

async function countInteractionsByMonth(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export class InteractionsController {
) {
return this.interactionDeletor.deleteInteraction({
interaction,
user,
usager,
structure: user.structure,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
import { StructuresModule } from "../../structures/structure.module";
import { UsagersModule } from "../../usagers/usagers.module";
import { UsersModule } from "../../users/users.module";
import { AppTestContext, AppTestHelper } from "../../util/test";
import {
AppTestContext,
AppTestHelper,
JEST_FAKE_TIMER,
} from "../../util/test";
import { Structure, Usager, UserStructure } from "../../_common/model";
import { InteractionDto } from "../dto";
import { InteractionsModule } from "../interactions.module";
Expand All @@ -20,8 +24,14 @@ describe("InteractionsDeletor", () => {
let user: UserStructure;
let usager: Usager;
let structure: Structure;
let firstDateInteraction: Date;
const MOCKED_NEW_DATE = "2022-12-12T19:45:30.000Z";

beforeAll(async () => {
jest
.useFakeTimers(JEST_FAKE_TIMER)
.setSystemTime(new Date(MOCKED_NEW_DATE));

context = await AppTestHelper.bootstrapTestApp({
imports: [
InteractionsModule,
Expand All @@ -37,12 +47,15 @@ describe("InteractionsDeletor", () => {
id: 1,
});
usager = await usagerRepository.findOneBy({
ref: 2,
ref: 6,
structureId: 1,
});
structure = await structureRepository.findOneBy({
id: 1,
});
user.structure = structure;
const lastInteraction = { ...usager.lastInteraction };
firstDateInteraction = new Date(lastInteraction.dateInteraction);
});

afterAll(async () => {
Expand All @@ -53,14 +66,7 @@ describe("InteractionsDeletor", () => {
expect(interactionsDeletor).toBeDefined();
});

it("Réception, suppression et restauration de 5 colis", async () => {
const usagerBefore = await usagerRepository.findOneBy({
ref: 2,
structureId: 1,
});

user.structure = await structureRepository.findOneBy({ id: 5 });

it("Réception, suppression de 5 colis", async () => {
const interaction1 = new InteractionDto();
interaction1.type = "colisIn";
interaction1.content = "Colis d'un distributeur";
Expand All @@ -72,20 +78,69 @@ describe("InteractionsDeletor", () => {
user,
});

expect(usagerAfterCreate.lastInteraction.colisIn).toEqual(
usagerBefore.lastInteraction.colisIn + 5
expect(usagerAfterCreate.lastInteraction.colisIn).toEqual(5);

const usagerAfterDelete = await interactionsDeletor.deleteInteraction({
interaction: interactionCreated,
usager: usagerAfterCreate,
structure,
});
expect(usagerAfterDelete.lastInteraction.colisIn).toEqual(
usager.lastInteraction.colisIn
);
});

{
const usagerAfterDelete = await interactionsDeletor.deleteInteraction({
interaction: interactionCreated,
usager: usagerAfterCreate,
user,
structure,
});
expect(usagerAfterDelete.lastInteraction.colisIn).toEqual(
usagerBefore.lastInteraction.colisIn
);
}
it("Distribution: doit mettre à jour la date de dernier passage", async () => {
usager = await usagerRepository.findOneBy({
ref: 6,
structureId: 1,
});

const interaction1 = new InteractionDto();
interaction1.type = "colisIn";
interaction1.nbCourrier = 9;
const { usager: newUsager } = await interactionsCreator.createInteraction({
interaction: interaction1,
usager,
user,
});

// Changement de la date par défaut pour la distribution
jest
.useFakeTimers(JEST_FAKE_TIMER)
.setSystemTime(new Date("2023-01-12T19:45:30.000Z"));

const interaction: InteractionDto = {
type: "colisOut",
content: "Distribution de colis sans procuration",
nbCourrier: 9,
structureId: 1,
userId: 1,
usagerRef: usager.ref,
userName: "Nom",
dateInteraction: new Date(),
};

const resultat = await interactionsCreator.createInteraction({
usager: newUsager,
user,
interaction,
});

expect(resultat.usager.lastInteraction.colisIn).toEqual(0);
expect(new Date(resultat.usager.lastInteraction.dateInteraction)).toEqual(
new Date()
);

const test = await interactionsDeletor.deleteInteraction({
usager: resultat.usager,
structure,
interaction: resultat.interaction,
});

expect(new Date(test.lastInteraction.dateInteraction)).toEqual(
firstDateInteraction
);
expect(test.lastInteraction.colisIn).toEqual(9);
});
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { Injectable, Inject, forwardRef } from "@nestjs/common";
import { interactionRepository, usagerRepository } from "../../database";
import {
interactionRepository,
usagerRepository,
userUsagerLoginRepository,
} from "../../database";
import { MessageSmsService } from "../../sms/services/message-sms.service";
import {
Interactions,
Usager,
UserStructure,
Structure,
UsagerLight,
INTERACTION_OK_LIST,
} from "../../_common/model";
import { interactionsCreator } from "./interactionsCreator.service";
import { interactionsTypeManager } from "./interactionsTypeManager.service";
import { In } from "typeorm";
import { differenceInCalendarDays } from "date-fns";

@Injectable()
export class InteractionsDeletor {
Expand All @@ -22,18 +27,18 @@ export class InteractionsDeletor {
public async deleteInteraction({
interaction,
usager,
user,
structure,
}: {
interaction: Interactions;
usager: Usager;
user: Pick<
UserStructure,
"id" | "structureId" | "nom" | "prenom" | "structure"
>;
structure: Pick<Structure, "id" | "sms" | "telephone">;
structure: Pick<Structure, "id" | "sms" | "telephone" | "portailUsager">;
}): Promise<UsagerLight> {
const direction = interactionsTypeManager.getDirection(interaction);

await interactionRepository.delete({
uuid: interaction.uuid,
});

if (direction === "in") {
// Suppression du SMS en file d'attente
await this.smsService.deleteSmsInteraction(
Expand All @@ -53,6 +58,21 @@ export class InteractionsDeletor {
structure,
usager,
});

//
const lastInteractionOut = await interactionRepository.findOne({
where: {
usagerUUID: usager.uuid,
type: In(INTERACTION_OK_LIST),
},
select: {
dateInteraction: true,
},
order: { dateInteraction: "DESC" },
});

usager.lastInteraction.dateInteraction =
lastInteractionOut?.dateInteraction ?? usager.decision.dateDebut;
} else if (interaction.type === "npai") {
usager.options.npai.actif = false;
usager.options.npai.dateDebut = null;
Expand All @@ -62,35 +82,35 @@ export class InteractionsDeletor {
});
}

await interactionRepository.delete({
uuid: interaction.uuid,
});

const dateInteractionWithGoodTimeZone = new Date(
usager.lastInteraction.dateInteraction
);

// Récupération de la bonne date de dernière interaction
if (INTERACTION_OK_LIST.indexOf(interaction.type) !== -1) {
// Seulement si aucun autre évènement n'a déjà changé la date de dernier passage
// C'est possible si un renouvellement a été effectué après la saisie de ce courrier
if (dateInteractionWithGoodTimeZone <= interaction.dateInteraction) {
const lastInteractionOk =
await interactionRepository.findLastInteractionOk({
user,
usager,
});
// Si le portail est activé, on récupère la date de dernière connexion
if (
structure.portailUsager.usagerLoginUpdateLastInteraction &&
usager.options.portailUsagerEnabled
) {
const lastUserUsagerLogin = await userUsagerLoginRepository.findOne({
where: {
usagerUUID: usager.uuid,
},
select: {
createdAt: true,
},
order: { createdAt: "DESC" },
});

// Si aucune interaction est trouvée, on remet la date de la décision actuelle
usager.lastInteraction.dateInteraction = lastInteractionOk
? lastInteractionOk.dateInteraction
: usager.decision.dateDebut;
if (lastUserUsagerLogin?.createdAt) {
if (
differenceInCalendarDays(
usager.lastInteraction.dateInteraction,
lastUserUsagerLogin.createdAt
) > 0
) {
usager.lastInteraction.dateInteraction =
lastUserUsagerLogin.createdAt;
}
}
}

return await interactionsCreator.updateUsagerAfterCreation({
usager,
structure: user.structure,
});
}
}
Loading

0 comments on commit cbe0c1f

Please sign in to comment.