137 行
4.4 KiB
TypeScript
137 行
4.4 KiB
TypeScript
import { apiRequestBlob, apiRequestData } from "./http";
|
|
import type { ConfidentialityLevel, EmployeeBasic } from "./projects";
|
|
|
|
export type ContractRegistryType = "purchase" | "sales" | "service" | "framework" | "other";
|
|
export type ContractRegistryPersistedStatus = "draft" | "active" | "archived";
|
|
export type ContractRegistryStatus = ContractRegistryPersistedStatus | "expired";
|
|
export type ContractRegistryExportFormat = "csv" | "xlsx";
|
|
|
|
export type ContractRegistryFilters = {
|
|
projectId?: string;
|
|
status?: ContractRegistryStatus;
|
|
contractType?: ContractRegistryType;
|
|
counterpartyName?: string;
|
|
expiryDateFrom?: string;
|
|
expiryDateTo?: string;
|
|
};
|
|
|
|
export type ContractRegistryItem = {
|
|
id: string;
|
|
contractNo: string;
|
|
projectId: string | null;
|
|
contractType: ContractRegistryType;
|
|
counterpartyName: string;
|
|
summary: string;
|
|
amount: string | null;
|
|
currency: string;
|
|
signedDate: string | null;
|
|
effectiveDate: string | null;
|
|
expiryDate: string | null;
|
|
confidentialityLevel: ConfidentialityLevel;
|
|
archiveLocation: string;
|
|
offlineArchiveNo: string | null;
|
|
ownerEmployeeId: string;
|
|
status: ContractRegistryStatus;
|
|
persistedStatus: ContractRegistryPersistedStatus;
|
|
createdAt: string | null;
|
|
updatedAt: string | null;
|
|
owner: EmployeeBasic | null;
|
|
project: { id: string; projectNo: string; name: string } | null;
|
|
};
|
|
|
|
function buildFiltersSearch(filters?: ContractRegistryFilters | string) {
|
|
const normalized =
|
|
typeof filters === "string"
|
|
? { projectId: filters }
|
|
: (filters ?? {});
|
|
const search = new URLSearchParams();
|
|
|
|
if (normalized.projectId) search.set("projectId", normalized.projectId);
|
|
if (normalized.status) search.set("status", normalized.status);
|
|
if (normalized.contractType) search.set("contractType", normalized.contractType);
|
|
if (normalized.counterpartyName) search.set("counterpartyName", normalized.counterpartyName);
|
|
if (normalized.expiryDateFrom) search.set("expiryDateFrom", normalized.expiryDateFrom);
|
|
if (normalized.expiryDateTo) search.set("expiryDateTo", normalized.expiryDateTo);
|
|
|
|
const suffix = search.toString();
|
|
return suffix ? `?${suffix}` : "";
|
|
}
|
|
|
|
export async function fetchContractsRegistry(filters?: ContractRegistryFilters | string) {
|
|
return apiRequestData<{ items: ContractRegistryItem[] }>(`/api/contracts-registry${buildFiltersSearch(filters)}`);
|
|
}
|
|
|
|
export async function fetchExpiringContracts(days = 30) {
|
|
return apiRequestData<{ days: number; total: number; items: ContractRegistryItem[] }>(
|
|
`/api/contracts-registry/expiring?days=${encodeURIComponent(String(days))}`,
|
|
);
|
|
}
|
|
|
|
export async function createContractRegistry(payload: {
|
|
contractNo: string;
|
|
projectId?: string | null;
|
|
contractType: ContractRegistryType;
|
|
counterpartyName: string;
|
|
summary: string;
|
|
amount?: string | null;
|
|
currency?: string;
|
|
signedDate?: string | null;
|
|
effectiveDate?: string | null;
|
|
expiryDate?: string | null;
|
|
confidentialityLevel?: ConfidentialityLevel;
|
|
archiveLocation: string;
|
|
offlineArchiveNo?: string | null;
|
|
ownerEmployeeId?: string;
|
|
status?: ContractRegistryPersistedStatus;
|
|
}) {
|
|
return apiRequestData<{ item: ContractRegistryItem }>("/api/contracts-registry", {
|
|
method: "POST",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function patchContractRegistry(
|
|
id: string,
|
|
payload: Partial<{
|
|
contractNo: string;
|
|
projectId: string | null;
|
|
contractType: ContractRegistryType;
|
|
counterpartyName: string;
|
|
summary: string;
|
|
amount: string | null;
|
|
currency: string;
|
|
signedDate: string | null;
|
|
effectiveDate: string | null;
|
|
expiryDate: string | null;
|
|
confidentialityLevel: ConfidentialityLevel;
|
|
archiveLocation: string;
|
|
offlineArchiveNo: string | null;
|
|
ownerEmployeeId: string;
|
|
status: ContractRegistryPersistedStatus;
|
|
}>,
|
|
) {
|
|
return apiRequestData<{ item: ContractRegistryItem }>(`/api/contracts-registry/${id}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function renewContractRegistry(id: string) {
|
|
return apiRequestData<{ item: ContractRegistryItem; sourceItem: ContractRegistryItem }>(
|
|
`/api/contracts-registry/${id}/renew`,
|
|
{
|
|
method: "POST",
|
|
},
|
|
);
|
|
}
|
|
|
|
export async function exportContractsRegistry(params: ContractRegistryFilters & { format: ContractRegistryExportFormat }) {
|
|
return apiRequestBlob("/api/exports/contracts-registry", {
|
|
method: "POST",
|
|
headers: {
|
|
"content-type": "application/json",
|
|
},
|
|
body: JSON.stringify(params),
|
|
});
|
|
}
|