-
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.
- Loading branch information
1 parent
0f4feed
commit 7b3164e
Showing
7 changed files
with
121 additions
and
10 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { defineConfig } from "$fresh/server.ts"; | |
import tailwind from "$fresh/plugins/tailwind.ts"; | ||
import { kvInsightsPlugin } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { kv } from "@/utils/db/kv.ts"; | ||
import "./utils/email.ts"; | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { kv, Event, Ticket, ShowTime } from "@/utils/db/kv.ts"; | ||
|
||
const cronTask = async () => { | ||
const events = kv.list<Event>({ prefix: ["event"] }); | ||
|
||
for await (const event of events) { | ||
const showtimes = event.value.showTimes | ||
.filter((s) => !s.hasEmailed) | ||
.sort( | ||
(a, b) => | ||
new Date(a.startDate).valueOf() - | ||
new Date(b.startDate).valueOf(), | ||
); | ||
|
||
const showtimesStartingIn24Hours = showtimes.filter( | ||
(showtime) => | ||
new Date(showtime.startDate).valueOf() - Date.now() < | ||
24 * 60 * 60 * 1000, | ||
); | ||
|
||
if (showtimesStartingIn24Hours.length > 0) { | ||
await kv.enqueue( | ||
JSON.stringify({ | ||
event, | ||
showtimesStartingIn24Hours, | ||
eventID: event.key[1], | ||
}), | ||
); | ||
} | ||
} | ||
}; | ||
|
||
cronTask(); | ||
|
||
// Deno.cron("check emails", "0 */4 * * *", cronTask); | ||
|
||
kv.listenQueue(async (msg) => { | ||
const message = JSON.parse(msg) as | ||
| { | ||
eventID: string; | ||
event: Event; | ||
showtimesStartingIn24Hours: Event["showTimes"]; | ||
} | ||
| { | ||
eventID: string; | ||
event: Event; | ||
showtime: ShowTime; | ||
cursor: string; | ||
}; | ||
|
||
if ("showtimesStartingIn24Hours" in message) { | ||
let delay = 0; | ||
|
||
for (const showtime of message.showtimesStartingIn24Hours) { | ||
await kv.set(["event", message.eventID], { | ||
...message.event, | ||
showTimes: message.event.showTimes.map((s) => | ||
s.id === showtime.id ? { ...s, hasEmailed: true } : s, | ||
), | ||
}); | ||
await kv.enqueue( | ||
JSON.stringify({ | ||
eventID: message.eventID, | ||
event: message.event, | ||
showtime, | ||
}), | ||
{ | ||
delay: delay, | ||
}, | ||
); | ||
|
||
delay += 1000 + Math.random() * 3000; | ||
} | ||
} else { | ||
await startEmails( | ||
message.eventID, | ||
message.event, | ||
message.showtime, | ||
message.cursor, | ||
); | ||
} | ||
}); | ||
|
||
const startEmails = async ( | ||
eventID: string, | ||
event: Event, | ||
showtime: ShowTime, | ||
cursor?: string, | ||
) => { | ||
const tickets = kv.list<Ticket>( | ||
{ prefix: ["ticket", eventID, showtime.id] }, | ||
{ cursor, limit: 18 }, | ||
); | ||
|
||
for await (const ticket of tickets) { | ||
console.log("Sending email to", ticket.value.userEmail); | ||
} | ||
|
||
if (tickets.cursor) { | ||
await kv.enqueue( | ||
JSON.stringify({ | ||
eventID, | ||
event, | ||
showtime, | ||
cursor: tickets.cursor, | ||
}), | ||
{ delay: 1000 }, | ||
); | ||
} | ||
}; |
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