-
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
4144d18
commit 60f56b4
Showing
4 changed files
with
144 additions
and
2 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
"rules": { "tags": ["fresh", "recommended"], "include": ["no-unused-vars"] } | ||
}, | ||
"imports": { | ||
"$fresh/": "https://deno.land/x/[email protected].6/", | ||
"$fresh/": "https://deno.land/x/[email protected].8/", | ||
"preact": "https://esm.sh/[email protected]", | ||
"preact/": "https://esm.sh/[email protected]/", | ||
"preact-render-to-string": "https://esm.sh/*[email protected]", | ||
|
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,89 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import { Event, getUser, kv, Ticket } from "@/utils/db/kv.ts"; | ||
import { isUUID } from "@/utils/db/misc.ts"; | ||
|
||
export const handler: Handlers = { | ||
async POST(req, _ctx) { | ||
const user = await getUser(req); | ||
if (!user) { | ||
return new Response(JSON.stringify({ error: "Not logged in" }), { | ||
status: 401, | ||
}); | ||
} | ||
|
||
const allowedEmails = JSON.parse(Deno.env.get("ALLOWED_EMAILS") ?? "[]"); | ||
|
||
if (!allowedEmails.includes(user.email)) { | ||
return new Response(JSON.stringify({ error: "Not authorized" }), { | ||
status: 401, | ||
}); | ||
} | ||
|
||
const { eventID, showtimeID }: { eventID: string; showtimeID: string } = | ||
await req.json(); | ||
|
||
if (!isUUID(eventID) || !isUUID(showtimeID)) { | ||
return new Response(JSON.stringify({ error: "Invalid UUID" }), { | ||
status: 400, | ||
}); | ||
} | ||
|
||
const event = await kv.get<Event>(["event", eventID]); | ||
|
||
if (!event || !event.value) { | ||
return new Response(JSON.stringify({ error: "Invalid event ID" }), { | ||
status: 400, | ||
}); | ||
} | ||
|
||
const showtime = event.value.showTimes.find((s) => s.id === showtimeID); | ||
|
||
if (!showtime) { | ||
return new Response(JSON.stringify({ error: "Invalid showtime ID" }), { | ||
status: 400, | ||
}); | ||
} | ||
|
||
const getAllTickets = async () => { | ||
const ticketsList = kv.list<Ticket>({ | ||
prefix: ["ticket", eventID, showtimeID], | ||
}); | ||
|
||
const tickets: [string, Ticket][] = []; | ||
|
||
for await (const ticket of ticketsList) { | ||
tickets.push([ticket.key[3] as string, ticket.value]); | ||
} | ||
|
||
return tickets; | ||
}; | ||
|
||
const tickets = await getAllTickets(); | ||
|
||
let delayInEmailSendSeconds = 1; | ||
|
||
for (const [ticketID, ticket] of tickets) { | ||
kv.enqueue( | ||
{ | ||
action: "sendEmail", | ||
payload: { | ||
to: ticket.userEmail, | ||
code: ticketID, | ||
eventID: eventID, | ||
tickets: ticket.tickets, | ||
eventName: event.value.name, | ||
}, | ||
}, | ||
{ | ||
delay: delayInEmailSendSeconds * 1000, | ||
}, | ||
); | ||
|
||
delayInEmailSendSeconds += 1; | ||
} | ||
|
||
return new Response(JSON.stringify({ success: true }), { | ||
status: 200, | ||
}); | ||
}, | ||
}; |
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