Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

feat: add badge renewal endpoints #2319

Merged
merged 9 commits into from
Dec 20, 2023
78 changes: 45 additions & 33 deletions src/config/ds/ds.config.ts
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,
};
4 changes: 3 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ 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_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,
FRONT_APP_URL: process.env.FRONT_APP_URL,
Expand Down
55 changes: 55 additions & 0 deletions src/controllers/accountController/getBadgeRenewalPage.ts
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'),
});
}
}
4 changes: 0 additions & 4 deletions src/controllers/adminController/getMattermostAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
90 changes: 90 additions & 0 deletions src/controllers/badgeRequestsController/postBadgeRenewalRequest.ts
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,
});
}
Loading
Loading