Skip to content

Commit

Permalink
Sosynpl[premieroctet#120] edited computeSuggestedFreelances, needs to…
Browse files Browse the repository at this point in the history
… be tested
  • Loading branch information
SeghirOumo committed Jul 12, 2024
1 parent 9161520 commit 77622de
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
2 changes: 1 addition & 1 deletion backend/web/server/plugins/sosynpl/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ const SEARCH_MODE={
const DEFAULT_SEARCH_RADIUS=20

module.exports={
SOSYNPL, ROLES, COMPANY_SIZE, WORK_MODE, WORK_DURATION, VALID_STATUS, VALID_STATUS_PENDING, SOURCE,
SOSYNPL, ROLES, COMPANY_SIZE, WORK_MODE, WORK_MODE_REMOTE, WORK_MODE_REMOTE_SITE, WORK_MODE_SITE, WORK_DURATION, WORK_DURATION_LESS_1_MONTH, WORK_DURATION_MORE_6_MONTH, WORK_DURATION__1_TO_6_MONTHS, VALID_STATUS, VALID_STATUS_PENDING, SOURCE,
DISCRIMINATOR_KEY, DISC_CUSTOMER, DISC_FREELANCE, DISC_ADMIN, DISC_CUSTOMER_FREELANCE, EXPERIENCE, ROLE_CUSTOMER, ROLE_FREELANCE, ROLE_ADMIN,
LEGAL_STATUS, DEACTIVATION_REASON, SUSPEND_REASON, ACTIVITY_STATE, ACTIVITY_STATE_ACTIVE, ACTIVITY_STATE_STANDBY,
ACTIVITY_STATE_SUSPENDED, ACTIVITY_STATE_DISABLED, MOBILITY, MOBILITY_CITY, MOBILITY_FRANCE, MOBILITY_REGIONS,
Expand Down
2 changes: 1 addition & 1 deletion backend/web/server/plugins/sosynpl/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ declareEnumField({model: 'softSkill', field: 'value', enumValues: SOFT_SKILLS})

/** Announce start */
declareVirtualField({model: 'announce', field: 'total_budget', instance: 'Number', requires: 'budget'})
declareComputedField({model: 'announce', field: 'suggested_freelances', getterFn: computeSuggestedFreelances})
declareComputedField({model: 'announce', field: 'suggested_freelances', require: 'experiences', getterFn: computeSuggestedFreelances})
declareEnumField({model: 'announce', field: 'duration_unit', enumValues: DURATION_UNIT})
declareEnumField({model: 'announce', field: 'mobility', enumValues: ANNOUNCE_MOBILITY})
declareEnumField({model: 'announce', field: 'soft_skills', enumValues: SS_PILAR})
Expand Down
70 changes: 67 additions & 3 deletions backend/web/server/plugins/sosynpl/search.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,77 @@
const lodash=require('lodash')
const CustomerFreelance = require("../../models/CustomerFreelance")
const User = require("../../models/User")
const { ROLE_FREELANCE, DEFAULT_SEARCH_RADIUS, AVAILABILITY_ON, ANNOUNCE_STATUS_ACTIVE, DURATION_FILTERS } = require("./consts")
const { ROLE_FREELANCE, DEFAULT_SEARCH_RADIUS, AVAILABILITY_ON, ANNOUNCE_STATUS_ACTIVE, DURATION_FILTERS, WORK_MODE, WORK_MODE_SITE, WORK_MODE_REMOTE, WORK_MODE_REMOTE_SITE, WORK_DURATION_LESS_1_MONTH, WORK_DURATION_MORE_6_MONTH, WORK_DURATION__1_TO_6_MONTHS, MOBILITY_FRANCE, MOBILITY_NONE } = require("./consts")
const { buildPopulates, loadFromDb } = require('../../utils/database')
const { computeDistanceKm } = require('../../../utils/functions')
const Announce = require('../../models/Announce')
const { REGIONS_FULL } = require('../../../utils/consts')

const computeSuggestedFreelances = async (userId, params, data) => {
return CustomerFreelance.find()
const computeSuggestedFreelances = async (userId, params, data) => {
const MAP_WORKMODE = {
0: WORK_MODE_SITE,
5: WORK_MODE_REMOTE,
}

const workMode = MAP_WORKMODE[data.homework_days] || WORK_MODE_REMOTE_SITE;

const workDuration =
data._duration_days < 30
? WORK_DURATION_LESS_1_MONTH
: data._duration_days > 180
? WORK_DURATION_MORE_6_MONTH
: WORK_DURATION__1_TO_6_MONTHS

const getRegionFromZipcode = (zipcode) => {
const departmentCode = zipcode.toString().substring(0, 2);
const region = lodash.pickBy(REGIONS_FULL, (region) =>
region.departements.includes(departmentCode)
)
return Object.keys(region)[0] || null
}

const mobilityFilter = () => {
if (data.homework_days === 5) {
return {}
}
if (data.mobility === MOBILITY_FRANCE) {
return { mobility: MOBILITY_FRANCE }
}
if (data.mobility === MOBILITY_NONE) {
const regionKey = getRegionFromZipcode(data.city.zipcode);
return {
$or: [
{
mobility: MOBILITY_CITY,
$expr: {
$lt: [
computeDistanceKm(data.city, '$mobility_city'),
'$mobility_city_distance',
],
},
},
{
mobility: MOBILITY_REGIONS,
mobility_regions: { $in: [regionKey] },
},
],
}
}
else return {}
}

const filter = {
main_job: data.job,
work_sector: { $in: data.sectors },
expertises: { $in: data.expertises },
softwares: { $in: data.softwares },
languages: { $in: data.languages },
main_experience: { $in: data.experience },
work_mode: workMode,
work_duration: workDuration,
...mobilityFilter(),
}
return CustomerFreelance.find(filter)
}

const PROFILE_TEXT_SEARCH_FIELDS=['position', 'description', 'motivation']
Expand Down
18 changes: 18 additions & 0 deletions backend/web/tests/sosynpl/search.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require('mongoose')
const moment = require('moment')
const { MONGOOSE_OPTIONS } = require('../../server/utils/database')
const Announce = require('../../server/models/Announce')
const Job = require('../../server/models/Job')

describe('Search', () => {
beforeAll(async() => {
const DBNAME=`test${moment().unix()}`
await mongoose.connect(`mongodb://localhost/${DBNAME}`, MONGOOSE_OPTIONS)
const job = await Job.create({ name: 'Developer', job_file: new mongoose.Types.ObjectId() })
const announce = await Announce.create({
user: new mongoose.Types.ObjectId(),
job: job._id,
title: 'Looking for Dev'
})
})
})
6 changes: 5 additions & 1 deletion backend/web/utils/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ const LANGUAGE_LEVEL={
const REGIONS=lodash(regionData)
.mapValues(v => v.region)
.value()
Object.freeze(REGIONS)

const REGIONS_FULL = regionData
Object.freeze(REGIONS_FULL)

const AVG_DAYS_IN_MONTH=30.436875

Expand All @@ -290,5 +294,5 @@ module.exports = {
IMAGE_SIZE_MARKER,
THUMBNAILS_DIR,
PURCHASE_STATUS, PURCHASE_STATUS_NEW, PURCHASE_STATUS_PENDING, PURCHASE_STATUS_COMPLETE, PURCHASE_STATUS_FAILED,
API_ROOT, NATIONALITIES, LANGUAGE_LEVEL, REGIONS, AVG_DAYS_IN_MONTH,
API_ROOT, NATIONALITIES, LANGUAGE_LEVEL, REGIONS, REGIONS_FULL, AVG_DAYS_IN_MONTH,
}

0 comments on commit 77622de

Please sign in to comment.