import {z} from 'zod' export type TradeSettingsRequest = {vatType?: number | undefined, description?: string | undefined, chartOfAccountId?: number | undefined}; export type ProductMasterRequestServiceUpdate = {id?: number | undefined, type: number, name: string, code?: (string | null) | undefined, categoryName?: (string | null) | undefined, sellSettings?: Array | undefined, productLists: Array<{id: number, unitName?: string | undefined, sellPrice?: number | undefined, buyPrice?: number | undefined, barcode?: (string | null) | undefined, isMainProduct?: boolean | undefined}>}; export type NonInventorySettingsRequest = {productNonInventoryType?: number | undefined}; export type ProductMasterRequestNonStockUpdate = {id?: number | undefined, type: number, name: string, code?: (string | null) | undefined, categoryName?: (string | null) | undefined, sellSettings?: Array | undefined, buySettings?: Array | undefined, nonInventorySettings?: Array | undefined, productLists: Array<{id: number, unitName?: string | undefined, sellPrice?: number | undefined, buyPrice?: number | undefined, barcode?: (string | null) | undefined, isMainProduct?: boolean | undefined}>}; export type InventorySettingsRequest = {inventoryPublishedOn?: (string | null) | undefined, inventoryQuantity?: number | undefined, inventoryPrice?: number | undefined}; export type ProductMasterRequestStockSingleUnitUpdate = {id?: number | undefined, type: number, name: string, code?: (string | null) | undefined, categoryName?: (string | null) | undefined, sellSettings?: Array | undefined, buySettings?: Array | undefined, inventorySettings?: Array | undefined, productLists: Array<{id: number, unitName?: string | undefined, sellPrice?: number | undefined, buyPrice?: number | undefined, barcode?: (string | null) | undefined, isMainProduct?: boolean | undefined}>}; export type ProductMasterRequestStockMultipleUnitUpdate = {id?: number | undefined, type: number, name: string, code?: (string | null) | undefined, categoryName?: (string | null) | undefined, sellSettings?: Array | undefined, buySettings?: Array | undefined, inventorySettings?: Array | undefined, productLists: Array<{id: number, unitName?: string | undefined, sellPrice?: number | undefined, buyPrice?: number | undefined, barcode?: (string | null) | undefined, isMainProduct?: boolean | undefined, convertRatio: number}>}; export type ProductMasterTypeUpdate = ProductMasterRequestServiceUpdate | ProductMasterRequestNonStockUpdate | ProductMasterRequestStockSingleUnitUpdate | ProductMasterRequestStockMultipleUnitUpdate; export type UpdateApiProductMastersIdArgs = {Authorization: string, id: string, body: ProductMasterTypeUpdate}; export type UpdateApiProductMastersIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tradeSettingsResponse = z.object({vatType: z.number().int().optional(), description: z.string().nullable().optional(), chartOfAccountId: z.number().int().nullable().optional(), chartOfAccountCode: z.string().nullable().optional(), chartOfAccountName: z.string().nullable().optional()}); export const nonInventorySettingsResponse = z.object({productNonInventoryType: z.number().int().nullable().optional()}); export const inventorySettingsResponse = z.object({inventoryPublishedOn: z.string().nullable().optional(), averageSellPrice: z.number().nullable().optional(), averageBuyPrice: z.number().nullable().optional(), remainingStock: z.number().nullable().optional(), totalValueInHand: z.number().nullable().optional(), warehouseId: z.number().int().nullable().optional()}); export const productListResponse = z.object({id: z.number().int().optional(), unitId: z.number().int().nullable().optional(), unitName: z.string().nullable().optional(), isMainProduct: z.boolean().optional(), sellPrice: z.number().nullable().optional(), sellPriceWithVat: z.number().nullable().optional(), buyPrice: z.number().nullable().optional(), buyPriceWithVat: z.number().nullable().optional(), barcode: z.string().nullable().optional(), convertRatio: z.number().optional(), hasMovement: z.boolean().optional()}); export const dataProductMasterResponse = z.object({id: z.number().int().optional(), type: z.number().int().optional(), name: z.string().optional(), code: z.string().nullable().optional(), categoryId: z.number().int().nullable().optional(), categoryName: z.string().nullable().optional(), sellSettings: z.array(tradeSettingsResponse).optional(), buySettings: z.array(tradeSettingsResponse).optional(), nonInventorySettings: z.array(nonInventorySettingsResponse).optional(), inventorySettings: z.array(inventorySettingsResponse).optional(), mainProductId: z.number().int().optional(), mainUnitId: z.number().int().nullable().optional(), mainUnitName: z.string().nullable().optional(), productLists: z.array(productListResponse).optional()}); export const productMasterResponse = z.object({status: z.boolean().optional(), message: z.string().optional(), code: z.number().int().optional(), data: z.array(dataProductMasterResponse).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Update Product Master * * PUT /product-masters/{id} */ export const updateApiProductMastersId = async (args: UpdateApiProductMastersIdArgs, options?: UpdateApiProductMastersIdOptions) => { const {baseUrl = "https://openapi.flowaccount.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/product-masters/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(productMasterResponse, await res.json()) };