From e54a9280eeb3067308f449efb7aae3a0a2b51a4a Mon Sep 17 00:00:00 2001 From: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Fri, 18 Apr 2025 17:11:06 -0600 Subject: [PATCH] add region route MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit readd migration, correctly this time 😭 oopsie missed this --- .../migration.sql | 4 + prisma/migrations/migration_lock.toml | 2 +- prisma/schema.prisma | 3 + server/routes/users/[id]/region.ts | 95 +++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 prisma/migrations/20250418232835_add_region_route/migration.sql create mode 100644 server/routes/users/[id]/region.ts diff --git a/prisma/migrations/20250418232835_add_region_route/migration.sql b/prisma/migrations/20250418232835_add_region_route/migration.sql new file mode 100644 index 0000000..c59151f --- /dev/null +++ b/prisma/migrations/20250418232835_add_region_route/migration.sql @@ -0,0 +1,4 @@ +-- AlterTable +ALTER TABLE "user_settings" ADD COLUMN "last_checked" TIMESTAMPTZ(0), +ADD COLUMN "region" VARCHAR(255), +ADD COLUMN "user_picked" BOOLEAN NOT NULL DEFAULT false; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml index 648c57f..044d57c 100644 --- a/prisma/migrations/migration_lock.toml +++ b/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually # It should be added in your version-control system (e.g., Git) -provider = "postgresql" \ No newline at end of file +provider = "postgresql" diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8b2e2d0..b9f543b 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -65,6 +65,9 @@ model user_settings { proxy_urls String[] trakt_key String? @db.VarChar(255) febbox_key String? @db.VarChar(255) + region String? @db.VarChar(255) + user_picked Boolean @default(false) + last_checked DateTime? @db.Timestamptz(0) } model users { diff --git a/server/routes/users/[id]/region.ts b/server/routes/users/[id]/region.ts new file mode 100644 index 0000000..d807000 --- /dev/null +++ b/server/routes/users/[id]/region.ts @@ -0,0 +1,95 @@ +import { useAuth, defineEventHandler, createError, readBody } from '#imports'; +import { z } from 'zod'; +import { scopedLogger } from '~/utils/logger'; +import { prisma } from '~/utils/prisma'; + +const log = scopedLogger('user-region'); + +const userRegionSchema = z.object({ + region: z.string(), + userPicked: z.boolean(), + lastChecked: z.number() +}); + +export default defineEventHandler(async (event) => { + const userId = event.context.params?.id; + + const session = await useAuth().getCurrentSession(); + + if (session.user !== userId) { + throw createError({ + statusCode: 403, + message: 'Permission denied' + }); + } + + if (event.method === 'GET') { + const settings = await prisma.user_settings.findUnique({ + where: { id: userId } + }); + + return { + region: settings?.region || 'unknown', + lastChecked: settings?.last_checked ? Math.floor(settings.last_checked.getTime() / 1000) : 0, + userPicked: settings?.user_picked || false + }; + } + + if (event.method === 'PUT') { + try { + const body = await readBody(event); + log.info('Updating user region', { userId, body }); + + const validatedBody = userRegionSchema.parse(body); + + const data = { + region: validatedBody.region, + user_picked: validatedBody.userPicked, + last_checked: new Date(validatedBody.lastChecked * 1000) + }; + + log.info('Preparing to upsert region settings', { userId, data }); + + const settings = await prisma.user_settings.upsert({ + where: { id: userId }, + update: data, + create: { + id: userId, + ...data + } + }); + + log.info('Region settings updated successfully', { userId }); + + return { + region: settings.region || 'unknown', + lastChecked: Math.floor(settings.last_checked.getTime() / 1000), + userPicked: settings.user_picked + }; + } catch (error) { + log.error('Failed to update region settings', { + userId, + error: error instanceof Error ? error.message : String(error) + }); + + if (error instanceof z.ZodError) { + throw createError({ + statusCode: 400, + message: 'Invalid region data', + cause: error.errors + }); + } + + throw createError({ + statusCode: 500, + message: 'Failed to update region settings', + cause: error instanceof Error ? error.message : 'Unknown error' + }); + } + } + + throw createError({ + statusCode: 405, + message: 'Method not allowed' + }); +}); \ No newline at end of file