mutate context dbUser along with db changes

This commit is contained in:
Michael Dausmann
2023-02-25 18:34:56 +11:00
parent a341a641e8
commit 61bab50aef
3 changed files with 19 additions and 4 deletions

View File

@@ -21,6 +21,6 @@
<button @click.prevent="store.joinUserToAccount(4)">Join user 4 to active account</button> <button @click.prevent="store.joinUserToAccount(4)">Join user 4 to active account</button>
<button @click.prevent="store.changeUserAccessWithinAccount(4, 'OWNER')">Change user 4 access within account 5 to OWNER (SHOULD FAIL)</button> <button @click.prevent="store.changeUserAccessWithinAccount(4, 'OWNER')">Change user 4 access within account 5 to OWNER (SHOULD FAIL)</button>
<button @click.prevent="store.changeUserAccessWithinAccount(4, 'ADMIN')">Change user 4 access within account 5 to ADMIN</button> <button @click.prevent="store.changeUserAccessWithinAccount(4, 'ADMIN')">Change user 4 access within account 5 to ADMIN</button>
<button @click.prevent="store.claimOwnershipOfAccount()">Claim Account 5 Ownership for current user</button> <button @click.prevent="store.claimOwnershipOfAccount()">Claim Ownership of current account for current user</button>
</div> </div>
</template> </template>

View File

@@ -15,6 +15,11 @@ export const userAccountRouter = router({
.query(async ({ ctx, input }) => { .query(async ({ ctx, input }) => {
const uaService = new UserAccountService(ctx.prisma); const uaService = new UserAccountService(ctx.prisma);
const account = await uaService.changeAccountPlan(input.account_id, input.plan_id); 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 { return {
account, account,
} }
@@ -33,6 +38,11 @@ export const userAccountRouter = router({
.query(async ({ ctx, input }) => { .query(async ({ ctx, input }) => {
const uaService = new UserAccountService(ctx.prisma); const uaService = new UserAccountService(ctx.prisma);
const membership = await uaService.changeUserAccessWithinAccount(input.user_id, input.account_id, input.access); 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 { return {
membership, membership,
} }
@@ -41,7 +51,12 @@ export const userAccountRouter = router({
.input(z.object({ account_id: z.number() })) .input(z.object({ account_id: z.number() }))
.query(async ({ ctx, input }) => { .query(async ({ ctx, input }) => {
const uaService = new UserAccountService(ctx.prisma); 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 { return {
membership, membership,
} }

View File

@@ -15,7 +15,7 @@ import { ACCOUNT_ACCESS } from '@prisma/client';
const t = initTRPC.context<Context>().create() const t = initTRPC.context<Context>().create()
/** /**
* auth middleware * auth middlewares
**/ **/
const isAuthed = t.middleware(({ next, ctx }) => { const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.user) { if (!ctx.user) {
@@ -45,7 +45,7 @@ const isAdminForInputAccountId = t.middleware(({ next, rawInput, ctx }) => {
}); });
/** /**
* Unprotected procedure * Procedures
**/ **/
export const publicProcedure = t.procedure; export const publicProcedure = t.procedure;
export const protectedProcedure = t.procedure.use(isAuthed); export const protectedProcedure = t.procedure.use(isAuthed);