From 8d1573d4ac94537d11844fa9b99edb056a8efea1 Mon Sep 17 00:00:00 2001 From: sudjoao Date: Tue, 20 Jun 2023 07:18:57 -0300 Subject: [PATCH 1/3] chore: update mocked badges --- .../seed/mockedInfos/mockedBadgesInfo.js | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/utils/seed/mockedInfos/mockedBadgesInfo.js b/src/utils/seed/mockedInfos/mockedBadgesInfo.js index 48a35201..df057830 100644 --- a/src/utils/seed/mockedInfos/mockedBadgesInfo.js +++ b/src/utils/seed/mockedInfos/mockedBadgesInfo.js @@ -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, }, ]; From 384648bc45b365b9ef2479341b470f340f2004a7 Mon Sep 17 00:00:00 2001 From: sudjoao Date: Tue, 20 Jun 2023 07:19:39 -0300 Subject: [PATCH 2/3] feat: add new fields in badge model and create logic to mark as viewed --- src/controllers/BadgeController.js | 25 +++++++++++++++++++------ src/models/Badge.js | 7 +++++++ src/repository/BadgeRepository.js | 5 +++++ src/routes/BadgeRoutes.js | 8 ++++++++ src/services/BadgeService.js | 13 ++++++++++--- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/controllers/BadgeController.js b/src/controllers/BadgeController.js index 2b929c3e..59939a9c 100644 --- a/src/controllers/BadgeController.js +++ b/src/controllers/BadgeController.js @@ -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 { @@ -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); diff --git a/src/models/Badge.js b/src/models/Badge.js index 76cef641..b9fae208 100644 --- a/src/models/Badge.js +++ b/src/models/Badge.js @@ -16,6 +16,13 @@ const badgeSchema = new mongoose.Schema( ref: 'User', required: true, }, + updatedAt: { + type: Date, + default: Date.now, + }, + visualizedAt: { + type: Date, + }, }, { collection: 'badge' }, ); diff --git a/src/repository/BadgeRepository.js b/src/repository/BadgeRepository.js index e221ac96..4f0cb5e1 100644 --- a/src/repository/BadgeRepository.js +++ b/src/repository/BadgeRepository.js @@ -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; diff --git a/src/routes/BadgeRoutes.js b/src/routes/BadgeRoutes.js index 1d190900..321748a5 100644 --- a/src/routes/BadgeRoutes.js +++ b/src/routes/BadgeRoutes.js @@ -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; diff --git a/src/services/BadgeService.js b/src/services/BadgeService.js index 4a4cda95..2d0ee1f3 100644 --- a/src/services/BadgeService.js +++ b/src/services/BadgeService.js @@ -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 }; } @@ -40,6 +39,14 @@ class BadgeService { const badges = await this.BadgeTemplateRepository.listAllSorted(); return parseBadgeTemplateByCategory(badges); } + + async markBadgeAsViewed(badgeId) { + const badge = await this.BadgeRepository.getById(badgeId); + console.log(badge); + console.log(Date.now()); + badge.visualizedAt = Date.now(); + await this.BadgeRepository.update(badge); + } } module.exports = BadgeService; From 70e638e24ee11673e37dea61984ce0c518ebe803 Mon Sep 17 00:00:00 2001 From: sudjoao Date: Thu, 22 Jun 2023 06:42:34 -0300 Subject: [PATCH 3/3] chore: removing consoles --- src/services/BadgeService.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/services/BadgeService.js b/src/services/BadgeService.js index 2d0ee1f3..0aced093 100644 --- a/src/services/BadgeService.js +++ b/src/services/BadgeService.js @@ -42,8 +42,6 @@ class BadgeService { async markBadgeAsViewed(badgeId) { const badge = await this.BadgeRepository.getById(badgeId); - console.log(badge); - console.log(Date.now()); badge.visualizedAt = Date.now(); await this.BadgeRepository.update(badge); }