|
| 1 | +/** |
| 2 | + * Outputs a JSON file with the students who have not collected their merch, for use by the mailmerge tool |
| 3 | + * to send a reminder to these people. |
| 4 | + * |
| 5 | + * Created with co-pilot. |
| 6 | + */ |
| 7 | +import { PrismaClient } from "@prisma/client"; |
| 8 | +// Load .env file ".env" |
| 9 | +import dotenv from "dotenv"; |
| 10 | +import fs from "fs/promises"; |
| 11 | + |
| 12 | +dotenv.config(); |
| 13 | + |
| 14 | +const prisma = new PrismaClient(); |
| 15 | + |
| 16 | +interface OutputRecord { |
| 17 | + to: string; |
| 18 | + name: string; |
| 19 | + shortcode: string; |
| 20 | + subject: string; |
| 21 | + cid: string; |
| 22 | + orderid: number; |
| 23 | + quantity: number; |
| 24 | +} |
| 25 | + |
| 26 | +async function getUncollectedPeople() { |
| 27 | + const uncollectedOrders = await prisma.variant.findFirst({ |
| 28 | + where: { |
| 29 | + RootItem: { |
| 30 | + name: "Duck T-Shirt (White)", |
| 31 | + }, |
| 32 | + variantName: "S (36\")", |
| 33 | + }, |
| 34 | + include: { |
| 35 | + OrderItem: { |
| 36 | + include: { |
| 37 | + Order: { |
| 38 | + include: { |
| 39 | + ImperialStudent: true, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + const outputRecords: OutputRecord[] = []; |
| 48 | + |
| 49 | + const studentMap = new Map<string, OutputRecord>(); |
| 50 | + |
| 51 | + if (!uncollectedOrders) { |
| 52 | + console.error("No uncollected orders found"); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + for (const orderItem of uncollectedOrders.OrderItem) { |
| 57 | + const student = orderItem.Order.ImperialStudent; |
| 58 | + const studentKey = student.email; |
| 59 | + |
| 60 | + if (orderItem.collected) { |
| 61 | + console.log(`Skipping ${studentKey} as already collected`); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (!studentMap.has(studentKey)) { |
| 66 | + studentMap.set(studentKey, { |
| 67 | + to: student.email, |
| 68 | + name: `${student.firstName} ${student.lastName}`, |
| 69 | + shortcode: student.shortcode, |
| 70 | + //itemsToCollect: [], |
| 71 | + subject: `Information about the Duck T-Shirt (Small) you ordered`, |
| 72 | + cid: student.cid, |
| 73 | + orderid: orderItem.orderId, |
| 74 | + quantity: orderItem.quantity, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + outputRecords.push(...studentMap.values()); |
| 81 | + |
| 82 | + await fs.writeFile("data/duck-refund.json", JSON.stringify(outputRecords, null, 2)); |
| 83 | +} |
| 84 | + |
| 85 | +getUncollectedPeople() |
| 86 | + .catch((e) => { |
| 87 | + console.error(e); |
| 88 | + }) |
| 89 | + .finally(async () => { |
| 90 | + await prisma.$disconnect(); |
| 91 | + }); |
0 commit comments