import { defineAction } from 'astro:actions'; import { Session } from '@util/session.ts'; import { Permissions } from '@util/permissions.ts'; import { db } from '@db/database.ts'; import { z } from 'astro:schema'; export const report = { addReport: defineAction({ input: z.object({ reason: z.string(), body: z.string().nullable(), createdAt: z.string().datetime().nullable(), reporter: z.number(), reported: z.number().nullable() }), handler: async (input, context) => { Session.actionSessionFromCookies(context.cookies, Permissions.Reports); const { id } = await db.addReport({ reason: input.reason, body: input.body, createdAt: input.createdAt, reporterTeamId: input.reporter, reportedTeamId: input.reported }); return { id: id }; } }), reportStatus: defineAction({ input: z.object({ reportId: z.number() }), handler: async (input, context) => { Session.actionSessionFromCookies(context.cookies, Permissions.Reports); return { reportStatus: await db.getReportStatus(input) }; } }), editReportStatus: defineAction({ input: z.object({ reportId: z.number(), status: z.enum(['open', 'closed']).nullable(), notice: z.string().nullable(), statement: z.string().nullable(), strikeReasonId: z.number().nullable() }), handler: async (input, context) => { Session.actionSessionFromCookies(context.cookies, Permissions.Reports); await db.transaction(async (tx) => { await tx.editReportStatus(input); if (input.strikeReasonId) { await db.editStrike({ reportId: input.reportId, strikeReasonId: input.strikeReasonId }); } }); } }), reports: defineAction({ input: z.object({ reporter: z.string().nullish(), reported: z.string().nullish() }), handler: async (input, context) => { Session.actionSessionFromCookies(context.cookies, Permissions.Reports); return { reports: await db.getReports(input) }; } }), addStrikeReason: defineAction({ input: z.object({ name: z.string(), weight: z.number() }), handler: async (input, context) => { Session.actionSessionFromCookies(context.cookies, Permissions.Admin); return await db.addStrikeReason(input); } }), editStrikeReason: defineAction({ input: z.object({ id: z.number(), name: z.string(), weight: z.number() }), handler: async (input, context) => { Session.actionSessionFromCookies(context.cookies, Permissions.Admin); await db.editStrikeReason(input); } }), deleteStrikeReason: defineAction({ input: z.object({ id: z.number() }), handler: async (input, context) => { Session.actionSessionFromCookies(context.cookies, Permissions.Admin); await db.deleteStrikeReason(input); } }), strikeReasons: defineAction({ handler: async (_, context) => { Session.actionSessionFromCookies(context.cookies, Permissions.Reports); return { strikeReasons: await db.getStrikeReasons({}) }; } }) };