import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' export type PlatformPartnerType = 'CARRIER' | 'SHIPPER' | 'CONSIGNEE' export type PlatformPartnerStatus = 'ACTIVE' | 'INACTIVE' export type PlatformPartnerCarrierCode = 'MATSON' | 'SML' | 'ZIM' | 'COSCO' | 'EMC' | 'HEDE' | 'OTHER' export interface PlatformPartnerResource { id: number | string tenantId: number | string dataScopeId: number | string partnerCode: string partnerName: string partnerType: PlatformPartnerType types: PlatformPartnerType[] carrierCode: PlatformPartnerCarrierCode | null contactName: string contactPhone: string contactEmail: string | null countryCode: string province: string | null city: string district: string | null addressLine: string postalCode: string | null status: PlatformPartnerStatus remark: string | null createdAt?: string | null updatedAt?: string | null } export interface PlatformPartnerPage { records: PlatformPartnerResource[] page: number size: number total: number pages: number } type PlatformPartnerPagePayload = Omit & { page: number | string size: number | string total: number | string pages: number | string } export interface PlatformPartnerQuery { page: number size: number keyword?: string type?: PlatformPartnerType status?: PlatformPartnerStatus } export interface SavePlatformPartnerRequest { partnerCode: string partnerName: string partnerType: PlatformPartnerType types: PlatformPartnerType[] carrierCode: PlatformPartnerCarrierCode | null contactName: string contactPhone: string contactEmail: string | null countryCode: string province: string | null city: string district: string | null addressLine: string postalCode: string | null status: PlatformPartnerStatus remark: string | null } export interface PlatformPartnerImportError { row: number field: string message: string } export interface PlatformPartnerImportResult { fileName: string totalRows: number importedRows: number success: boolean errors: PlatformPartnerImportError[] } const basePath = '/platform/v1/partners' const platformResourceScope = 0 function normalizePartner(resource: PlatformPartnerResource): PlatformPartnerResource { return { ...resource, dataScopeId: resource.dataScopeId ?? resource.tenantId, types: resource.types?.length ? resource.types : [resource.partnerType], carrierCode: resource.carrierCode ?? null, remark: resource.remark ?? null, } } export async function listPlatformPartners(query: PlatformPartnerQuery) { const response = await http.get, ApiEnvelope>(basePath, { params: query, }) const page = unwrapApiResponse(response) return { ...page, records: page.records.map(normalizePartner), page: Number(page.page), size: Number(page.size), total: Number(page.total), pages: Number(page.pages), } } export async function getPlatformPartner(partnerId: number | string) { const response = await http.get, ApiEnvelope>( `${basePath}/${partnerId}`, { params: { tenantId: platformResourceScope } }, ) return normalizePartner(unwrapApiResponse(response)) } export async function downloadPlatformPartnerImportTemplate() { return http.get(`${basePath}/import-template`, { responseType: 'blob' }) } export async function importPlatformPartners(file: File) { const form = new FormData() form.append('file', file) const response = await http.post, ApiEnvelope>( `${basePath}/import`, form, { params: { tenantId: platformResourceScope } }, ) return unwrapApiResponse(response) } export async function createPlatformPartner(payload: SavePlatformPartnerRequest) { const response = await http.post, ApiEnvelope>(basePath, payload, { params: { tenantId: platformResourceScope }, }) return unwrapApiResponse(response) } export async function updatePlatformPartner( partnerId: number | string, payload: SavePlatformPartnerRequest, ) { const response = await http.put, ApiEnvelope>(`${basePath}/${partnerId}`, payload, { params: { tenantId: platformResourceScope }, }) return unwrapApiResponse(response) } export async function changePlatformPartnerStatus( partnerId: number | string, status: PlatformPartnerStatus, ) { const response = await http.patch, ApiEnvelope>(`${basePath}/${partnerId}/status`, { status }, { params: { tenantId: platformResourceScope }, }) return unwrapApiResponse(response) }