import { apiRequestData } from "./http"; export type DocumentCategoryEntityType = | "reimbursement" | "leave" | "purchase_request" | "travel_request" | "asset_card" | "general"; export type DocumentResetPeriod = "none" | "daily" | "monthly" | "yearly"; export type HolidayCalendarDayType = "holiday" | "workday" | "adjusted_workday"; export type LegalEntityItem = { id: string; code: string; name: string; shortName: string | null; currencyCode: string; taxNo: string | null; enabled: boolean; archivedAt: string | null; createdAt: string | null; updatedAt: string | null; }; export type CostCenterItem = { id: string; legalEntityId: string; code: string; name: string; parentId: string | null; ownerEmployeeId: string | null; enabled: boolean; archivedAt: string | null; createdAt: string | null; updatedAt: string | null; legalEntity: { id: string; code: string; name: string; } | null; parent: { id: string; name: string; } | null; owner: { id: string; employeeNo: string; name: string; } | null; }; export type DocumentCategoryItem = { id: string; code: string; name: string; entityType: DocumentCategoryEntityType; sequencePrefix: string; numberLength: number; resetPeriod: DocumentResetPeriod; defaultWorkflowKey: string | null; enabled: boolean; archivedAt: string | null; createdAt: string | null; updatedAt: string | null; }; export type HolidayCalendarItem = { id: string; code: string; name: string; timezone: string; legalEntityId: string | null; enabled: boolean; archivedAt: string | null; createdAt: string | null; updatedAt: string | null; daysCount: number; isDefaultForLegalEntity: boolean; legalEntity: { id: string; code: string; name: string; } | null; }; export type HolidayCalendarDayItem = { id: string; date: string; dayType: HolidayCalendarDayType; name: string; note: string | null; createdAt: string | null; updatedAt: string | null; }; export type SupplierItem = { id: string; code: string; name: string; shortName: string | null; taxNo: string | null; contactName: string | null; contactPhone: string | null; paymentTerms: string | null; legalEntityId: string | null; enabled: boolean; archivedAt: string | null; createdAt: string | null; updatedAt: string | null; legalEntity: { id: string; code: string; name: string; } | null; }; export type MasterDataListResponse = { includeArchived: boolean; items: T[]; }; function toQueryString(query: Record) { const params = new URLSearchParams(); for (const [key, value] of Object.entries(query)) { if (value == null || value === "") { continue; } params.set(key, typeof value === "boolean" ? String(value) : value); } const encoded = params.toString(); return encoded ? `?${encoded}` : ""; } export async function fetchLegalEntities(params: { includeArchived?: boolean; enabled?: boolean } = {}) { return apiRequestData>( `/api/master-data/legal-entities${toQueryString(params as Record)}`, ); } export async function createLegalEntity(payload: { code: string; name: string; shortName?: string | null; currencyCode: string; taxNo?: string | null; enabled?: boolean; }) { return apiRequestData<{ legalEntity: LegalEntityItem }>("/api/master-data/legal-entities", { method: "POST", body: JSON.stringify(payload), }); } export async function patchLegalEntity( legalEntityId: string, payload: Partial<{ code: string; name: string; shortName: string | null; currencyCode: string; taxNo: string | null; enabled: boolean; }>, ) { return apiRequestData<{ legalEntity: LegalEntityItem }>(`/api/master-data/legal-entities/${legalEntityId}`, { method: "PATCH", body: JSON.stringify(payload), }); } export async function archiveLegalEntity(legalEntityId: string) { return apiRequestData<{ legalEntity: LegalEntityItem; outcome: string }>( `/api/master-data/legal-entities/${legalEntityId}/archive`, { method: "POST", body: JSON.stringify({}), }, ); } export async function fetchCostCenters(params: { includeArchived?: boolean; enabled?: boolean; legalEntityId?: string; } = {}) { return apiRequestData>( `/api/master-data/cost-centers${toQueryString(params as Record)}`, ); } export async function createCostCenter(payload: { legalEntityId: string; code: string; name: string; parentId?: string | null; ownerEmployeeId?: string | null; enabled?: boolean; }) { return apiRequestData<{ costCenter: CostCenterItem }>("/api/master-data/cost-centers", { method: "POST", body: JSON.stringify(payload), }); } export async function patchCostCenter( costCenterId: string, payload: Partial<{ legalEntityId: string; code: string; name: string; parentId: string | null; ownerEmployeeId: string | null; enabled: boolean; }>, ) { return apiRequestData<{ costCenter: CostCenterItem }>(`/api/master-data/cost-centers/${costCenterId}`, { method: "PATCH", body: JSON.stringify(payload), }); } export async function archiveCostCenter(costCenterId: string) { return apiRequestData<{ costCenter: CostCenterItem; outcome: string }>( `/api/master-data/cost-centers/${costCenterId}/archive`, { method: "POST", body: JSON.stringify({}), }, ); } export async function fetchDocumentCategories(params: { includeArchived?: boolean; enabled?: boolean; entityType?: DocumentCategoryEntityType; } = {}) { return apiRequestData>( `/api/master-data/document-categories${toQueryString(params as Record)}`, ); } export async function createDocumentCategory(payload: { code: string; name: string; entityType: DocumentCategoryEntityType; sequencePrefix: string; numberLength?: number; resetPeriod: DocumentResetPeriod; defaultWorkflowKey?: string | null; enabled?: boolean; }) { return apiRequestData<{ documentCategory: DocumentCategoryItem }>( "/api/master-data/document-categories", { method: "POST", body: JSON.stringify(payload), }, ); } export async function patchDocumentCategory( documentCategoryId: string, payload: Partial<{ code: string; name: string; entityType: DocumentCategoryEntityType; sequencePrefix: string; numberLength: number; resetPeriod: DocumentResetPeriod; defaultWorkflowKey: string | null; enabled: boolean; }>, ) { return apiRequestData<{ documentCategory: DocumentCategoryItem }>( `/api/master-data/document-categories/${documentCategoryId}`, { method: "PATCH", body: JSON.stringify(payload), }, ); } export async function archiveDocumentCategory(documentCategoryId: string) { return apiRequestData<{ documentCategory: DocumentCategoryItem; outcome: string }>( `/api/master-data/document-categories/${documentCategoryId}/archive`, { method: "POST", body: JSON.stringify({}), }, ); } export async function fetchHolidayCalendars(params: { includeArchived?: boolean; enabled?: boolean; legalEntityId?: string; } = {}) { return apiRequestData>( `/api/master-data/holiday-calendars${toQueryString(params as Record)}`, ); } export async function createHolidayCalendar(payload: { code: string; name: string; timezone?: string; legalEntityId?: string | null; enabled?: boolean; }) { return apiRequestData<{ holidayCalendar: HolidayCalendarItem }>("/api/master-data/holiday-calendars", { method: "POST", body: JSON.stringify(payload), }); } export async function patchHolidayCalendar( holidayCalendarId: string, payload: Partial<{ code: string; name: string; timezone: string; legalEntityId: string | null; enabled: boolean; }>, ) { return apiRequestData<{ holidayCalendar: HolidayCalendarItem }>( `/api/master-data/holiday-calendars/${holidayCalendarId}`, { method: "PATCH", body: JSON.stringify(payload), }, ); } export async function archiveHolidayCalendar(holidayCalendarId: string) { return apiRequestData<{ holidayCalendar: HolidayCalendarItem; outcome: string }>( `/api/master-data/holiday-calendars/${holidayCalendarId}/archive`, { method: "POST", body: JSON.stringify({}), }, ); } export async function fetchHolidayCalendarDays(holidayCalendarId: string) { return apiRequestData<{ items: HolidayCalendarDayItem[] }>( `/api/master-data/holiday-calendars/${holidayCalendarId}/days`, ); } export async function replaceHolidayCalendarDays( holidayCalendarId: string, items: Array<{ date: string; dayType: HolidayCalendarDayType; name: string; note?: string | null; }>, ) { return apiRequestData<{ holidayCalendarId: string; replacedCount: number }>( `/api/master-data/holiday-calendars/${holidayCalendarId}/days`, { method: "PUT", body: JSON.stringify({ items }), }, ); } export async function fetchSuppliers(params: { includeArchived?: boolean; enabled?: boolean; legalEntityId?: string; q?: string; } = {}) { return apiRequestData>( `/api/master-data/suppliers${toQueryString(params as Record)}`, ); } export async function createSupplier(payload: { code: string; name: string; shortName?: string | null; taxNo?: string | null; contactName?: string | null; contactPhone?: string | null; paymentTerms?: string | null; legalEntityId?: string | null; enabled?: boolean; }) { return apiRequestData<{ supplier: SupplierItem }>("/api/master-data/suppliers", { method: "POST", body: JSON.stringify(payload), }); } export async function patchSupplier( supplierId: string, payload: Partial<{ code: string; name: string; shortName: string | null; taxNo: string | null; contactName: string | null; contactPhone: string | null; paymentTerms: string | null; legalEntityId: string | null; enabled: boolean; }>, ) { return apiRequestData<{ supplier: SupplierItem }>(`/api/master-data/suppliers/${supplierId}`, { method: "PATCH", body: JSON.stringify(payload), }); } export async function archiveSupplier(supplierId: string) { return apiRequestData<{ supplier: SupplierItem; outcome: string }>( `/api/master-data/suppliers/${supplierId}/archive`, { method: "POST", body: JSON.stringify({}), }, ); }