49 lines
2.1 KiB
TypeScript
49 lines
2.1 KiB
TypeScript
import UserAccountService from '~~/lib/services/user.account.service';
|
|
import { protectedProcedure, router } from '../trpc'
|
|
import { ACCOUNT_ACCESS } from '@prisma/client';
|
|
import { z } from 'zod';
|
|
|
|
export const userAccountRouter = router({
|
|
getDBUser: protectedProcedure
|
|
.query(({ ctx }) => {
|
|
return {
|
|
dbUser: ctx.dbUser,
|
|
}
|
|
}),
|
|
changeAccountPlan: protectedProcedure
|
|
.input(z.object({ account_id: z.number(), plan_id: z.number() }))
|
|
.query(async ({ ctx, input }) => {
|
|
const uaService = new UserAccountService(ctx.prisma);
|
|
const account = await uaService.changeAccountPlan(input.account_id, input.plan_id);
|
|
return {
|
|
account,
|
|
}
|
|
}),
|
|
joinUserToAccount: protectedProcedure
|
|
.input(z.object({ account_id: z.number() }))
|
|
.query(async ({ ctx, input }) => {
|
|
const uaService = new UserAccountService(ctx.prisma);
|
|
const membership = (ctx.dbUser?.id)?await uaService.joinUserToAccount(ctx.dbUser?.id, input.account_id):null;
|
|
return {
|
|
membership,
|
|
}
|
|
}),
|
|
changeUserAccessWithinAccount: protectedProcedure // TODO - should be protectedAdmin (i.e. ctx.dbUser.id should be admin within the session account)
|
|
.input(z.object({ user_id: z.number(), account_id: z.number(), access: z.enum([ACCOUNT_ACCESS.ADMIN, ACCOUNT_ACCESS.OWNER, ACCOUNT_ACCESS.READ_ONLY, ACCOUNT_ACCESS.READ_WRITE]) }))
|
|
.query(async ({ ctx, input }) => {
|
|
const uaService = new UserAccountService(ctx.prisma);
|
|
const membership = await uaService.changeUserAccessWithinAccount(input.user_id, input.account_id, input.access);
|
|
return {
|
|
membership,
|
|
}
|
|
}),
|
|
claimOwnershipOfAccount: protectedProcedure // TODO - should be protectedAdmin (i.e. ctx.dbUser.id should be admin within the session account)
|
|
.input(z.object({ account_id: z.number() }))
|
|
.query(async ({ ctx, input }) => {
|
|
const uaService = new UserAccountService(ctx.prisma);
|
|
const membership = await uaService.claimOwnershipOfAccount(ctx.dbUser?.id, input.account_id);
|
|
return {
|
|
membership,
|
|
}
|
|
}),
|
|
}) |