Skip to content

Commit

Permalink
Cyberleague: [premieroctet#135] Add related_events computed field to …
Browse files Browse the repository at this point in the history
…event schema
  • Loading branch information
Bastien-Wappizy committed Sep 19, 2024
1 parent 37b33db commit 80f47da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions backend/web/server/plugins/cyberleague/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ declareVirtualField({
},
})
declareVirtualField({model: 'event', field: 'registered_users_count', instance: 'Number'})
declareComputedField({model: 'event', field: 'related_events', requires:'start_date', getterFn: getRelated('event')})

// Enums Mission Schema
declareEnumField({model: 'mission', field: 'estimation_duration_unit', enumValues: ESTIMATED_DURATION_UNITS})
Expand Down
10 changes: 9 additions & 1 deletion backend/web/server/plugins/cyberleague/related.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const lodash = require('lodash')
const moment=require('moment')
const { loadFromDb, idEqual } = require("../../utils/database")

const compareCompanies = (company, expertises) => {
Expand All @@ -7,11 +8,18 @@ const compareCompanies = (company, expertises) => {
}

const getRelated = (model) => {
if (model == 'event') {
return async (userId, params, data) => {
const events = await loadFromDb({model: `event`, fields: [`start_date`, `name`, `picture`, `end_date`,`company.name`, `expertise_set.expertises`]})
return lodash.orderBy(lodash.filter(events, (e) => !idEqual(data._id,e._id) && moment(e.start_date).isAfter(moment())), (e) => e.start_date).slice(0, 10)
}
}

if (model == `company`) {
return async (userId, params, data) => {
const companies = await loadFromDb({model: `company`, fields: [`expertise_set.expertises`, `name`, `picture`, `baseline`]})
return lodash.orderBy(lodash.filter(companies, (c) => !idEqual(data._id,c._id)), (c) => compareCompanies(c, data.expertise_set?.expertises), `desc`).slice(0, 10)
}
}
}

if (model == `user`) {
Expand Down
6 changes: 6 additions & 0 deletions backend/web/server/plugins/cyberleague/schemas/EventSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ const EventSchema = new Schema({
type: String,
required: false,
},
related_events: {
type: [{
type: Schema.Types.ObjectId,
ref: 'event'
}]
}
}, schemaOptions)

/* eslint-disable prefer-arrow-callback */
Expand Down

0 comments on commit 80f47da

Please sign in to comment.