Skip to content

Commit

Permalink
feat: ajoute la condition d'envoi du sondage de 3 jours supplémentair…
Browse files Browse the repository at this point in the history
…es après l'envoi du sondage par email
  • Loading branch information
Shamzic committed Nov 17, 2023
1 parent 81b444b commit 9803602
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
28 changes: 27 additions & 1 deletion backend/lib/messaging/sending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EmailCategory, SmsCategory } from "../../../lib/enums/messaging.js"
import { SurveyCategory } from "../../../lib/enums/survey.js"
import Followups from "../../models/followup.js"
import { Followup } from "../../../lib/types/followup.js"
import { Survey } from "../../../lib/types/survey.js"

const DaysBeforeInitialSms = 6
const DaysBeforeInitialEmail = 6
Expand Down Expand Up @@ -150,6 +151,31 @@ export async function processSendEmails(
}
}

function getEmailSurvey(followup: Followup): Survey | undefined {
return followup.surveys.find((survey) =>
[
SurveyCategory.TrackClickOnBenefitActionEmail,
SurveyCategory.TrackClickOnSimulationUsefulnessEmail,
].includes(survey.type)
)
}

export function shouldSendSurveyBySms(followup: Followup, today?): boolean {
if (followup.phone && !followup.email) {
return true
}

if (followup.phone && followup.email) {
const emailSurvey = getEmailSurvey(followup)
if (emailSurvey && emailSurvey.answers.length === 0) {
const surveyEmailCreatedAt = dayjs(emailSurvey.createdAt)
const surveyEmailCreatedAtPlus3Days = surveyEmailCreatedAt.add(3, "day")
return (today || dayjs()).isAfter(surveyEmailCreatedAtPlus3Days)
}
}
return false
}

async function sendMultipleInitialSms(limit: number) {
const followups: any[] = await Followups.find({
surveys: {
Expand All @@ -173,7 +199,7 @@ async function sendMultipleInitialSms(limit: number) {
.limit(limit)

const results: { ok?: any; ko?: any }[] = await Promise.all(
followups.map(async (followup: Followup) => {
followups.filter(shouldSendSurveyBySms).map(async (followup: Followup) => {
try {
const result = await followup.sendSurveyBySms(
SurveyCategory.TrackClickOnBenefitActionSms
Expand Down
1 change: 1 addition & 0 deletions lib/types/survey.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Survey {
messageId?: string
error?: string | any
type: SurveyCategory
createdAt?: string | Date
}

interface FetchSurvey {
Expand Down
76 changes: 76 additions & 0 deletions tests/unit/lib/messaging/messaging-sms.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { expect } from "@jest/globals"
import { shouldSendSurveyBySms } from "@backend/lib/messaging/sending.js"
import dayjs from "dayjs"

describe("shouldSendSurveyBySms condition tests", () => {
let followup
beforeEach(() => {
followup = {
surveys: [],
phone: "0600000000",
email: "test@test.fr",
sentAt: dayjs("2023-11-17"),
smsSentAt: dayjs("2023-11-17"),
smsSurveySentAt: dayjs("2023-11-17"),
}
})

it("should not sent sms without phone field inside the followup", async () => {
followup.phone = undefined
expect(shouldSendSurveyBySms(followup, dayjs("2023-11-19"))).toBe(false)
})

it("should sent sms without email field inside the followup", async () => {
followup.email = undefined
expect(shouldSendSurveyBySms(followup, dayjs("2023-11-19"))).toBe(true)
})

it("should not send sms if followup has phone and already track-click-on-benefit-action-sms survey", async () => {
followup.surveys = [
{
type: "track-click-on-benefit-action-sms",
createdAt: dayjs("2023-11-16"),
answers: [],
},
]
expect(shouldSendSurveyBySms(followup, dayjs("2023-11-19"))).toBe(false)
})

it("should not send sms if followup has both phone and email sended 2 days ago without answers", async () => {
followup.surveys = [
{
type: "track-click-on-simulation-usefulness-email",
createdAt: dayjs("2023-11-17"),
answers: [],
},
]
expect(shouldSendSurveyBySms(followup, dayjs("2023-11-19"))).toBe(false)
})

it("should send sms if followup has both phone and email sended more than 3 days ago without answers", async () => {
followup.surveys = [
{
type: "track-click-on-simulation-usefulness-email",
createdAt: dayjs("2023-11-16").subtract(1, "hour"),
answers: [],
},
]
expect(shouldSendSurveyBySms(followup, dayjs("2023-11-19"))).toBe(true)
})

it("should not send sms if followup has both phone and email sended more than 3 days ago and email survey answers", async () => {
followup.surveys = [
{
type: "track-click-on-simulation-usefulness-email",
createdAt: dayjs("2023-11-16").subtract(1, "hour"),
answers: [
{
question: "mock-question",
value: "mock-value",
},
],
},
]
expect(shouldSendSurveyBySms(followup, dayjs("2023-11-19"))).toBe(false)
})
})

0 comments on commit 9803602

Please sign in to comment.