Skip to content

Commit

Permalink
fix(interaction): fix date of last interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Nov 29, 2023
1 parent f5f988f commit 0e18a3c
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 98 deletions.
4 changes: 4 additions & 0 deletions packages/backend/src/auth/services/structures-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const APP_USER_PUBLIC_ATTRIBUTES: (keyof UserStructurePublic)[] = [
"lastLogin",
"acceptTerms",
"role",
"lastLogin",
"createdAt",
];

@Injectable()
Expand Down Expand Up @@ -129,6 +131,8 @@ export class StructuresAuthService {
"responsable",
"structureType",
"ville",
"lastLogin",
"createdAt",
"sms",
"portailUsager",
"acceptTerms",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { In, IsNull } from "typeorm";
import { In, IsNull, Like, Not } from "typeorm";
import { getDateForMonthInterval } from "../../../stats/services";
import { FranceRegion } from "../../../util/territoires";
import { Usager, UserStructureAuthenticated } from "../../../_common/model";
Expand All @@ -13,14 +13,14 @@ import { InteractionType } from "@domifa/common";
export const interactionRepository = myDataSource
.getRepository<Interactions>(InteractionsTable)
.extend({
findLastInteractionOk,
findLastInteractionInWithContent,
countInteractionsByMonth,
countPendingInteraction,
countPendingInteractionsIn,
countVisiteOut,
updateInteractionAfterDistribution,
totalInteractionAllUsagersStructure,
getLastInteractionOut,
});

async function updateInteractionAfterDistribution(
Expand All @@ -40,25 +40,6 @@ async function updateInteractionAfterDistribution(
);
}

async function findLastInteractionOk({
user,
usager,
}: {
user: Pick<UserStructureAuthenticated, "structureId">;
usager: Pick<Usager, "uuid">;
}): Promise<Interactions> {
const lastInteractions = await interactionRepository.findOne({
where: {
structureId: user.structureId,
usagerUUID: usager.uuid,
type: In(INTERACTION_OK_LIST),
},
order: { dateInteraction: "DESC" },
});

return lastInteractions ?? undefined;
}

async function findLastInteractionInWithContent({
user,
usager,
Expand Down Expand Up @@ -265,3 +246,27 @@ async function totalInteractionAllUsagersStructure({
})
);
}

async function getLastInteractionOut(
usager: Pick<Usager, "uuid">
): Promise<Pick<Interactions, "uuid" | "dateInteraction"> | null> {
return interactionRepository.findOne({
where: [
{
usagerUUID: usager.uuid,
type: In(INTERACTION_OK_LIST),
content: Not(Like(`'%Courrier remis au mandataire%' `)),
},
{
usagerUUID: usager.uuid,
type: In(INTERACTION_OK_LIST),
content: IsNull(),
},
],
select: {
dateInteraction: true,
uuid: true,
},
order: { dateInteraction: "DESC" },
});
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { Injectable, Inject, forwardRef } from "@nestjs/common";
import {
interactionRepository,
usagerRepository,
userUsagerLoginRepository,
} from "../../database";
import { interactionRepository, usagerRepository } from "../../database";
import { MessageSmsService } from "../../sms/services/message-sms.service";
import {
Interactions,
Usager,
Structure,
UsagerLight,
INTERACTION_OK_LIST,
} from "../../_common/model";
import { interactionsCreator } from "./interactionsCreator.service";
import { interactionsTypeManager } from "./interactionsTypeManager.service";
import { In, Like, Not } from "typeorm";
import { differenceInCalendarDays } from "date-fns";
import { getLastInteractionOut } from "./getLastInteractionDate.service";

@Injectable()
export class InteractionsDeletor {
Expand Down Expand Up @@ -67,51 +61,11 @@ export class InteractionsDeletor {
});
}

if (INTERACTION_OK_LIST.indexOf(interaction.type) !== -1) {
const lastInteractionOut = await interactionRepository.findOne({
where: {
usagerUUID: usager.uuid,
type: In(INTERACTION_OK_LIST),
content: Not(Like("%Courrier remis au mandataire%")),
uuid: Not(interaction.uuid),
},
select: {
dateInteraction: true,
},
order: { dateInteraction: "DESC" },
});

usager.lastInteraction.dateInteraction =
lastInteractionOut?.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 (
differenceInCalendarDays(
usager.lastInteraction.dateInteraction,
lastUserUsagerLogin.createdAt
) > 0
) {
usager.lastInteraction.dateInteraction =
lastUserUsagerLogin.createdAt;
}
}
}
usager.lastInteraction.dateInteraction = await getLastInteractionOut(
usager,
structure,
interaction
);

