Skip to content

Commit 4aa0f53

Browse files
feat: Multi-location inventories endpoints (#20)
1 parent 603ee40 commit 4aa0f53

File tree

6 files changed

+281
-0
lines changed

6 files changed

+281
-0
lines changed

src/endpoints/mli-locations.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { buildURL } from '../utils/helpers'
2+
import RequestFactory from '../factories/request'
3+
4+
class InventoryLocationsEndpoint {
5+
constructor(endpoint) {
6+
this.request = new RequestFactory(endpoint)
7+
8+
this.endpoint = 'inventories/locations'
9+
}
10+
11+
All() {
12+
const { filter, limit, offset } = this
13+
return this.request.send(
14+
buildURL(this.endpoint, {
15+
filter,
16+
limit,
17+
offset
18+
}),
19+
'GET'
20+
)
21+
}
22+
23+
Get(locationId) {
24+
return this.request.send(`${this.endpoint}/${locationId}`, 'GET')
25+
}
26+
27+
Create(body) {
28+
return this.request.send(this.endpoint, 'POST', {
29+
type: 'inventory_location',
30+
attributes: body
31+
})
32+
}
33+
34+
Update(locationId, body) {
35+
return this.request.send(`${this.endpoint}/${locationId}`, 'PUT', {
36+
type: 'inventory_location',
37+
attributes: body
38+
})
39+
}
40+
41+
Delete(locationId) {
42+
return this.request.send(`${this.endpoint}/${locationId}`, 'DELETE')
43+
}
44+
45+
Limit(value) {
46+
this.limit = value
47+
return this
48+
}
49+
50+
Offset(value) {
51+
this.offset = value
52+
return this
53+
}
54+
}
55+
56+
export default InventoryLocationsEndpoint
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import RequestFactory from '../factories/request'
2+
import { buildURL } from '../utils/helpers'
3+
import InventoryLocationsEndpoint from './mli-locations'
4+
5+
class MultiLocationInventories {
6+
constructor(endpoint) {
7+
const config = { ...endpoint }
8+
config.headers = {
9+
...config.headers,
10+
'ep-inventories-multi-location': true
11+
}
12+
this.request = new RequestFactory(endpoint)
13+
14+
this.Locations = new InventoryLocationsEndpoint(config)
15+
16+
this.endpoint = 'inventories'
17+
}
18+
19+
All() {
20+
const { filter, limit, offset } = this
21+
return this.request.send(
22+
buildURL(this.endpoint, {
23+
filter,
24+
limit,
25+
offset
26+
}),
27+
'GET'
28+
)
29+
}
30+
31+
Get(productId) {
32+
return this.request.send(`${this.endpoint}/${productId}`, 'GET')
33+
}
34+
35+
Create(body) {
36+
return this.request.send(`${this.endpoint}}`, 'POST', {
37+
type: 'stock',
38+
attributes: body
39+
})
40+
}
41+
42+
Delete(productId) {
43+
return this.request.send(`${this.endpoint}/${productId}`, 'DELETE')
44+
}
45+
46+
Limit(value) {
47+
this.limit = value
48+
return this
49+
}
50+
51+
Offset(value) {
52+
this.offset = value
53+
return this
54+
}
55+
}
56+
57+
export default MultiLocationInventories

src/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { SubscriptionDunningRulesEndpoint } from './types/subscription-dunning-r
6969
import { SubscriptionProrationPoliciesEndpoint } from './types/subscription-proration-policies'
7070
import { SubscriptionInvoicesEndpoint } from './types/subscription-invoices'
7171
import { CustomRelationshipsEndpoint } from './types/custom-relationships'
72+
import { MultiLocationInventoriesEndpoint } from './types/multi-location-inventories'
7273

7374
export * from './types/config'
7475
export * from './types/storage'
@@ -149,6 +150,8 @@ export * from './types/subscription-proration-policies'
149150
export * from './types/subscription-invoices'
150151
export * from './types/custom-relationships'
151152
export * from './types/pcm-custom-relationship'
153+
export * from './types/mli-locations'
154+
export * from './types/multi-location-inventories'
152155

153156
// UMD
154157
export as namespace elasticpath
@@ -219,6 +222,7 @@ export class ElasticPath {
219222
SubscriptionProrationPolicies: SubscriptionProrationPoliciesEndpoint
220223
SubscriptionInvoices: SubscriptionInvoicesEndpoint
221224
CustomRelationships: CustomRelationshipsEndpoint
225+
MultiLocationInventories: MultiLocationInventoriesEndpoint
222226

223227
Cart(id?: string): CartEndpoint // This optional cart id is super worrying when using the SDK in a node server :/
224228
constructor(config: Config)

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import SubscriptionDunningRulesEndpoint from './endpoints/subscription-dunning-r
6060
import SubscriptionProrationPoliciesEndpoint from './endpoints/subscription-proration-policies'
6161
import SubscriptionInvoicesEndpoint from './endpoints/subscription-invoices'
6262
import CustomRelationshipsEndpoint from './endpoints/custom-relationships'
63+
import MultiLocationInventoriesEndpoint from './endpoints/multi-location-inventories'
6364

6465
import {
6566
cartIdentifier,
@@ -150,6 +151,7 @@ export default class ElasticPath {
150151
new SubscriptionProrationPoliciesEndpoint(config)
151152
this.SubscriptionInvoices = new SubscriptionInvoicesEndpoint(config)
152153
this.CustomRelationships = new CustomRelationshipsEndpoint(config)
154+
this.MultiLocationInventories = new MultiLocationInventoriesEndpoint(config)
153155
}
154156

155157
// Expose `Cart` class on ElasticPath class

src/types/mli-locations.d.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Inventory Locations
3+
*/
4+
import { Identifiable, Resource, ResourcePage } from './core'
5+
6+
type InventoryLocationType = 'inventory_location'
7+
8+
export interface GeolocationDetails {
9+
lat: number
10+
lon: number
11+
}
12+
export interface LocationAttributes {
13+
name: string
14+
external_ref?: string
15+
description?: string
16+
address: string[]
17+
geolocation: GeolocationDetails
18+
}
19+
export interface LocationMeta {
20+
timestamps: {
21+
created_at: string
22+
updated_at: string
23+
}
24+
}
25+
26+
/**
27+
* Core Location Base Interface
28+
*/
29+
export interface LocationBase {
30+
type: InventoryLocationType
31+
attributes: LocationAttributes
32+
meta: LocationMeta
33+
}
34+
35+
export interface Location extends Identifiable, LocationBase {}
36+
37+
export interface CreateLocationBody extends LocationAttributes {}
38+
39+
export interface UpdateLocationBody extends Identifiable, LocationAttributes {}
40+
41+
/**
42+
* Location Endpoints
43+
*/
44+
export interface LocationsEndpoint {
45+
endpoint: 'inventory/locations'
46+
47+
/**
48+
* List All Locations
49+
*/
50+
All(): Promise<ResourcePage<Location>>
51+
52+
/**
53+
* Get Location
54+
* @param locationId - The ID of the Location.
55+
*/
56+
Get(locationId: string): Promise<Resource<Location>>
57+
58+
/**
59+
* Create Location
60+
* @param body - The base attributes of the Locations
61+
*/
62+
Create(body: CreateLocationBody): Promise<Resource<Location>>
63+
64+
/**
65+
* Update Location
66+
* @param locationId - The ID of the Location.
67+
* @param body - The base attributes of the Locations.
68+
*/
69+
Update(
70+
locationId: string,
71+
body: UpdateLocationBody
72+
): Promise<Resource<Location>>
73+
74+
/**
75+
* Delete Location
76+
* @param locationId - The ID of the Location.
77+
*/
78+
Delete(locationId: string): Promise<{}>
79+
80+
Limit(value: number): LocationsEndpoint
81+
82+
Offset(value: number): LocationsEndpoint
83+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Multi Location Inventory
3+
*/
4+
import { LocationsEndpoint } from './mli-locations'
5+
import { Identifiable, Resource, ResourcePage } from './core'
6+
7+
type StockType = 'stock'
8+
9+
export interface StockMeta {
10+
timestamps: {
11+
created_at: string
12+
updated_at: string
13+
}
14+
}
15+
16+
export interface StockBaseLocations {
17+
[key: string]: {
18+
available: number
19+
}
20+
}
21+
22+
export interface StockBaseAttributes {
23+
product_id: string
24+
locations: StockBaseLocations
25+
}
26+
27+
export interface StockResponseLocations {
28+
[key: string]: {
29+
available: number
30+
allocated: number
31+
total: number
32+
}
33+
}
34+
export interface StockResponseAttributes extends StockBaseAttributes {
35+
allocated: number
36+
total: number
37+
locations: StockResponseLocations
38+
}
39+
40+
export interface StockResponse extends Identifiable, StockMeta {
41+
type: StockType
42+
attributes: StockResponseAttributes
43+
}
44+
45+
/**
46+
* Multi Location Inventories Endpoints
47+
*/
48+
export interface MultiLocationInventoriesEndpoint {
49+
endpoint: 'inventory'
50+
51+
Locations: LocationsEndpoint
52+
53+
/**
54+
* Get Stock for all Products
55+
*/
56+
All(): Promise<ResourcePage<StockResponse>>
57+
58+
/**
59+
* Get Stock for Product
60+
* @param productId The ID of the product.
61+
*/
62+
Get(productId: string): Promise<Resource<StockResponse>>
63+
64+
/**
65+
* Create Stock for Product
66+
* @param body - The base attributes of the inventory stock.
67+
*/
68+
Create(body: StockBaseAttributes): Promise<Resource<StockResponse>>
69+
70+
/**
71+
* Delete Stock for Product
72+
* @param productId The ID of the product.
73+
*/
74+
Delete(productId: string): Promise<{}>
75+
76+
Limit(value: number): MultiLocationInventoriesEndpoint
77+
78+
Offset(value: number): MultiLocationInventoriesEndpoint
79+
}

0 commit comments

Comments
 (0)