330 lines
9.1 KiB
TypeScript
330 lines
9.1 KiB
TypeScript
import { api } from "./client";
|
|
import type { AlertSeverity, CoverageStatus } from "./assets";
|
|
|
|
export type ContractStatus = CoverageStatus | "superseded";
|
|
export type DemoStatus = "on_demo" | "overdue_return" | "returned";
|
|
|
|
export interface ContractItem {
|
|
id: number;
|
|
asset_id: number;
|
|
asset_code: string;
|
|
asset_name: string;
|
|
department_id: number;
|
|
department_name: string | null;
|
|
contract_type: string;
|
|
provider_id: number;
|
|
provider_name: string | null;
|
|
start_date: string;
|
|
end_date: string;
|
|
coverage_period_text: string | null;
|
|
renewed_from_id: number | null;
|
|
is_superseded: boolean;
|
|
status: ContractStatus | string;
|
|
severity: AlertSeverity | string;
|
|
days_to_expiry: number;
|
|
attachment_count: number;
|
|
remarks: string | null;
|
|
created_at: string;
|
|
}
|
|
|
|
export interface ContractListParams {
|
|
q?: string;
|
|
department_id?: number;
|
|
asset_id?: number;
|
|
provider_id?: number;
|
|
contract_type?: string;
|
|
status?: string;
|
|
include_superseded?: boolean;
|
|
page?: number;
|
|
page_size?: number;
|
|
}
|
|
|
|
export interface ContractCreatePayload {
|
|
asset_id: number;
|
|
contract_type: string;
|
|
provider_id: number;
|
|
start_date: string;
|
|
end_date: string;
|
|
coverage_period_text?: string | null;
|
|
remarks?: string | null;
|
|
}
|
|
|
|
export interface ContractRenewPayload {
|
|
contract_type?: string;
|
|
provider_id?: number;
|
|
start_date: string;
|
|
end_date: string;
|
|
coverage_period_text?: string | null;
|
|
remarks?: string | null;
|
|
}
|
|
|
|
export interface WarrantyItem {
|
|
id: number;
|
|
asset_id: number;
|
|
asset_code: string;
|
|
asset_name: string;
|
|
department_id: number;
|
|
department_name: string | null;
|
|
duration_months: number;
|
|
start_date: string;
|
|
end_date: string;
|
|
provider_id: number | null;
|
|
provider_name: string | null;
|
|
status: CoverageStatus | string;
|
|
severity: AlertSeverity | string;
|
|
days_to_expiry: number;
|
|
attachment_count: number;
|
|
remarks: string | null;
|
|
created_at: string;
|
|
}
|
|
|
|
export interface WarrantyListParams {
|
|
q?: string;
|
|
department_id?: number;
|
|
asset_id?: number;
|
|
status?: string;
|
|
page?: number;
|
|
page_size?: number;
|
|
}
|
|
|
|
export interface WarrantyCreatePayload {
|
|
asset_id: number;
|
|
duration_months: number;
|
|
start_date: string;
|
|
end_date: string;
|
|
provider_id?: number | null;
|
|
remarks?: string | null;
|
|
}
|
|
|
|
export interface InsuranceItem {
|
|
id: number;
|
|
asset_id: number;
|
|
asset_code: string;
|
|
asset_name: string;
|
|
department_id: number;
|
|
department_name: string | null;
|
|
insurer_id: number;
|
|
insurer_name: string | null;
|
|
policy_no: string;
|
|
start_date: string;
|
|
end_date: string;
|
|
coverage_amount: number | null;
|
|
status: CoverageStatus | string;
|
|
severity: AlertSeverity | string;
|
|
days_to_expiry: number;
|
|
attachment_count: number;
|
|
remarks: string | null;
|
|
created_at: string;
|
|
}
|
|
|
|
export interface InsuranceListParams {
|
|
q?: string;
|
|
department_id?: number;
|
|
asset_id?: number;
|
|
insurer_id?: number;
|
|
status?: string;
|
|
page?: number;
|
|
page_size?: number;
|
|
}
|
|
|
|
export interface InsuranceCreatePayload {
|
|
asset_id: number;
|
|
insurer_id: number;
|
|
policy_no: string;
|
|
start_date: string;
|
|
end_date: string;
|
|
coverage_amount?: number | null;
|
|
remarks?: string | null;
|
|
}
|
|
|
|
export interface DemoEquipmentItem {
|
|
id: number;
|
|
name: string;
|
|
equipment_type: string;
|
|
serial_no?: string | null;
|
|
supplier_id: number;
|
|
supplier_name: string | null;
|
|
department_id: number;
|
|
department_name: string | null;
|
|
location_id: number | null;
|
|
location_name: string | null;
|
|
start_date: string;
|
|
expected_return_date: string;
|
|
actual_return_date: string | null;
|
|
status: DemoStatus | string;
|
|
severity: AlertSeverity | string;
|
|
days_remaining: number;
|
|
attachment_count: number;
|
|
remarks: string | null;
|
|
created_at: string;
|
|
}
|
|
|
|
export interface DemoListParams {
|
|
q?: string;
|
|
supplier_id?: number;
|
|
department_id?: number;
|
|
status?: string;
|
|
date_from?: string;
|
|
date_to?: string;
|
|
page?: number;
|
|
page_size?: number;
|
|
}
|
|
|
|
export interface DemoCreatePayload {
|
|
name: string;
|
|
equipment_type: string;
|
|
serial_no?: string | null;
|
|
supplier_id: number;
|
|
department_id: number;
|
|
location_id?: number | null;
|
|
start_date: string;
|
|
expected_return_date: string;
|
|
remarks?: string | null;
|
|
}
|
|
|
|
export interface VisitAssetBrief {
|
|
id: number;
|
|
asset_code: string;
|
|
name: string;
|
|
}
|
|
|
|
export interface VisitComplaintBrief {
|
|
id: number;
|
|
complaint_no: string;
|
|
call_status: string;
|
|
priority: string;
|
|
description: string;
|
|
}
|
|
|
|
export interface VisitItem {
|
|
id: number;
|
|
department_id: number;
|
|
department_name: string | null;
|
|
visited_by_id: number;
|
|
visited_by_name: string | null;
|
|
visit_date: string;
|
|
observations: string;
|
|
new_requirements?: string | null;
|
|
assets: VisitAssetBrief[];
|
|
complaints: VisitComplaintBrief[];
|
|
created_at: string;
|
|
}
|
|
|
|
export interface VisitListParams {
|
|
q?: string;
|
|
department_id?: number;
|
|
visited_by_id?: number;
|
|
date_from?: string;
|
|
date_to?: string;
|
|
page?: number;
|
|
page_size?: number;
|
|
}
|
|
|
|
export interface VisitCreatePayload {
|
|
department_id: number;
|
|
visit_date: string;
|
|
observations: string;
|
|
new_requirements?: string | null;
|
|
asset_ids?: number[];
|
|
complaint_ids?: number[];
|
|
}
|
|
|
|
function toQS(params: Record<string, string | number | boolean | undefined>): string {
|
|
const sp = new URLSearchParams();
|
|
for (const [k, v] of Object.entries(params)) {
|
|
if (v === undefined || v === "") continue;
|
|
sp.set(k, String(v));
|
|
}
|
|
const s = sp.toString();
|
|
return s ? `?${s}` : "";
|
|
}
|
|
|
|
export const contractsApi = {
|
|
list: (params: ContractListParams = {}) =>
|
|
api.get<{ items: ContractItem[]; total: number }>(
|
|
`/contracts${toQS(params as Record<string, string | number | boolean | undefined>)}`
|
|
),
|
|
create: (data: ContractCreatePayload) => api.post<ContractItem>("/contracts", data),
|
|
update: (id: number, data: Partial<ContractCreatePayload>) =>
|
|
api.patch<ContractItem>(`/contracts/${id}`, data),
|
|
remove: (id: number) => api.delete<void>(`/contracts/${id}`),
|
|
renew: (id: number, data: ContractRenewPayload) =>
|
|
api.post<{ old_contract: ContractItem; new_contract: ContractItem }>(
|
|
`/contracts/${id}/renew`,
|
|
data
|
|
),
|
|
history: (assetId: number) =>
|
|
api.get<{ items: ContractItem[] }>(`/contracts/history/${assetId}`),
|
|
downloadImportTemplate: async () => {
|
|
const { blob } = await api.downloadBlob("/contracts/import-template");
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement("a");
|
|
a.href = url;
|
|
a.download = "contracts_import_template.xlsx";
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
URL.revokeObjectURL(url);
|
|
},
|
|
};
|
|
|
|
export const warrantiesApi = {
|
|
list: (params: WarrantyListParams = {}) =>
|
|
api.get<{ items: WarrantyItem[]; total: number }>(
|
|
`/warranties${toQS(params as Record<string, string | number | boolean | undefined>)}`
|
|
),
|
|
create: (data: WarrantyCreatePayload) => api.post<WarrantyItem>("/warranties", data),
|
|
update: (id: number, data: Partial<WarrantyCreatePayload>) =>
|
|
api.patch<WarrantyItem>(`/warranties/${id}`, data),
|
|
remove: (id: number) => api.delete<void>(`/warranties/${id}`),
|
|
};
|
|
|
|
export const insuranceApi = {
|
|
list: (params: InsuranceListParams = {}) =>
|
|
api.get<{ items: InsuranceItem[]; total: number }>(
|
|
`/insurance${toQS(params as Record<string, string | number | boolean | undefined>)}`
|
|
),
|
|
create: (data: InsuranceCreatePayload) => api.post<InsuranceItem>("/insurance", data),
|
|
update: (id: number, data: Partial<InsuranceCreatePayload>) =>
|
|
api.patch<InsuranceItem>(`/insurance/${id}`, data),
|
|
remove: (id: number) => api.delete<void>(`/insurance/${id}`),
|
|
downloadImportTemplate: async () => {
|
|
const { blob } = await api.downloadBlob("/insurance/import-template");
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement("a");
|
|
a.href = url;
|
|
a.download = "insurance_import_template.xlsx";
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
URL.revokeObjectURL(url);
|
|
},
|
|
};
|
|
|
|
export const demoEquipmentApi = {
|
|
list: (params: DemoListParams = {}) =>
|
|
api.get<{ items: DemoEquipmentItem[]; total: number }>(
|
|
`/demo-equipment${toQS(params as Record<string, string | number | boolean | undefined>)}`
|
|
),
|
|
create: (data: DemoCreatePayload) =>
|
|
api.post<DemoEquipmentItem>("/demo-equipment", data),
|
|
update: (id: number, data: Partial<DemoCreatePayload>) =>
|
|
api.patch<DemoEquipmentItem>(`/demo-equipment/${id}`, data),
|
|
remove: (id: number) => api.delete<void>(`/demo-equipment/${id}`),
|
|
markReturned: (id: number, data: { actual_return_date: string; remarks?: string | null }) =>
|
|
api.post<DemoEquipmentItem>(`/demo-equipment/${id}/return`, data),
|
|
};
|
|
|
|
export const visitsApi = {
|
|
list: (params: VisitListParams = {}) =>
|
|
api.get<{ items: VisitItem[]; total: number }>(
|
|
`/visits${toQS(params as Record<string, string | number | boolean | undefined>)}`
|
|
),
|
|
get: (id: number) => api.get<VisitItem>(`/visits/${id}`),
|
|
create: (data: VisitCreatePayload) => api.post<VisitItem>("/visits", data),
|
|
update: (id: number, data: Partial<VisitCreatePayload>) =>
|
|
api.patch<VisitItem>(`/visits/${id}`, data),
|
|
linkComplaint: (id: number, complaintId: number) =>
|
|
api.post<VisitItem>(`/visits/${id}/link-complaint`, { complaint_id: complaintId }),
|
|
};
|