Skip to content

Commit

Permalink
Merge pull request #163 from mia-ajuda/develop
Browse files Browse the repository at this point in the history
Mia Ajuda V3.0.0 (Corgi)
  • Loading branch information
sudjoao authored Sep 19, 2023
2 parents ca7cf87 + 50f7707 commit 4347341
Show file tree
Hide file tree
Showing 68 changed files with 2,973 additions and 1,271 deletions.
Binary file removed .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": 2020
},
"rules": {
"indent": [
Expand Down
11 changes: 9 additions & 2 deletions src/config/database.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const mongoose = require('mongoose');
const CategorySeed = require('../utils/seed/CategorySeed');
const UserSeed = require('../utils/seed/UserSeed');
const HelpSeed = require('../utils/seed/HelpSeed');
const HelpSeed = require('../utils/seed/HelpOfferCampaignSeed');
const NotificationSeed = require('../utils/seed/NotificationSeed');
const BadgeSeed = require('../utils/seed/BadgeTemplateSeed');
const TimelineEventTemplateSeed = require('../utils/seed/TimelineEventTemplateSeed');

const databaseURL = process.env.DATABASE_URL || 'mongodb://mongo:27017/miaAjudaDB';
const envType = process.env.NODE_ENV || 'development';

const databaseConnect = async () => {
try {
await mongoose.connect(databaseURL, { useNewUrlParser: true, useUnifiedTopology: true });
await mongoose.connect(databaseURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('Banco de dados conectado!');
mongoose.set('useFindAndModify', false);

Expand All @@ -19,6 +24,8 @@ const databaseConnect = async () => {
await UserSeed();
await HelpSeed();
await NotificationSeed();
await BadgeSeed();
await TimelineEventTemplateSeed();
}
} catch (err) {
console.log('Não foi possível inicicializar corretamente a base de dados!');
Expand Down
32 changes: 32 additions & 0 deletions src/controllers/ActivityController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const ActivityService = require('../services/ActivityService');

class ActivityController {
constructor() {
this.ActivityService = new ActivityService();
}

async fetchActivityList(req, res) {
const { id } = req.query;
const { isUserEntity } = global;
const coords = req.query.coords.split(',').map((coord) => Number(coord));
const categoryArray = req.query.categoryId?.split(',');
const activitiesArray = req.query.activityId?.split(',') || ['getAll'];
const getOtherUsers = req.query.getOtherUsers === 'true';

try {
const activityList = await this.ActivityService.fetchActivityList(
coords,
id,
isUserEntity,
categoryArray,
activitiesArray,
getOtherUsers,
);
return res.json(activityList);
} catch (error) {
return res.status(400).json({ error: error.message });
}
}
}

module.exports = ActivityController;
75 changes: 75 additions & 0 deletions src/controllers/BadgeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const BadgeService = require('../services/BadgeService');
const saveError = require('../utils/ErrorHistory');
const parseBadgeByCategory = require('../utils/parseBadgeByCategory');

class BadgeController {
constructor() {
this.BadgeService = new BadgeService();
}

async updateOrCreateBadge(req, res, next) {
const { userId, category } = req.body;

try {
let result = await this.BadgeService.createOrUpdateBadge(
userId,
category,
);
if (
result.badge.template.nextBadge
&& result.badge.currentValue >= result.badge.template.nextBadge.neededValue
) {
result = await this.BadgeService.updateBadgeReference(result.badge);
}
res.status(201).json(result);
next();
} catch (err) {
await saveError(err);
res.status(400).json({ error: err.message });
next();
}
}

async getBadgeHistory(req, res, next) {
const userId = req.query.userId || null;
try {
const userBadges = await this.BadgeService.getBadgeList(userId);
const allBadges = await this.BadgeService.getAllBadges();
const parsedUserBadges = parseBadgeByCategory(userBadges);
res.status(200).json({ userBadges: parsedUserBadges, allBadges });
next();
} catch (err) {
await saveError(err);
res.status(400).json({ error: err.message });
next();
}
}

async getBadgeList(req, res, next) {
const userId = req.query.userId || null;
try {
const result = await this.BadgeService.getBadgeList(userId);
res.status(200).json(result);
next();
} catch (err) {
await saveError(err);
res.status(400).json({ error: err.message });
next();
}
}

async markBadgeAsViewed(req, res, next) {
const { badgeId } = req.params;
try {
await this.BadgeService.markBadgeAsViewed(badgeId);
res.status(200).json({ message: 'Badge visualized' });
next();
} catch (err) {
await saveError(err);
res.status(400).json({ error: err.message });
next();
}
}
}

module.exports = BadgeController;
5 changes: 4 additions & 1 deletion src/controllers/EntityController.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class EntityController {
notificationToken: req.body.notificationToken,
deviceId: req.body.deviceId,
address: req.body.address,
biography: req.body.biography,
};
try {
const result = await this.entityService.editEntityById(data);
Expand Down Expand Up @@ -122,7 +123,9 @@ class EntityController {
}

try {
const result = await this.entityService.checkEntityExistence(entityIdentifier);
const result = await this.entityService.checkEntityExistence(
entityIdentifier,
);
res.status(200).json(result);
next();
} catch (err) {
Expand Down
43 changes: 43 additions & 0 deletions src/controllers/FeedbackController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const FeedbackService = require('../services/FeedbackService');
const TimelineEventService = require('../services/TimelineEventService');
const saveError = require('../utils/ErrorHistory');
const timelineEnum = require('../utils/enums/timelineEnum');

class FeedbackController {
constructor() {
this.FeedbackService = new FeedbackService();
this.TimelineEventService = new TimelineEventService();
}

async create(req, res) {
try {
const { message, sender, receiver } = req.body;
const feedback = {
sender,
receiver,
message,
};
const newFeedback = await this.FeedbackService.create(feedback);
await this.TimelineEventService.create({ user: sender, template: timelineEnum.feedback });
res.status(200).json(newFeedback);
} catch (error) {
saveError(error);
res.status(500).json({ error: 'Erro ao criar feedback' });
}
}

async listByReceiver(req, res) {
try {
const { receiver } = req.params;
const feedbacks = await this.FeedbackService.listByReceiver(
receiver,
);
res.status(200).json(feedbacks);
} catch (error) {
saveError(error);
res.status(500).json({ error: 'Erro ao listar feedbacks' });
}
}
}

module.exports = FeedbackController;
7 changes: 6 additions & 1 deletion src/controllers/HelpController.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
const HelpService = require('../services/HelpService');
const UserService = require('../services/UserService');
const TimelineEventService = require('../services/TimelineEventService');
const saveError = require('../utils/ErrorHistory');
const timelineEnum = require('../utils/enums/timelineEnum');

class HelpController {
constructor() {
this.HelpService = new HelpService();
this.UserService = new UserService();
this.TimelineEventService = new TimelineEventService();
}

async createHelp(req, res, next) {
const data = {
...req.body,
};

try {
await this.HelpService.createHelp(data);
await this.TimelineEventService.create({ user: data.ownerId, template: timelineEnum.createRequest });
res.status(201).send();
next();
} catch (err) {
Expand Down Expand Up @@ -119,6 +122,7 @@ class HelpController {

try {
await this.HelpService.ownerConfirmation(data);
await this.TimelineEventService.create({ user: data.ownerId, template: timelineEnum.finishRequest });
res.status(204).send();
next();
} catch (err) {
Expand Down Expand Up @@ -147,6 +151,7 @@ class HelpController {

try {
await this.HelpService.addPossibleHelpers(id, idHelper);
await this.TimelineEventService.create({ user: idHelper, template: timelineEnum.offerHelp });
res.status(204).send();
next();
} catch (err) {
Expand Down
7 changes: 6 additions & 1 deletion src/controllers/HelpOfferController.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const HelpOfferService = require('../services/HelpOfferService');
const TimelineEventService = require('../services/TimelineEventService');
const saveError = require('../utils/ErrorHistory');
const timelineEnum = require('../utils/enums/timelineEnum');

class OfferedHelpController {
constructor() {
this.HelpOfferService = new HelpOfferService();
this.TimelineEventService = new TimelineEventService();
}

async createHelpOffer(req, res) {
try {
const newHelpOffer = await this.HelpOfferService.createNewHelpOffer(
req.body,
);
await this.TimelineEventService.create({ user: req.body.ownerId, template: timelineEnum.offer });
return res.json(newHelpOffer);
} catch (error) {
return res.status(400).json({ error: error.message });
Expand All @@ -35,8 +39,9 @@ class OfferedHelpController {
const { userId } = req.query;
const getOtherUsers = req.query.getOtherUsers === 'true';
const { isUserEntity } = global;
const coords = req.query.coords?.split(',')?.map((coord) => Number(coord)) || '';
try {
const helpOffers = await this.HelpOfferService.listHelpsOffers(userId, isUserEntity, null, getOtherUsers);
const helpOffers = await this.HelpOfferService.listHelpsOffers(userId, isUserEntity, null, getOtherUsers, coords);
return res.json(helpOffers);
} catch (error) {
return res.status(400).json({ error: error.message });
Expand Down
Loading

0 comments on commit 4347341

Please sign in to comment.