return await interactionsCreator.updateUsagerAfterCreation({
usager,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { differenceInCalendarDays } from "date-fns";
import {
INTERACTION_OK_LIST,
Interactions,
Structure,
Usager,
} from "../../_common/model";

import {
interactionRepository,
userUsagerLoginRepository,
} from "../../database";
export const getLastInteractionOut = async (
usager: Usager,
structure: Pick<Structure, "id" | "sms" | "telephone" | "portailUsager">,
interaction?: Interactions
) => {
let dateInteraction = await getDateFromInteraction(usager, interaction);
dateInteraction = await getDateFromUserLogin(
usager,
structure,
dateInteraction
);

return shouldReturnDateInteraction(usager, dateInteraction)
? dateInteraction
: usager.decision.dateDebut;
};

const getDateFromInteraction = async (
usager: Usager,
interaction: Interactions
): Promise<Date | null> => {
if (
(interaction && INTERACTION_OK_LIST.includes(interaction.type)) ||
!interaction
) {
const lastInteractionOut =
await interactionRepository.getLastInteractionOut(usager);
return lastInteractionOut ? lastInteractionOut.dateInteraction : null;
}
return null;
};

const getDateFromUserLogin = async (
usager: Usager,
structure: Pick<Structure, "id" | "sms" | "telephone" | "portailUsager">,
dateInteraction: Date | null
): Promise<Date | null> => {
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 (
!dateInteraction ||
differenceInCalendarDays(
lastUserUsagerLogin.createdAt,
dateInteraction
) > 0
) {
return lastUserUsagerLogin.createdAt;
}
}
}
return dateInteraction;
};

const shouldReturnDateInteraction = (
usager: Usager,
dateInteraction: Date | null
): boolean => {
return (
dateInteraction &&
differenceInCalendarDays(
dateInteraction,
new Date(usager.decision.dateDebut)
) > 0
);
};
15 changes: 9 additions & 6 deletions packages/backend/src/sms/services/message-sms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ import { generateScheduleSendDate } from "./generators/generateScheduleSendDate"
import { getPhoneString } from "../../util/phone/phoneUtils.service";
import { interactionsTypeManager } from "../../interactions/services";
import { PhoneNumberFormat } from "google-libphonenumber";
import { INTERACTIONS_IN, INTERACTIONS_OUT } from "@domifa/common";
import {
INTERACTIONS_IN,
INTERACTIONS_OUT,
InteractionType,
} from "@domifa/common";

@Injectable()
export class MessageSmsService {
// Suppression d'un SMS si le courrier a été distribué
public async deleteSmsInteractionOut(
usager: Pick<Usager, "ref" | "contactByPhone">,
structure: Pick<Structure, "id" | "sms" | "telephone">,
interaction: InteractionDto
interactionType: InteractionType
) {
if (!structure.sms.enabledByDomifa && !structure.sms.enabledByStructure) {
return null;
Expand All @@ -35,7 +39,7 @@ export class MessageSmsService {
const smsOnHold = await messageSmsRepository.findSmsOnHold({
usagerRef: usager.ref,
structureId: structure.id,
interactionType: interaction.type,
interactionType,
});

if (smsOnHold) {
Expand Down Expand Up @@ -145,7 +149,7 @@ export class MessageSmsService {
if (
structure.sms.enabledByDomifa &&
structure.sms.enabledByStructure &&
usager.contactByPhone === true
usager.contactByPhone
) {
// Courrier / Colis / Recommandé entrant = Envoi de SMS à prévoir
if (INTERACTIONS_IN.includes(interaction.type)) {
Expand All @@ -156,8 +160,7 @@ export class MessageSmsService {
const inType = interactionsTypeManager.getOppositeDirectionalType({
type: interaction.type,
});
interaction.type = inType;
await this.deleteSmsInteractionOut(usager, structure, interaction);
await this.deleteSmsInteractionOut(usager, structure, inType);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import { AuthGuard } from "@nestjs/passport";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { Response } from "express";

import {
interactionRepository,
usagerNotesRepository,
usagerRepository,
} from "../../database";
import { usagerNotesRepository, usagerRepository } from "../../database";

import {
Usager,
Expand All @@ -41,6 +37,7 @@ import {
UsagerDecision,
} from "@domifa/common";
import { format } from "date-fns";
import { getLastInteractionOut } from "../../interactions/services/getLastInteractionDate.service";

@Controller("usagers-decision")
@ApiTags("usagers-decision")
Expand Down Expand Up @@ -145,20 +142,15 @@ export class UsagersDecisionController {
// On récupère la dernière décision
usager.decision = usager.historique[usager.historique.length - 1];

const lastInteractionOk = await interactionRepository.findLastInteractionOk(
{
user,
usager,
}
usager.lastInteraction.dateInteraction = await getLastInteractionOut(
usager,
user.structure
);

// Si aucune interaction est trouvée, on remet la date de la décision actuelle
usager.lastInteraction.dateInteraction = lastInteractionOk
? lastInteractionOk.dateInteraction
: usager.decision.dateDebut
? usager.decision.dateDebut
: // Cas extrême, aucune date définie
usager.decision.dateDecision;
if (!usager.lastInteraction.dateInteraction) {
usager.lastInteraction.dateInteraction =
usager.decision.dateDebut ?? usager.decision.dateDecision;
}

const createdBy = {
userId: user.id,
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/assets/files/news.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"date": "2023-11-30",
"description": "Nouveautés du 30 Novembre 2023",
"description": "",
"content": [
{
"type": "new",
Expand Down

0 comments on commit 0e18a3c

Please sign in to comment.