91 行
2.6 KiB
TypeScript
91 行
2.6 KiB
TypeScript
import { apiRequestData } from "./http";
|
|
|
|
export type ExpenseStandardPerUnit = "item" | "day" | "trip";
|
|
export type ExpenseStandardControlMode = "warn" | "block";
|
|
export type ExpenseStandardScopeType = "global" | "department";
|
|
|
|
export type ExpenseStandard = {
|
|
id: string;
|
|
expenseTypeId: string;
|
|
expenseTypeName: string | null;
|
|
name: string;
|
|
maxAmount: string;
|
|
perUnit: ExpenseStandardPerUnit;
|
|
controlMode: ExpenseStandardControlMode;
|
|
scopeType: ExpenseStandardScopeType;
|
|
scopeKey: string | null;
|
|
scopeLabel: string;
|
|
enabled: boolean;
|
|
note: string | null;
|
|
createdAt: string | null;
|
|
updatedAt: string | null;
|
|
};
|
|
|
|
export type ExpenseStandardListResponse = {
|
|
expenseTypeId: string | null;
|
|
scopeType: ExpenseStandardScopeType | null;
|
|
enabled: boolean | null;
|
|
items: ExpenseStandard[];
|
|
};
|
|
|
|
export type ExpenseStandardListQuery = {
|
|
expenseTypeId?: string;
|
|
scopeType?: ExpenseStandardScopeType;
|
|
enabled?: boolean;
|
|
};
|
|
|
|
export type CreateExpenseStandardPayload = {
|
|
expenseTypeId: string;
|
|
name: string;
|
|
maxAmount: string;
|
|
perUnit?: ExpenseStandardPerUnit;
|
|
controlMode?: ExpenseStandardControlMode;
|
|
scopeType?: ExpenseStandardScopeType;
|
|
scopeKey?: string | null;
|
|
enabled?: boolean;
|
|
note?: string | null;
|
|
};
|
|
|
|
export type UpdateExpenseStandardPayload = Partial<CreateExpenseStandardPayload>;
|
|
|
|
function toQueryString(query: Record<string, string | number | boolean | 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 fetchExpenseStandards(query: ExpenseStandardListQuery = {}) {
|
|
return apiRequestData<ExpenseStandardListResponse>(
|
|
`/api/expense-standards${toQueryString(query as Record<string, string | number | boolean | undefined>)}`,
|
|
);
|
|
}
|
|
|
|
export async function createExpenseStandard(payload: CreateExpenseStandardPayload) {
|
|
return apiRequestData<ExpenseStandard>("/api/expense-standards", {
|
|
method: "POST",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function updateExpenseStandard(standardId: string, payload: UpdateExpenseStandardPayload) {
|
|
return apiRequestData<ExpenseStandard>(`/api/expense-standards/${standardId}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function deleteExpenseStandard(standardId: string) {
|
|
return apiRequestData<ExpenseStandard>(`/api/expense-standards/${standardId}`, {
|
|
method: "DELETE",
|
|
body: JSON.stringify({}),
|
|
});
|
|
}
|