diff --git a/lib/services/user.account.service.ts b/lib/services/user.account.service.ts index 7cbec83..5d70778 100644 --- a/lib/services/user.account.service.ts +++ b/lib/services/user.account.service.ts @@ -63,7 +63,7 @@ export default class UserAccountService { }, include: { memberships: {include: { account: true - }}} + }}} }); } @@ -71,11 +71,14 @@ export default class UserAccountService { return this.prisma.user.delete({ where: { id: user_id } }); } - async joinUserToAccount(user_id: number, account_id: number) { + async joinUserToAccount(user_id: number, account_id: number): Promise { return this.prisma.membership.create({ data: { user_id: user_id, account_id: account_id + }, + include: { + account: true } }); } @@ -144,6 +147,9 @@ export default class UserAccountService { }, data: { access: ACCOUNT_ACCESS.OWNER, + }, + include: { + account: true } }); } @@ -176,6 +182,9 @@ export default class UserAccountService { }, data: { access: access, + }, + include: { + account: true } }); } diff --git a/pages/dashboard.vue b/pages/dashboard.vue index 9bddcc6..5d95a11 100644 --- a/pages/dashboard.vue +++ b/pages/dashboard.vue @@ -5,29 +5,12 @@ middleware: ['auth'], }); - const { $client } = useNuxtApp(); - const store = useAppStore(); const { notes } = storeToRefs(store); // ensure the notes list is reactive onMounted(async () => { await store.initUser(); }) - - async function joinUserToAccount(){ - const { data: membership } = await $client.userAccount.joinUserToAccount.useQuery(); - console.log(`added membership on current account: ${JSON.stringify(membership)}`); - } - - async function changeUserAccessWithinAccount(){ - const { data: membership } = await $client.userAccount.changeUserAccessWithinAccount.useQuery(); - console.log(`updated membership on current account: ${JSON.stringify(membership)}`); - } - - async function claimOwnershipOfAccount(){ - const { data: membership } = await $client.userAccount.claimOwnershipOfAccount.useQuery(); - console.log(`updated membership on current account: ${JSON.stringify(membership)}`); - } diff --git a/server/trpc/routers/user.account.router.ts b/server/trpc/routers/user.account.router.ts index ba4d991..0952131 100644 --- a/server/trpc/routers/user.account.router.ts +++ b/server/trpc/routers/user.account.router.ts @@ -20,25 +20,28 @@ export const userAccountRouter = router({ } }), joinUserToAccount: protectedProcedure - .query(async ({ ctx }) => { + .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, 5):null; // todo - account should be an input param and remove this shit + 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) - .query(async ({ ctx }) => { + .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(3, 5, ACCOUNT_ACCESS.ADMIN); // todo - member and access should be an input param (from UI) account should be the session account + 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) - .query(async ({ ctx }) => { + .input(z.object({ account_id: z.number() })) + .query(async ({ ctx, input }) => { 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 + const membership = await uaService.claimOwnershipOfAccount(ctx.dbUser?.id, input.account_id); return { membership, } diff --git a/stores/app.store.ts b/stores/app.store.ts index e18b0c8..9ff96c3 100644 --- a/stores/app.store.ts +++ b/stores/app.store.ts @@ -1,4 +1,4 @@ -import { Membership, Note, User } from ".prisma/client" +import { ACCOUNT_ACCESS, Note } from ".prisma/client" import { defineStore } from "pinia" import { FullDBUser, MembershipWithAccount } from "~~/lib/services/user.account.service" @@ -49,6 +49,30 @@ export const useAppStore = defineStore('app', { if(account.value?.account){ this.activeMembership.account = account.value.account; } + }, + async joinUserToAccount(account_id: number){ + if(!this.activeMembership) { return; } + const { $client } = useNuxtApp(); + const { data: membership } = await $client.userAccount.joinUserToAccount.useQuery({account_id}); + if(membership.value?.membership){ + this.activeMembership = membership.value.membership; + } + }, + async changeUserAccessWithinAccount(user_id: number,account_id: number, access: ACCOUNT_ACCESS){ + if(!this.activeMembership) { return; } + const { $client } = useNuxtApp(); + const { data: membership } = await $client.userAccount.changeUserAccessWithinAccount.useQuery({user_id, account_id, access}); + if(membership.value?.membership){ + this.activeMembership = membership.value.membership; + } + }, + async claimOwnershipOfAccount(account_id: number){ + if(!this.activeMembership) { return; } + const { $client } = useNuxtApp(); + const { data: membership } = await $client.userAccount.claimOwnershipOfAccount.useQuery({account_id}); + if(membership.value?.membership){ + this.activeMembership = membership.value.membership; + } } } });