Skip to content

Commit

Permalink
Hotfix - Filtro de ajuda/oferta da Instituição já candidatada (#118)
Browse files Browse the repository at this point in the history
* Feat: filtering the help and offer already applied

* Fix: passing isUserEntity as params
  • Loading branch information
thiagohdaqw authored Jul 6, 2022
1 parent ecd2ed1 commit 62d9d7e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/controllers/HelpController.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class HelpController {

async getHelpList(req, res, next) {
const { id } = req.query;
const isUserEntity = global.isUserEntity;
const coords = req.query.coords.split(',').map((coord) => Number(coord));
const categoryArray = req.query.categoryId ? req.query.categoryId.split(',') : null;
/* A requisição do Query é feita com o formato "34312ID12312,12312ID13213",
Expand All @@ -49,6 +50,7 @@ class HelpController {
const result = await this.HelpService.getHelpList(
coords,
id,
isUserEntity,
categoryArray,
);
res.status(200);
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/HelpOfferController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class OfferedHelpController {
async listHelpsOffers(req, res) {
const userId = req.query.userId;
const getOtherUsers = req.query.getOtherUsers == 'true' ? true : false;
const isUserEntity = global.isUserEntity;
try {
const helpOffers = await this.HelpOfferService.listHelpsOffers(userId, null, getOtherUsers);
const helpOffers = await this.HelpOfferService.listHelpsOffers(userId, isUserEntity, null, getOtherUsers);
return res.json(helpOffers);
} catch (error) {
return res.status(400).json({ error: error.message });
Expand Down
12 changes: 9 additions & 3 deletions src/repository/HelpOfferRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ class OfferdHelpRepository extends BaseRepository {
return super.$findOne(query, helpOfferFields, populate);
}

async list(userId, categoryArray, getOtherUsers) {
async list(userId, isUserEntity, categoryArray, getOtherUsers) {
const matchQuery = this.getHelpOfferListQuery(
userId,
isUserEntity,
true,
getOtherUsers,
categoryArray
Expand Down Expand Up @@ -84,11 +85,16 @@ class OfferdHelpRepository extends BaseRepository {

return super.$list(matchQuery, helpOfferFields, populate, sort);
}
getHelpOfferListQuery(userId, active, getOtherUsers, categoryArray) {
getHelpOfferListQuery(userId, isUserEntity, active, getOtherUsers, categoryArray) {
var matchQuery = { active };
if (!getOtherUsers) {
matchQuery.possibleHelpedUsers = { $not: { $in: [ObjectID(userId)] } };
matchQuery.ownerId = { $ne: ObjectID(userId) };

if(isUserEntity){
matchQuery.possibleEntities = { $nin: [ObjectID(userId)] };
}else{
matchQuery.possibleHelpedUsers = { $nin: [ObjectID(userId)] };
}
} else {
matchQuery.ownerId = { $eq: ObjectID(userId) };
}
Expand Down
11 changes: 8 additions & 3 deletions src/repository/HelpRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,19 @@ class HelpRepository extends BaseRepository {
await super.$update(help);
}

async shortList(coords, id, categoryArray) {
async shortList(coords, id, isUserEntity, categoryArray) {
const matchQuery = {
active: true,
possibleHelpers: { $not: { $in: [ObjectID(id)] } },
ownerId: { $not: { $in: [ObjectID(id)] } },
ownerId: { $ne: ObjectID(id) },
status: 'waiting'
};

if(isUserEntity){
matchQuery.possibleEntities = { $nin: [ObjectID(id)] };
}else{
matchQuery.possibleHelpers = { $nin: [ObjectID(id)] };
}

if (categoryArray) {
matchQuery.categoryId = {
$in: categoryArray.map((categoryString) => ObjectID(categoryString)),
Expand Down
5 changes: 2 additions & 3 deletions src/services/HelpOfferService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const UserService = require("./UserService");
const EntityService = require("./EntityService");
const NotificationService = require("./NotificationService");
const NotificationMixin = require("../utils/NotificationMixin");
const isEntity = require('../utils/IsEntity');
const { notificationTypesEnum } = require("../models/Notification");
const saveError = require('../utils/ErrorHistory');
const { findConnections, sendMessage } = require("../../websocket");
Expand Down Expand Up @@ -35,8 +34,8 @@ class OfferedHelpService {
return help;
}

async listHelpsOffers(userId, categoryArray, getOtherUsers) {
const helpOffers = await this.OfferedHelpRepository.list(userId, categoryArray, getOtherUsers);
async listHelpsOffers(userId, isUserEntity, categoryArray,getOtherUsers) {
const helpOffers = await this.OfferedHelpRepository.list(userId, isUserEntity, categoryArray,getOtherUsers);
return helpOffers;
}

Expand Down
3 changes: 2 additions & 1 deletion src/services/HelpService.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ class HelpService {
return Help;
}

async getHelpList(coords, id, categoryArray) {
async getHelpList(coords, id, isUserEntity, categoryArray) {
const Helplist = await this.HelpRepository.shortList(
coords,
id,
isUserEntity,
categoryArray
);
if (!Helplist) {
Expand Down

0 comments on commit 62d9d7e

Please sign in to comment.