This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add badge renewal endpoints (#2319)
* feat: add badge renewal endpoints * feat: badge renewal, add controller files and test file * feat: fetch pending badge info to check if state is accepted or not * test: add change mock call to get dossier from ds api * fix: send error if dossier not found * build: remove useless import * test: add test on get post badge api * test: fix mock add necessary values * build: add annotations to createPrefillDossier returned type
- Loading branch information
1 parent
3198391
commit 866c81e
Showing
15 changed files
with
653 additions
and
295 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,52 @@ | ||
import makeDS from "@/lib/ds" | ||
import config from ".." | ||
import makeDS from '@/lib/ds'; | ||
import config from '..'; | ||
|
||
const makeFakeMathods = () => { | ||
let dossierInt = 0 | ||
return { | ||
getAllDossiersForDemarche: async(demarcheNumber) => {}, | ||
getDossierForDemarche: async(dossierNumber) => {}, | ||
createPrefillDossier: async(demarcheNumber, {}) => { | ||
dossierInt = dossierInt + 1 | ||
return { | ||
dossier_url: 'https://www.demarches-simplifiees.fr/commencer/demande?prefill_token=untoken', | ||
state: 'prefilled', | ||
dossier_id: `${dossierInt}==`, | ||
dossier_number: dossierInt, | ||
dossier_prefill_token: 'untoken' | ||
} | ||
} | ||
} | ||
} | ||
let DS_METHODS = makeFakeMathods() | ||
let dossierInt = 0; | ||
let dossiers = []; | ||
return { | ||
getAllDossiersForDemarche: async (demarcheNumber) => { | ||
return dossiers; | ||
}, | ||
getDossierForDemarche: async (dossierNumber) => { | ||
return dossiers.find((d) => d.dossier_number === dossierNumber); | ||
}, | ||
createPrefillDossier: async (demarcheNumber, {}) => { | ||
dossierInt = dossierInt + 1; | ||
const dossier = { | ||
dossier_url: | ||
'https://www.demarches-simplifiees.fr/commencer/demande?prefill_token=untoken', | ||
state: 'prefilled', | ||
dossier_id: `${dossierInt}==`, | ||
dossier_number: dossierInt, | ||
dossier_prefill_token: 'untoken', | ||
annotations: [ | ||
{ | ||
label: 'Status', | ||
stringValue: '', | ||
}, | ||
], | ||
}; | ||
dossiers.push(dossier); | ||
return dossier; | ||
}, | ||
}; | ||
}; | ||
let DS_METHODS = makeFakeMathods(); | ||
|
||
if (process.env.NODE_ENV !== 'test') { | ||
|
||
try { | ||
DS_METHODS = makeDS({ | ||
DS_TOKEN: config.DS_TOKEN | ||
}) | ||
|
||
} catch (e) { | ||
console.error(e) | ||
process.exit(1) | ||
} | ||
try { | ||
DS_METHODS = makeDS({ | ||
DS_TOKEN: config.DS_TOKEN, | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
process.exit(1); | ||
} | ||
} else { | ||
console.log('Emails will go through a FAKE email service (no mails sent).') | ||
console.log('DS wil use fake ds api.'); | ||
} | ||
|
||
export default { | ||
...DS_METHODS | ||
} | ||
...DS_METHODS, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import config from '@config'; | ||
import * as utils from '@controllers/utils'; | ||
import { MemberWithPermission } from '@models/member'; | ||
import DS from '@/config/ds/ds.config'; | ||
import { BadgeDossier } from '@/models/badgeDemande'; | ||
import { BADGE_REQUEST, BadgeRequest } from '@/models/badgeRequests'; | ||
import { getBadgeRequestWithStatus } from '@/db/dbBadgeRequests'; | ||
import db from '@/db'; | ||
import { DBUser } from '@/models/dbUser'; | ||
|
||
export async function getBadgeRenewalPage(req, res) { | ||
try { | ||
const [currentUser, dbUser]: [MemberWithPermission, DBUser] = | ||
await Promise.all([ | ||
(async () => utils.userInfos(req.auth.id, true))(), | ||
db('users').where({ username: req.auth.id }).first(), | ||
]); | ||
// const dossiers = await DS.getAllDossiersForDemarche(config.DS_DEMARCHE_NUMBER) | ||
let badgeRequest: BadgeRequest = await getBadgeRequestWithStatus( | ||
req.auth.id, | ||
BADGE_REQUEST.BADGE_RENEWAL_REQUEST_PENDING | ||
); | ||
let dossier; | ||
if (badgeRequest) { | ||
try { | ||
dossier = (await DS.getDossierForDemarche( | ||
badgeRequest.dossier_number | ||
)) as unknown as BadgeDossier; | ||
} catch (e) { | ||
// dossier is no filled yet | ||
} | ||
} | ||
const title = 'Demande de badge'; | ||
res.json({ | ||
title, | ||
dossier, | ||
currentUserId: req.auth.id, | ||
primaryEmail: dbUser.primary_email, | ||
firstName: currentUser.userInfos.fullname.split(' ')[0], | ||
lastName: currentUser.userInfos.fullname.split(' ')[1].toUpperCase(), | ||
attributaire: currentUser.userInfos.employer.split('/')[1], | ||
endDate: currentUser.userInfos.end, | ||
domain: config.domain, | ||
isExpired: currentUser.isExpired, | ||
badgeRequest, | ||
subActiveTab: 'badge', | ||
activeTab: 'account', | ||
isAdmin: config.ESPACE_MEMBRE_ADMIN.includes(req.auth.id), | ||
}); | ||
} catch (err) { | ||
return res.status(500).json({ | ||
error: req.flash('error'), | ||
}); | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
src/controllers/badgeRequestsController/postBadgeRenewalRequest.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,90 @@ | ||
import { | ||
createBadgeRequest, | ||
getBadgeRequestWithStatus, | ||
} from '@/db/dbBadgeRequests'; | ||
import { BadgeRequest, BADGE_REQUEST } from '@/models/badgeRequests'; | ||
import DS from '@/config/ds/ds.config'; | ||
import config from '@/config'; | ||
import { MemberWithPermission } from '@/models/member'; | ||
import { capitalizeWords, userInfos } from '../utils'; | ||
import { BadgeDossier } from '@/models/badgeDemande'; | ||
|
||
const buildRequestId = () => { | ||
return ''; | ||
}; | ||
|
||
const computeStartDate = () => { | ||
const date = new Date(); | ||
const minimalDelay = 14; // badge can be issue min 2 weeks after demande | ||
date.setDate(date.getDate() + minimalDelay); | ||
date.toISOString().split('T')[0]; | ||
return date; | ||
}; | ||
|
||
export async function postBadgeRenewalRequest(req, res) { | ||
const [currentUser]: [MemberWithPermission] = await Promise.all([ | ||
(async () => userInfos(req.auth.id, true))(), | ||
]); | ||
const endDate = currentUser.userInfos.end; | ||
const startDate = computeStartDate(); | ||
let isRequestPending = false; | ||
let badgeRequest: BadgeRequest = await getBadgeRequestWithStatus( | ||
req.auth.id, | ||
BADGE_REQUEST.BADGE_RENEWAL_REQUEST_PENDING | ||
); | ||
if (!!badgeRequest) { | ||
try { | ||
let dossier: BadgeDossier = (await DS.getDossierForDemarche( | ||
badgeRequest.dossier_number | ||
)) as unknown as BadgeDossier; | ||
|
||
if ( | ||
['en_construction', 'en_instruction', 'prefilled'].includes( | ||
dossier.state | ||
) | ||
) { | ||
isRequestPending = true; | ||
} | ||
} catch (e) { | ||
// dossier is no filled yet | ||
} | ||
} | ||
if (!isRequestPending) { | ||
try { | ||
const names = req.auth.id.split('.'); | ||
const firstname = capitalizeWords(names.shift()); | ||
const lastname = names.map((name) => capitalizeWords(name)).join(' '); | ||
let dossier = (await DS.createPrefillDossier( | ||
config.DS_DEMARCHE_RENEWAL_BADGE_NUMBER, | ||
{ | ||
identite_prenom: firstname, | ||
identite_nom: lastname, | ||
champ_Q2hhbXAtMzcwOTI5Mw: endDate, | ||
} | ||
)) as unknown as { | ||
dossier_number: number; | ||
dossier_url: string; | ||
dossier_prefill_token: string; | ||
}; | ||
if (dossier && typeof dossier.dossier_number) { | ||
let dossier_number = dossier.dossier_number; | ||
badgeRequest = await createBadgeRequest({ | ||
username: req.auth.id, | ||
status: BADGE_REQUEST.BADGE_RENEWAL_REQUEST_PENDING, | ||
start_date: startDate, | ||
end_date: new Date(endDate), | ||
dossier_number, | ||
request_id: buildRequestId(), | ||
ds_token: dossier.dossier_prefill_token, | ||
}); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
return res.status(200).json({ | ||
request_id: badgeRequest.request_id, | ||
dossier_token: badgeRequest.ds_token, | ||
dossier_number: badgeRequest.dossier_number, | ||
}); | ||
} |
Oops, something went wrong.