WS-3 session 1b-i Tier 2. Scope: composables, lib, stores, plugins, types, utils, navigation, main.ts. Mechanical fixes only — predominantly newline-before-return, arrow-parens, antfu/if-newline, padding-line-between-statements, plus one unicorn/prefer-includes (.some(p => x === p) → .includes(x)) in router guards. Excludes (per session prompt): - apps/app/vite.config.ts (Tier 3) - apps/app/themeConfig.ts (Tier 3) - apps/app/vitest.config.ts (Tier 3) - All .vue files (already in Tier 1) Hand-reviewed diffs for the three auth/router-critical files before committing: - src/lib/axios.ts: reviewed clean. Pure mechanical (quote-props on Accept header, curly-strip on single-statement ifs, one blank line before impersonationStore.clearState()). No type-import changes, no logic touched. - src/stores/useAuthStore.ts: reviewed clean. curly-strip + padding before returns. The initialize()/doInitialize() race-condition guard on isInitialized is preserved verbatim. - src/plugins/1.router/guards.ts: reviewed clean. if-newline reformat + one .some() → .includes() rewrite that's behaviorally identical for primitive equality on the guestOnlyPaths string array. Tests + typecheck verified green post-fix: - apps/app vitest: 49 passed (unchanged) - apps/app vue-tsc: clean (unchanged) Lint baseline progression: - Pre-Tier-2: 422 problems (post-Tier-1) - Post-Tier-2: 246 problems Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
205 lines
5.5 KiB
TypeScript
205 lines
5.5 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
import type { Ref } from 'vue'
|
|
import { apiClient } from '@/lib/axios'
|
|
import type {
|
|
ActivityLogEntry,
|
|
AdminOrganisation,
|
|
AdminOrganisationDetail,
|
|
AdminUser,
|
|
CreateOrganisationPayload,
|
|
PlatformStats,
|
|
UpdateAdminOrganisationPayload,
|
|
UpdateAdminUserPayload,
|
|
} from '@/types/admin'
|
|
|
|
interface ApiResponse<T> {
|
|
success: boolean
|
|
data: T
|
|
message?: string
|
|
}
|
|
|
|
interface PaginatedResponse<T> {
|
|
data: T[]
|
|
links: Record<string, string | null>
|
|
meta: {
|
|
current_page: number
|
|
per_page: number
|
|
total: number
|
|
last_page: number
|
|
}
|
|
}
|
|
|
|
// ─── Organisations ──────────────────────────────────────────
|
|
|
|
export function useAdminOrganisations(params: Ref<Record<string, string | number | undefined>>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'organisations', params],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<PaginatedResponse<AdminOrganisation>>(
|
|
'/admin/organisations',
|
|
{ params: params.value },
|
|
)
|
|
|
|
return data
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useAdminOrganisation(id: Ref<string>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'organisations', id],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<ApiResponse<AdminOrganisationDetail>>(
|
|
`/admin/organisations/${id.value}`,
|
|
)
|
|
|
|
return data.data
|
|
},
|
|
enabled: () => !!id.value,
|
|
})
|
|
}
|
|
|
|
export function useUpdateAdminOrganisation() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ id, payload }: { id: string; payload: UpdateAdminOrganisationPayload }) => {
|
|
const { data } = await apiClient.put<ApiResponse<AdminOrganisation>>(
|
|
`/admin/organisations/${id}`,
|
|
payload,
|
|
)
|
|
|
|
return data.data
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useCreateOrganisation() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async (payload: CreateOrganisationPayload) => {
|
|
const { data } = await apiClient.post<ApiResponse<AdminOrganisation>>(
|
|
'/organisations',
|
|
payload,
|
|
)
|
|
|
|
return data.data
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useDeleteAdminOrganisation() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async (id: string) => {
|
|
await apiClient.delete(`/admin/organisations/${id}`)
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// ─── Users ──────────────────────────────────────────────────
|
|
|
|
export function useAdminUsers(params: Ref<Record<string, string | number | undefined>>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'users', params],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<PaginatedResponse<AdminUser>>(
|
|
'/admin/users',
|
|
{ params: params.value },
|
|
)
|
|
|
|
return data
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useAdminUser(id: Ref<string>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'users', id],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<ApiResponse<AdminUser>>(
|
|
`/admin/users/${id.value}`,
|
|
)
|
|
|
|
return data.data
|
|
},
|
|
enabled: () => !!id.value,
|
|
})
|
|
}
|
|
|
|
export function useUpdateAdminUser() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ id, payload }: { id: string; payload: UpdateAdminUserPayload }) => {
|
|
const { data } = await apiClient.put<ApiResponse<AdminUser>>(
|
|
`/admin/users/${id}`,
|
|
payload,
|
|
)
|
|
|
|
return data.data
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'users'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useDeleteAdminUser() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async (id: string) => {
|
|
await apiClient.delete(`/admin/users/${id}`)
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'users'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// ─── Stats ──────────────────────────────────────────────────
|
|
|
|
export function usePlatformStats() {
|
|
return useQuery({
|
|
queryKey: ['admin', 'stats'],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<{ data: PlatformStats }>(
|
|
'/admin/stats',
|
|
)
|
|
|
|
return data.data
|
|
},
|
|
})
|
|
}
|
|
|
|
// ─── Activity Log ───────────────────────────────────────────
|
|
|
|
export function useAdminActivityLog(params: Ref<Record<string, string | number | undefined>>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'activity-log', params],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<PaginatedResponse<ActivityLogEntry>>(
|
|
'/admin/activity-log',
|
|
{ params: params.value },
|
|
)
|
|
|
|
return data
|
|
},
|
|
})
|
|
}
|
|
|
|
// ─── Impersonation ──────────────────────────────────────────
|
|
// Impersonation API calls are now handled directly by useImpersonationStore.
|