Skip to content

Commit

Permalink
fix: split model for public & private
Browse files Browse the repository at this point in the history
  • Loading branch information
kevbarns committed Dec 23, 2024
1 parent 430d58f commit 5ce1be7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ObjectId } from "mongodb"
import { IApplicationApiPayload, JOB_STATUS } from "shared"
import { IApplicationApiPublic, JOB_STATUS } from "shared"
import { NIVEAUX_POUR_LBA, RECRUITER_STATUS } from "shared/constants"
import { LBA_ITEM_TYPE } from "shared/constants/lbaitem"
import { applicationTestFile, wrongApplicationTestFile } from "shared/fixtures/application.fixture"
Expand Down Expand Up @@ -118,7 +118,7 @@ describe("POST /v2/application", () => {
})

it("Return 202 and create an application using a recruter lba", async () => {
const body: IApplicationApiPayload = {
const body: IApplicationApiPublic = {
applicant_attachment_name: "cv.pdf",
applicant_attachment_content: applicationTestFile,
applicant_email: "[email protected]",
Expand Down Expand Up @@ -174,7 +174,7 @@ describe("POST /v2/application", () => {

it("Return 202 and create an application using a recruiter", async () => {
const job = recruiter.jobs[0]
const body: IApplicationApiPayload = {
const body: IApplicationApiPublic = {
applicant_attachment_name: "cv.pdf",
applicant_attachment_content: applicationTestFile,
applicant_email: "[email protected]",
Expand Down Expand Up @@ -230,7 +230,7 @@ describe("POST /v2/application", () => {
})
it("return 400 as file type is not supported", async () => {
const job = recruiter.jobs[0]
const body: IApplicationApiPayload = {
const body: IApplicationApiPublic = {
applicant_attachment_name: "cv.pdf",
applicant_attachment_content: wrongApplicationTestFile,
applicant_email: "[email protected]",
Expand Down
17 changes: 14 additions & 3 deletions server/src/services/application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ import { isEmailBurner } from "burner-email-providers"
import dayjs from "dayjs"
import { fileTypeFromBuffer } from "file-type"
import { ObjectId } from "mongodb"
import { ApplicationScanStatus, IApplication, IApplicationApiPayloadOutput, IJob, ILbaCompany, INewApplicationV1, IRecruiter, JOB_STATUS, assertUnreachable } from "shared"
import {
ApplicationScanStatus,
IApplication,
IApplicationApiPrivateOutput,
IApplicationApiPublicOutput,
IJob,
ILbaCompany,
INewApplicationV1,
IRecruiter,
JOB_STATUS,
assertUnreachable,
} from "shared"
import { ApplicantIntention } from "shared/constants/application"
import { BusinessErrorCodes } from "shared/constants/errorCodes"
import { LBA_ITEM_TYPE, LBA_ITEM_TYPE_OLD, getDirectJobPath, newItemTypeToOldItemType } from "shared/constants/lbaitem"
Expand Down Expand Up @@ -229,7 +240,7 @@ export const sendApplicationV2 = async ({
caller,
source,
}: {
newApplication: IApplicationApiPayloadOutput
newApplication: IApplicationApiPublicOutput | IApplicationApiPrivateOutput
caller?: string
source?: ITrackingCookies
}): Promise<{ _id: ObjectId }> => {
Expand Down Expand Up @@ -514,7 +525,7 @@ const newApplicationToApplicationDocument = async (newApplication: INewApplicati
/**
* @description Initialize application object from query parameters
*/
const newApplicationToApplicationDocumentV2 = async (newApplication: IApplicationApiPayloadOutput, LbaJob: IJobOrCompany, caller?: string) => {
const newApplicationToApplicationDocumentV2 = async (newApplication: IApplicationApiPublicOutput | IApplicationApiPrivateOutput, LbaJob: IJobOrCompany, caller?: string) => {
const now = new Date()
const application: IApplication = {
...offreOrCompanyToCompanyFields(LbaJob),
Expand Down
23 changes: 15 additions & 8 deletions shared/models/applications.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ const ZNewApplicationTransitionToV2 = ZApplication.extend({
// KBA 20241011 to remove once V2 is LIVE and V1 support has ended
export type INewApplicationV1 = z.output<typeof ZNewApplicationTransitionToV2>

const ZApplicationV2Base = ZApplication.pick({
type JobCollectionName = "recruteurslba" | "jobs_partners" | "recruiters"

export const ZApplicationApiPrivate = ZApplication.pick({
applicant_first_name: true,
applicant_last_name: true,
applicant_email: true,
Expand All @@ -183,10 +185,6 @@ const ZApplicationV2Base = ZApplication.pick({
}).extend({
applicant_message: ZApplication.shape.applicant_message_to_company.optional(),
applicant_attachment_content: z.string().max(4_215_276).describe("Le contenu du fichier du CV du candidat. La taille maximale autorisée est de 3 Mo."),
})

type JobCollectionName = "recruteurslba" | "jobs_partners" | "recruiters"
export const ZApplicationApiPayload = ZApplicationV2Base.extend({
recipient_id: z
.string()
.transform((recipientId) => {
Expand All @@ -201,9 +199,18 @@ export const ZApplicationApiPayload = ZApplicationV2Base.extend({
})
.describe("Identifiant unique de la ressource vers laquelle la candidature est faite, préfixé par le nom de la collection"),
})
export type IApplicationApiPayloadOutput = z.output<typeof ZApplicationApiPayload>
export type IApplicationApiPayload = z.input<typeof ZApplicationApiPayload>
export type IApplicationApiPayloadJSON = Jsonify<z.input<typeof ZApplicationV2Base>>

export const ZApplicationApiPublic = ZApplicationApiPrivate.omit({
caller: true,
job_searched_by_user: true,
})

export type IApplicationApiPublicOutput = z.output<typeof ZApplicationApiPublic>
export type IApplicationApiPrivateOutput = z.output<typeof ZApplicationApiPrivate>
export type IApplicationApiPublic = z.input<typeof ZApplicationApiPublic>
export type IApplicationApiPrivate = z.input<typeof ZApplicationApiPrivate>
export type IApplicationApiPrivateJSON = Jsonify<z.input<typeof ZApplicationApiPrivate>>
export type IApplicationApiPublicJSON = Jsonify<z.input<typeof ZApplicationApiPublic>>

export default {
zod: ZApplication,
Expand Down
2 changes: 1 addition & 1 deletion shared/models/lbaItem.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const ZLbaItemJob = z
.object({
description: z.string().nullish(), // pe -> description | matcha -> description | partner -> offer_description
employeurDescription: z.string().nullish(), // matcha -> job.job_employer_description | partner -> workplace_description
creationDate: z.date().nullable(), // pe -> dateCreation | matcha -> createdAt |
creationDate: z.date().nullable(), // pe -> dateCreation | matcha -> createdAt | parnter -> offer_creation
id: z.string().nullish(), // pe -> id | matcha -> id mongo offre | partner -> partner_id
contractType: z.string().nullish(), // pe -> typeContrat | matcha -> offres.type
contractDescription: z.string().nullish(), // pe -> typeContratLibelle
Expand Down
6 changes: 3 additions & 3 deletions shared/routes/application.routes.v2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "../helpers/zodWithOpenApi"
import { ZApplicationApiPayload } from "../models"
import { ZApplicationApiPrivate, ZApplicationApiPublic } from "../models"

import { IRoutesDef } from "./common.routes"

Expand All @@ -8,7 +8,7 @@ export const zApplicationRoutesV2 = {
"/application": {
path: "/application",
method: "post",
body: ZApplicationApiPayload,
body: ZApplicationApiPublic,
response: {
"202": z.object({
id: z.string(),
Expand All @@ -19,7 +19,7 @@ export const zApplicationRoutesV2 = {
"/_private/application": {
path: "/_private/application",
method: "post",
body: ZApplicationApiPayload,
body: ZApplicationApiPrivate,
response: {
"200": z.object({}),
},
Expand Down
6 changes: 3 additions & 3 deletions ui/components/ItemDetail/CandidatureLba/services/getSchema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IApplicationApiPayloadJSON, ZApplicationApiPayload } from "shared"
import { IApplicationApiPrivateJSON, ZApplicationApiPrivate } from "shared"
import { validatePhone } from "shared/validators/phoneValidator"
import { z } from "zod"

import { sessionStorageGet } from "@/utils/localStorage"

export type IApplicationSchemaInitValues = Omit<IApplicationApiPayloadJSON, "caller">
export type IApplicationSchemaInitValues = Omit<IApplicationApiPrivateJSON, "caller">

export function getInitialSchemaValues(): IApplicationSchemaInitValues {
const inSessionValue = JSON.parse(sessionStorageGet("application-form-values"))
Expand All @@ -19,7 +19,7 @@ export function getInitialSchemaValues(): IApplicationSchemaInitValues {
}
}

export const ApplicationFormikSchema = ZApplicationApiPayload.pick({
export const ApplicationFormikSchema = ZApplicationApiPrivate.pick({
applicant_first_name: true,
applicant_last_name: true,
applicant_email: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useContext } from "react"
import { IApplicationApiPayload, ILbaItemLbaCompany, ILbaItemLbaJob } from "shared"
import { IApplicationApiPrivate, ILbaItemLbaCompany, ILbaItemLbaJob } from "shared"

import { DisplayContext } from "@/context/DisplayContextProvider"
import { getItemId } from "@/utils/getItemId"
Expand Down Expand Up @@ -40,7 +40,7 @@ export default async function submitCandidature({
}) {
setSendingState("currently_sending")

const payload: IApplicationApiPayload = {
const payload: IApplicationApiPrivate = {
applicant_first_name: formValues.applicant_first_name,
applicant_last_name: formValues.applicant_last_name,
applicant_email: formValues.applicant_email,
Expand Down

0 comments on commit 5ce1be7

Please sign in to comment.