Skip to content

Commit

Permalink
feat: update application service
Browse files Browse the repository at this point in the history
  • Loading branch information
kevbarns committed Dec 18, 2024
1 parent de6da96 commit 6685379
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 93 deletions.
6 changes: 4 additions & 2 deletions server/src/jobs/applications/processApplications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { deleteApplicationCvFile, processApplicationEmails, processApplicationSc
import { isClamavAvailable } from "@/services/clamav.service"

import { getDbCollection } from "../../common/utils/mongodbUtils"
import { getApplicantFromDB } from "../../services/applicant.service"

const maxDuration = 1000 * 60 * 8 // 8 minutes

Expand Down Expand Up @@ -57,11 +58,12 @@ const processApplicationGroup = async (applicationFilter: Filter<IApplication>,
if (new Date().getTime() >= timeoutTs) {
return
}
const applicant = await getApplicantFromDB({ _id: application.applicant_id })
try {
let hasVirus: boolean = false
if (application.scan_status !== ApplicationScanStatus.NO_VIRUS_DETECTED) {
try {
hasVirus = await processApplicationScanForVirus(application)
hasVirus = await processApplicationScanForVirus(application, applicant)
if (hasVirus) {
results.virusDetected++
}
Expand All @@ -71,7 +73,7 @@ const processApplicationGroup = async (applicationFilter: Filter<IApplication>,
}
}
if (!hasVirus) {
await processApplicationEmails.sendEmailsIfNeeded(application)
await processApplicationEmails.sendEmailsIfNeeded(application, applicant)
results.success++
}
await deleteApplicationCvFile(application)
Expand Down
38 changes: 38 additions & 0 deletions server/src/services/applicant.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ObjectId } from "bson"
import { Filter } from "mongodb"
import { IApplicant, IApplicantNew } from "shared"

import { getDbCollection } from "../common/utils/mongodbUtils"

export const getApplicantFromDB = (payload: Filter<IApplicant>) => getDbCollection("applicants").findOne({ payload })

const updateApplicantLastConnectionDate = (applicantId: ObjectId) => getDbCollection("applicants").updateOne({ _id: applicantId }, { $set: { last_connection: new Date() } })

const createApplicant = async (applicant: IApplicantNew) => {
const { firstname, lastname, phone, email } = applicant
const now = new Date()
const payload: IApplicant = {
_id: new ObjectId(),
firstname,
lastname,
phone,
email,
last_connection: now,
createdAt: now,
updatedAt: now,
}
await getDbCollection("applicants").insertOne(payload)
return payload
}
export const getOrCreateApplicantAndUpdateLastConnection = async (applicant: IApplicantNew | IApplicant) => {
let user = await getApplicantFromDB({ email: applicant.email })

if (user) {
// update last_connection date on applicant collection (last application = last connection)
await updateApplicantLastConnectionDate(user._id)
} else {
user = await createApplicant(applicant)
}

return user
}
Loading

0 comments on commit 6685379

Please sign in to comment.