100 lines
3.2 KiB
TypeScript
100 lines
3.2 KiB
TypeScript
import apiClient from "@/lib/api/client";
|
|
import { fetchBranches, type BranchListParams, type BranchListResponse } from "@/features/branches/api/branchesApi";
|
|
|
|
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
|
|
export interface AddBranchUserPayload {
|
|
branchId: number;
|
|
userId: number;
|
|
}
|
|
|
|
export interface AddRoleUserPayload {
|
|
roleId: number;
|
|
userId: number;
|
|
}
|
|
|
|
export interface AddPermissionRolePayload {
|
|
permissionIds: number[];
|
|
roleId: number;
|
|
}
|
|
|
|
// export interface AddPermissionUserPayload {
|
|
// permissionId: number;
|
|
// userId: number;
|
|
// effect?: string;
|
|
// }
|
|
|
|
// Re-export so shared hooks can import from one place
|
|
export type { BranchListParams, BranchListResponse };
|
|
|
|
// ─── File Upload ──────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Builds the public URL for an uploaded file.
|
|
* The backend stores files under `{apiOrigin}/file/{filename}`, while the
|
|
* API base URL itself ends with `/api/admin`.
|
|
*/
|
|
export function buildFileUrl(filename: string): string {
|
|
const apiBase = import.meta.env.VITE_API_BASE_URL || "/api/admin";
|
|
const origin = apiBase.replace(/\/api\/admin\/?$/, "");
|
|
return `${origin}/file/${filename}`;
|
|
}
|
|
|
|
/**
|
|
* POST /file — multipart image upload.
|
|
* Response envelope: { status, message, code, data: "....png" }.
|
|
* The axios response interceptor unwraps `data`, so this returns the filename.
|
|
*/
|
|
export async function postFile(file: File): Promise<string> {
|
|
const formData = new FormData();
|
|
formData.append("file", file);
|
|
const { data } = await apiClient.post<string>("/file", formData, {
|
|
headers: { "Content-Type": "multipart/form-data" },
|
|
});
|
|
return data;
|
|
}
|
|
|
|
// ─── API Functions ────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* POST /branch-users
|
|
* Assigns a branch to a user. Used by operators, managers, etc.
|
|
*/
|
|
export async function postBranchUser(payload: AddBranchUserPayload): Promise<void> {
|
|
await apiClient.post("/branch-users", payload);
|
|
}
|
|
|
|
/**
|
|
* POST /role-users
|
|
* Assigns a role to a user.
|
|
*/
|
|
export async function postRoleUser(payload: AddRoleUserPayload): Promise<void> {
|
|
await apiClient.post("/role-users", payload);
|
|
}
|
|
|
|
/**
|
|
* POST /permission-roles/bulk
|
|
* Assigns permissions to a role.
|
|
*/
|
|
export async function postPermissionRole(payload: AddPermissionRolePayload): Promise<void> {
|
|
await apiClient.post("/permission-roles/bulk", payload);
|
|
}
|
|
|
|
// /**
|
|
// * POST /permission-users
|
|
// * Assigns a permission to a user.
|
|
// */
|
|
// export async function postPermissionUser(payload: AddPermissionUserPayload): Promise<void> {
|
|
// await apiClient.post("/permission-users", {
|
|
// permissionId: payload.permissionId,
|
|
// userId: payload.userId,
|
|
// effect: payload.effect ?? "allow",
|
|
// });
|
|
// }
|
|
|
|
/**
|
|
* Proxy for branch list fetching — kept here so shared hooks
|
|
* don't import directly from the branches feature.
|
|
*/
|
|
export { fetchBranches };
|