parameterise admin functions and move to store actions
This commit is contained in:
@@ -71,11 +71,14 @@ export default class UserAccountService {
|
|||||||
return this.prisma.user.delete({ where: { id: user_id } });
|
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<MembershipWithAccount> {
|
||||||
return this.prisma.membership.create({
|
return this.prisma.membership.create({
|
||||||
data: {
|
data: {
|
||||||
user_id: user_id,
|
user_id: user_id,
|
||||||
account_id: account_id
|
account_id: account_id
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
account: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -144,6 +147,9 @@ export default class UserAccountService {
|
|||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
access: ACCOUNT_ACCESS.OWNER,
|
access: ACCOUNT_ACCESS.OWNER,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
account: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -176,6 +182,9 @@ export default class UserAccountService {
|
|||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
access: access,
|
access: access,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
account: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,29 +5,12 @@
|
|||||||
middleware: ['auth'],
|
middleware: ['auth'],
|
||||||
});
|
});
|
||||||
|
|
||||||
const { $client } = useNuxtApp();
|
|
||||||
|
|
||||||
const store = useAppStore();
|
const store = useAppStore();
|
||||||
const { notes } = storeToRefs(store); // ensure the notes list is reactive
|
const { notes } = storeToRefs(store); // ensure the notes list is reactive
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await store.initUser();
|
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)}`);
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
@@ -35,8 +18,8 @@
|
|||||||
<p v-for="note in notes">{{ note.note_text }}</p>
|
<p v-for="note in notes">{{ note.note_text }}</p>
|
||||||
|
|
||||||
<button @click.prevent="store.changeAccountPlan(2)">Change Account Plan to 2</button>
|
<button @click.prevent="store.changeAccountPlan(2)">Change Account Plan to 2</button>
|
||||||
<button @click.prevent="joinUserToAccount()">Join user to account</button>
|
<button @click.prevent="store.joinUserToAccount(5)">Join user to account 5</button>
|
||||||
<button @click.prevent="changeUserAccessWithinAccount()">Change user access within account</button>
|
<button @click.prevent="store.changeUserAccessWithinAccount(4, 5, 'ADMIN')">Change user 4 access within account 5 to ADMIN</button>
|
||||||
<button @click.prevent="claimOwnershipOfAccount()">Claim Account Ownership</button>
|
<button @click.prevent="store.claimOwnershipOfAccount(5)">Claim Account 5 Ownership for current user</button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -20,25 +20,28 @@ export const userAccountRouter = router({
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
joinUserToAccount: protectedProcedure
|
joinUserToAccount: protectedProcedure
|
||||||
.query(async ({ ctx }) => {
|
.input(z.object({ account_id: z.number() }))
|
||||||
|
.query(async ({ ctx, input }) => {
|
||||||
const uaService = new UserAccountService(ctx.prisma);
|
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 {
|
return {
|
||||||
membership,
|
membership,
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
changeUserAccessWithinAccount: protectedProcedure // TODO - should be protectedAdmin (i.e. ctx.dbUser.id should be admin within the session account)
|
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 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 {
|
return {
|
||||||
membership,
|
membership,
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
claimOwnershipOfAccount: protectedProcedure // TODO - should be protectedAdmin (i.e. ctx.dbUser.id should be admin within the session account)
|
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 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 {
|
return {
|
||||||
membership,
|
membership,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Membership, Note, User } from ".prisma/client"
|
import { ACCOUNT_ACCESS, Note } from ".prisma/client"
|
||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { FullDBUser, MembershipWithAccount } from "~~/lib/services/user.account.service"
|
import { FullDBUser, MembershipWithAccount } from "~~/lib/services/user.account.service"
|
||||||
|
|
||||||
@@ -49,6 +49,30 @@ export const useAppStore = defineStore('app', {
|
|||||||
if(account.value?.account){
|
if(account.value?.account){
|
||||||
this.activeMembership.account = 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user