- useMembers.ts gains a scope param ('organisation' | 'platform') on list,
invite, update-role, and remove; endpoints branch accordingly.
- Platform Admin's [id].vue now consumes useMembers via scope='platform';
deleted the duplicated useInviteOrganisationMember / useRemoveOrganisationMember
/ useUpdateOrganisationMemberRole helpers from useAdmin.ts.
- Deduplicated InviteMemberPayload / UpdateMemberRolePayload / AdminOrganisationMember
from types/admin.ts; Member is now the canonical type.
- SettingsMembers.vue and EditMemberRoleDialog.vue removed (no remaining imports).
- InviteMemberDialog accepts an optional scope prop and is restricted to the
two organisation-level roles matching the /members UX.
143 lines
4.3 KiB
TypeScript
143 lines
4.3 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
import type { Ref } from 'vue'
|
|
import { apiClient } from '@/lib/axios'
|
|
import type {
|
|
AcceptInvitationPayload,
|
|
AcceptInvitationResponse,
|
|
InvitationDetailResponse,
|
|
InviteMemberPayload,
|
|
Member,
|
|
MemberListResponse,
|
|
MemberScope,
|
|
UpdateMemberRolePayload,
|
|
} from '@/types/member'
|
|
|
|
interface ApiResponse<T> {
|
|
success: boolean
|
|
data: T
|
|
message?: string
|
|
}
|
|
|
|
function memberListKey(scope: MemberScope, orgId: Ref<string>) {
|
|
return ['members', scope, orgId] as const
|
|
}
|
|
|
|
function invalidateScopedKeys(scope: MemberScope, orgId: Ref<string>, queryClient: ReturnType<typeof useQueryClient>) {
|
|
queryClient.invalidateQueries({ queryKey: memberListKey(scope, orgId) })
|
|
if (scope === 'platform')
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations', orgId] })
|
|
}
|
|
|
|
function endpointList(scope: MemberScope, orgId: string) {
|
|
return scope === 'platform'
|
|
? `/admin/organisations/${orgId}`
|
|
: `/organisations/${orgId}/members`
|
|
}
|
|
|
|
function endpointInvite(scope: MemberScope, orgId: string) {
|
|
return scope === 'platform'
|
|
? `/admin/organisations/${orgId}/invite`
|
|
: `/organisations/${orgId}/invite`
|
|
}
|
|
|
|
function endpointMember(scope: MemberScope, orgId: string, userId: string) {
|
|
return scope === 'platform'
|
|
? `/admin/organisations/${orgId}/members/${userId}`
|
|
: `/organisations/${orgId}/members/${userId}`
|
|
}
|
|
|
|
export function useMembersList(scope: MemberScope, orgId: Ref<string>) {
|
|
return useQuery({
|
|
queryKey: memberListKey(scope, orgId),
|
|
queryFn: async () => {
|
|
const url = endpointList(scope, orgId.value)
|
|
if (scope === 'platform') {
|
|
const { data } = await apiClient.get<ApiResponse<{ members: Member[] }>>(url)
|
|
return {
|
|
data: data.data.members,
|
|
meta: {
|
|
total_members: data.data.members.length,
|
|
pending_invitations_count: 0,
|
|
pending_invitations: [],
|
|
},
|
|
} satisfies MemberListResponse
|
|
}
|
|
const { data } = await apiClient.get<MemberListResponse>(url)
|
|
return data
|
|
},
|
|
enabled: () => !!orgId.value,
|
|
})
|
|
}
|
|
|
|
export function useInviteMember(scope: MemberScope, orgId: Ref<string>) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async (payload: InviteMemberPayload) => {
|
|
const { data } = await apiClient.post<ApiResponse<Member>>(endpointInvite(scope, orgId.value), payload)
|
|
return data.data
|
|
},
|
|
onSuccess: () => invalidateScopedKeys(scope, orgId, queryClient),
|
|
})
|
|
}
|
|
|
|
export function useUpdateMemberRole(scope: MemberScope, orgId: Ref<string>) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ userId, role }: UpdateMemberRolePayload) => {
|
|
const { data } = await apiClient.put<ApiResponse<Member>>(
|
|
endpointMember(scope, orgId.value, userId),
|
|
{ role },
|
|
)
|
|
return data.data
|
|
},
|
|
onSuccess: () => invalidateScopedKeys(scope, orgId, queryClient),
|
|
})
|
|
}
|
|
|
|
export function useRemoveMember(scope: MemberScope, orgId: Ref<string>) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async (userId: string) => {
|
|
await apiClient.delete(endpointMember(scope, orgId.value, userId))
|
|
},
|
|
onSuccess: () => invalidateScopedKeys(scope, orgId, queryClient),
|
|
})
|
|
}
|
|
|
|
export function useRevokeInvitation(orgId: Ref<string>) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async (invitationId: string) => {
|
|
await apiClient.delete(`/organisations/${orgId.value}/invitations/${invitationId}`)
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: memberListKey('organisation', orgId) })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useInvitationDetail(token: Ref<string>) {
|
|
return useQuery({
|
|
queryKey: ['invitation', token],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<ApiResponse<InvitationDetailResponse>>(`/invitations/${token.value}`)
|
|
return data.data
|
|
},
|
|
enabled: () => !!token.value,
|
|
retry: false,
|
|
})
|
|
}
|
|
|
|
export function useAcceptInvitation() {
|
|
return useMutation({
|
|
mutationFn: async ({ token, ...payload }: AcceptInvitationPayload & { token: string }) => {
|
|
const { data } = await apiClient.post<AcceptInvitationResponse>(`/invitations/${token}/accept`, payload)
|
|
return data
|
|
},
|
|
})
|
|
}
|