-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
raffles: implement consolation prize system
- Loading branch information
Showing
7 changed files
with
123 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,51 @@ | ||
import { prisma } from '@worksheets/prisma'; | ||
import { RafflesService } from '@worksheets/services/raffles'; | ||
import { NotificationsService } from '@worksheets/services/notifications'; | ||
import { | ||
EXPIRED_RAFFLE_PROPS, | ||
RafflesService, | ||
} from '@worksheets/services/raffles'; | ||
import { createCronJob } from '@worksheets/util/cron'; | ||
import { retryTransaction } from '@worksheets/util/prisma'; | ||
|
||
export default createCronJob(async () => { | ||
const raffles = new RafflesService(prisma); | ||
await raffles.processExpiredRaffles(); | ||
const notifications = new NotificationsService(prisma); | ||
const expiredRaffles = await prisma.raffle.findMany({ | ||
where: { | ||
expiresAt: { | ||
lte: new Date(), | ||
}, | ||
status: 'ACTIVE', | ||
}, | ||
select: EXPIRED_RAFFLE_PROPS, | ||
}); | ||
console.info(`Found ${expiredRaffles.length} expired raffles.`); | ||
|
||
for (const expired of expiredRaffles) { | ||
console.info(`Processing expired raffle ${expired.id}.`); | ||
const { winners, losers, raffle } = await retryTransaction( | ||
prisma, | ||
async (tx) => { | ||
const raffles = new RafflesService(tx); | ||
return await raffles.processExpiredRaffle(expired); | ||
} | ||
); | ||
console.info(`Processed expired raffle ${expired.id}.`); | ||
for (const winner of winners) { | ||
await notifications.send('won-raffle', { | ||
raffle, | ||
user: winner.user, | ||
item: raffle.item, | ||
}); | ||
} | ||
await notifications.send('lost-raffle', { | ||
...raffle, | ||
participants: losers, | ||
}); | ||
await notifications.send('raffle-expired', { | ||
...raffle, | ||
name: raffle.name ?? raffle.item.name, | ||
}); | ||
} | ||
|
||
console.info(`Finished processing ${expiredRaffles.length} expired raffles.`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters