diff --git a/scripts/fc-lookup.ts b/scripts/fc-lookup.ts new file mode 100644 index 000000000..801663246 --- /dev/null +++ b/scripts/fc-lookup.ts @@ -0,0 +1,38 @@ +import "dotenv/config"; +import invariant from "tiny-invariant"; +import { db } from "~/db/sql"; +import { logger } from "~/utils/logger"; + +const friendCode = process.argv[2]?.trim(); + +invariant(friendCode, "friend code is required (argument 1)"); + +async function main() { + const allFcs = await db + .selectFrom("UserFriendCode") + .innerJoin("User", "User.id", "UserFriendCode.userId") + .select([ + "UserFriendCode.friendCode", + "User.id as userId", + "User.discordId", + "User.discordUniqueName", + ]) + .orderBy("UserFriendCode.createdAt", "asc") + .whereRef("User.id", "=", "UserFriendCode.submitterUserId") + .execute(); + + const matches = allFcs.filter((fc) => fc.friendCode === friendCode); + + if (matches.length === 0) { + logger.info("No matches found"); + return; + } + + for (const match of matches) { + logger.info( + `${match.friendCode} - ${match.discordUniqueName} - ${match.discordId}`, + ); + } +} + +void main(); diff --git a/scripts/recent-fcs.ts b/scripts/recent-fcs.ts new file mode 100644 index 000000000..d1f45a4db --- /dev/null +++ b/scripts/recent-fcs.ts @@ -0,0 +1,59 @@ +import "dotenv/config"; +import { db } from "~/db/sql"; +import { databaseTimestampToDate } from "~/utils/dates"; +import { logger } from "~/utils/logger"; + +async function main() { + const allFcs = await db + .selectFrom("UserFriendCode") + .innerJoin("User", "User.id", "UserFriendCode.userId") + .select(["UserFriendCode.friendCode", "User.id as userId"]) + .orderBy("UserFriendCode.createdAt", "asc") + .whereRef("User.id", "=", "UserFriendCode.submitterUserId") + .execute(); + + const fcMap = new Map(); + for (const fc of allFcs) { + const fcs = fcMap.get(fc.friendCode) ?? []; + fcs.push(fc.userId); + fcMap.set(fc.friendCode, fcs); + } + + const friendCodeAdders = await db + .selectFrom("UserFriendCode") + .innerJoin("User", "User.id", "UserFriendCode.userId") + .select([ + "UserFriendCode.friendCode", + "UserFriendCode.createdAt", + "User.id", + "User.discordId", + "User.discordUniqueName", + ]) + .orderBy("UserFriendCode.createdAt", "desc") + .whereRef("User.id", "=", "UserFriendCode.submitterUserId") + .limit(90) + .execute(); + + let result = ""; + + let date = ""; + for (const [i, friendCodeAdder] of friendCodeAdders.entries()) { + const utc = databaseTimestampToDate( + friendCodeAdder.createdAt, + ).toUTCString(); + const newDate = utc.split(",")[0]; + if (date !== newDate) { + date = newDate; + result += "\n"; + } + + const isDuplicate = + (fcMap.get(friendCodeAdder.friendCode) ?? [])?.length > 1; + + result += `${i < 9 ? "0" : ""}${i + 1}) ${utc} - ${friendCodeAdder.friendCode}${isDuplicate ? " >>DUPLICATE<<" : ""} - ${friendCodeAdder.discordUniqueName} - ${friendCodeAdder.discordId}\n`; + } + + logger.info(result); +} + +void main();