164 lines
4.7 KiB
TypeScript
164 lines
4.7 KiB
TypeScript
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
|
|
|
|
export type PlatformPartnerType = 'CUSTOMER' | 'SUPPLIER' | 'CARRIER' | 'CUSTOMS_BROKER' | 'SHIPPER' | 'CONSIGNEE'
|
|
export type PlatformPartnerLegacyType = PlatformPartnerType | 'BOTH'
|
|
export type PlatformPartnerStatus = 'ACTIVE' | 'INACTIVE'
|
|
|
|
export interface PlatformPartnerResource {
|
|
id: number | string
|
|
tenantId: number | string
|
|
dataScopeId: number | string
|
|
partnerCode: string
|
|
partnerName: string
|
|
partnerType: PlatformPartnerLegacyType
|
|
types: PlatformPartnerType[]
|
|
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<PlatformPartnerPage, 'page' | 'size' | 'total' | 'pages'> & {
|
|
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[]
|
|
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 {
|
|
const legacyTypes: PlatformPartnerType[] = resource.partnerType === 'BOTH'
|
|
? ['CUSTOMER', 'SUPPLIER']
|
|
: [resource.partnerType]
|
|
return {
|
|
...resource,
|
|
dataScopeId: resource.dataScopeId ?? resource.tenantId,
|
|
types: resource.types?.length ? resource.types : legacyTypes,
|
|
remark: resource.remark ?? null,
|
|
}
|
|
}
|
|
|
|
export async function listPlatformPartners(query: PlatformPartnerQuery) {
|
|
const response = await http.get<ApiEnvelope<PlatformPartnerPagePayload>, ApiEnvelope<PlatformPartnerPagePayload>>(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<PlatformPartnerResource>, ApiEnvelope<PlatformPartnerResource>>(
|
|
`${basePath}/${partnerId}`,
|
|
{ params: { tenantId: platformResourceScope } },
|
|
)
|
|
return normalizePartner(unwrapApiResponse(response))
|
|
}
|
|
|
|
export async function downloadPlatformPartnerImportTemplate() {
|
|
return http.get<Blob, Blob>(`${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<PlatformPartnerImportResult>, ApiEnvelope<PlatformPartnerImportResult>>(
|
|
`${basePath}/import`,
|
|
form,
|
|
{ params: { tenantId: platformResourceScope } },
|
|
)
|
|
return unwrapApiResponse(response)
|
|
}
|
|
|
|
export async function createPlatformPartner(payload: SavePlatformPartnerRequest) {
|
|
const response = await http.post<ApiEnvelope<number | string>, ApiEnvelope<number | string>>(basePath, payload, {
|
|
params: { tenantId: platformResourceScope },
|
|
})
|
|
return unwrapApiResponse(response)
|
|
}
|
|
|
|
export async function updatePlatformPartner(
|
|
partnerId: number | string,
|
|
payload: SavePlatformPartnerRequest,
|
|
) {
|
|
const response = await http.put<ApiEnvelope<null>, ApiEnvelope<null>>(`${basePath}/${partnerId}`, payload, {
|
|
params: { tenantId: platformResourceScope },
|
|
})
|
|
return unwrapApiResponse(response)
|
|
}
|
|
|
|
export async function changePlatformPartnerStatus(
|
|
partnerId: number | string,
|
|
status: PlatformPartnerStatus,
|
|
) {
|
|
const response = await http.patch<ApiEnvelope<null>, ApiEnvelope<null>>(`${basePath}/${partnerId}/status`, { status }, {
|
|
params: { tenantId: platformResourceScope },
|
|
})
|
|
return unwrapApiResponse(response)
|
|
}
|