-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(interaction): fix date of last interaction
- Loading branch information
Showing
7 changed files
with
142 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/backend/src/interactions/services/getLastInteractionDate.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters