From 80f47da8fd0c1ab8063fc33c3efc0ddde58d3575 Mon Sep 17 00:00:00 2001 From: Bastien Seree Date: Thu, 19 Sep 2024 11:09:39 +0200 Subject: [PATCH] Cyberleague: [#135] Add related_events computed field to event schema --- backend/web/server/plugins/cyberleague/functions.js | 1 + backend/web/server/plugins/cyberleague/related.js | 10 +++++++++- .../server/plugins/cyberleague/schemas/EventSchema.js | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/web/server/plugins/cyberleague/functions.js b/backend/web/server/plugins/cyberleague/functions.js index f1fd9b26c4..7c7a478831 100644 --- a/backend/web/server/plugins/cyberleague/functions.js +++ b/backend/web/server/plugins/cyberleague/functions.js @@ -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}) diff --git a/backend/web/server/plugins/cyberleague/related.js b/backend/web/server/plugins/cyberleague/related.js index aac97bd7dc..e530262be1 100644 --- a/backend/web/server/plugins/cyberleague/related.js +++ b/backend/web/server/plugins/cyberleague/related.js @@ -1,4 +1,5 @@ const lodash = require('lodash') +const moment=require('moment') const { loadFromDb, idEqual } = require("../../utils/database") const compareCompanies = (company, expertises) => { @@ -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`) { diff --git a/backend/web/server/plugins/cyberleague/schemas/EventSchema.js b/backend/web/server/plugins/cyberleague/schemas/EventSchema.js index 18f6125126..51e724386f 100644 --- a/backend/web/server/plugins/cyberleague/schemas/EventSchema.js +++ b/backend/web/server/plugins/cyberleague/schemas/EventSchema.js @@ -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 */