From 61bab50aef009f97ff914ad00e56752985e6902b Mon Sep 17 00:00:00 2001 From: Michael Dausmann Date: Sat, 25 Feb 2023 18:34:56 +1100 Subject: [PATCH] mutate context dbUser along with db changes --- pages/dashboard.vue | 2 +- server/trpc/routers/user.account.router.ts | 17 ++++++++++++++++- server/trpc/trpc.ts | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pages/dashboard.vue b/pages/dashboard.vue index 4079731..714c172 100644 --- a/pages/dashboard.vue +++ b/pages/dashboard.vue @@ -21,6 +21,6 @@ - + diff --git a/server/trpc/routers/user.account.router.ts b/server/trpc/routers/user.account.router.ts index 835a90e..eb14e83 100644 --- a/server/trpc/routers/user.account.router.ts +++ b/server/trpc/routers/user.account.router.ts @@ -15,6 +15,11 @@ export const userAccountRouter = router({ .query(async ({ ctx, input }) => { const uaService = new UserAccountService(ctx.prisma); const account = await uaService.changeAccountPlan(input.account_id, input.plan_id); + + if(account){ + ctx.dbUser.memberships = ctx.dbUser.memberships.map(m => m.account_id !== account.id ? m : { ...m, account }); + } + return { account, } @@ -33,6 +38,11 @@ export const userAccountRouter = router({ .query(async ({ ctx, input }) => { const uaService = new UserAccountService(ctx.prisma); const membership = await uaService.changeUserAccessWithinAccount(input.user_id, input.account_id, input.access); + + if(membership && ctx.dbUser?.id == input.user_id){ + ctx.dbUser.memberships = ctx.dbUser.memberships.map(m => m.id !== membership.id ? m : membership); + } + return { membership, } @@ -41,7 +51,12 @@ export const userAccountRouter = router({ .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); + const membership = await uaService.claimOwnershipOfAccount(ctx.dbUser.id, input.account_id); + + if(membership && ctx.dbUser){ + ctx.dbUser.memberships = ctx.dbUser.memberships.map(m => m.id !== membership.id ? m : membership); + } + return { membership, } diff --git a/server/trpc/trpc.ts b/server/trpc/trpc.ts index 6457352..963939c 100644 --- a/server/trpc/trpc.ts +++ b/server/trpc/trpc.ts @@ -15,7 +15,7 @@ import { ACCOUNT_ACCESS } from '@prisma/client'; const t = initTRPC.context().create() /** - * auth middleware + * auth middlewares **/ const isAuthed = t.middleware(({ next, ctx }) => { if (!ctx.user) { @@ -45,7 +45,7 @@ const isAdminForInputAccountId = t.middleware(({ next, rawInput, ctx }) => { }); /** - * Unprotected procedure + * Procedures **/ export const publicProcedure = t.procedure; export const protectedProcedure = t.procedure.use(isAuthed);