From 1588eb79fb3f512975214744868088c0a8588447 Mon Sep 17 00:00:00 2001 From: Sander Date: Sat, 22 Jun 2024 16:15:53 +0200 Subject: [PATCH] earn points when your songs are being played --- apps/expo/app/profile/index.tsx | 2 +- apps/expo/src/components/PageTemplate.tsx | 2 +- packages/api/src/service/BadgeService.ts | 16 ++++++++++++++-- packages/api/src/service/FissaService.ts | 23 +++++++++++++++++------ packages/api/src/service/VoteService.ts | 2 +- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/apps/expo/app/profile/index.tsx b/apps/expo/app/profile/index.tsx index 944ed73..23b6463 100644 --- a/apps/expo/app/profile/index.tsx +++ b/apps/expo/app/profile/index.tsx @@ -20,7 +20,7 @@ const Index = () => { }, [signOut, replace]) return ( - + 0 ? BADGE.UP_VOTES_RECEIVED : BADGE.DOWN_VOTES_RECEIVED await transaction.badges.upsert({ where: { userId_name: { userId: forUser, name: forName } }, create: { userId: forUser, name: forName, score: 1 }, update: { score: { increment: 1 } } }) - + await this.pointsEarned(forUser, vote) } }) } catch (error) { console.warn(error) } } + + async pointsEarned(userId: string, amount: number) { + try { + await this.db.badges.upsert(({ + where: { userId_name: { userId, name: BADGE.POINTS_EARNED } }, + create: { userId, name: BADGE.POINTS_EARNED, score: amount }, + update: { score: { increment: amount } } + })) + } catch (error) { + console.warn(error) + } + } } diff --git a/packages/api/src/service/FissaService.ts b/packages/api/src/service/FissaService.ts index 5055243..929a617 100644 --- a/packages/api/src/service/FissaService.ts +++ b/packages/api/src/service/FissaService.ts @@ -103,14 +103,24 @@ export class FissaService extends ServiceWithContext { }; skipTrack = async (pin: string, userId: string) => { - const fissa = await this.byId(pin, userId); + await this.db.$transaction(async transaction => { + const fissa = await this.byId(pin, userId); - if (fissa.userId !== userId) throw new NotTheHost(); - if (!fissa.currentlyPlayingId) throw new FissaIsPaused(); + if (fissa.userId !== userId) throw new NotTheHost(); + if (!fissa.currentlyPlayingId) throw new FissaIsPaused(); - await this.db.track.update({ - where: { pin_trackId: { pin, trackId: fissa.currentlyPlayingId } }, - data: { totalScore: { increment: EarnedPoints.SkipTrack }, score: 0 }, + const track = await transaction.track.update({ + where: { pin_trackId: { pin, trackId: fissa.currentlyPlayingId } }, + data: { totalScore: { increment: EarnedPoints.SkipTrack }, score: 0 }, + }) + + if (track.userId) { + await transaction.userInFissa.update({ + where: { pin_userId: { pin, userId: track.userId } }, + data: { points: { increment: EarnedPoints.SkipTrack } }, + }) + await this.badgeService.pointsEarned(track.userId, EarnedPoints.SkipTrack) + } }) return this.playNextTrack(pin, true); @@ -245,6 +255,7 @@ export class FissaService extends ServiceWithContext { where: { pin_userId: { pin, userId: currentlyPlaying.by.userId } }, data: { points: { increment: EarnedPoints.PlayedTrack } } }) + await this.badgeService.pointsEarned(currentlyPlaying.by.userId, EarnedPoints.PlayedTrack) } } diff --git a/packages/api/src/service/VoteService.ts b/packages/api/src/service/VoteService.ts index 9bb860e..f9766ad 100644 --- a/packages/api/src/service/VoteService.ts +++ b/packages/api/src/service/VoteService.ts @@ -38,7 +38,7 @@ export class VoteService extends ServiceWithContext { }) if (track.userId && track.userId !== userId) { - await this.badgeService.voted(vote, track.userId) + await this.badgeService.voted(voteWeight, track.userId) await transaction.userInFissa.update({ where: { pin_userId: { pin, userId: track.userId } }, data: { points: { increment: voteWeight } }