Skip to content

Commit

Permalink
Merge pull request #158 from mia-ajuda/226-FinishOfferBadge
Browse files Browse the repository at this point in the history
226 - Refatoração na estrutura de badge
  • Loading branch information
sudjoao authored Jul 16, 2023
2 parents 5dddb0e + 3b8e2da commit 06b1556
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 22 deletions.
25 changes: 19 additions & 6 deletions src/controllers/BadgeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ class BadgeController {
}
}

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 {
Expand All @@ -43,13 +58,11 @@ class BadgeController {
}
}

async getBadgeHistory(req, res, next) {
const userId = req.query.userId || null;
async markBadgeAsViewed(req, res, next) {
const { badgeId } = req.params;
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 });
await this.BadgeService.markBadgeAsViewed(badgeId);
res.status(200).json({ message: 'Badge visualized' });
next();
} catch (err) {
await saveError(err);
Expand Down
7 changes: 7 additions & 0 deletions src/models/Badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const badgeSchema = new mongoose.Schema(
ref: 'User',
required: true,
},
updatedAt: {
type: Date,
default: Date.now,
},
visualizedAt: {
type: Date,
},
},
{ collection: 'badge' },
);
Expand Down
5 changes: 5 additions & 0 deletions src/repository/BadgeRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class BadgeRepository extends BaseRepository {
);
return result;
}

async getById(id) {
const result = await super.$getById(id, null, this.populateData);
return result;
}
}

module.exports = BadgeRepository;
8 changes: 8 additions & 0 deletions src/routes/BadgeRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ routes.get('/badges/history', async (req, res, next) => {
badgeController.getBadgeHistory(req, res, next);
});

routes.put('/badges/:badgeId', async (req, res, next) => {
badgeController.markBadgeAsViewed(req, res, next);
});

routes.get('/badges/list', async (req, res, next) => {
badgeController.getBadgeList(req, res, next);
});

module.exports = routes;
11 changes: 8 additions & 3 deletions src/services/BadgeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@ class BadgeService {
async createOrUpdateBadge(userId, category) {
const badges = await this.BadgeRepository.listByUserId(userId);
let badge = badges.find((item) => item.template.category === category);
let recentUpdated = false;
if (!badge) {
const referenceBadge = await this.BadgeTemplateRepository.getFirstRankByCategory(category);
badge = await this.BadgeRepository.create({
user: userId,
template: referenceBadge._id,
});
recentUpdated = true;
}
badge.currentValue += 1;
const updatedBadge = await this.BadgeRepository.update(badge);
return { badge: updatedBadge, recentUpdated };
return { badge: updatedBadge };
}

async updateBadgeReference(badge) {
badge.template = badge.template.nextBadge;
badge.updatedAt = Date.now();
const updatedBadge = await this.BadgeRepository.update(badge);
return { badge: updatedBadge, recentUpdated: true };
}
Expand All @@ -40,6 +39,12 @@ class BadgeService {
const badges = await this.BadgeTemplateRepository.listAllSorted();
return parseBadgeTemplateByCategory(badges);
}

async markBadgeAsViewed(badgeId) {
const badge = await this.BadgeRepository.getById(badgeId);
badge.visualizedAt = Date.now();
await this.BadgeRepository.update(badge);
}
}

module.exports = BadgeService;
26 changes: 13 additions & 13 deletions src/utils/seed/mockedInfos/mockedBadgesInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ const mockedBadges = [
},
{
_id: 6,
name: 'Testador Beta 3',
description: 'Paritcipou do beta do app',
iconName: 'home',
name: 'Mudando Vidas 3',
description: 'Finalizou dez ofertas no aplicativo',
iconName: 'stars',
neededValue: 5,
rank: 3,
category: 'tester',
category: 'help',
},
{
_id: 5,
name: 'Testador Beta 2',
description: 'Paritcipou do beta do app',
iconName: 'home',
neededValue: 2,
name: 'Mudando Vidas 2',
description: 'Finalizou cinco ofertas no aplicativo',
iconName: 'stars',
neededValue: 5,
rank: 2,
category: 'tester',
category: 'help',
nextBadge: 6,
},
{
_id: 4,
name: 'Testador Beta 1',
description: 'Paritcipou do beta do app',
iconName: 'home',
name: 'Mudando Vidas 1',
description: 'Criou uma oferta no aplicativo',
iconName: 'stars',
neededValue: 1,
rank: 1,
category: 'tester',
category: 'help',
nextBadge: 5,
},
];
Expand Down

0 comments on commit 06b1556

Please sign in to comment.