import { type FastifyInstance } from 'fastify' import { isEditorAuthorized } from '../utils/authorization' import { OnBehalfQuery } from '../types' import { PrismaClient, type User } from '../../generated/prisma' import { withAccelerate } from "@prisma/extension-accelerate" const prisma = new PrismaClient().$extends(withAccelerate()) export default async function usersRoutes( fastify: FastifyInstance, ): Promise { fastify.get('/user', async function (request, reply) { // // const twitchAccessToken = request.session.get('twitchAccessToken') // const api = new Api({ // baseURL: NOCO_ORIGIN, // headers: { // "xc-token": NOCO_API_TOKEN // } // }); // const waifus = ctx.body; // ctx.log.info(ctx, 'waifus as follows') // ctx.log.info(ctx, JSON.stringify(waifus)) // const results = await api.dbViewRow.create("noco", NOCO_BASE, NOCO_WAIFUS_TABLE, NOCO_DEFAULT_VIEW, waifus) // .catch(err => { // console.error("Failed to create waifus:", JSON.stringify(waifus), err); // return null; // }) const user = request.session.get('user') console.log(user) reply.send(user) }) fastify.put('/settings', async function (request, reply) { const { onBehalfOf } = request.query as OnBehalfQuery; const userId = request.session.get('user_id'); // Get the authenticated user const user = await prisma.user.findFirstOrThrow({ where: { id: userId } }); console.log('onbehalfof=' + onBehalfOf) // Determine which user is being updated const targetUser = onBehalfOf ? await prisma.user.findFirstOrThrow({ where: { twitchName: onBehalfOf } }) : user; const raw = request.body as Record; const waifuChoicePoolSize = raw.waifuChoicePoolSize ? Number(raw.waifuChoicePoolSize) : undefined; const maxOnScreenWaifus = raw.maxOnScreenWaifus ? Number(raw.maxOnScreenWaifus) : undefined; const redeemCost = raw.redeemCost ? Number(raw.redeemCost) : undefined; const data: Record = { waifuChoicePoolSize, maxOnScreenWaifus, twitchName: targetUser.twitchName, redeemCost, }; // Only the actual user can modify editor-related fields if (!onBehalfOf) { data.editorTwitchNames = Array.isArray(raw.editorTwitchNames) ? raw.editorTwitchNames : [raw.editorTwitchNames].filter(Boolean); data.modsAreEditors = Boolean(raw.modsAreEditors); } console.log('>>> data s follows') console.log(data) console.log('target user is as follows') console.log(targetUser) await prisma.user.update({ where: { id: targetUser.id }, data }); await fastify.graphileWorker.addJob('consolidate_twitch_channel_rewards', { userId }) reply.send(data); }); fastify.get('/settings', async function (request, reply) { const { onBehalfOf } = request.query as { onBehalfOf?: string; }; const userId = request.session.get('user_id'); const requester = await prisma.user.findFirstOrThrow({ where: { id: userId } }); let targetUser = requester; if (onBehalfOf) { if (!requester.twitchName) { return reply.status(500).send({ error: true, message: 'Requesting editor does not have a twitchName defined. Please log out and in, then contact admin if error persists.' }); } const isSelfRequest = onBehalfOf === requester.twitchName; if (!isSelfRequest) { const authorized = await isEditorAuthorized(requester.twitchName, onBehalfOf); if (!authorized) { return reply.status(401).send({ error: true, message: 'Requesting editor is not authorized to edit settings for this channel.' }); } targetUser = await prisma.user.findFirstOrThrow({ where: { twitchName: onBehalfOf } }); } } const { twitchChannels, waifuChoicePoolSize, maxOnScreenWaifus, editorTwitchNames, modsAreEditors, twitchName, redeemCost, } = targetUser; reply.send({ waifuChoicePoolSize, maxOnScreenWaifus, editorTwitchNames, modsAreEditors, twitchChannels, twitchName, redeemCost }); }); }