|
| 1 | +import type { Actions, PageServerLoad } from './$types'; |
| 2 | +import { redirect, error, fail } from '@sveltejs/kit'; |
| 3 | +import prismaArg from '$lib/prisma/prismaArg'; |
| 4 | +import { toSteam64FromAny, allIdVariantsForSteam64 } from '$lib/whois/utils'; |
| 5 | + |
| 6 | +type AltLink = { |
| 7 | + steam_id: string; |
| 8 | + main_steam_id: string | null; |
| 9 | + linked_at: Date; |
| 10 | + linked_by: string | null; |
| 11 | +}; |
| 12 | + |
| 13 | +function isValidSteam64(id: string | null | undefined): id is string { |
| 14 | + return !!id && /^\d{17}$/.test(id); |
| 15 | +} |
| 16 | + |
| 17 | +export const load: PageServerLoad = async (event) => { |
| 18 | + const user = event.locals.user as { steamid: string; role?: string } | null; |
| 19 | + |
| 20 | + if (!user) throw redirect(302, '/'); |
| 21 | + if (user.role !== 'owner') throw error(403, 'Forbidden'); |
| 22 | + |
| 23 | + // Fetch all rows and normalize to 64-bit IDs for grouping/display |
| 24 | + const rows = (await prismaArg.whois_alt_links.findMany()) as unknown as AltLink[]; |
| 25 | + |
| 26 | + const normalized = rows |
| 27 | + .map((r) => { |
| 28 | + const main64 = r.main_steam_id ? toSteam64FromAny(r.main_steam_id) : null; |
| 29 | + const alt64 = toSteam64FromAny(r.steam_id); |
| 30 | + return { |
| 31 | + steam_id_64: alt64, |
| 32 | + main_steam_id_64: main64, |
| 33 | + linked_at: r.linked_at, |
| 34 | + linked_by: r.linked_by || null |
| 35 | + }; |
| 36 | + }) |
| 37 | + .filter((r) => !!r.steam_id_64) as Array<{ |
| 38 | + steam_id_64: string; |
| 39 | + main_steam_id_64: string | null; |
| 40 | + linked_at: Date; |
| 41 | + linked_by: string | null; |
| 42 | + }>; |
| 43 | + |
| 44 | + const mainsSet = new Set<string>(); |
| 45 | + for (const r of normalized) { |
| 46 | + if (r.main_steam_id_64 && isValidSteam64(r.main_steam_id_64)) mainsSet.add(r.main_steam_id_64); |
| 47 | + } |
| 48 | + const altIds = new Set(normalized.filter((r) => r.main_steam_id_64).map((r) => r.steam_id_64)); |
| 49 | + for (const r of normalized) { |
| 50 | + if (!altIds.has(r.steam_id_64) && isValidSteam64(r.steam_id_64)) mainsSet.add(r.steam_id_64); |
| 51 | + } |
| 52 | + |
| 53 | + const mains = Array.from(mainsSet); |
| 54 | + const groups = mains.map((main) => ({ |
| 55 | + main, |
| 56 | + alts: normalized.filter((r) => r.main_steam_id_64 === main).map((r) => r.steam_id_64) |
| 57 | + })); |
| 58 | + |
| 59 | + // also return ungrouped: mains with no explicit alts |
| 60 | + return { user, groups }; |
| 61 | +}; |
| 62 | + |
| 63 | +export const actions: Actions = { |
| 64 | + add_main: async ({ request, locals }) => { |
| 65 | + const user = locals.user as { steamid: string; role?: string } | null; |
| 66 | + if (!user || user.role !== 'owner') return fail(403, { message: 'Forbidden' }); |
| 67 | + const form = await request.formData(); |
| 68 | + const mainIn = String(form.get('main') || '').trim(); |
| 69 | + const main = toSteam64FromAny(mainIn); |
| 70 | + if (!main) return fail(400, { message: 'Invalid Steam ID' }); |
| 71 | + |
| 72 | + // Ensure a row exists for the main itself (self-main is represented by absence in table; no-op) |
| 73 | + // We do nothing here except ensure the main is present as a standalone group when loading. |
| 74 | + return { ok: true }; |
| 75 | + }, |
| 76 | + |
| 77 | + add_alt: async ({ request, locals }) => { |
| 78 | + const user = locals.user as { steamid: string; role?: string } | null; |
| 79 | + if (!user || user.role !== 'owner') return fail(403, { message: 'Forbidden' }); |
| 80 | + const form = await request.formData(); |
| 81 | + const mainIn = String(form.get('main') || '').trim(); |
| 82 | + const altIn = String(form.get('alt') || '').trim(); |
| 83 | + const main = toSteam64FromAny(mainIn); |
| 84 | + const alt = toSteam64FromAny(altIn); |
| 85 | + if (!main || !alt) return fail(400, { message: 'Invalid Steam ID' }); |
| 86 | + if (main === alt) return fail(400, { message: 'Alt cannot equal main' }); |
| 87 | + |
| 88 | + await prismaArg.whois_alt_links.upsert({ |
| 89 | + where: { steam_id: alt }, |
| 90 | + update: { main_steam_id: main, linked_by: user.steamid }, |
| 91 | + create: { steam_id: alt, main_steam_id: main, linked_by: user.steamid } |
| 92 | + }); |
| 93 | + return { ok: true }; |
| 94 | + }, |
| 95 | + |
| 96 | + edit_alt: async ({ request, locals }) => { |
| 97 | + const user = locals.user as { steamid: string; role?: string } | null; |
| 98 | + if (!user || user.role !== 'owner') return fail(403, { message: 'Forbidden' }); |
| 99 | + const form = await request.formData(); |
| 100 | + const altIn = String(form.get('alt') || '').trim(); |
| 101 | + const mainIn = String(form.get('main') || '').trim(); |
| 102 | + const alt = toSteam64FromAny(altIn); |
| 103 | + const main = toSteam64FromAny(mainIn); |
| 104 | + if (!alt || !main) return fail(400, { message: 'Invalid Steam ID' }); |
| 105 | + await prismaArg.whois_alt_links.update({ |
| 106 | + where: { steam_id: alt }, |
| 107 | + data: { main_steam_id: main, linked_by: user.steamid } |
| 108 | + }); |
| 109 | + return { ok: true }; |
| 110 | + }, |
| 111 | + |
| 112 | + delete_alt: async ({ request, locals }) => { |
| 113 | + const user = locals.user as { steamid: string; role?: string } | null; |
| 114 | + if (!user || user.role !== 'owner') return fail(403, { message: 'Forbidden' }); |
| 115 | + const form = await request.formData(); |
| 116 | + const altIn = String(form.get('alt') || '').trim(); |
| 117 | + const alt64 = toSteam64FromAny(altIn); |
| 118 | + if (!alt64) return fail(400, { message: 'Invalid Steam ID' }); |
| 119 | + const variants = allIdVariantsForSteam64(alt64); |
| 120 | + await prismaArg.whois_alt_links.deleteMany({ where: { steam_id: { in: variants } } }); |
| 121 | + return { ok: true }; |
| 122 | + } |
| 123 | +}; |
| 124 | + |
| 125 | + |
0 commit comments