Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mise à jour des dépendances de Mongo #4740

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j'aurais ajouté un commentaire sur cette constante ou nommé de manière explicite dans une constante globale

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()
Comment on lines +13 to +21
Copy link
Contributor

@Shamzic Shamzic Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu as testé cette partie manuellement pour vérifier que c'était iso-fonctionnel ?
Ce serait bien d'ajouter un test ici je trouve

}
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
Loading
Loading