import type { AdminCustomer, AdminCustomerDetail, OffsetPaginated, PermissionGroup, RoleDetail, UserSummary, } from '@sport/types'; import type { HttpClient } from '../http-client'; export interface UserListParams { page?: number; perPage?: number; q?: string; type?: string; status?: string; } export interface CreateUserPayload { email: string; password: string; firstName: string; lastName: string; phone?: string; type: 'STAFF' | 'ADMIN' | 'SUPER_ADMIN'; roleIds: string[]; } export interface UpdateUserPayload { firstName?: string; lastName?: string; phone?: string | null; status?: 'ACTIVE' | 'INVITED' | 'SUSPENDED'; type?: 'STAFF' | 'ADMIN' | 'SUPER_ADMIN'; roleIds?: string[]; } export interface RolePayload { key?: string; name?: string; description?: string | null; permissions?: string[]; } /** Back-office administration. Every call requires an admin-audience token. */ export interface AdminResource { listUsers(params?: UserListParams): Promise>; getUser(id: string): Promise; createUser(payload: CreateUserPayload): Promise; updateUser(id: string, payload: UpdateUserPayload): Promise; resetUserPassword(id: string, password: string): Promise<{ ok: true }>; listCustomers(params?: { page?: number; perPage?: number; q?: string; }): Promise>; getCustomer(id: string): Promise; listRoles(): Promise; getRole(id: string): Promise; listPermissions(): Promise; createRole( payload: Required> & RolePayload, ): Promise; updateRole(id: string, payload: RolePayload): Promise; deleteRole(id: string): Promise; } export function createAdminResource(http: HttpClient): AdminResource { // Administration data is never cached: an operator editing permissions must // see the result of their own change immediately. const uncached = { cache: 'no-store' } as const; return { listUsers: (params = {}) => http.get>('/admin/users', { ...uncached, query: { ...params } }), getUser: (id) => http.get(`/admin/users/${encodeURIComponent(id)}`, uncached), createUser: (payload) => http.post('/admin/users', payload, uncached), updateUser: (id, payload) => http.patch(`/admin/users/${encodeURIComponent(id)}`, payload, uncached), resetUserPassword: (id, password) => http.post<{ ok: true }>( `/admin/users/${encodeURIComponent(id)}/password`, { password }, uncached, ), listCustomers: (params = {}) => http.get>('/admin/customers', { ...uncached, query: { ...params }, }), getCustomer: (id) => http.get(`/admin/customers/${encodeURIComponent(id)}`, uncached), listRoles: () => http.get('/admin/roles', uncached), getRole: (id) => http.get(`/admin/roles/${encodeURIComponent(id)}`, uncached), listPermissions: () => http.get('/admin/roles/permissions', uncached), createRole: (payload) => http.post('/admin/roles', payload, uncached), updateRole: (id, payload) => http.patch(`/admin/roles/${encodeURIComponent(id)}`, payload, uncached), deleteRole: (id) => http.delete(`/admin/roles/${encodeURIComponent(id)}`, uncached), }; }