diff --git a/README.md b/README.md index 664c617..ff3f429 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ npx prisma generate # TODO - add role to membership and have methods for changing role, making sure one owner etc (done) - remove @unique so users can have multiple accounts (done) -- add concept of 'current' account for user.. maybe put account on context or session. maybe just on DB...'current' boolean on membership? +- add concept of 'current' account for user.. maybe put account on context or session. maybe just on DB...'current' boolean on membership? (done but app state is messy) - add max_notes property to plan and account as an example of a 'limit' property (done) - add spinup script somehow to create plans???.... should I use some sort of generator like sidebase? - team invitation thingy diff --git a/components/AppHeader.vue b/components/AppHeader.vue index 97ef0b9..4e30555 100644 --- a/components/AppHeader.vue +++ b/components/AppHeader.vue @@ -1,6 +1,16 @@ diff --git a/server/trpc/context.ts b/server/trpc/context.ts index 5d59eed..c35e72f 100644 --- a/server/trpc/context.ts +++ b/server/trpc/context.ts @@ -1,5 +1,5 @@ -import { PrismaClient } from '@prisma/client'; -import type { inferAsyncReturnType } from '@trpc/server' +import { Membership, PrismaClient, User as DBUser } from '@prisma/client'; +import { inferAsyncReturnType, TRPCError } from '@trpc/server' import { H3Event } from 'h3'; import { serverSupabaseClient } from '#supabase/server'; import SupabaseClient from '@supabase/supabase-js/dist/module/SupabaseClient'; @@ -8,8 +8,8 @@ import UserAccountService from '~~/lib/services/user.account.service'; let prisma: PrismaClient | undefined let supabase: SupabaseClient | undefined -let user: User | null = null; -let dbUser: any | undefined +let user: User | null; +let dbUser: (DBUser & { memberships: Membership[]; }) | null export async function createContext(event: H3Event){ if (!supabase) { @@ -31,6 +31,13 @@ export async function createContext(event: H3Event){ } } + if(!supabase || !user || !prisma || !dbUser) { + throw new TRPCError({ + code: 'INTERNAL_SERVER_ERROR', + message: 'Unable to fetch user data, please try again later.', + }); + } + // TODO - This seems excessive, trim context when I have figured out what I actually need return { supabase, diff --git a/server/trpc/routers/notes.router.ts b/server/trpc/routers/notes.router.ts index 6e62162..d16fe73 100644 --- a/server/trpc/routers/notes.router.ts +++ b/server/trpc/routers/notes.router.ts @@ -1,12 +1,13 @@ import NotesService from '~~/lib/services/notes.service'; -import { protectedProcedure, router } from '../trpc' +import { protectedProcedure, router } from '../trpc'; +import { z } from 'zod'; export const notesRouter = router({ getForCurrentUser: protectedProcedure - .query(async ({ ctx }) => { + .input(z.object({ account_id: z.number() })) + .query(async ({ ctx, input }) => { 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); + const notes = await notesService.getNotesForAccountId(input.account_id); return { notes, } diff --git a/server/trpc/routers/user.account.router.ts b/server/trpc/routers/user.account.router.ts index f2c3037..188f854 100644 --- a/server/trpc/routers/user.account.router.ts +++ b/server/trpc/routers/user.account.router.ts @@ -3,10 +3,17 @@ import { protectedProcedure, router } from '../trpc' import { ACCOUNT_ACCESS } from '@prisma/client'; export const userAccountRouter = router({ + getDBUser: protectedProcedure + .query(({ ctx }) => { + return { + dbUser: ctx.dbUser, + } + }), 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 + // TODO - account id and plan should be an input param and then the ternary removed + const account = (ctx.dbUser?.memberships[0].account_id)?await uaService.changeAccountPlan(ctx.dbUser?.memberships[0].account_id, 2):null; return { account, } @@ -14,7 +21,7 @@ export const userAccountRouter = router({ 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 + const membership = (ctx.dbUser?.id)?await uaService.joinUserToAccount(ctx.dbUser?.id, 5):null; // todo - account should be an input param and remove this shit return { membership, }