Skip to content

Commit

Permalink
Merge pull request #162 from mia-ajuda/fix/wrong-helpoffer-return
Browse files Browse the repository at this point in the history
HOTFIX - Tela de minhas ofertas quebrando
  • Loading branch information
sudjoao authored Jul 8, 2023
2 parents 00b3e1c + 37f484b commit 5dddb0e
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 12 deletions.
9 changes: 9 additions & 0 deletions src/models/Campaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ const campaignSchema = new mongoose.Schema({
index: '2dsphere',
required: false,
},
index: {
type: Number,
default: 1,
unique: true,
},
},
{
collection: 'campaign',
Expand Down Expand Up @@ -91,7 +96,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);
9 changes: 9 additions & 0 deletions src/models/Help.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ const helpSchema = new mongoose.Schema(
index: '2dsphere',
required: false,
},
index: {
type: Number,
default: 1,
unique: true,
},
},
{
collection: 'userHelp',
Expand Down Expand Up @@ -103,7 +108,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);
7 changes: 7 additions & 0 deletions src/models/HelpOffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const offeredHelpSchema = new Schema(
index: '2dsphere',
required: false,
},
index: {
type: Number,
default: 1,
unique: true,
},
},
{
collection: 'helpOffer',
Expand Down Expand Up @@ -111,4 +116,6 @@ offeredHelpSchema.virtual('distanceValue')
offeredHelpSchema.virtual('distance')
.get(() => this.distance);

offeredHelpSchema.virtual('type').get(() => 'offer');

module.exports = model('OfferedHelp', offeredHelpSchema);
3 changes: 1 addition & 2 deletions src/repository/CampaignRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 4 additions & 5 deletions src/repository/HelpOfferRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -89,6 +88,7 @@ class OfferdHelpRepository extends BaseRepository {
'creationDate',
'location',
'description',
'index',
];
const sort = { creationDate: -1 };
const user = {
Expand All @@ -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) => {
Expand All @@ -121,10 +121,9 @@ class OfferdHelpRepository extends BaseRepository {

helpOffersWithDistances.sort((a, b) => a.distanceValue - b.distanceValue);

helpOffers = helpOffersWithDistances;
return helpOffersWithDistances;
}

return addHelpTypeToList(helpOffers, 'offer');
return helpOffers;
}

getHelpOfferListQuery(
Expand Down
4 changes: 2 additions & 2 deletions src/repository/HelpRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -115,6 +114,7 @@ class HelpRepository extends BaseRepository {
'ownerId',
'creationDate',
'location',
'index',
];
const user = {
path: 'user',
Expand All @@ -133,7 +133,7 @@ class HelpRepository extends BaseRepository {

helpsWithDistance.sort((a, b) => a.distanceValue - b.distanceValue);

return addHelpTypeToList(helpsWithDistance, 'help');
return helpsWithDistance;
}

async countDocuments(id) {
Expand Down
1 change: 0 additions & 1 deletion src/services/SocialNetworkService.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ class SocialNetworkService {
);

const offers = await this.offerdHelpRepository.list(
null,
userId,
false,
categoryArray,
Expand Down
2 changes: 0 additions & 2 deletions src/utils/addHelpTypeToList.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/utils/seed/HelpOfferCampaignSeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions src/utils/seed/addIndexToOldActivities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const addIndexToOldActivities = async (activityList) => {
activityList.forEach(async (activity, index) => {
activity.index = index;
await activity.save();
});
};
module.exports = addIndexToOldActivities;

0 comments on commit 5dddb0e

Please sign in to comment.