All files / src/lib/api/admin users.ts

100% Statements 11/11
100% Branches 2/2
100% Functions 9/9
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                              95x   3x 3x                 3x             3x           2x             2x                   2x           4x                           4x             2x          
import { request } from "../base";
 
export type AdminUser = {
	id: string;
	name: string;
	email: string;
	emailVerified: boolean;
	phoneNumber: string | null;
	banned: boolean;
	banReason: string | null;
	createdAt: string;
	updatedAt: string;
};
 
export type UserTenantRole = {
	id: number;
	userId: string;
	tenantType: "platform" | "pro";
	tenantId: string | null;
	role: string;
	dateCreated: string;
};
 
export const adminUsersApi = {
	async listUsers(params?: Record<string, string>) {
		const query = params ? `?${new URLSearchParams(params).toString()}` : "";
		return request<AdminUser[]>(`/api/admin/users${query}`);
	},
 
	async createUser(data: {
		name: string;
		email: string;
		password: string;
		phoneNumber?: string;
	}) {
		return request<AdminUser>(`/api/admin/users`, {
			method: "POST",
			body: data,
		});
	},
 
	async getUser(id: string) {
		return request<{ user: AdminUser; roles: UserTenantRole[] }>(
			`/api/admin/users/${id}`,
		);
	},
 
	async updateUser(id: string, data: Partial<AdminUser>) {
		return request<AdminUser>(`/api/admin/users/${id}`, {
			method: "PUT",
			body: data,
		});
	},
 
	async resetUserPassword(id: string, password: string) {
		return request<{ message: string }>(
			`/api/admin/users/${id}/reset-password`,
			{
				method: "POST",
				body: { password },
			},
		);
	},
 
	async deleteUser(id: string) {
		return request(`/api/admin/users/${id}`, {
			method: "DELETE",
		});
	},
 
	async banUser(id: string, banned: boolean, banReason?: string) {
		return request<AdminUser>(`/api/admin/users/${id}/ban`, {
			method: "POST",
			body: { banned, banReason },
		});
	},
 
	async addUserRole(
		userId: string,
		data: {
			tenantType: "platform" | "pro";
			tenantId?: string;
			role: string;
		},
	) {
		return request<UserTenantRole>(`/api/admin/users/${userId}/roles`, {
			method: "POST",
			body: data,
		});
	},
 
	async removeUserRole(userId: string, roleId: number) {
		return request(`/api/admin/users/${userId}/roles/${roleId}`, {
			method: "DELETE",
		});
	},
};