From fe64e5769a8b6b2f0eaa573a29898b73ba4e1923 Mon Sep 17 00:00:00 2001 From: Rostyk <34774987+rostyk-kanafotskyy@users.noreply.github.com> Date: Tue, 22 Oct 2024 20:48:45 +0300 Subject: [PATCH] feat: added Update endpoint for MLI, interface updates (#35) BREAKING CHANGE: --- src/endpoints/multi-location-inventories.js | 19 +++- src/types/multi-location-inventories.d.ts | 105 ++++++++++++++------ 2 files changed, 89 insertions(+), 35 deletions(-) diff --git a/src/endpoints/multi-location-inventories.js b/src/endpoints/multi-location-inventories.js index 1d1a152..dac233d 100644 --- a/src/endpoints/multi-location-inventories.js +++ b/src/endpoints/multi-location-inventories.js @@ -30,19 +30,28 @@ class MultiLocationInventories { ) } - Get(productId) { - return this.request.send(`${this.endpoint}/${productId}`, 'GET') + Get(inventoryId) { + return this.request.send(`${this.endpoint}/${inventoryId}`, 'GET') } - Create(body) { + Create(body, productId) { return this.request.send(`${this.endpoint}}`, 'POST', { type: 'stock', + id: productId, attributes: body }) } - Delete(productId) { - return this.request.send(`${this.endpoint}/${productId}`, 'DELETE') + Update(inventoryId, body) { + return this.request.send(`${this.endpoint}/${inventoryId}`, 'PUT', { + type: 'stock', + id: inventoryId, + attributes: body + }) + } + + Delete(inventoryId) { + return this.request.send(`${this.endpoint}/${inventoryId}`, 'DELETE') } Limit(value) { diff --git a/src/types/multi-location-inventories.d.ts b/src/types/multi-location-inventories.d.ts index 93cc21d..8569a36 100644 --- a/src/types/multi-location-inventories.d.ts +++ b/src/types/multi-location-inventories.d.ts @@ -4,50 +4,81 @@ import { LocationsEndpoint } from './mli-locations' import { Identifiable, Resource, ResourcePage } from './core' -type StockType = 'stock' +/** + * Multi Location Inventory Types + */ +export type InventoryResourceType = 'stock' -export interface StockMeta { - timestamps: { - created_at: string - updated_at: string - } +/** + * Base Types + */ +export interface Timestamps { + created_at: string + updated_at: string +} + +/** + * Location-specific inventory quantities + */ +export interface LocationQuantities { + available: number + allocated: number + total: number +} + +/** + * Create Operation Types + */ +export interface LocationCreateQuantity { + available: number } -export interface StockBaseLocations { - [key: string]: { - available: number +export interface StockCreate { + available?: number + locations?: { + [locationId: string]: LocationCreateQuantity } } -export interface StockBaseAttributes { - product_id: string - locations: StockBaseLocations +/** + * Update Operation Types + */ +export interface LocationUpdateQuantity { + allocated?: number + available?: number } -export interface StockResponseLocations { - [key: string]: { - available: number - allocated: number - total: number +export interface StockUpdate { + locations?: { + [locationId: string]: LocationUpdateQuantity | null } } -export interface StockResponseAttributes extends StockBaseAttributes { + +/** + * Response Types + */ +export interface StockLocationsMap { + [locationId: string]: LocationQuantities +} + +export interface StockAttributes { allocated: number + available: number total: number - locations: StockResponseLocations + locations: StockLocationsMap } -export interface StockResponse extends Identifiable, StockMeta { - type: StockType - attributes: StockResponseAttributes +export interface StockResponse extends Identifiable { + type: InventoryResourceType + attributes: StockAttributes + timestamps: Timestamps } /** - * Multi Location Inventories Endpoints + * Multi Location Inventories Endpoint Interface */ export interface MultiLocationInventoriesEndpoint { endpoint: 'inventory' - Locations: LocationsEndpoint /** @@ -57,21 +88,35 @@ export interface MultiLocationInventoriesEndpoint { /** * Get Stock for Product - * @param productId The ID of the product. + * @param inventoryId - The inventory identifier */ - Get(productId: string): Promise> + Get(inventoryId: string): Promise> /** * Create Stock for Product - * @param body - The base attributes of the inventory stock. + * @param payload - Initial stock information with overall availability or per-location quantities + * @param productId - Optional product identifier + */ + Create( + payload: StockCreate, + productId?: string + ): Promise> + + /** + * Update Stock for Product + * @param inventoryId - The inventory identifier + * @param payload - Location-specific updates. Set location to null to remove it */ - Create(body: StockBaseAttributes): Promise> + Update( + inventoryId: string, + payload: StockUpdate + ): Promise> /** * Delete Stock for Product - * @param productId The ID of the product. + * @param inventoryId - The inventory identifier */ - Delete(productId: string): Promise<{}> + Delete(inventoryId: string): Promise<{}> Limit(value: number): MultiLocationInventoriesEndpoint