membership functions - split and compose router functions - schema changes

This commit is contained in:
Michael Dausmann
2023-02-08 23:14:46 +11:00
parent 087526eb50
commit de976ab43b
10 changed files with 282 additions and 71 deletions

View File

@@ -3,26 +3,15 @@
* On a bigger app, you will probably want to split this file up into multiple files.
*/
import { createNuxtApiHandler } from 'trpc-nuxt'
import { z } from 'zod'
import { publicProcedure, router } from '~/server/trpc/trpc'
import { createContext } from '~~/server/trpc/context';
import NotesService from '~~/lib/services/notes.service';
import { notesRouter } from '~~/server/trpc/routers/notes.router';
import { userAccountRouter } from '~~/server/trpc/routers/user.account.router';
export const appRouter = router({
notes: publicProcedure
.input(
z.object({
text: z.string().nullish(),
}),
)
.query(async ({ ctx, input }) => {
const notesService = new NotesService(ctx.prisma);
const notes = await notesService.getNotesForAccountId(ctx.dbUser.membership?.account_id);
return {
notes,
}
}),
notes: notesRouter,
userAccount: userAccountRouter,
})
// export only the type definition of the API

View File

@@ -4,7 +4,7 @@ import { H3Event } from 'h3';
import { serverSupabaseClient } from '#supabase/server';
import SupabaseClient from '@supabase/supabase-js/dist/module/SupabaseClient';
import { User } from '@supabase/supabase-js';
import UserService from '~~/lib/services/user.service';
import UserAccountService from '~~/lib/services/user.account.service';
let prisma: PrismaClient | undefined
let supabase: SupabaseClient | undefined
@@ -22,7 +22,7 @@ export async function createContext(event: H3Event){
prisma = new PrismaClient()
}
if (!dbUser && user) {
const userService = new UserService(prisma);
const userService = new UserAccountService(prisma);
dbUser = await userService.getUserBySupabaseId(user.id);
if (!dbUser && user) {

View File

@@ -0,0 +1,14 @@
import NotesService from '~~/lib/services/notes.service';
import { protectedProcedure, router } from '../trpc'
export const notesRouter = router({
getForCurrentUser: protectedProcedure
.query(async ({ ctx }) => {
const notesService = new NotesService(ctx.prisma);
console.log(`fetching notes for account_id: ${ctx.dbUser.memberships[0].account_id}`);
const notes = await notesService.getNotesForAccountId(ctx.dbUser.memberships[0].account_id);
return {
notes,
}
}),
})

View File

@@ -0,0 +1,38 @@
import UserAccountService from '~~/lib/services/user.account.service';
import { protectedProcedure, router } from '../trpc'
import { ACCOUNT_ACCESS } from '@prisma/client';
export const userAccountRouter = router({
changeAccountPlan: protectedProcedure
.query(async ({ ctx }) => {
const uaService = new UserAccountService(ctx.prisma);
const account = await uaService.changeAccountPlan(ctx.dbUser.memberships[0].account_id, 2); // todo - plan should be an in put param
return {
account,
}
}),
joinUserToAccount: protectedProcedure
.query(async ({ ctx }) => {
const uaService = new UserAccountService(ctx.prisma);
const membership = await uaService.joinUserToAccount(ctx.dbUser.id, 5); // todo - account should be an input param
return {
membership,
}
}),
changeUserAccessWithinAccount: protectedProcedure // TODO - should be protectedAdmin (i.e. ctx.dbUser.id should be admin within the session account)
.query(async ({ ctx }) => {
const uaService = new UserAccountService(ctx.prisma);
const membership = await uaService.changeUserAccessWithinAccount(3, 5, ACCOUNT_ACCESS.ADMIN); // todo - member and access should be an input param (from UI) account should be the session account
return {
membership,
}
}),
claimOwnershipOfAccount: protectedProcedure // TODO - should be protectedAdmin (i.e. ctx.dbUser.id should be admin within the session account)
.query(async ({ ctx }) => {
const uaService = new UserAccountService(ctx.prisma);
const membership = await uaService.claimOwnershipOfAccount(3, 5); // todo - member should be an input param (from UI) account should be the session account
return {
membership,
}
}),
})

View File

@@ -7,15 +7,29 @@
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/v10/procedures
*/
import { initTRPC } from '@trpc/server'
import { initTRPC, TRPCError } from '@trpc/server'
import { Context } from './context';
const t = initTRPC.context<Context>().create()
/**
* auth middleware
**/
const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.user) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return next({
ctx: {
user: ctx.user,
},
});
});
/**
* Unprotected procedure
**/
export const publicProcedure = t.procedure;
export const protectedProcedure = t.procedure.use(isAuthed);
export const router = t.router;
export const middleware = t.middleware;