From a5c10b710326b6bcc26d8603099493186db01351 Mon Sep 17 00:00:00 2001 From: sudjoao Date: Sun, 2 Jul 2023 07:34:40 -0300 Subject: [PATCH 1/6] chore: improve logic to not return virtual to front --- src/repository/HelpOfferRepository.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/repository/HelpOfferRepository.js b/src/repository/HelpOfferRepository.js index b5ed0cad..844b7e2c 100644 --- a/src/repository/HelpOfferRepository.js +++ b/src/repository/HelpOfferRepository.js @@ -110,7 +110,7 @@ class OfferdHelpRepository extends BaseRepository { const populate = [user, categories, possibleHelpedUsers, possibleEntities]; - let helpOffers = await super.$list(matchQuery, helpOfferFields, populate, sort); + const helpOffers = await super.$list(matchQuery, helpOfferFields, populate, sort); if (coords) { const helpOffersWithDistances = helpOffers.map((offer) => { @@ -121,10 +121,9 @@ class OfferdHelpRepository extends BaseRepository { helpOffersWithDistances.sort((a, b) => a.distanceValue - b.distanceValue); - helpOffers = helpOffersWithDistances; + return addHelpTypeToList(helpOffersWithDistances, 'offer'); } - - return addHelpTypeToList(helpOffers, 'offer'); + return helpOffers; } getHelpOfferListQuery( From 099e0de7c20dcf0544e9e4189374c4f39be37348 Mon Sep 17 00:00:00 2001 From: sudjoao Date: Sun, 2 Jul 2023 07:36:13 -0300 Subject: [PATCH 2/6] chore: create virtual to avoid unnecessary loop --- src/models/Campaign.js | 4 ++++ src/models/Help.js | 4 ++++ src/models/HelpOffer.js | 2 ++ src/repository/CampaignRepository.js | 3 +-- src/repository/HelpOfferRepository.js | 3 +-- src/repository/HelpRepository.js | 3 +-- src/utils/addHelpTypeToList.js | 2 -- 7 files changed, 13 insertions(+), 8 deletions(-) delete mode 100644 src/utils/addHelpTypeToList.js diff --git a/src/models/Campaign.js b/src/models/Campaign.js index 36d871a7..44719fe1 100644 --- a/src/models/Campaign.js +++ b/src/models/Campaign.js @@ -91,7 +91,11 @@ campaignSchema.virtual('distances') campaignSchema.virtual('distanceValue') .get(() => this.distanceValue); + campaignSchema.virtual('distance') .get(() => this.distance); +campaignSchema.virtual('type') + .get(() => 'campaign'); + module.exports = mongoose.model('Campaign', campaignSchema); diff --git a/src/models/Help.js b/src/models/Help.js index cbbc9ec4..79e80377 100644 --- a/src/models/Help.js +++ b/src/models/Help.js @@ -103,7 +103,11 @@ helpSchema.virtual('distances') helpSchema.virtual('distanceValue') .get(() => this.distanceValue); + helpSchema.virtual('distance') .get(() => this.distance); +helpSchema.virtual('type') + .get(() => 'help'); + module.exports = mongoose.model('Help', helpSchema); diff --git a/src/models/HelpOffer.js b/src/models/HelpOffer.js index ebe7a29a..5524acc1 100644 --- a/src/models/HelpOffer.js +++ b/src/models/HelpOffer.js @@ -111,4 +111,6 @@ offeredHelpSchema.virtual('distanceValue') offeredHelpSchema.virtual('distance') .get(() => this.distance); +offeredHelpSchema.virtual('type').get(() => 'offer'); + module.exports = model('OfferedHelp', offeredHelpSchema); diff --git a/src/repository/CampaignRepository.js b/src/repository/CampaignRepository.js index 8f0ae4cb..052cf689 100644 --- a/src/repository/CampaignRepository.js +++ b/src/repository/CampaignRepository.js @@ -3,7 +3,6 @@ const BaseRepository = require('./BaseRepository'); const Campaign = require('../models/Campaign'); const EntitySchema = require('../models/Entity'); const getLocation = require('../utils/getLocation'); -const addHelpTypeToList = require('../utils/addHelpTypeToList'); class CampaignRepository extends BaseRepository { constructor() { @@ -67,7 +66,7 @@ class CampaignRepository extends BaseRepository { campaignsWithDistances.sort((a, b) => a.distanceValue - b.distanceValue); - return addHelpTypeToList(campaignsWithDistances, 'campaign'); + return campaignsWithDistances; } async getCampaignListByStatus(userId, statusList) { diff --git a/src/repository/HelpOfferRepository.js b/src/repository/HelpOfferRepository.js index 844b7e2c..8cbd7994 100644 --- a/src/repository/HelpOfferRepository.js +++ b/src/repository/HelpOfferRepository.js @@ -2,7 +2,6 @@ const { ObjectID } = require('mongodb'); const BaseRepository = require('./BaseRepository'); const OfferedHelp = require('../models/HelpOffer'); const getLocation = require('../utils/getLocation'); -const addHelpTypeToList = require('../utils/addHelpTypeToList'); class OfferdHelpRepository extends BaseRepository { constructor() { @@ -121,7 +120,7 @@ class OfferdHelpRepository extends BaseRepository { helpOffersWithDistances.sort((a, b) => a.distanceValue - b.distanceValue); - return addHelpTypeToList(helpOffersWithDistances, 'offer'); + return helpOffersWithDistances; } return helpOffers; } diff --git a/src/repository/HelpRepository.js b/src/repository/HelpRepository.js index f869a370..ab9a9132 100644 --- a/src/repository/HelpRepository.js +++ b/src/repository/HelpRepository.js @@ -3,7 +3,6 @@ const { ObjectID } = require('mongodb'); const BaseRepository = require('./BaseRepository'); const HelpSchema = require('../models/Help'); const getLocation = require('../utils/getLocation'); -const addHelpTypeToList = require('../utils/addHelpTypeToList'); class HelpRepository extends BaseRepository { constructor() { @@ -133,7 +132,7 @@ class HelpRepository extends BaseRepository { helpsWithDistance.sort((a, b) => a.distanceValue - b.distanceValue); - return addHelpTypeToList(helpsWithDistance, 'help'); + return helpsWithDistance; } async countDocuments(id) { diff --git a/src/utils/addHelpTypeToList.js b/src/utils/addHelpTypeToList.js deleted file mode 100644 index c826c3c3..00000000 --- a/src/utils/addHelpTypeToList.js +++ /dev/null @@ -1,2 +0,0 @@ -const addHelpTypeToList = (helpList, type) => helpList.map((help) => ({ ...help, type })); -module.exports = addHelpTypeToList; From a8d9f42d2754abe0972c9575b6ba25055b97c9a6 Mon Sep 17 00:00:00 2001 From: sudjoao Date: Sat, 8 Jul 2023 08:47:26 -0300 Subject: [PATCH 3/6] chore: create index field in activities --- src/models/Campaign.js | 5 +++++ src/models/Help.js | 5 +++++ src/models/HelpOffer.js | 5 +++++ src/repository/HelpOfferRepository.js | 1 + src/repository/HelpRepository.js | 1 + 5 files changed, 17 insertions(+) diff --git a/src/models/Campaign.js b/src/models/Campaign.js index 44719fe1..7fbe5b1d 100644 --- a/src/models/Campaign.js +++ b/src/models/Campaign.js @@ -52,6 +52,11 @@ const campaignSchema = new mongoose.Schema({ index: '2dsphere', required: false, }, + index: { + type: Number, + default: 0, + unique: false, + }, }, { collection: 'campaign', diff --git a/src/models/Help.js b/src/models/Help.js index 79e80377..2154d3f8 100644 --- a/src/models/Help.js +++ b/src/models/Help.js @@ -63,6 +63,11 @@ const helpSchema = new mongoose.Schema( index: '2dsphere', required: false, }, + index: { + type: Number, + default: 0, + unique: false, + }, }, { collection: 'userHelp', diff --git a/src/models/HelpOffer.js b/src/models/HelpOffer.js index 5524acc1..28ee5a65 100644 --- a/src/models/HelpOffer.js +++ b/src/models/HelpOffer.js @@ -60,6 +60,11 @@ const offeredHelpSchema = new Schema( index: '2dsphere', required: false, }, + index: { + type: Number, + default: 0, + unique: false, + }, }, { collection: 'helpOffer', diff --git a/src/repository/HelpOfferRepository.js b/src/repository/HelpOfferRepository.js index 8cbd7994..8d6ab784 100644 --- a/src/repository/HelpOfferRepository.js +++ b/src/repository/HelpOfferRepository.js @@ -88,6 +88,7 @@ class OfferdHelpRepository extends BaseRepository { 'creationDate', 'location', 'description', + 'index', ]; const sort = { creationDate: -1 }; const user = { diff --git a/src/repository/HelpRepository.js b/src/repository/HelpRepository.js index ab9a9132..faac47c3 100644 --- a/src/repository/HelpRepository.js +++ b/src/repository/HelpRepository.js @@ -114,6 +114,7 @@ class HelpRepository extends BaseRepository { 'ownerId', 'creationDate', 'location', + 'index', ]; const user = { path: 'user', From 21b612491e1a11a242eb7c732567b23a5f02c7e4 Mon Sep 17 00:00:00 2001 From: sudjoao Date: Sat, 8 Jul 2023 08:47:44 -0300 Subject: [PATCH 4/6] chore: create function to populate index in old activities --- src/utils/seed/HelpOfferCampaignSeed.js | 5 +++++ src/utils/seed/addIndexToOldActivities.js | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 src/utils/seed/addIndexToOldActivities.js diff --git a/src/utils/seed/HelpOfferCampaignSeed.js b/src/utils/seed/HelpOfferCampaignSeed.js index bbd51ecd..8e86f096 100644 --- a/src/utils/seed/HelpOfferCampaignSeed.js +++ b/src/utils/seed/HelpOfferCampaignSeed.js @@ -8,6 +8,7 @@ const Campaign = require('../../models/Campaign'); const HelpOffer = require('../../models/HelpOffer'); const mockedOfferInfo = require('./mockedInfos/mockedOfferInfo'); const mockedHelpInfo = require('./mockedInfos/mockedHelpInfo'); +const addIndexToOldActivities = require('./addIndexToOldActivities'); const status = [ 'waiting', @@ -32,6 +33,10 @@ const seedHelp = async () => { || helpOfferCollection.length > 0 || campaignCollection.length > 0 ) { + // this will be removed after first run in production + addIndexToOldActivities(helpCollection); + addIndexToOldActivities(helpOfferCollection); + addIndexToOldActivities(campaignCollection); return; } diff --git a/src/utils/seed/addIndexToOldActivities.js b/src/utils/seed/addIndexToOldActivities.js new file mode 100644 index 00000000..8254655a --- /dev/null +++ b/src/utils/seed/addIndexToOldActivities.js @@ -0,0 +1,7 @@ +const addIndexToOldActivities = async (activityList) => { + activityList.forEach(async (activity, index) => { + activity.index = index; + await activity.save(); + }); +}; +module.exports = addIndexToOldActivities; From 1f9ab283ba4088def0f1f1fef79b182b4d9ee5fe Mon Sep 17 00:00:00 2001 From: sudjoao Date: Sat, 8 Jul 2023 16:35:52 -0300 Subject: [PATCH 5/6] chore: remove unecessary null --- src/services/SocialNetworkService.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/SocialNetworkService.js b/src/services/SocialNetworkService.js index df72c992..fffd7868 100644 --- a/src/services/SocialNetworkService.js +++ b/src/services/SocialNetworkService.js @@ -114,7 +114,6 @@ class SocialNetworkService { ); const offers = await this.offerdHelpRepository.list( - null, userId, false, categoryArray, From 37f484b84ea21e139f7356e72a31f975ad2a0987 Mon Sep 17 00:00:00 2001 From: sudjoao Date: Sat, 8 Jul 2023 16:43:17 -0300 Subject: [PATCH 6/6] chore: change index --- src/models/Campaign.js | 4 ++-- src/models/Help.js | 4 ++-- src/models/HelpOffer.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/models/Campaign.js b/src/models/Campaign.js index 7fbe5b1d..4a7cdd9a 100644 --- a/src/models/Campaign.js +++ b/src/models/Campaign.js @@ -54,8 +54,8 @@ const campaignSchema = new mongoose.Schema({ }, index: { type: Number, - default: 0, - unique: false, + default: 1, + unique: true, }, }, { diff --git a/src/models/Help.js b/src/models/Help.js index 2154d3f8..340b2175 100644 --- a/src/models/Help.js +++ b/src/models/Help.js @@ -65,8 +65,8 @@ const helpSchema = new mongoose.Schema( }, index: { type: Number, - default: 0, - unique: false, + default: 1, + unique: true, }, }, { diff --git a/src/models/HelpOffer.js b/src/models/HelpOffer.js index 28ee5a65..7db11275 100644 --- a/src/models/HelpOffer.js +++ b/src/models/HelpOffer.js @@ -62,8 +62,8 @@ const offeredHelpSchema = new Schema( }, index: { type: Number, - default: 0, - unique: false, + default: 1, + unique: true, }, }, {