UNPKG

channelape-sdk

Version:
92 lines 4.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Q = require("q"); const Version_1 = require("../../model/Version"); const Resource_1 = require("../../model/Resource"); const RestService_1 = require("../../service/RestService"); const GenerateApiError_1 = require("../../utils/GenerateApiError"); const InventoryItemQuantitiesService_1 = require("../quantities/InventoryItemQuantitiesService"); class InventoriesService extends RestService_1.default { constructor(client, locationsService) { super(); this.client = client; this.locationsService = locationsService; this.EXPECTED_GET_OR_UPDATE_STATUS = 200; this.EXPECTED_POST_STATUS = 201; this.inventoryQuantitiesService = new InventoryItemQuantitiesService_1.default(client, this, this.locationsService); } quantities() { return this.inventoryQuantitiesService; } get(inventoryItemIdOrBusinessId, sku) { if (sku) { return this.retrieveInventoryItemByBusinessAndSku(sku, inventoryItemIdOrBusinessId); } return this.retrieveInventoryItemById(inventoryItemIdOrBusinessId); } create(inventoryItemCreationRequest) { const deferred = Q.defer(); const requestUrl = `/${Version_1.default.V1}${Resource_1.default.INVENTORIES}`; const options = { data: inventoryItemCreationRequest }; this.client.post(requestUrl, options, (error, response, body) => this.mapResponseToPromise(requestUrl, deferred, error, response, body, this.EXPECTED_POST_STATUS)); return deferred.promise; } update(inventoryItemUpdateRequest) { const deferred = Q.defer(); const requestUrl = `/${Version_1.default.V1}${Resource_1.default.INVENTORIES}/${inventoryItemUpdateRequest.id}`; const options = { data: inventoryItemUpdateRequest }; this.client.put(requestUrl, options, (error, response, body) => this.mapResponseToPromise(requestUrl, deferred, error, response, body, this.EXPECTED_GET_OR_UPDATE_STATUS)); return deferred.promise; } retrieveInventoryItemById(inventoryItemIdOrBusinessId) { const deferred = Q.defer(); const requestUrl = `/${Version_1.default.V1}${Resource_1.default.INVENTORIES}/${inventoryItemIdOrBusinessId}`; this.client.get(requestUrl, {}, (error, response, body) => this.mapResponseToPromise(requestUrl, deferred, error, response, body, this.EXPECTED_GET_OR_UPDATE_STATUS)); return deferred.promise; } retrieveInventoryItemByBusinessAndSku(sku, inventoryItemIdOrBusinessId) { return new Promise((resolve) => { const requestUrl = `/${Version_1.default.V1}${Resource_1.default.INVENTORIES}`; const options = { params: { sku, businessId: inventoryItemIdOrBusinessId } }; this.client.get(requestUrl, options, (error, response, body) => { const requestResponse = { error, response, body }; resolve(this.mapInventoryItemsPromise(requestUrl, requestResponse, this.EXPECTED_GET_OR_UPDATE_STATUS)); }); }); } mapInventoryItemsPromise(requestUrl, requestCallbackParams, expectedStatusCode) { return new Promise((resolve, reject) => { if (requestCallbackParams.error) { reject(requestCallbackParams.error); } else if (requestCallbackParams.response.status === expectedStatusCode) { const data = requestCallbackParams.body; resolve(data.inventoryItems.map(inventoryItem => this.formatInventoryItem(inventoryItem))); } else { const channelApeErrorResponse = (0, GenerateApiError_1.default)(requestUrl, requestCallbackParams.response, requestCallbackParams.body, this.EXPECTED_GET_OR_UPDATE_STATUS); reject(channelApeErrorResponse); } }); } formatInventoryItem(inventoryItem) { inventoryItem.createdAt = new Date(inventoryItem.createdAt); inventoryItem.updatedAt = new Date(inventoryItem.updatedAt); return inventoryItem; } } exports.default = InventoriesService; //# sourceMappingURL=InventoriesService.js.map