Skip to content

Commit

Permalink
feat(etape): retourne l'étape courante en fonction des informations f…
Browse files Browse the repository at this point in the history
…ournies SocialGouv#30
  • Loading branch information
risseraka committed Mar 10, 2021
1 parent 968dfed commit e808e41
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 70 deletions.
17 changes: 17 additions & 0 deletions back/hasura/metadata/actions.graphql
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
type Query {
getCurrentEtape (
infos: Informations!
): CurrentEtape
}

input Informations {
projet : Boolean
conception : Boolean
grossesse : Boolean
enfant : Boolean
enfants : Boolean
date : String
}

type CurrentEtape {
id : String
date : String
}
21 changes: 18 additions & 3 deletions back/hasura/metadata/actions.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
actions: []
actions:
- name: getCurrentEtape
definition:
kind: ""
handler: http://51.91.58.234:1337/etapes/get-current
forward_client_headers: true
custom_types:
enums: []
input_objects: []
objects: []
input_objects:
- name: Informations
objects:
- name: CurrentEtape
relationships:
- remote_table:
schema: public
name: etapes
name: etape
type: object
field_mapping:
id: id
scalars: []
8 changes: 8 additions & 0 deletions back/strapi/api/etape/config/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
"policies": []
}
},
{
"method": "POST",
"path": "/etapes/get-current",
"handler": "etape.getCurrent",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/etapes/count",
Expand Down
103 changes: 102 additions & 1 deletion back/strapi/api/etape/controllers/etape.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,105 @@
* to customize this controller
*/

module.exports = {};
const ETAPE_PROJET = 1
const ETAPE_CONCEPTION = 2
const ETAPE_GROSSESSE_DEBUT = 3
const ETAPE_GROSSESSE_SUITE_FIN = 4
const ETAPE_ACCOUCHEMENT = 5
const ETAPE_ENFANT_3_PREMIERS_MOIS = 6
const ETAPE_ENFANT_4_MOIS_1_AN = 7
const ETAPE_ENFANT_1_AN_2_ANS = 8

const JOUR = 24 * 3600 * 1000
const SEMAINE = JOUR * 7

const GROSSESSE_TRIMESTRE_1 = 0
const GROSSESSE_TRIMESTRE_2 = SEMAINE * 16
const GROSSESSE_TRIMESTRE_3 = SEMAINE * 27
const GROSSESSE_TOTAL_SA = SEMAINE * 41

const ENFANT_3_MOIS = JOUR * 30 * 3
const ENFANT_1_AN = JOUR * 365
const ENFANT_2_ANS = JOUR * 365 * 2

const calcGrossesse = (ctx, terme) => {
const now = new Date()

const grossesseDebut = new Date(terme.getTime() - GROSSESSE_TOTAL_SA)

if (grossesseDebut > now) {
return ctx.badRequest('terme too much in the future')
} else if (terme < now) {
return ctx.badRequest('terme in the past')
}

const trimestre2 = new Date(grossesseDebut.getTime() + GROSSESSE_TRIMESTRE_2)

return now < trimestre2 ? ETAPE_GROSSESSE_DEBUT : ETAPE_GROSSESSE_SUITE_FIN
}

const calcEnfant = (ctx, naissance) => {
const now = new Date()

if (naissance > now) {
return ctx.badRequest('naissance in the future')
}

const naissanceTime = naissance.getTime()
const enfant3Mois = new Date(naissanceTime + ENFANT_3_MOIS)
const enfant1An = new Date(naissanceTime + ENFANT_1_AN)
const enfant2Ans = new Date(naissanceTime + ENFANT_2_ANS)

if (now > enfant2Ans) {
return ctx.badRequest('enfant > 2 ans')
} else if (now > enfant1An) {
return ETAPE_ENFANT_1_AN_2_ANS
} else if (now > enfant3Mois) {
return ETAPE_ENFANT_4_MOIS_1_AN
}
return ETAPE_ENFANT_3_PREMIERS_MOIS
}

module.exports = {
async getCurrent(ctx) {
if (
!ctx.request.body ||
!ctx.request.body.input ||
!ctx.request.body.input.infos
) {
return ctx.badRequest('missing informations')
}

const infos = ctx.request.body.input.infos

if (infos.grossesse || infos.enfant || infos.enfants) {
const dateString = infos.date

if (!dateString) {
return ctx.badRequest('missing date')
}

const date = new Date(dateString)

if (date.toString() === 'Invalid Date') {
return ctx.badRequest('invalid date')
}

let id

if (infos.grossesse) {
id = calcGrossesse(ctx, date)
} else {
id = calcEnfant(ctx, date)
}

return { id, date }
} else if (infos.projet) {
return { id: ETAPE_PROJET }
} else if (infos.conception) {
return { id: ETAPE_CONCEPTION }
}

return { id: null }
}
};
2 changes: 1 addition & 1 deletion back/strapi/api/parcours/models/parcours.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"type": "text"
},
"etapes": {
"collection": "etape",
"via": "parcours",
"collection": "etape",
"dominant": true
}
}
Expand Down
Loading

0 comments on commit e808e41

Please sign in to comment.