mirror of
https://github.com/p-stream/backend.git
synced 2026-01-11 20:10:33 +00:00
parent
e54a9280ee
commit
ad1ded7abb
4 changed files with 1 additions and 103 deletions
|
|
@ -1,4 +0,0 @@
|
|||
-- 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;
|
||||
|
|
@ -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"
|
||||
provider = "postgresql"
|
||||
|
|
@ -65,9 +65,6 @@ 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 {
|
||||
|
|
|
|||
|
|
@ -1,95 +0,0 @@
|
|||
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'
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue