From ca6ebfc8a7040af1fad96f4a53ea6c54925b0efd Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Tue, 19 Dec 2023 14:35:56 +0100 Subject: [PATCH 1/9] feat: add badge renewal endpoints --- src/config/index.ts | 5 +- .../adminController/getMattermostAdmin.ts | 4 - .../postBadgeRequest.ts | 162 ++++++++++++------ .../updateBadgeRequestStatus.ts | 25 ++- .../baseInfo/getBaseInfoUpdate.ts | 1 - src/db/dbBadgeRequests.ts | 62 ++++--- src/index.ts | 24 ++- src/models/badgeRequests.ts | 26 +-- src/routes/routes.ts | 4 + 9 files changed, 213 insertions(+), 100 deletions(-) diff --git a/src/config/index.ts b/src/config/index.ts index 8e5ff8093..2061fac48 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -58,7 +58,10 @@ export default { DS_DEMARCHE_NUMBER: process.env.DS_DEMARCHE_NUMBER ? parseInt(process.env.DS_DEMARCHE_NUMBER) : null, - DS_DEMARCHE_ID: process.env.DS_DEMARCHE_ID, + DS_DEMARCHE_RENOUVELLEMENT_BADGE_NUMBER: process.env + .DS_DEMARCHE_RENOUVELLEMENT_BADGE_NUMBER + ? parseInt(process.env.DS_DEMARCHE_RENOUVELLEMENT_BADGE_NUMBER) + : null, EMAIL_DEFAULT_PLAN: process.env.EMAIL_DEFAULT_PLAN || EMAIL_PLAN_TYPE.EMAIL_PLAN_BASIC, FRONT_APP_URL: process.env.FRONT_APP_URL, diff --git a/src/controllers/adminController/getMattermostAdmin.ts b/src/controllers/adminController/getMattermostAdmin.ts index caf77b291..f3baf88b3 100644 --- a/src/controllers/adminController/getMattermostAdmin.ts +++ b/src/controllers/adminController/getMattermostAdmin.ts @@ -42,21 +42,17 @@ export function getMattermostAdminApi(req, res) { async function getMattermostAdminPageData(req, res, onSuccess, onError) { let users = []; - console.log('LCS TEST 1'); if (process.env.NODE_ENV === 'production') { users = await getMattermostUsersWithStatus({ nbDays: 90, }); } - console.log('LCS TEST 2'); - const channels: MattermostChannel[] = await getAllChannels( config.mattermostTeamId ); try { - console.log('LCS TEST 3'); const title = 'Admin Mattermost'; return onSuccess({ title, diff --git a/src/controllers/badgeRequestsController/postBadgeRequest.ts b/src/controllers/badgeRequestsController/postBadgeRequest.ts index 19ccbc0c6..75b4f791b 100644 --- a/src/controllers/badgeRequestsController/postBadgeRequest.ts +++ b/src/controllers/badgeRequestsController/postBadgeRequest.ts @@ -1,63 +1,117 @@ -import { createBadgeRequest, getBadgeRequest } 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 { createBadgeRequest, getBadgeRequest } 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'; const buildRequestId = () => { - return '' -} + 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 -} + 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 postBadgeRequest(req, res) { - const [currentUser] : [MemberWithPermission] = await Promise.all([ - (async () => userInfos(req.auth.id, true))() - ]); - const endDate = currentUser.userInfos.end - const startDate = computeStartDate() - - let badgeRequest : BadgeRequest = await getBadgeRequest(req.auth.id) - - if (!badgeRequest) { - try { - - const names = req.auth.id.split('.') - const firstname = capitalizeWords(names.shift()) - const lastname = names.map(name => capitalizeWords(name)).join(' ') - console.log('firstname', firstname) - let dossier = await DS.createPrefillDossier(config.DS_DEMARCHE_NUMBER, { - firstname, - lastname, - date: endDate, - attributaire: currentUser.userInfos.employer, - }) 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_REQUEST_PENDING, - start_date: startDate, - end_date: new Date(endDate), - dossier_number, - request_id: buildRequestId(), - ds_token: dossier.dossier_prefill_token - }) - } - } catch(e) { - console.log(e) + const [currentUser]: [MemberWithPermission] = await Promise.all([ + (async () => userInfos(req.auth.id, true))(), + ]); + const endDate = currentUser.userInfos.end; + const startDate = computeStartDate(); + + let badgeRequest: BadgeRequest = await getBadgeRequest(req.auth.id); + + if (!badgeRequest) { + try { + const names = req.auth.id.split('.'); + const firstname = capitalizeWords(names.shift()); + const lastname = names.map((name) => capitalizeWords(name)).join(' '); + console.log('firstname', firstname); + let dossier = (await DS.createPrefillDossier(config.DS_DEMARCHE_NUMBER, { + firstname, + lastname, + date: endDate, + attributaire: currentUser.userInfos.employer, + })) 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_REQUEST_PENDING, + start_date: startDate, + end_date: new Date(endDate), + dossier_number, + request_id: buildRequestId(), + ds_token: dossier.dossier_prefill_token, + }); + } + } catch (e) { + console.log(e); + } + } + return res.status(200).json({ + request_id: badgeRequest.request_id, + dossier_token: badgeRequest.ds_token, + dossier_number: badgeRequest.dossier_number, + }); +} + +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 badgeRequest: BadgeRequest = await getBadgeRequest(req.auth.id); + + if (!badgeRequest) { + try { + const names = req.auth.id.split('.'); + const firstname = capitalizeWords(names.shift()); + const lastname = names.map((name) => capitalizeWords(name)).join(' '); + console.log('firstname', firstname); + let dossier = (await DS.createPrefillDossier( + config.DS_DEMARCHE_RENOUVELLEMENT_BADGE_NUMBER, + { + firstname, + lastname, + date: endDate, + attributaire: currentUser.userInfos.employer, } + )) 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.log(e); } - return res.status(200).json({ - request_id: badgeRequest.request_id, - dossier_token: badgeRequest.ds_token, - dossier_number: badgeRequest.dossier_number - }) + } + return res.status(200).json({ + request_id: badgeRequest.request_id, + dossier_token: badgeRequest.ds_token, + dossier_number: badgeRequest.dossier_number, + }); } diff --git a/src/controllers/badgeRequestsController/updateBadgeRequestStatus.ts b/src/controllers/badgeRequestsController/updateBadgeRequestStatus.ts index afd601523..0b2bbf475 100644 --- a/src/controllers/badgeRequestsController/updateBadgeRequestStatus.ts +++ b/src/controllers/badgeRequestsController/updateBadgeRequestStatus.ts @@ -1,11 +1,24 @@ -import { updateBadgeRequest } from "@/db/dbBadgeRequests"; -import { BADGE_REQUEST } from "@/models/badgeRequests"; +import { updateBadgeRequest } from '@/db/dbBadgeRequests'; +import { BADGE_REQUEST } from '@/models/badgeRequests'; export async function updateBadgeRequestStatus(req, res) { + await updateBadgeRequest( + { + status: BADGE_REQUEST.BADGE_REQUEST_SENT, + }, + req.auth.id + ); - await updateBadgeRequest({ - status: BADGE_REQUEST.BADGE_REQUEST_SENT, - }, req.auth.id) + return res.json({}); +} + +export async function updateBadgeRenewalRequestStatus(req, res) { + await updateBadgeRequest( + { + status: BADGE_REQUEST.BADGE_RENEWAL_REQUEST_SENT, + }, + req.auth.id + ); - return res.json({}) + return res.json({}); } diff --git a/src/controllers/usersController/baseInfo/getBaseInfoUpdate.ts b/src/controllers/usersController/baseInfo/getBaseInfoUpdate.ts index dde0eb0b2..c4fab341b 100644 --- a/src/controllers/usersController/baseInfo/getBaseInfoUpdate.ts +++ b/src/controllers/usersController/baseInfo/getBaseInfoUpdate.ts @@ -89,7 +89,6 @@ const getBaseInfo = async (req, res, onSuccess, onError) => { }) .orderBy('created_at', 'desc') .first(); - console.log('LCS TEST'); onSuccess({ title, formValidationErrors, diff --git a/src/db/dbBadgeRequests.ts b/src/db/dbBadgeRequests.ts index 999d9fa8a..1a66a86c0 100644 --- a/src/db/dbBadgeRequests.ts +++ b/src/db/dbBadgeRequests.ts @@ -1,26 +1,48 @@ -import db from "."; -import { BadgeRequest } from '@models/badgeRequests' +import db from '.'; +import { BadgeRequest } from '@models/badgeRequests'; -interface CreateBadgeRequestProps extends Omit{} -interface UpdateBadgeRequestProps extends Partial>{} +interface CreateBadgeRequestProps + extends Omit {} +interface UpdateBadgeRequestProps + extends Partial> {} -export const createBadgeRequest = (props: CreateBadgeRequestProps) : Promise => { - return db('badge_requests').insert({ - ...props - }).returning('*').then(res => res[0]) -} +const BADGE_REQUEST_TABLE = 'badge_requests'; -export const updateBadgeRequest = async(props: UpdateBadgeRequestProps, username: string) : Promise => { - console.log(props, username) - await db('badge_requests').update({ - ...props - }).where({ - username +export const createBadgeRequest = ( + props: CreateBadgeRequestProps +): Promise => { + return db(BADGE_REQUEST_TABLE) + .insert({ + ...props, }) - return -} + .returning('*') + .then((res) => res[0]); +}; -export const getBadgeRequest = (username: string) : Promise => { - return db('badge_requests').where({ username}).first() -} +export const updateBadgeRequest = async ( + props: UpdateBadgeRequestProps, + username: string +): Promise => { + console.log(props, username); + await db(BADGE_REQUEST_TABLE) + .update({ + ...props, + }) + .where({ + username, + }); + return; +}; + +export const getBadgeRequest = ( + username: string +): Promise => { + return db(BADGE_REQUEST_TABLE).where({ username }).first(); +}; +export const getBadgeRequestWithStatus = ( + username: string, + status: BadgeRequest['status'] +): Promise => { + return db(BADGE_REQUEST_TABLE).where({ username, status }).first(); +}; diff --git a/src/index.ts b/src/index.ts index fcb85977d..93d8f7fc6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,8 +45,14 @@ import { getBadgePage, getBadgePageApi, } from './controllers/accountController/getBadgePage'; -import { postBadgeRequest } from './controllers/badgeRequestsController/postBadgeRequest'; -import { updateBadgeRequestStatus } from './controllers/badgeRequestsController/updateBadgeRequestStatus'; +import { + postBadgeRenewalRequest, + postBadgeRequest, +} from './controllers/badgeRequestsController/postBadgeRequest'; +import { + updateBadgeRenewalRequestStatus, + updateBadgeRequestStatus, +} from './controllers/badgeRequestsController/updateBadgeRequestStatus'; import makeSessionStore from './infra/sessionStore/sessionStore'; import { getJwtTokenForUser, getToken } from '@/helpers/session'; import getAllIncubators from './controllers/incubatorController/api/getAllIncubators'; @@ -55,6 +61,7 @@ import { getStartupInfoCreate, getStartupInfoCreateApi, } from './controllers/startupController/getStartupInfoCreate'; +import { getBadgeRenewalPage } from './controllers/accountController/getBadgeRenewalPage'; export const app = express(); app.set('trust proxy', 1); @@ -383,17 +390,30 @@ app.post( ); app.get(routes.ACCOUNT_GET_BADGE_REQUEST_PAGE, getBadgePage); app.get(routes.ACCOUNT_GET_BADGE_REQUEST_PAGE_API, getBadgePageApi); +app.get(routes.ACCOUNT_GET_BADGE_RENEWAL_REQUEST_PAGE_API, getBadgeRenewalPage); + app.post( routes.API_POST_BADGE_REQUEST, express.json({ type: '*/*' }), postBadgeRequest ); +app.post( + routes.API_POST_BADGE_RENEWAL_REQUEST, + express.json({ type: '*/*' }), + postBadgeRenewalRequest +); app.put( routes.API_UPDATE_BADGE_REQUEST_STATUS, express.json({ type: '*/*' }), updateBadgeRequestStatus ); +app.put( + routes.API_UPDATE_BADGE_RENEWAL_REQUEST_STATUS, + express.json({ type: '*/*' }), + updateBadgeRenewalRequestStatus +); + app.get(routes.GET_COMMUNITY, communityController.getCommunity); app.get(routes.GET_COMMUNITY_API, communityController.getCommunityApi); app.get(routes.GET_USER, communityController.getUser); diff --git a/src/models/badgeRequests.ts b/src/models/badgeRequests.ts index 3f98591de..c2c36d871 100644 --- a/src/models/badgeRequests.ts +++ b/src/models/badgeRequests.ts @@ -1,17 +1,19 @@ export enum BADGE_REQUEST { - BADGE_REQUEST_PENDING = 'BADGE_REQUEST_PENDING', - BADGE_REQUEST_SENT = "BADGE_REQUEST_SENT" + BADGE_REQUEST_PENDING = 'BADGE_REQUEST_PENDING', + BADGE_REQUEST_SENT = 'BADGE_REQUEST_SENT', + BADGE_RENEWAL_REQUEST_PENDING = 'BADGE_RENEWAL_REQUEST_PENDING', + BADGE_RENEWAL_REQUEST_SENT = 'BADGE_RENEWAL_REQUEST_SENT', } export interface BadgeRequest { - id: number, - status: BADGE_REQUEST, - start_date: Date, - end_date: Date, - created_at: Date, - updated_at: Date, - request_id: string, - username: string, - dossier_number: number, - ds_token: string + id: number; + status: BADGE_REQUEST; + start_date: Date; + end_date: Date; + created_at: Date; + updated_at: Date; + request_id: string; + username: string; + dossier_number: number; + ds_token: string; } diff --git a/src/routes/routes.ts b/src/routes/routes.ts index ae4a34904..496a925bd 100644 --- a/src/routes/routes.ts +++ b/src/routes/routes.ts @@ -75,8 +75,12 @@ class routes { '/api/public/account/base-info/:username'; static ACCOUNT_GET_BADGE_REQUEST_PAGE = '/account/badge-demande'; static ACCOUNT_GET_BADGE_REQUEST_PAGE_API = '/api/account/badge-demande'; + static ACCOUNT_GET_BADGE_RENEWAL_REQUEST_PAGE_API = + '/api/account/badge-demande/renewal'; static API_POST_BADGE_REQUEST = '/api/badge'; + static API_POST_BADGE_RENEWAL_REQUEST = '/api/badge/renewal'; static API_UPDATE_BADGE_REQUEST_STATUS = '/api/badge/status'; + static API_UPDATE_BADGE_RENEWAL_REQUEST_STATUS = '/api/badge/renewal/status'; static PULL_REQUEST_GET_PRS: string = '/api/pull-requests'; static ME: string = '/api/me'; From d08607a9f84cad7bb32a5ce79bfb92f1d9727e2e Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Tue, 19 Dec 2023 14:36:37 +0100 Subject: [PATCH 2/9] feat: badge renewal, add controller files and test file --- .../accountController/getBadgeRenewalPage.ts | 56 ++++++++++++ .../postBadgeRenewalRequest.ts | 67 +++++++++++++++ tests/test-badge-renewal-request.ts | 86 +++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 src/controllers/accountController/getBadgeRenewalPage.ts create mode 100644 src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts create mode 100644 tests/test-badge-renewal-request.ts diff --git a/src/controllers/accountController/getBadgeRenewalPage.ts b/src/controllers/accountController/getBadgeRenewalPage.ts new file mode 100644 index 000000000..8aa4c05ff --- /dev/null +++ b/src/controllers/accountController/getBadgeRenewalPage.ts @@ -0,0 +1,56 @@ +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, onSuccess, onError) { + console.log('LCS GET BADGE RENEWAL REQUEST'); + 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'), + }); + } +} diff --git a/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts b/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts new file mode 100644 index 000000000..38c052136 --- /dev/null +++ b/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts @@ -0,0 +1,67 @@ +import { createBadgeRequest, getBadgeRequest } 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'; + +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 badgeRequest: BadgeRequest = await getBadgeRequest(req.auth.id); + + if (!badgeRequest) { + 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_RENOUVELLEMENT_BADGE_NUMBER, + { + firstname, + lastname, + date: 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.log(e); + } + } + return res.status(200).json({ + request_id: badgeRequest.request_id, + dossier_token: badgeRequest.ds_token, + dossier_number: badgeRequest.dossier_number, + }); +} diff --git a/tests/test-badge-renewal-request.ts b/tests/test-badge-renewal-request.ts new file mode 100644 index 000000000..700aa2aa9 --- /dev/null +++ b/tests/test-badge-renewal-request.ts @@ -0,0 +1,86 @@ +import chai from 'chai'; +import chaiHttp from 'chai-http'; +import app from '@/index'; +import routes from '@/routes/routes'; +import utils from './utils'; +import sinon from 'sinon'; +import * as session from '@/helpers/session'; + +chai.use(chaiHttp); + +describe('POST /api/badge/renewal unauthenticated', () => { + it('should not be able to post badge request if not connected', async () => { + const res = await chai + .request(app) + .post(routes.API_POST_BADGE_RENEWAL_REQUEST) + .send({}); + + res.should.have.status(401); + }); +}); + +describe('POST /api/badge/renewal authenticated', () => { + let getToken; + + beforeEach(() => { + getToken = sinon.stub(session, 'getToken'); + getToken.returns(utils.getJWT('membre.actif')); + }); + + afterEach(() => { + getToken.restore(); + }); + + it('should be able to post badge request', async () => { + const res = await chai + .request(app) + .post(routes.API_POST_BADGE_RENEWAL_REQUEST) + .set('content-type', 'application/json') + .send(); + res.should.have.status(200); + const res2 = await chai + .request(app) + .post(routes.API_POST_BADGE_RENEWAL_REQUEST) + .set('content-type', 'application/json') + .send(); + res2.should.have.status(200); + res2.body.dossier_number.should.equals(res.body.dossier_number); + }); +}); + +describe('GET /api/account/badge-demande/renewal page unauthenticated', () => { + it('should not be able to get badge request page', (done) => { + chai + .request(app) + .get(routes.ACCOUNT_GET_BADGE_RENEWAL_REQUEST_PAGE_API) + .redirects(0) + .end((err, res) => { + res.should.have.status(302); + done(); + }); + }); +}); + +describe('GET /api/account/badge-demande/renewal page authenticated', () => { + let getToken; + + beforeEach(() => { + getToken = sinon.stub(session, 'getToken'); + getToken.returns(utils.getJWT('membre.actif')); + }); + + afterEach(() => { + getToken.restore(); + }); + + it('should be able to get badge request page', (done) => { + chai + .request(app) + .get(routes.ACCOUNT_GET_BADGE_RENEWAL_REQUEST_PAGE_API) + .redirects(0) + .end((err, res) => { + res.should.have.status(200); + done(); + }); + }); +}); From 75b33fe69a75900e0bdafaffcafd242281e88baf Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Wed, 20 Dec 2023 14:53:27 +0100 Subject: [PATCH 3/9] feat: fetch pending badge info to check if state is accepted or not --- src/config/index.ts | 5 +- .../accountController/getBadgeRenewalPage.ts | 3 +- .../postBadgeRenewalRequest.ts | 38 +++-- .../postBadgeRequest.ts | 97 +++++------ src/db/dbBadgeRequests.ts | 5 +- src/index.ts | 8 +- src/lib/ds.ts | 156 +++++++++--------- tests/test-badge-renewal-request.ts | 2 +- 8 files changed, 156 insertions(+), 158 deletions(-) diff --git a/src/config/index.ts b/src/config/index.ts index 2061fac48..c5376ac1f 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -58,9 +58,8 @@ export default { DS_DEMARCHE_NUMBER: process.env.DS_DEMARCHE_NUMBER ? parseInt(process.env.DS_DEMARCHE_NUMBER) : null, - DS_DEMARCHE_RENOUVELLEMENT_BADGE_NUMBER: process.env - .DS_DEMARCHE_RENOUVELLEMENT_BADGE_NUMBER - ? parseInt(process.env.DS_DEMARCHE_RENOUVELLEMENT_BADGE_NUMBER) + DS_DEMARCHE_RENEWAL_BADGE_NUMBER: process.env.DS_DEMARCHE_RENEWAL_BADGE_NUMBER + ? parseInt(process.env.DS_DEMARCHE_RENEWAL_BADGE_NUMBER) : null, EMAIL_DEFAULT_PLAN: process.env.EMAIL_DEFAULT_PLAN || EMAIL_PLAN_TYPE.EMAIL_PLAN_BASIC, diff --git a/src/controllers/accountController/getBadgeRenewalPage.ts b/src/controllers/accountController/getBadgeRenewalPage.ts index 8aa4c05ff..7312e4b78 100644 --- a/src/controllers/accountController/getBadgeRenewalPage.ts +++ b/src/controllers/accountController/getBadgeRenewalPage.ts @@ -8,8 +8,7 @@ import { getBadgeRequestWithStatus } from '@/db/dbBadgeRequests'; import db from '@/db'; import { DBUser } from '@/models/dbUser'; -export async function getBadgeRenewalPage(req, res, onSuccess, onError) { - console.log('LCS GET BADGE RENEWAL REQUEST'); +export async function getBadgeRenewalPage(req, res) { try { const [currentUser, dbUser]: [MemberWithPermission, DBUser] = await Promise.all([ diff --git a/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts b/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts index 38c052136..bffbfa100 100644 --- a/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts +++ b/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts @@ -1,9 +1,13 @@ -import { createBadgeRequest, getBadgeRequest } from '@/db/dbBadgeRequests'; +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 ''; @@ -23,20 +27,34 @@ export async function postBadgeRenewalRequest(req, res) { ]); const endDate = currentUser.userInfos.end; const startDate = computeStartDate(); - - let badgeRequest: BadgeRequest = await getBadgeRequest(req.auth.id); - - if (!badgeRequest) { + 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'].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_RENOUVELLEMENT_BADGE_NUMBER, + config.DS_DEMARCHE_RENEWAL_BADGE_NUMBER, { - firstname, - lastname, - date: endDate, + identite_prenom: firstname, + identite_nom: lastname, + champ_Q2hhbXAtMzcwOTI5Mw: endDate, } )) as unknown as { dossier_number: number; @@ -56,7 +74,7 @@ export async function postBadgeRenewalRequest(req, res) { }); } } catch (e) { - console.log(e); + console.error(e); } } return res.status(200).json({ diff --git a/src/controllers/badgeRequestsController/postBadgeRequest.ts b/src/controllers/badgeRequestsController/postBadgeRequest.ts index 75b4f791b..0a56ae778 100644 --- a/src/controllers/badgeRequestsController/postBadgeRequest.ts +++ b/src/controllers/badgeRequestsController/postBadgeRequest.ts @@ -1,9 +1,14 @@ -import { createBadgeRequest, getBadgeRequest } from '@/db/dbBadgeRequests'; +import { + createBadgeRequest, + getBadgeRequest, + 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 ''; @@ -24,71 +29,45 @@ export async function postBadgeRequest(req, res) { const endDate = currentUser.userInfos.end; const startDate = computeStartDate(); - let badgeRequest: BadgeRequest = await getBadgeRequest(req.auth.id); - - if (!badgeRequest) { + let badgeRequest: BadgeRequest = await getBadgeRequestWithStatus( + req.auth.id, + BADGE_REQUEST.BADGE_REQUEST_PENDING + ); + let isRequestPending = false; + if (badgeRequest) { try { - const names = req.auth.id.split('.'); - const firstname = capitalizeWords(names.shift()); - const lastname = names.map((name) => capitalizeWords(name)).join(' '); - console.log('firstname', firstname); - let dossier = (await DS.createPrefillDossier(config.DS_DEMARCHE_NUMBER, { - firstname, - lastname, - date: endDate, - attributaire: currentUser.userInfos.employer, - })) 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_REQUEST_PENDING, - start_date: startDate, - end_date: new Date(endDate), - dossier_number, - request_id: buildRequestId(), - ds_token: dossier.dossier_prefill_token, - }); + let dossier: BadgeDossier = (await DS.getDossierForDemarche( + badgeRequest.dossier_number + )) as unknown as BadgeDossier; + if (['en_construction', 'en_instruction'].includes(dossier.state)) { + isRequestPending = true; } } catch (e) { - console.log(e); + // dossier is no filled yet } } - return res.status(200).json({ - request_id: badgeRequest.request_id, - dossier_token: badgeRequest.ds_token, - dossier_number: badgeRequest.dossier_number, - }); -} - -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 badgeRequest: BadgeRequest = await getBadgeRequest(req.auth.id); - - if (!badgeRequest) { + if (!isRequestPending) { try { const names = req.auth.id.split('.'); const firstname = capitalizeWords(names.shift()); const lastname = names.map((name) => capitalizeWords(name)).join(' '); - console.log('firstname', firstname); - let dossier = (await DS.createPrefillDossier( - config.DS_DEMARCHE_RENOUVELLEMENT_BADGE_NUMBER, - { - firstname, - lastname, - date: endDate, - attributaire: currentUser.userInfos.employer, - } - )) as unknown as { + let dossier = (await DS.createPrefillDossier(config.DS_DEMARCHE_NUMBER, { + champ_Q2hhbXAtNjYxNzM5: firstname, + champ_Q2hhbXAtNjYxNzM3: lastname, + identite_prenom: firstname, + identite_nom: lastname, + champ_Q2hhbXAtNjYxNzM4: currentUser.userInfos.employer + ? currentUser.userInfos.employer.split('/')[1] + : '', + champ_Q2hhbXAtNjcxODAy: endDate, + champ_Q2hhbXAtMzE0MzkxNA: [ + 'Locaux SEGUR 5.413, 5.416, 5.420, 5.425, 5.424, 5.428 et cantine', + ], + // "champ_Q2hhbXAtMzE0MzkxNA":["Locaux SEGUR 5.413, 5.416, 5.420, 5.425, 5.424, 5.428 et cantine","Parking"], + // "champ_Q2hhbXAtMzE4MjQ0Ng":"Texte court", + // "champ_Q2hhbXAtMzE4MjQ0Nw":"true", + // "champ_Q2hhbXAtMzE4MjQ0Mw":"Locaux SEGUR 5.413, 5.416, 5.420, 5.425, 5.424, 5.428 et cantine" + })) as unknown as { dossier_number: number; dossier_url: string; dossier_prefill_token: string; @@ -97,7 +76,7 @@ export async function postBadgeRenewalRequest(req, res) { let dossier_number = dossier.dossier_number; badgeRequest = await createBadgeRequest({ username: req.auth.id, - status: BADGE_REQUEST.BADGE_RENEWAL_REQUEST_PENDING, + status: BADGE_REQUEST.BADGE_REQUEST_PENDING, start_date: startDate, end_date: new Date(endDate), dossier_number, @@ -106,7 +85,7 @@ export async function postBadgeRenewalRequest(req, res) { }); } } catch (e) { - console.log(e); + console.error(e); } } return res.status(200).json({ diff --git a/src/db/dbBadgeRequests.ts b/src/db/dbBadgeRequests.ts index 1a66a86c0..99ed62065 100644 --- a/src/db/dbBadgeRequests.ts +++ b/src/db/dbBadgeRequests.ts @@ -44,5 +44,8 @@ export const getBadgeRequestWithStatus = ( username: string, status: BadgeRequest['status'] ): Promise => { - return db(BADGE_REQUEST_TABLE).where({ username, status }).first(); + return db(BADGE_REQUEST_TABLE) + .where({ username, status }) + .orderBy('created_at', 'desc') + .first(); }; diff --git a/src/index.ts b/src/index.ts index 93d8f7fc6..a0ea6f2e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,10 +45,7 @@ import { getBadgePage, getBadgePageApi, } from './controllers/accountController/getBadgePage'; -import { - postBadgeRenewalRequest, - postBadgeRequest, -} from './controllers/badgeRequestsController/postBadgeRequest'; +import { postBadgeRequest } from './controllers/badgeRequestsController/postBadgeRequest'; import { updateBadgeRenewalRequestStatus, updateBadgeRequestStatus, @@ -62,6 +59,7 @@ import { getStartupInfoCreateApi, } from './controllers/startupController/getStartupInfoCreate'; import { getBadgeRenewalPage } from './controllers/accountController/getBadgeRenewalPage'; +import { postBadgeRenewalRequest } from './controllers/badgeRequestsController/postBadgeRenewalRequest'; export const app = express(); app.set('trust proxy', 1); @@ -388,9 +386,9 @@ app.post( express.json({ type: '*/*' }), usersController.publicPostBaseInfoUpdate ); +app.get(routes.ACCOUNT_GET_BADGE_RENEWAL_REQUEST_PAGE_API, getBadgeRenewalPage); app.get(routes.ACCOUNT_GET_BADGE_REQUEST_PAGE, getBadgePage); app.get(routes.ACCOUNT_GET_BADGE_REQUEST_PAGE_API, getBadgePageApi); -app.get(routes.ACCOUNT_GET_BADGE_RENEWAL_REQUEST_PAGE_API, getBadgeRenewalPage); app.post( routes.API_POST_BADGE_REQUEST, diff --git a/src/lib/ds.ts b/src/lib/ds.ts index 393c81c48..ecf95a3e1 100644 --- a/src/lib/ds.ts +++ b/src/lib/ds.ts @@ -1,28 +1,27 @@ -import axios from "axios"; - - +import axios from 'axios'; const makeDS = ({ DS_TOKEN }) => { - const getDSConfig = () => { - if (!DS_TOKEN) { - const errorMessage = - 'Unable to launch ds api calls without env var DS_TOKEN'; - console.error(errorMessage); - throw new Error(errorMessage); - } - return { - headers: { - Authorization: `Bearer ${DS_TOKEN}`, - 'Content-Type': 'application/json' - }, - }; + const getDSConfig = () => { + if (!DS_TOKEN) { + const errorMessage = + 'Unable to launch ds api calls without env var DS_TOKEN'; + console.error(errorMessage); + throw new Error(errorMessage); + } + return { + headers: { + Authorization: `Bearer ${DS_TOKEN}`, + 'Content-Type': 'application/json', + }, }; - - function getAllDossiersForDemarche(demarcheId) { - return axios.post( - `https://www.demarches-simplifiees.fr/api/v2/graphql`, - JSON.stringify({ - query: ` + }; + + function getAllDossiersForDemarche(demarcheId) { + return axios + .post( + `https://www.demarches-simplifiees.fr/api/v2/graphql`, + JSON.stringify({ + query: ` query getDemarche($demarcheNumber: Int!) { demarche(number: $demarcheNumber) { id @@ -43,18 +42,21 @@ const makeDS = ({ DS_TOKEN }) => { } } }`, - variables: { - "demarcheNumber": demarcheId, - } - }), getDSConfig()).then(resp => resp.data.data.demarche.dossiers); - } - - - function getDossierForDemarche(dossierNumber){ - return axios.post( - `https://www.demarches-simplifiees.fr/api/v2/graphql`, - JSON.stringify({ - query: ` + variables: { + demarcheNumber: demarcheId, + }, + }), + getDSConfig() + ) + .then((resp) => resp.data.data.demarche.dossiers); + } + + function getDossierForDemarche(dossierNumber) { + return axios + .post( + `https://www.demarches-simplifiees.fr/api/v2/graphql`, + JSON.stringify({ + query: ` query getDossier( $dossierNumber: Int! $includeRevision: Boolean = false @@ -413,50 +415,50 @@ const makeDS = ({ DS_TOKEN }) => { } `, - variables: { - "dossierNumber": dossierNumber - }}) - , getDSConfig()).then(resp => resp.data.data.dossier); - } + variables: { + dossierNumber: dossierNumber, + }, + }), + getDSConfig() + ) + .then((resp) => resp.data.data.dossier); + } - function createPrefillDossier(demarcheId, { - firstname, - lastname, - date, - attributaire - }) : Promise<{ - dossier_url: string, - state: string, - dossier_id: string, - dossier_number: number, - dossier_prefill_token: string - }> { - return axios.post(`https://www.demarches-simplifiees.fr/api/public/v1/demarches/${demarcheId}/dossiers`, { - "champ_Q2hhbXAtNjYxNzM5": firstname, - "champ_Q2hhbXAtNjYxNzM3": lastname, - "champ_Q2hhbXAtNjYxNzM4": attributaire.split('/')[1], - "champ_Q2hhbXAtNjcxODAy": date, - "champ_Q2hhbXAtMzE0MzkxNA":["Locaux SEGUR 5.413, 5.416, 5.420, 5.425, 5.424, 5.428 et cantine"], - // "champ_Q2hhbXAtMzE0MzkxNA":["Locaux SEGUR 5.413, 5.416, 5.420, 5.425, 5.424, 5.428 et cantine","Parking"], - // "champ_Q2hhbXAtMzE4MjQ0Ng":"Texte court", - // "champ_Q2hhbXAtMzE4MjQ0Nw":"true", - // "champ_Q2hhbXAtMzE4MjQ0Mw":"Locaux SEGUR 5.413, 5.416, 5.420, 5.425, 5.424, 5.428 et cantine" - }, { + function createPrefillDossier( + demarcheId, + params + ): Promise<{ + dossier_url: string; + state: string; + dossier_id: string; + dossier_number: number; + dossier_prefill_token: string; + }> { + return axios + .post( + `https://www.demarches-simplifiees.fr/api/public/v1/demarches/${demarcheId}/dossiers`, + { + ...params, + }, + { headers: { - 'Content-Type': 'application/json' - } - }).then(resp => { - return resp.data - }).catch(e => { - console.log(e) - }) - } + 'Content-Type': 'application/json', + }, + } + ) + .then((resp) => { + return resp.data; + }) + .catch((e) => { + console.log(e); + }); + } - return { - getDossierForDemarche, - getAllDossiersForDemarche, - createPrefillDossier - } -} + return { + getDossierForDemarche, + getAllDossiersForDemarche, + createPrefillDossier, + }; +}; -export default makeDS \ No newline at end of file +export default makeDS; diff --git a/tests/test-badge-renewal-request.ts b/tests/test-badge-renewal-request.ts index 700aa2aa9..06684d491 100644 --- a/tests/test-badge-renewal-request.ts +++ b/tests/test-badge-renewal-request.ts @@ -73,7 +73,7 @@ describe('GET /api/account/badge-demande/renewal page authenticated', () => { getToken.restore(); }); - it('should be able to get badge request page', (done) => { + it('should be able to get badge renewal request page', (done) => { chai .request(app) .get(routes.ACCOUNT_GET_BADGE_RENEWAL_REQUEST_PAGE_API) From 55f1c032a364c174e15b888f86c6cb933b86feb3 Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Wed, 20 Dec 2023 15:42:10 +0100 Subject: [PATCH 4/9] test: add change mock call to get dossier from ds api --- src/config/ds/ds.config.ts | 72 ++++++++++--------- .../postBadgeRenewalRequest.ts | 9 ++- .../postBadgeRequest.ts | 7 +- 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/src/config/ds/ds.config.ts b/src/config/ds/ds.config.ts index 32a7b8216..719519533 100644 --- a/src/config/ds/ds.config.ts +++ b/src/config/ds/ds.config.ts @@ -1,40 +1,46 @@ -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', + }; + 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, +}; diff --git a/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts b/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts index bffbfa100..3e90f8789 100644 --- a/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts +++ b/src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts @@ -32,12 +32,17 @@ export async function postBadgeRenewalRequest(req, res) { req.auth.id, BADGE_REQUEST.BADGE_RENEWAL_REQUEST_PENDING ); - if (badgeRequest) { + if (!!badgeRequest) { try { let dossier: BadgeDossier = (await DS.getDossierForDemarche( badgeRequest.dossier_number )) as unknown as BadgeDossier; - if (['en_construction', 'en_instruction'].includes(dossier.state)) { + + if ( + ['en_construction', 'en_instruction', 'prefilled'].includes( + dossier.state + ) + ) { isRequestPending = true; } } catch (e) { diff --git a/src/controllers/badgeRequestsController/postBadgeRequest.ts b/src/controllers/badgeRequestsController/postBadgeRequest.ts index 0a56ae778..868e65bb1 100644 --- a/src/controllers/badgeRequestsController/postBadgeRequest.ts +++ b/src/controllers/badgeRequestsController/postBadgeRequest.ts @@ -39,7 +39,12 @@ export async function postBadgeRequest(req, res) { let dossier: BadgeDossier = (await DS.getDossierForDemarche( badgeRequest.dossier_number )) as unknown as BadgeDossier; - if (['en_construction', 'en_instruction'].includes(dossier.state)) { + + if ( + ['en_construction', 'en_instruction', 'prefilled'].includes( + dossier.state + ) + ) { isRequestPending = true; } } catch (e) { From 27cab6335dbfdccada51bac23197c2e85536774e Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Wed, 20 Dec 2023 15:45:55 +0100 Subject: [PATCH 5/9] fix: send error if dossier not found --- src/lib/ds.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/ds.ts b/src/lib/ds.ts index ecf95a3e1..7b4b38c43 100644 --- a/src/lib/ds.ts +++ b/src/lib/ds.ts @@ -421,7 +421,13 @@ const makeDS = ({ DS_TOKEN }) => { }), getDSConfig() ) - .then((resp) => resp.data.data.dossier); + .then((resp) => { + if (resp.data) { + return resp.data.data.dossier; + } else { + throw new Error('Dossier not found'); + } + }); } function createPrefillDossier( From 94fd8485f8fefd45175cdb357a3f1f6a5645c0bb Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Wed, 20 Dec 2023 15:58:40 +0100 Subject: [PATCH 6/9] build: remove useless import --- src/controllers/badgeRequestsController/postBadgeRequest.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/controllers/badgeRequestsController/postBadgeRequest.ts b/src/controllers/badgeRequestsController/postBadgeRequest.ts index 868e65bb1..1041acca2 100644 --- a/src/controllers/badgeRequestsController/postBadgeRequest.ts +++ b/src/controllers/badgeRequestsController/postBadgeRequest.ts @@ -1,6 +1,5 @@ import { createBadgeRequest, - getBadgeRequest, getBadgeRequestWithStatus, } from '@/db/dbBadgeRequests'; import { BadgeRequest, BADGE_REQUEST } from '@/models/badgeRequests'; From 18caf11de76338b329940f5fe99c5f377090dfe4 Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Wed, 20 Dec 2023 16:05:48 +0100 Subject: [PATCH 7/9] test: add test on get post badge api --- tests/test-badge-request.ts | 184 +++++++++++++++++++----------------- 1 file changed, 97 insertions(+), 87 deletions(-) diff --git a/tests/test-badge-request.ts b/tests/test-badge-request.ts index a949dc6c4..2cc780286 100644 --- a/tests/test-badge-request.ts +++ b/tests/test-badge-request.ts @@ -4,105 +4,115 @@ import app from '@/index'; import routes from '@/routes/routes'; import utils from './utils'; import sinon from 'sinon'; -import * as session from '@/helpers/session' +import * as session from '@/helpers/session'; chai.use(chaiHttp); describe('POST /api/badge unauthenticated', () => { - - it('should not be able to post badge request if not connected', async () => { - const res = await chai.request(app) - .post(routes.API_POST_BADGE_REQUEST) - .send({}) - - res.should.have.status(401); - }) + it('should not be able to post badge request if not connected', async () => { + const res = await chai + .request(app) + .post(routes.API_POST_BADGE_REQUEST) + .send({}); + + res.should.have.status(401); + }); }); describe('POST /api/badge authenticated', () => { - let getToken - - beforeEach(() => { - getToken = sinon.stub(session, 'getToken') - getToken.returns(utils.getJWT('membre.actif')) - }) - - afterEach(() => { - getToken.restore() - }) - - it('should be able to post badge request', async () => { - const res = await chai.request(app) - .post(routes.API_POST_BADGE_REQUEST) - .set('content-type', 'application/json') - .send() - res.should.have.status(200); - const res2 = await chai.request(app) - .post(routes.API_POST_BADGE_REQUEST) - .set('content-type', 'application/json') - .send() - res2.should.have.status(200); - res2.body.dossier_number.should.equals(res.body.dossier_number) - }) -}) - + let getToken; + + beforeEach(() => { + getToken = sinon.stub(session, 'getToken'); + getToken.returns(utils.getJWT('membre.actif')); + }); + + afterEach(() => { + getToken.restore(); + }); + + it('should be able to post badge request', async () => { + const res = await chai + .request(app) + .post(routes.API_POST_BADGE_REQUEST) + .set('content-type', 'application/json') + .send(); + res.should.have.status(200); + const res2 = await chai + .request(app) + .post(routes.API_POST_BADGE_REQUEST) + .set('content-type', 'application/json') + .send(); + res2.should.have.status(200); + res2.body.dossier_number.should.equals(res.body.dossier_number); + }); +}); describe('POST /api/badge/status', () => { - - let getToken - - beforeEach(() => { - getToken = sinon.stub(session, 'getToken') - getToken.returns(utils.getJWT('membre.actif')) - }) - - afterEach(() => { - getToken.restore() - }) - - it('should be able to update badge request', async () => { - const res = await chai.request(app) - .put(routes.API_UPDATE_BADGE_REQUEST_STATUS) - .set('content-type', 'application/json') - res.should.have.status(200); - }) + let getToken; + + beforeEach(() => { + getToken = sinon.stub(session, 'getToken'); + getToken.returns(utils.getJWT('membre.actif')); + }); + + afterEach(() => { + getToken.restore(); + }); + + it('should be able to update badge request', async () => { + const res = await chai + .request(app) + .put(routes.API_UPDATE_BADGE_REQUEST_STATUS) + .set('content-type', 'application/json'); + res.should.have.status(200); + }); }); describe('GET /account/badge page unauthenticated', () => { - it('should not be able to get badge request page', (done) => { - chai.request(app) - .get(routes.ACCOUNT_GET_BADGE_REQUEST_PAGE) - .redirects(0) - .end((err, res) => { - res.should.have.status(302); - done() - }); - }) -}) - + it('should not be able to get badge request page', (done) => { + chai + .request(app) + .get(routes.ACCOUNT_GET_BADGE_REQUEST_PAGE) + .redirects(0) + .end((err, res) => { + res.should.have.status(302); + done(); + }); + }); +}); describe('GET /account/badge page authenticated', () => { - - let getToken - - beforeEach(() => { - getToken = sinon.stub(session, 'getToken') - getToken.returns(utils.getJWT('membre.actif')) - }) - - afterEach(() => { - getToken.restore() - }) - - it('should be able to get badge request page', (done) => { - chai.request(app) - .get(routes.ACCOUNT_GET_BADGE_REQUEST_PAGE) - .redirects(0) - .end((err, res) => { - res.should.have.status(200); - done() - }); - }) + let getToken; + + beforeEach(() => { + getToken = sinon.stub(session, 'getToken'); + getToken.returns(utils.getJWT('membre.actif')); + }); + + afterEach(() => { + getToken.restore(); + }); + + it('should be able to get badge request page', (done) => { + chai + .request(app) + .get(routes.ACCOUNT_GET_BADGE_REQUEST_PAGE) + .redirects(0) + .end((err, res) => { + res.should.have.status(200); + done(); + }); + }); + + it('should be able to get badge request page api', (done) => { + chai + .request(app) + .get(routes.ACCOUNT_GET_BADGE_REQUEST_PAGE_API) + .redirects(0) + .end((err, res) => { + res.should.have.status(200); + done(); + }); + }); }); - - From e46e765ae97571646f32d63d392f39883ed45701 Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Wed, 20 Dec 2023 16:18:37 +0100 Subject: [PATCH 8/9] test: fix mock add necessary values --- src/config/ds/ds.config.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/config/ds/ds.config.ts b/src/config/ds/ds.config.ts index 719519533..32a62d72c 100644 --- a/src/config/ds/ds.config.ts +++ b/src/config/ds/ds.config.ts @@ -20,6 +20,12 @@ const makeFakeMathods = () => { dossier_id: `${dossierInt}==`, dossier_number: dossierInt, dossier_prefill_token: 'untoken', + annotations: [ + { + label: 'Status', + stringValue: '', + }, + ], }; dossiers.push(dossier); return dossier; From b8d251410ccda6c5a209251db601ad712b45ea72 Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Wed, 20 Dec 2023 16:22:47 +0100 Subject: [PATCH 9/9] build: add annotations to createPrefillDossier returned type --- src/lib/ds.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/ds.ts b/src/lib/ds.ts index 7b4b38c43..c8075590f 100644 --- a/src/lib/ds.ts +++ b/src/lib/ds.ts @@ -439,6 +439,7 @@ const makeDS = ({ DS_TOKEN }) => { dossier_id: string; dossier_number: number; dossier_prefill_token: string; + annotations: { label: string; stringValue: string }[]; }> { return axios .post(