From 01bdf6316da68b09663f73151488bf6f1b4a9940 Mon Sep 17 00:00:00 2001 From: Kyle Kemp Date: Tue, 19 Sep 2023 07:34:58 -0500 Subject: [PATCH] feat(lottery): notify winner if they win via ticket numbers --- .../modules/lottery/buyinlottery.service.ts | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/server/src/modules/lottery/buyinlottery.service.ts b/server/src/modules/lottery/buyinlottery.service.ts index 783ba16..dcdc196 100644 --- a/server/src/modules/lottery/buyinlottery.service.ts +++ b/server/src/modules/lottery/buyinlottery.service.ts @@ -69,6 +69,28 @@ export class BuyInLotteryService implements OnModuleInit { const newRecord = new LotteryBuyInDraw(this.generateBuyTicketNumber()); emCtx.persist(newRecord); await emCtx.flush(); + + const winningTicket = await this.buyinTickets.findOne({ + ticketNumber: newRecord.ticketNumber, + createdAt: { $gte: startOfToday() }, + }); + if (!winningTicket) return; + + this.events.emit('notification.create', { + userId: winningTicket.userId, + notification: { + liveAt: new Date(), + text: `You have won the ticket lottery with ticket ${winningTicket.ticketNumber}!`, + actions: [ + { + text: 'Claim', + action: 'navigate', + actionData: { url: '/' }, + }, + ], + }, + expiresAfterHours: 1, + }); } public async buyTicket(userId: string): Promise { @@ -175,13 +197,7 @@ export class BuyInLotteryService implements OnModuleInit { text: `You have won the ticket lottery with ticket ${ todayTicket.ticketNumber } and got ${total.toLocaleString()} coins!`, - actions: [ - { - text: 'Claim', - action: 'navigate', - actionData: { url: '/' }, - }, - ], + actions: [], }, expiresAfterHours: 1, });