文件
hyb-oa/client/src/api/budgets.ts
T

110 行
2.6 KiB
TypeScript

import { apiRequestData } from "./http";
export type BudgetScopeType = "global" | "department_legacy" | "cost_center_scoped" | "invalid";
export type BudgetPool = {
id: string;
code: string;
name: string;
scopeType: BudgetScopeType;
departmentId: string | null;
legalEntityId: string | null;
costCenterId: string | null;
department: {
id: string;
name: string;
} | null;
legalEntity: {
id: string;
code: string;
name: string;
} | null;
costCenter: {
id: string;
code: string;
name: string;
legalEntityId: string;
} | null;
status: "active" | "archived";
periodStart: string | null;
periodEnd: string | null;
allocatedAmount: string;
usedAmount: string;
remainingAmount: string;
warningThresholdPercent: number;
archivedAt: string | null;
createdAt: string | null;
updatedAt: string | null;
};
export type BudgetListResponse = {
page: number;
pageSize: number;
total: number;
items: BudgetPool[];
};
export type BudgetListQuery = {
status?: "active" | "archived";
departmentId?: string;
legalEntityId?: string;
costCenterId?: string;
page?: number;
pageSize?: number;
};
function toQueryString(query: Record<string, string | number | undefined>) {
const params = new URLSearchParams();
for (const [key, value] of Object.entries(query)) {
if (value == null || value === "") {
continue;
}
params.set(key, String(value));
}
const encoded = params.toString();
return encoded ? `?${encoded}` : "";
}
export async function fetchBudgets(query: BudgetListQuery = {}) {
return apiRequestData<BudgetListResponse>(`/api/budgets${toQueryString(query as Record<string, string | number | undefined>)}`);
}
export async function createBudget(payload: {
code: string;
name: string;
departmentId?: string | null;
legalEntityId?: string | null;
costCenterId?: string | null;
periodStart: string;
periodEnd: string;
allocatedAmount: string;
warningThresholdPercent?: number;
}) {
return apiRequestData<BudgetPool>(`/api/budgets`, {
method: "POST",
body: JSON.stringify(payload),
});
}
export async function updateBudget(
budgetId: string,
payload: Partial<{
name: string;
allocatedAmount: string;
warningThresholdPercent: number;
status: "active" | "archived";
}>,
) {
return apiRequestData<BudgetPool>(`/api/budgets/${budgetId}`, {
method: "PATCH",
body: JSON.stringify(payload),
});
}
export async function archiveBudget(budgetId: string) {
return apiRequestData<BudgetPool>(`/api/budgets/${budgetId}/archive`, {
method: "POST",
body: JSON.stringify({}),
});
}