107 lines
3.5 KiB
TypeScript
107 lines
3.5 KiB
TypeScript
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<OffsetPaginated<UserSummary>>;
|
|
getUser(id: string): Promise<UserSummary>;
|
|
createUser(payload: CreateUserPayload): Promise<UserSummary>;
|
|
updateUser(id: string, payload: UpdateUserPayload): Promise<UserSummary>;
|
|
resetUserPassword(id: string, password: string): Promise<{ ok: true }>;
|
|
|
|
listCustomers(params?: {
|
|
page?: number;
|
|
perPage?: number;
|
|
q?: string;
|
|
}): Promise<OffsetPaginated<AdminCustomer>>;
|
|
getCustomer(id: string): Promise<AdminCustomerDetail>;
|
|
|
|
listRoles(): Promise<RoleDetail[]>;
|
|
getRole(id: string): Promise<RoleDetail>;
|
|
listPermissions(): Promise<PermissionGroup[]>;
|
|
createRole(
|
|
payload: Required<Pick<RolePayload, 'key' | 'name'>> & RolePayload,
|
|
): Promise<RoleDetail>;
|
|
updateRole(id: string, payload: RolePayload): Promise<RoleDetail>;
|
|
deleteRole(id: string): Promise<void>;
|
|
}
|
|
|
|
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<OffsetPaginated<UserSummary>>('/admin/users', { ...uncached, query: { ...params } }),
|
|
getUser: (id) => http.get<UserSummary>(`/admin/users/${encodeURIComponent(id)}`, uncached),
|
|
createUser: (payload) => http.post<UserSummary>('/admin/users', payload, uncached),
|
|
updateUser: (id, payload) =>
|
|
http.patch<UserSummary>(`/admin/users/${encodeURIComponent(id)}`, payload, uncached),
|
|
resetUserPassword: (id, password) =>
|
|
http.post<{ ok: true }>(
|
|
`/admin/users/${encodeURIComponent(id)}/password`,
|
|
{ password },
|
|
uncached,
|
|
),
|
|
|
|
listCustomers: (params = {}) =>
|
|
http.get<OffsetPaginated<AdminCustomer>>('/admin/customers', {
|
|
...uncached,
|
|
query: { ...params },
|
|
}),
|
|
getCustomer: (id) =>
|
|
http.get<AdminCustomerDetail>(`/admin/customers/${encodeURIComponent(id)}`, uncached),
|
|
|
|
listRoles: () => http.get<RoleDetail[]>('/admin/roles', uncached),
|
|
getRole: (id) => http.get<RoleDetail>(`/admin/roles/${encodeURIComponent(id)}`, uncached),
|
|
listPermissions: () => http.get<PermissionGroup[]>('/admin/roles/permissions', uncached),
|
|
createRole: (payload) => http.post<RoleDetail>('/admin/roles', payload, uncached),
|
|
updateRole: (id, payload) =>
|
|
http.patch<RoleDetail>(`/admin/roles/${encodeURIComponent(id)}`, payload, uncached),
|
|
deleteRole: (id) => http.delete<void>(`/admin/roles/${encodeURIComponent(id)}`, uncached),
|
|
};
|
|
}
|