refactor(members): consolidate Platform Admin + Org members into shared useMembers
- 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.
This commit is contained in:
@@ -5,14 +5,11 @@ import type {
|
||||
ActivityLogEntry,
|
||||
AdminOrganisation,
|
||||
AdminOrganisationDetail,
|
||||
AdminOrganisationMember,
|
||||
AdminUser,
|
||||
CreateOrganisationPayload,
|
||||
InviteMemberPayload,
|
||||
PlatformStats,
|
||||
UpdateAdminOrganisationPayload,
|
||||
UpdateAdminUserPayload,
|
||||
UpdateMemberRolePayload,
|
||||
} from '@/types/admin'
|
||||
|
||||
interface ApiResponse<T> {
|
||||
@@ -107,55 +104,6 @@ export function useDeleteAdminOrganisation() {
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Organisation Members ──────────────────────────────────
|
||||
|
||||
export function useInviteOrganisationMember() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ organisationId, payload }: { organisationId: string; payload: InviteMemberPayload }) => {
|
||||
const { data } = await apiClient.post<ApiResponse<AdminOrganisationMember | null>>(
|
||||
`/admin/organisations/${organisationId}/invite`,
|
||||
payload,
|
||||
)
|
||||
return data
|
||||
},
|
||||
onSuccess: (_data, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations', variables.organisationId] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useRemoveOrganisationMember() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ organisationId, userId }: { organisationId: string; userId: string }) => {
|
||||
await apiClient.delete(`/admin/organisations/${organisationId}/members/${userId}`)
|
||||
},
|
||||
onSuccess: (_data, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations', variables.organisationId] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateOrganisationMemberRole() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ organisationId, userId, payload }: { organisationId: string; userId: string; payload: UpdateMemberRolePayload }) => {
|
||||
const { data } = await apiClient.put<ApiResponse<AdminOrganisationMember>>(
|
||||
`/admin/organisations/${organisationId}/members/${userId}`,
|
||||
payload,
|
||||
)
|
||||
return data.data
|
||||
},
|
||||
onSuccess: (_data, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations', variables.organisationId] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Users ──────────────────────────────────────────────────
|
||||
|
||||
export function useAdminUsers(params: Ref<Record<string, string | number | undefined>>) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
InviteMemberPayload,
|
||||
Member,
|
||||
MemberListResponse,
|
||||
MemberScope,
|
||||
UpdateMemberRolePayload,
|
||||
} from '@/types/member'
|
||||
|
||||
@@ -17,55 +18,92 @@ interface ApiResponse<T> {
|
||||
message?: string
|
||||
}
|
||||
|
||||
export function useMemberList(orgId: Ref<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: ['members', orgId],
|
||||
queryKey: memberListKey(scope, orgId),
|
||||
queryFn: async () => {
|
||||
const { data } = await apiClient.get<MemberListResponse>(`/organisations/${orgId.value}/members`)
|
||||
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(orgId: Ref<string>) {
|
||||
export function useInviteMember(scope: MemberScope, orgId: Ref<string>) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (payload: InviteMemberPayload) => {
|
||||
const { data } = await apiClient.post<ApiResponse<Member>>(`/organisations/${orgId.value}/invite`, payload)
|
||||
const { data } = await apiClient.post<ApiResponse<Member>>(endpointInvite(scope, orgId.value), payload)
|
||||
return data.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['members', orgId] })
|
||||
},
|
||||
onSuccess: () => invalidateScopedKeys(scope, orgId, queryClient),
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateMemberRole(orgId: Ref<string>) {
|
||||
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>>(`/organisations/${orgId.value}/members/${userId}`, { role })
|
||||
const { data } = await apiClient.put<ApiResponse<Member>>(
|
||||
endpointMember(scope, orgId.value, userId),
|
||||
{ role },
|
||||
)
|
||||
return data.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['members', orgId] })
|
||||
},
|
||||
onSuccess: () => invalidateScopedKeys(scope, orgId, queryClient),
|
||||
})
|
||||
}
|
||||
|
||||
export function useRemoveMember(orgId: Ref<string>) {
|
||||
export function useRemoveMember(scope: MemberScope, orgId: Ref<string>) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (userId: string) => {
|
||||
await apiClient.delete(`/organisations/${orgId.value}/members/${userId}`)
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['members', orgId] })
|
||||
await apiClient.delete(endpointMember(scope, orgId.value, userId))
|
||||
},
|
||||
onSuccess: () => invalidateScopedKeys(scope, orgId, queryClient),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -77,7 +115,7 @@ export function useRevokeInvitation(orgId: Ref<string>) {
|
||||
await apiClient.delete(`/organisations/${orgId.value}/invitations/${invitationId}`)
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['members', orgId] })
|
||||
queryClient.invalidateQueries({ queryKey: memberListKey('organisation', orgId) })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user