Skip to content

Commit

Permalink
earn points when your songs are being played
Browse files Browse the repository at this point in the history
  • Loading branch information
xiduzo committed Jun 22, 2024
1 parent 7d1c346 commit 1588eb7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/expo/app/profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Index = () => {
}, [signOut, replace])

return (
<PageTemplate fullScreen className="p-6 pb-8">
<PageTemplate fullScreen className="p-6 pb-4">
<Stack.Screen
options={{
headerShown: true,
Expand Down
2 changes: 1 addition & 1 deletion apps/expo/src/components/PageTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const pageTemplate = cva("m-auto h-full w-full justify-between", {
*/
fullScreen: {
true: "max-w-screen-2xl",
false: "p-6 pb-8 max-w-lg",
false: "p-6 pb-4 max-w-lg",
},
},
defaultVariants: {
Expand Down
16 changes: 14 additions & 2 deletions packages/api/src/service/BadgeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,30 @@ export class BadgeService extends ServiceWithContext {
update: { score: { increment: 1 } }
})

if (forUser) {
if (forUser && forUser !== userId) {
const forName = vote > 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)
}
}
}
23 changes: 17 additions & 6 deletions packages/api/src/service/FissaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/service/VoteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
Expand Down

0 comments on commit 1588eb7

Please sign in to comment.