From 8de32bcb3ff98bc908bfb4ca85a8b20435c3f894 Mon Sep 17 00:00:00 2001 From: gunshiz Date: Fri, 31 Jul 2026 21:14:02 +0700 Subject: [PATCH] fix(admin): timesteampz --- .gitignore | 2 + db/schema/form.ts | 3 +- drizzle/0016_wide_talos.sql | 6 + drizzle/meta/0016_snapshot.json | 692 ++++++++++++++++++++++++++++++++ drizzle/meta/_journal.json | 7 + lib/admin-dashboard.ts | 3 +- 6 files changed, 711 insertions(+), 2 deletions(-) create mode 100644 drizzle/0016_wide_talos.sql create mode 100644 drizzle/meta/0016_snapshot.json diff --git a/.gitignore b/.gitignore index ec22680..af29cf4 100644 --- a/.gitignore +++ b/.gitignore @@ -47,6 +47,8 @@ drizzle/* !drizzle/0013_regular_unus.sql !drizzle/0014_lethal_pepper_potts.sql !drizzle/0015_form_closes_at.sql +!drizzle/0016_wide_talos.sql !drizzle/meta/ drizzle/meta/* !drizzle/meta/0015_snapshot.json +!drizzle/meta/0016_snapshot.json diff --git a/db/schema/form.ts b/db/schema/form.ts index be8020f..396a4c5 100644 --- a/db/schema/form.ts +++ b/db/schema/form.ts @@ -1,5 +1,6 @@ import { pgSchema, + pgTable, text, timestamp, boolean, @@ -63,7 +64,7 @@ export const questions = formSchema.table("question", { .default("none"), }); -export const cdn = formSchema.table("cdn", { +export const cdn = pgTable("cdn", { id: text("id") .primaryKey() .$defaultFn(() => uuidv7()), diff --git a/drizzle/0016_wide_talos.sql b/drizzle/0016_wide_talos.sql new file mode 100644 index 0000000..77d7ff9 --- /dev/null +++ b/drizzle/0016_wide_talos.sql @@ -0,0 +1,6 @@ +ALTER TABLE "form"."cdn" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint +ALTER TABLE "form"."cdn" SET SCHEMA "public"; +--> statement-breakpoint +ALTER TABLE "form"."answer" DROP CONSTRAINT "answer_image_cdn_id_cdn_id_fk"; +--> statement-breakpoint +ALTER TABLE "form"."answer" ADD CONSTRAINT "answer_image_cdn_id_cdn_id_fk" FOREIGN KEY ("image_cdn_id") REFERENCES "public"."cdn"("id") ON DELETE set null ON UPDATE cascade; \ No newline at end of file diff --git a/drizzle/meta/0016_snapshot.json b/drizzle/meta/0016_snapshot.json new file mode 100644 index 0000000..620c338 --- /dev/null +++ b/drizzle/meta/0016_snapshot.json @@ -0,0 +1,692 @@ +{ + "id": "94182237-a4fa-4101-b263-2689df4a2fad", + "prevId": "fec233ea-49f6-439d-8512-cbb880c8b1d0", + "version": "7", + "dialect": "postgresql", + "tables": { + "account.account": { + "name": "account", + "schema": "account", + "columns": { + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "providerAccountId": { + "name": "providerAccountId", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "refresh_token": { + "name": "refresh_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "access_token": { + "name": "access_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "expires_at": { + "name": "expires_at", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "token_type": { + "name": "token_type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "id_token": { + "name": "id_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "session_state": { + "name": "session_state", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "account_userId_user_id_fk": { + "name": "account_userId_user_id_fk", + "tableFrom": "account", + "tableTo": "user", + "schemaTo": "account", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "account_provider_providerAccountId_pk": { + "name": "account_provider_providerAccountId_pk", + "columns": [ + "provider", + "providerAccountId" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "account.session": { + "name": "session", + "schema": "account", + "columns": { + "sessionToken": { + "name": "sessionToken", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires": { + "name": "expires", + "type": "timestamp", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "session_userId_user_id_fk": { + "name": "session_userId_user_id_fk", + "tableFrom": "session", + "tableTo": "user", + "schemaTo": "account", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "account.user": { + "name": "user", + "schema": "account", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "emailVerified": { + "name": "emailVerified", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "image": { + "name": "image", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "discord_id": { + "name": "discord_id", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "user_email_unique": { + "name": "user_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "account.verificationToken": { + "name": "verificationToken", + "schema": "account", + "columns": { + "identifier": { + "name": "identifier", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "token": { + "name": "token", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires": { + "name": "expires", + "type": "timestamp", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": { + "verificationToken_identifier_token_pk": { + "name": "verificationToken_identifier_token_pk", + "columns": [ + "identifier", + "token" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "form.answer": { + "name": "answer", + "schema": "form", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "submission_id": { + "name": "submission_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "question_id": { + "name": "question_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "image_cdn_id": { + "name": "image_cdn_id", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "answer_submission_id_submission_id_fk": { + "name": "answer_submission_id_submission_id_fk", + "tableFrom": "answer", + "tableTo": "submission", + "schemaTo": "form", + "columnsFrom": [ + "submission_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "answer_question_id_question_id_fk": { + "name": "answer_question_id_question_id_fk", + "tableFrom": "answer", + "tableTo": "question", + "schemaTo": "form", + "columnsFrom": [ + "question_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "answer_image_cdn_id_cdn_id_fk": { + "name": "answer_image_cdn_id_cdn_id_fk", + "tableFrom": "answer", + "tableTo": "cdn", + "columnsFrom": [ + "image_cdn_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "cascade" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.cdn": { + "name": "cdn", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "data": { + "name": "data", + "type": "bytea", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "size": { + "name": "size", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "owner_discord_id": { + "name": "owner_discord_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "form.form": { + "name": "form", + "schema": "form", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "is_open": { + "name": "is_open", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "closes_at": { + "name": "closes_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "discord_webhook_url": { + "name": "discord_webhook_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "discord_webhook_template": { + "name": "discord_webhook_template", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "discord_webhook_update_template": { + "name": "discord_webhook_update_template", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "allowed_roles": { + "name": "allowed_roles", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + }, + "denied_roles": { + "name": "denied_roles", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + }, + "allow_multiple_submissions": { + "name": "allow_multiple_submissions", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "form.question": { + "name": "question", + "schema": "form", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "form_id": { + "name": "form_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'text'" + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "image_url": { + "name": "image_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "display_order": { + "name": "display_order", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "required": { + "name": "required", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "options": { + "name": "options", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + }, + "allow_other": { + "name": "allow_other", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "image_answer_mode": { + "name": "image_answer_mode", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'none'" + } + }, + "indexes": {}, + "foreignKeys": { + "question_form_id_form_id_fk": { + "name": "question_form_id_form_id_fk", + "tableFrom": "question", + "tableTo": "form", + "schemaTo": "form", + "columnsFrom": [ + "form_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "form.submission": { + "name": "submission", + "schema": "form", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "form_id": { + "name": "form_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_discord_id": { + "name": "user_discord_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_name": { + "name": "user_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "admin_note": { + "name": "admin_note", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "submitted_at": { + "name": "submitted_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "edit_history": { + "name": "edit_history", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + } + }, + "indexes": { + "submission_form_submitted_at_id_idx": { + "name": "submission_form_submitted_at_id_idx", + "columns": [ + { + "expression": "form_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "submitted_at", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "submission_form_id_form_id_fk": { + "name": "submission_form_id_form_id_fk", + "tableFrom": "submission", + "tableTo": "form", + "schemaTo": "form", + "columnsFrom": [ + "form_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": { + "account": "account", + "form": "form" + }, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index ad01250..a1749e9 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -113,6 +113,13 @@ "when": 1785431046640, "tag": "0015_form_closes_at", "breakpoints": true + }, + { + "idx": 16, + "version": "7", + "when": 1785435766276, + "tag": "0016_wide_talos", + "breakpoints": true } ] } \ No newline at end of file diff --git a/lib/admin-dashboard.ts b/lib/admin-dashboard.ts index d462926..4fab7c1 100644 --- a/lib/admin-dashboard.ts +++ b/lib/admin-dashboard.ts @@ -64,12 +64,13 @@ export async function getAdminDashboardData( ): Promise { const { todayStart, tomorrowStart, last7DaysStart } = getDashboardDateWindows(now); + const nowSql = now.toISOString(); const todayStartSql = toPostgresTimestamp(todayStart); const tomorrowStartSql = toPostgresTimestamp(tomorrowStart); const last7DaysStartSql = toPostgresTimestamp(last7DaysStart); const totalResponses = sql`count(${submissions.id})::integer`; const acceptingSubmissions = sql`${forms.isOpen} and ( - ${forms.closesAt} is null or ${forms.closesAt} > ${now} + ${forms.closesAt} is null or ${forms.closesAt} > ${nowSql}::timestamptz )`; const responsesToday = sql`count(${submissions.id}) filter ( where ${submissions.submittedAt} >= ${todayStartSql}::timestamp