Skip to content

Commit

Permalink
Mise à jour des dépendances de Mongo (#4740)
Browse files Browse the repository at this point in the history
* chore: maj des dépendances mongo

* fix: breacking changes de la maj de mongo

* fix: erreur d'instanciation avec mongo

* feat: passage en mode async/await pour la nouvelle version de mongoose

* ajout d'un mock autour des survey

* feat: mise à jour de la pertinance des tests

* mise à jour du test
  • Loading branch information
jenovateurs authored Jan 30, 2025
1 parent cb7b7ca commit 574504d
Show file tree
Hide file tree
Showing 9 changed files with 726 additions and 340 deletions.
30 changes: 13 additions & 17 deletions backend/controllers/followups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@ import { sendSimulationResultsEmail } from "../lib/messaging/email/email-service
import { sendSimulationResultsSms } from "../lib/messaging/sms/sms-service.js"
import { ErrorType, ErrorStatus, ErrorName } from "../../lib/enums/error.js"

export function followup(
export async function followup(
req: Request,
res: Response,
next: NextFunction,
id: string
) {
Followups.findById(id)
.populate("simulation")
.exec(function (err: any, followup: Followup | null) {
if (err) {
return next(err)
}
// no matching followup or wrong or missing access token
if (
!followup?.accessToken ||
followup.accessToken !== req?.query?.token
) {
return res.redirect("/")
}
req.followup = followup
simulationController.simulation(req, res, next, followup.simulation)
})
try {
const followup = await Followups.findById(id).populate("simulation").exec()

if (!followup?.accessToken || followup.accessToken !== req?.query?.token) {
return res.redirect("/")
}

req.followup = followup
simulationController.simulation(req, res, next, followup.simulation)
} catch (err) {
return next(err)
}
}

async function createSimulationRecapUrl(req: Request, res: Response) {
Expand Down
51 changes: 30 additions & 21 deletions backend/controllers/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,40 @@ function setSimulationOnRequest(req: Request, simulation: Simulation) {
req.situation = generateSituation(req.simulation)
}

function simulation(
async function simulation(
req: Request,
res,
next,
simulationOrSimulationId: Simulation | Simulation["_id"] | string
) {
if (
simulationOrSimulationId &&
typeof simulationOrSimulationId === "object" &&
simulationOrSimulationId._id
"_id" in simulationOrSimulationId
) {
const simulation = simulationOrSimulationId as Simulation
setSimulationOnRequest(req, simulation)
return next()
}

const simulationId = simulationOrSimulationId as Simulation["_id"]
Simulations.findById(simulationId, (err, simulation) => {
if (!simulation) return res.sendStatus(404)
if (err) return next(err)
try {
const simulationId = simulationOrSimulationId as Simulation["_id"]
const simulation = await Simulations.findById(simulationId)

if (!simulation) {
return res.sendStatus(404)
}

setSimulationOnRequest(req, simulation)
next()
})
} catch (err) {
next(err)
}
}

function attachAccessCookie(req: Request, res, next?) {
const cookiesParameters = {
maxAge: 7 * 24 * 3600 * 1000,
maxAge: 604800000, // Duration: 7 days in milliseconds
sameSite: config.baseURL.startsWith("https") ? "none" : "lax",
secure: config.baseURL.startsWith("https"),
}
Expand All @@ -56,7 +63,7 @@ function attachAccessCookie(req: Request, res, next?) {
)
res.cookie(
"lastestSimulation",
req.simulation?._id.toString(),
req.simulation?._id?.toString(),
cookiesParameters
)
next && next()
Expand Down Expand Up @@ -98,23 +105,25 @@ function clearCookies(req: Request, res) {
}
}

function create(req: Request, res, next) {
if (req.body._id)
async function create(req: Request, res, next) {
if (req.body._id) {
return res.status(403).send({
error:
"You cant provide _id when saving a situation. _id will be generated automatically.",
"You can't provide _id when saving a situation. _id will be generated automatically.",
})
}

return Simulations.create(
omit(req.body, "createdAt", "status", "token"),
(err, persistedSimulation) => {
if (err) return next(err)
try {
const persistedSimulation = await Simulations.create(
omit(req.body, "createdAt", "status", "token")
)

clearCookies(req, res)
req.simulation = persistedSimulation
next && next()
}
)
clearCookies(req, res)
req.simulation = persistedSimulation
next && next()
} catch (err) {
next(err)
}
}

function openfiscaResponse(req: Request, res, next) {
Expand Down
5 changes: 4 additions & 1 deletion backend/models/followup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { SurveyType } from "../../lib/enums/survey.js"
import { Followup } from "../../lib/types/followup.d.js"
import { FollowupModel } from "../types/models.d.js"
import FollowupSchema from "./followup-schema.js"
import SurveySchema from "./survey-schema.js"

FollowupSchema.static("findByEmail", function (email: string) {
return this.find({ email })
})

const SurveyModel = mongoose.model<Survey>("Survey", SurveySchema)
FollowupSchema.method(
"addSurveyIfMissing",
async function (surveyType: SurveyType): Promise<Survey> {
let survey = this.surveys.find((survey) => survey.type === surveyType)
if (!survey) {
survey = await this.surveys.create({ type: surveyType })
survey = new SurveyModel({ type: surveyType })
this.surveys.push(survey)
await this.save()
}
return survey
}
Expand Down
2 changes: 1 addition & 1 deletion backend/models/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ SimulationSchema.method("getSituation", function () {

SimulationSchema.method("compute", function (showPrivate) {
const situation = this.getSituation()
const id = this._id
const id = String(this._id)
return new Promise(function (resolve, reject) {
openfisca.calculate(situation, function (err, openfiscaResponse) {
if (err) {
Expand Down
1 change: 0 additions & 1 deletion lib/types/followup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ interface FollowupAttributes {
interface FollowupMethods {
addSurveyIfMissing(surveyType: SurveyType): Promise<Survey>
updateSurvey(action: SurveyType, data?: any)
updateTemporarySimulationSurvey()
}

interface FollowupVirtuals {
Expand Down
Loading

0 comments on commit 574504d

Please sign in to comment.