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

feat(api): 3339 - une inscription manuelle valide le dossier #4542

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions api/src/controllers/young/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import { FileTypeResult } from "file-type";
import { requestValidatorMiddleware } from "../../middlewares/requestValidatorMiddleware";
import { authMiddleware } from "../../middlewares/authMiddleware";
import { accessControlMiddleware } from "../../middlewares/accessControlMiddleware";
import { validateYoungPhase1 } from "../../young/youngService";

const router = express.Router();
const YoungAuth = new AuthObject(YoungModel);
Expand Down Expand Up @@ -293,6 +294,9 @@ router.post("/invite", passport.authenticate("referent", { session: false, failW
young.set({ status: YOUNG_STATUS.WAITING_VALIDATION });
await young.save({ fromUser: req.user });

// TODO : call validateYoungPhase1 from youngService
await validateYoungPhase1(young, req.user);

const toName = `${young.firstName} ${young.lastName}`;
const cta = `${config.APP_URL}/auth/signup/invite?token=${invitation_token}&utm_campaign=transactionnel+compte+cree&utm_source=notifauto&utm_medium=mail+166+activer`;
const fromName = `${req.user.firstName} ${req.user.lastName}`;
Expand Down
75 changes: 72 additions & 3 deletions api/src/young/youngService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { format } from "date-fns";

import { ERRORS, FUNCTIONAL_ERRORS, UserDto, YOUNG_PHASE, YOUNG_STATUS, YOUNG_STATUS_PHASE1, YoungDto, YoungType } from "snu-lib";

import { YoungDocument, YoungModel } from "../models";
import {
ERRORS,
FUNCTIONAL_ERRORS,
getDepartmentForEligibility,
isAdmin,
UserDto,
YOUNG_PHASE,
YOUNG_SOURCE,
YOUNG_STATUS,
YOUNG_STATUS_PHASE1,
YoungDto,
YoungType,
} from "snu-lib";

import { ClasseModel, CohortModel, LigneBusModel, SessionPhase1Model, YoungDocument, YoungModel } from "../models";
import { generatePdfIntoBuffer } from "../utils/pdf-renderer";

import { YOUNG_DOCUMENT, YOUNG_DOCUMENT_PHASE_TEMPLATE } from "./youngDocument";
import { isLocalTransport } from "./youngCertificateService";
import { logger } from "../logger";
import { getCompletionObjectifs } from "../services/inscription-goal";
import { updatePlacesSessionPhase1, updateSeatsTakenInBusLine } from "../utils";

export const generateConvocationsForMultipleYoungs = async (youngs: YoungDto[]): Promise<Buffer> => {
const validatedYoungsWithSession = getValidatedYoungsWithSession(youngs);
Expand Down Expand Up @@ -169,3 +183,58 @@ export const mightAddInProgressStatus = async (young: YoungDocument, user: UserD
logger.info(`YoungService - mightAddInProgressStatus(), Status set to IN_PROGRESS for YoungId:${young.id}`);
}
};

export const validateYoungPhase1 = async (young: YoungDocument, user: UserDto): Promise<YoungType> => {
//From put("/young/:id"
// first part
const cohort = await CohortModel.findById(young.cohortId);
if (!cohort) {
throw new Error(ERRORS.COHORT_NOT_FOUND);
}
// schoolDepartment pour les scolarisés et HZR sinon department pour les non scolarisés
const departement = getDepartmentForEligibility(young);
const completionObjectif = await getCompletionObjectifs(departement, cohort.name);
if (completionObjectif.isAtteint) {
throw new Error(FUNCTIONAL_ERRORS.INSCRIPTION_GOAL_REACHED);
// remove this
// return res.status(400).send({
// ok: false,
// code: completionObjectif.region.isAtteint ? FUNCTIONAL_ERRORS.INSCRIPTION_GOAL_REGION_REACHED : FUNCTIONAL_ERRORS.INSCRIPTION_GOAL_REACHED,
// });
}

// second part
if (young.source === YOUNG_SOURCE.CLE) {
const classe = await ClasseModel.findById(young.classeId);
if (!classe) {
throw new Error(ERRORS.CLASSE_NOT_FOUND);
}
const now = new Date();
const isInsctructionOpen = now < cohort.instructionEndDate;
const remainingPlaces = classe.totalSeats - classe.seatsTaken;
if (remainingPlaces <= 0) {
throw new Error(ERRORS.OPERATION_NOT_ALLOWED);
}
if (!isInsctructionOpen && !isAdmin(user)) {
throw new Error(ERRORS.OPERATION_NOT_ALLOWED);
}
} else {
const now = new Date();
const isInsctructionOpen = now < cohort.instructionEndDate;
if (!isInsctructionOpen && !isAdmin(user)) {
throw new Error(ERRORS.OPERATION_NOT_ALLOWED);
}
}

//update places in cohesionCenter
if (young.sessionPhase1Id) {
const sessionPhase1 = await SessionPhase1Model.findById(young.sessionPhase1Id);
if (sessionPhase1) await updatePlacesSessionPhase1(sessionPhase1, user);
}
//update places in bus
if (young.ligneId) {
const bus = await LigneBusModel.findById(young.ligneId);
if (bus) await updateSeatsTakenInBusLine(bus);
}
return young;
};
Loading