From 01f796c0daba149347793dcd6260fb93bf4fccd9 Mon Sep 17 00:00:00 2001 From: Rostyk Date: Sat, 21 Sep 2024 15:59:17 +0300 Subject: [PATCH 1/2] feat: multi location inventories endpoints --- src/endpoints/mli-locations.js | 56 ++++++++++++++ src/endpoints/multi-location-inventories.js | 57 ++++++++++++++ src/index.d.ts | 4 + src/index.js | 2 + src/types/mli-locations.d.ts | 83 +++++++++++++++++++++ src/types/multi-location-inventories.d.ts | 79 ++++++++++++++++++++ 6 files changed, 281 insertions(+) create mode 100644 src/endpoints/mli-locations.js create mode 100644 src/endpoints/multi-location-inventories.js create mode 100644 src/types/mli-locations.d.ts create mode 100644 src/types/multi-location-inventories.d.ts diff --git a/src/endpoints/mli-locations.js b/src/endpoints/mli-locations.js new file mode 100644 index 0000000..f988508 --- /dev/null +++ b/src/endpoints/mli-locations.js @@ -0,0 +1,56 @@ +import { buildURL } from '../utils/helpers' +import RequestFactory from '../factories/request' + +class InventoryLocationsEndpoint { + constructor(endpoint) { + this.request = new RequestFactory(endpoint) + + this.endpoint = 'inventories/locations' + } + + All() { + const { filter, limit, offset } = this + return this.request.send( + buildURL(this.endpoint, { + filter, + limit, + offset + }), + 'GET' + ) + } + + Get(locationId) { + return this.request.send(`${this.endpoint}/${locationId}`, 'GET') + } + + Create(body) { + return this.request.send(this.endpoint, 'POST', { + type: 'inventory_location', + attributes: body + }) + } + + Update(locationId, body) { + return this.request.send(`${this.endpoint}/${locationId}`, 'PUT', { + type: 'inventory_location', + attributes: body + }) + } + + Delete(locationId) { + return this.request.send(`${this.endpoint}/${locationId}`, 'DELETE') + } + + Limit(value) { + this.limit = value + return this + } + + Offset(value) { + this.offset = value + return this + } +} + +export default InventoryLocationsEndpoint diff --git a/src/endpoints/multi-location-inventories.js b/src/endpoints/multi-location-inventories.js new file mode 100644 index 0000000..9489114 --- /dev/null +++ b/src/endpoints/multi-location-inventories.js @@ -0,0 +1,57 @@ +import RequestFactory from '../factories/request' +import { buildURL } from '../utils/helpers' +import InventoryLocationsEndpoint from './mli-locations' + +class MultiLocationInventories { + constructor(endpoint) { + const config = { ...endpoint } + config.headers = { + ...config.headers, + 'ep-inventories-multi-location': true + } + this.request = new RequestFactory(endpoint) + + this.Locations = new InventoryLocationsEndpoint(config) + + this.endpoint = 'inventories' + } + + All() { + const { filter, limit, offset } = this + return this.request.send( + buildURL(this.endpoint, { + filter, + limit, + offset + }), + 'GET' + ) + } + + Get(productId) { + return this.request.send(`${this.endpoint}/${productId}`, 'GET') + } + + Create(body) { + return this.request.send(`${this.endpoint}}`, 'POST', { + type: 'stock', + attributes: body + }) + } + + Delete(productId) { + return this.request.send(`${this.endpoint}/${productId}`, 'DELETE') + } + + Limit(value) { + this.limit = value + return this + } + + Offset(value) { + this.offset = value + return this + } +} + +export default MultiLocationInventories diff --git a/src/index.d.ts b/src/index.d.ts index aa8d82f..6370d82 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -69,6 +69,7 @@ import { SubscriptionDunningRulesEndpoint } from './types/subscription-dunning-r import { SubscriptionProrationPoliciesEndpoint } from './types/subscription-proration-policies' import { SubscriptionInvoicesEndpoint } from './types/subscription-invoices' import { CustomRelationshipsEndpoint } from './types/custom-relationships' +import { MultiLocationInventoriesEndpoint } from './types/multi-location-inventories' export * from './types/config' export * from './types/storage' @@ -149,6 +150,8 @@ export * from './types/subscription-proration-policies' export * from './types/subscription-invoices' export * from './types/custom-relationships' export * from './types/pcm-custom-relationship' +export * from './types/mli-locations' +export * from './types/multi-location-inventories' // UMD export as namespace elasticpath @@ -219,6 +222,7 @@ export class ElasticPath { SubscriptionProrationPolicies: SubscriptionProrationPoliciesEndpoint SubscriptionInvoices: SubscriptionInvoicesEndpoint CustomRelationships: CustomRelationshipsEndpoint + MultiLocationInventories: MultiLocationInventoriesEndpoint Cart(id?: string): CartEndpoint // This optional cart id is super worrying when using the SDK in a node server :/ constructor(config: Config) diff --git a/src/index.js b/src/index.js index 163acbd..ee47e6f 100644 --- a/src/index.js +++ b/src/index.js @@ -60,6 +60,7 @@ import SubscriptionDunningRulesEndpoint from './endpoints/subscription-dunning-r import SubscriptionProrationPoliciesEndpoint from './endpoints/subscription-proration-policies' import SubscriptionInvoicesEndpoint from './endpoints/subscription-invoices' import CustomRelationshipsEndpoint from './endpoints/custom-relationships' +import MultiLocationInventoriesEndpoint from './endpoints/multi-location-inventories' import { cartIdentifier, @@ -150,6 +151,7 @@ export default class ElasticPath { new SubscriptionProrationPoliciesEndpoint(config) this.SubscriptionInvoices = new SubscriptionInvoicesEndpoint(config) this.CustomRelationships = new CustomRelationshipsEndpoint(config) + this.MultiLocationInventories = new MultiLocationInventoriesEndpoint(config) } // Expose `Cart` class on ElasticPath class diff --git a/src/types/mli-locations.d.ts b/src/types/mli-locations.d.ts new file mode 100644 index 0000000..a4bf5c2 --- /dev/null +++ b/src/types/mli-locations.d.ts @@ -0,0 +1,83 @@ +/** + * Inventory Locations + */ +import { Identifiable, Resource, ResourcePage } from './core' + +type InventoryLocationType = 'inventory_location' + +export interface GeolocationDetails { + lat: number + lon: number +} +export interface LocationAttributes { + name: string + external_ref?: string + description?: string + address: string[] + geolocation: GeolocationDetails +} +export interface LocationMeta { + timestamps: { + created_at: string + updated_at: string + } +} + +/** + * Core Location Base Interface + */ +export interface LocationBase { + type: InventoryLocationType + attributes: LocationAttributes + meta: LocationMeta +} + +export interface Location extends Identifiable, LocationBase {} + +export interface CreateLocationBody extends LocationAttributes {} + +export interface UpdateLocationBody extends Identifiable, LocationAttributes {} + +/** + * Location Endpoints + */ +export interface LocationsEndpoint { + endpoint: 'inventory/locations' + + /** + * List All Locations + */ + All(): Promise> + + /** + * Get Location + * @param inventoryId - The inventoryId of the Location. + */ + Get(inventoryId: string): Promise> + + /** + * Create Location + * @param body - The base attributes of the Locations + */ + Create(body: CreateLocationBody): Promise> + + /** + * Update Location + * @param inventoryId - The inventoryId of the Location. + * @param body - The base attributes of the Locations. + */ + Update( + inventoryId: string, + body: UpdateLocationBody + ): Promise> + + /** + * Delete Location + * @param inventoryId - The inventoryId of the Location. + */ + Delete(inventoryId: string): Promise<{}> + + Limit(value: number): LocationsEndpoint + + Offset(value: number): LocationsEndpoint +} diff --git a/src/types/multi-location-inventories.d.ts b/src/types/multi-location-inventories.d.ts new file mode 100644 index 0000000..93cc21d --- /dev/null +++ b/src/types/multi-location-inventories.d.ts @@ -0,0 +1,79 @@ +/** + * Multi Location Inventory + */ +import { LocationsEndpoint } from './mli-locations' +import { Identifiable, Resource, ResourcePage } from './core' + +type StockType = 'stock' + +export interface StockMeta { + timestamps: { + created_at: string + updated_at: string + } +} + +export interface StockBaseLocations { + [key: string]: { + available: number + } +} + +export interface StockBaseAttributes { + product_id: string + locations: StockBaseLocations +} + +export interface StockResponseLocations { + [key: string]: { + available: number + allocated: number + total: number + } +} +export interface StockResponseAttributes extends StockBaseAttributes { + allocated: number + total: number + locations: StockResponseLocations +} + +export interface StockResponse extends Identifiable, StockMeta { + type: StockType + attributes: StockResponseAttributes +} + +/** + * Multi Location Inventories Endpoints + */ +export interface MultiLocationInventoriesEndpoint { + endpoint: 'inventory' + + Locations: LocationsEndpoint + + /** + * Get Stock for all Products + */ + All(): Promise> + + /** + * Get Stock for Product + * @param productId The ID of the product. + */ + Get(productId: string): Promise> + + /** + * Create Stock for Product + * @param body - The base attributes of the inventory stock. + */ + Create(body: StockBaseAttributes): Promise> + + /** + * Delete Stock for Product + * @param productId The ID of the product. + */ + Delete(productId: string): Promise<{}> + + Limit(value: number): MultiLocationInventoriesEndpoint + + Offset(value: number): MultiLocationInventoriesEndpoint +} From bbaca8ebff18d504109439a04db03d59727ef554 Mon Sep 17 00:00:00 2001 From: Rostyk Date: Mon, 23 Sep 2024 12:03:32 +0300 Subject: [PATCH 2/2] fix: type names --- src/types/mli-locations.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/types/mli-locations.d.ts b/src/types/mli-locations.d.ts index a4bf5c2..23f1987 100644 --- a/src/types/mli-locations.d.ts +++ b/src/types/mli-locations.d.ts @@ -51,9 +51,9 @@ export interface LocationsEndpoint { /** * Get Location - * @param inventoryId - The inventoryId of the Location. + * @param locationId - The ID of the Location. */ - Get(inventoryId: string): Promise> + Get(locationId: string): Promise> /** * Create Location @@ -63,19 +63,19 @@ export interface LocationsEndpoint { /** * Update Location - * @param inventoryId - The inventoryId of the Location. + * @param locationId - The ID of the Location. * @param body - The base attributes of the Locations. */ Update( - inventoryId: string, + locationId: string, body: UpdateLocationBody ): Promise> /** * Delete Location - * @param inventoryId - The inventoryId of the Location. + * @param locationId - The ID of the Location. */ - Delete(inventoryId: string): Promise<{}> + Delete(locationId: string): Promise<{}> Limit(value: number): LocationsEndpoint