diff --git a/.travis.yml b/.travis.yml index 00a8e9a..9667f4b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,9 +12,9 @@ notifications: on_success: always on_failure: always node_js: + - "10" - "9" - "8" - - "7" before_install: - sudo apt-get update - sudo apt-get install nodejs diff --git a/CHANGELOG.md b/CHANGELOG.md index 533581b..5ccc55c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +[OST Javascript SDK v2.2.3](https://github.com/ostdotcom/ost-sdk-js/tree/v2.2.3) +--- + +* Added redemptions module to call user redemptions management OST APIs. +* Added redeemable SKUs module to call redeemable SKUs OST APIs. + [OST Javascript SDK v2.2.2](https://github.com/ostdotcom/ost-sdk-js/tree/v2.2.2) --- diff --git a/README.md b/README.md index 7d44ea7..3ccd359 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ The preferred way to install the OST JavaScript SDK is to use the npm package ma .then(function(res) { console.log(JSON.stringify(res)); }) .catch(function(err) { console.log(JSON.stringify(err)); }); ``` - + ### Devices Module * Initialize Devices service object to perform device specific actions. @@ -737,3 +737,95 @@ For executing transactions, you need to understand the 4 modules described below let resp = webhooksService.verifySignature(version, stringifiedData,requestTimestamp, signature, webhookSecret); console.log(resp); ``` + +### Redemption Modules + +Two modules of redemption, "Redeemable SKUs" and "User Redemptions", are described below. + +#### Redeemable SKUs Module + +* Initialize Redeemable SKUs service object to perform redeemable skus specific actions. + + ```js + const redeemableSkusService = ostObj.services.redeemable_skus; + ``` +* Get Redeemable SKU detail using the redeemable sku id. + + ```js + // Mandatory API parameters + + // Fetch details of following redeemable sku. + let redeemableSkuId = 'c2c__'; + + redeemableSkusService.get({ redeemable_sku_id: redeemableSkuId }) + .then(function(res) { console.log(JSON.stringify(res)); }) + .catch(function(err) { console.log(JSON.stringify(err)); }); + ``` + +* Get Redeemable SKUs List. Pagination is supported by this API. + + ```js + // Mandatory API parameters + // NOTE: No mandatory parameters. + + // Optional API parameters + + // Limit. + let limit = 10; + + // Array of redeemable SKU ids. + let redeemableSkuIds = ['1001', '1002']; + + // Pagination identifier from the previous API call response. Not needed for page one. + let paginationIdentifier = 'e77y___'; + + redeemableSkusService.getList({limit: limit, pagination_identifier: paginationIdentifier, redeemable_sku_ids: redeemableSkuIds }) + .then(function(res) { console.log(JSON.stringify(res)); }) + .catch(function(err) { console.log(JSON.stringify(err)); }); + ``` + +#### User Redemptions Module + +* Initialize Redemptions service object to perform user redemption specific actions. + + ```js + const redemptionsService = ostObj.services.redemptions; + ``` + +* Get User redemption details using the userId and redemptionId. + + ```js + // Mandatory API parameters + + // UserId of user for whom redemption details needs to be fetched. + let userId = 'c2c__'; + + // Unique identifier of the redemption of user. + let redemptionId = 'c2c__'; + + redemptionsService.get({ user_id: userId, redemption_id: redemptionId }) + .then(function(res) { console.log(JSON.stringify(res)); }) + .catch(function(err) { console.log(JSON.stringify(err)); }); + ``` + +* Get User Redemptions List. Pagination is supported by this API. + + ```js + // Mandatory API parameters + let userId = 'c2c__'; + + // Optional API parameters + + // Limit. + let limit = 10; + + // Array of user redemption uuids. + let redemptionIds = ['a743___', 'a743___']; + + // Pagination identifier from the previous API call response. Not needed for page one. + let paginationIdentifier = 'e77y___'; + + redemptionsService.getList({ user_id: userId, limit: limit, pagination_identifier: paginationIdentifier, redemption_ids: redemptionIds }) + .then(function(res) { console.log(JSON.stringify(res)); }) + .catch(function(err) { console.log(JSON.stringify(err)); }); + ``` diff --git a/VERSION b/VERSION index b1b25a5..5859406 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.2 +2.2.3 diff --git a/lib/validate.js b/lib/validate.js index 81e58ab..03dea57 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -71,6 +71,40 @@ ValidateKlass.prototype = { } }, + /** + * Get redeemable sku id from params. + * + * @param {object} params + * @param {string} params.redeemable_sku_id + */ + getRedeemableSkuId: function (params) { + const oThis = this; + if (oThis.isValid(params.redeemable_sku_id)) { + var redeemableSkuId = params.redeemable_sku_id; + delete params.redeemable_sku_id; + return redeemableSkuId + } else { + throw new Error('redeemable_sku_id missing or invalid in request params'); + } + }, + + /** + * Get redemption uid from params. + * + * @param {object} params + * @param {string} params.redemption_id + */ + getRedemptionId: function (params) { + const oThis = this; + if (oThis.isValid(params.redemption_id)) { + let redemption_id = params.redemption_id; + delete params.redemption_id; + return redemption_id + } else { + throw new Error('redemption_id missing or invalid in request params.'); + } + }, + /** * Get webhook id from params. @@ -170,4 +204,4 @@ ValidateKlass.prototype = { }; -module.exports = new ValidateKlass(); \ No newline at end of file +module.exports = new ValidateKlass(); diff --git a/package.json b/package.json index 20f6872..18edad3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ostdotcom/ost-sdk-js", - "version": "2.2.2", + "version": "2.2.3", "description": "OST Platform SDK for JavaScript.", "main": "index.js", "scripts": { diff --git a/services/manifest.js b/services/manifest.js index 168384e..2ed0930 100644 --- a/services/manifest.js +++ b/services/manifest.js @@ -21,6 +21,8 @@ const rootPrefix = ".." , transactionsKlass = require(rootPrefix + '/services/transactions') , baseTokensKlass = require(rootPrefix + '/services/base_tokens') , webhooksKlass = require(rootPrefix + '/services/webhooks') + , redemptionsKlass = require(rootPrefix + '/services/redemptions') + , redeemableSkusKlass = require(rootPrefix + '/services/redeemable_skus') ; // hide request object @@ -51,6 +53,8 @@ const manifest = function (params) { oThis.transactions = new transactionsKlass(_requestObj); oThis.base_tokens = new baseTokensKlass(_requestObj); oThis.webhooks = new webhooksKlass(_requestObj); + oThis.redemptions = new redemptionsKlass(_requestObj); + oThis.redeemable_skus = new redeemableSkusKlass(_requestObj); return oThis; }; @@ -81,7 +85,11 @@ manifest.prototype = { base_tokens: null, - webhooks: null + webhooks: null, + + redemptions: null, + + redeemable_skus: null, }; -module.exports = manifest; \ No newline at end of file +module.exports = manifest; diff --git a/services/redeemable_skus.js b/services/redeemable_skus.js new file mode 100644 index 0000000..9d35bbe --- /dev/null +++ b/services/redeemable_skus.js @@ -0,0 +1,66 @@ +"use strict"; + +/** + * Redeemable SKUs Service + * + * @module services/redeemable_skus + */ + +const rootPrefix = ".." + , validate = require(rootPrefix + '/lib/validate') +; + +// hide request object +var _requestObj = null; + +/** + * Redeemable SKUs Service constructor. + * + * @constructor + */ +const redeemable_skus = function (requestObj) { + const oThis = this; + + // Assign request object. + _requestObj = requestObj; + + // Define the url prefix. + oThis.urlPrefix = '/redeemable-skus'; + + return oThis; +}; + +redeemable_skus.prototype = { + /** + * Get redeemable skus list. + * + * @param {object} params + * + * @returns {*} + */ + getList: function(params) { + const oThis = this; + + params = params || {}; + + return _requestObj.get(oThis.urlPrefix, params); + }, + + /** + * Get redeemable sku by redeemable sku id. + * + * @param {object} params + * + * @returns {*} + */ + get: function(params) { + const oThis = this; + + params = params || {}; + + return _requestObj.get(oThis.urlPrefix + "/" + validate.getRedeemableSkuId(params), params); + } + +}; + +module.exports = redeemable_skus; diff --git a/services/redemptions.js b/services/redemptions.js new file mode 100644 index 0000000..61ea8c3 --- /dev/null +++ b/services/redemptions.js @@ -0,0 +1,67 @@ +"use strict"; + +/** + * Redemptions Service + * + * @module services/redemptions + */ + +const rootPrefix = ".." + , validate = require(rootPrefix + '/lib/validate') +; + +// hide request object +var _requestObj = null; + +/** + * Redemptions Service constructor + * + * @constructor + */ +const redemptions = function (requestObj) { + const oThis = this; + + // Assign request object + _requestObj = requestObj; + + // Define the url prefix + oThis.urlPrefix = '/users'; + oThis.redemptionsUrlPrefix = '/redemptions'; + + return oThis; +}; + +redemptions.prototype = { + /** + * Get user redemptions list. + * + * @param {object} params + * + * @returns {*} + */ + getList: function(params) { + const oThis = this; + + params = params || {}; + + return _requestObj.get(oThis.urlPrefix + "/" + validate.getUserId(params) + oThis.redemptionsUrlPrefix, params); + }, + + /** + * Get user redemption by redemption id. + * + * @param {object} params + * + * @returns {*} + */ + get: function(params) { + const oThis = this; + + params = params || {}; + + return _requestObj.get(oThis.urlPrefix + "/" + validate.getUserId(params) + oThis.redemptionsUrlPrefix + '/' + validate.getRedemptionId(params), params); + } + +}; + +module.exports = redemptions; diff --git a/services/users.js b/services/users.js index 71cb6ae..179d1cc 100644 --- a/services/users.js +++ b/services/users.js @@ -26,6 +26,7 @@ const users = function (requestObj) { // Define the url prefix oThis.urlPrefix = '/users'; + oThis.redemptionsUrlPrefix = '/redemptions'; return oThis; }; @@ -72,7 +73,6 @@ users.prototype = { return _requestObj.get(oThis.urlPrefix + "/" + validate.getUserId(params), params); } - }; module.exports = users; diff --git a/test/index.js b/test/index.js index 927e7a3..9ea1911 100644 --- a/test/index.js +++ b/test/index.js @@ -34,6 +34,8 @@ const rootPrefix = "..", transactionsService = ostObj.services.transactions, baseTokensService = ostObj.services.base_tokens, webhooksService = ostObj.services.webhooks, + redemptionsService = ostObj.services.redemptions, + redeemableSkusService = ostObj.services.redeemable_skus, userId = process.env.OST_KIT_USER_ID, @@ -44,7 +46,9 @@ const rootPrefix = "..", sessionAddrs = process.env.OST_KIT_SESSION_ADDRESS, ruleAddress = process.env.OST_KIT_RULE_ADDRESS, user2TokenHolderAddress = process.env.OST_KIT_USER2_TOKEN_HOLDER_ADDRESS, - recoveryOwnerAddrs = process.env.OST_KIT_RECOVERY_OWNER_ADDRESS + recoveryOwnerAddress = process.env.OST_KIT_RECOVERY_OWNER_ADDRESS, + redemptionId = process.env.OST_KIT_REDEMPTION_ID, + redeemableSkuId = process.env.OST_KIT_REDEEMABLE_SKU_ID ; let webhookId = null; @@ -227,7 +231,7 @@ function getRecoveryOwnerAddress() { it("test get recovery owners", async function () { let res = await recoveryOwnersService.get({ user_id: userId, - recovery_owner_address: recoveryOwnerAddrs //"0x4e9314f004026F89Fc52790c3357b2D34FBA93b0" + recovery_owner_address: recoveryOwnerAddress //"0x4e9314f004026F89Fc52790c3357b2D34FBA93b0" }).catch(function (err) { console.log(JSON.stringify(err)); assert.fail('get recovery owners'); @@ -373,6 +377,56 @@ function deleteWebhook() { }); } + +function getRedemption() { + it("Test get user redemption", async function () { + let res = await redemptionsService.get({ + user_id: userId, + redemption_id: redemptionId + }).catch(function (err) { + console.log(JSON.stringify(err)); + assert.fail('Get user redemption.'); + }); + assert.equal(res.success, true); + }); +} + + +function listRedemptions() { + it("Test list user redemptions", async function () { + let res = await redemptionsService.getList({ + user_id: userId + }).catch(function (err) { + console.log(JSON.stringify(err)); + assert.fail('List user redemptions.'); + }); + assert.equal(res.success, true); + }); +} + +function getRedeemableSku() { + it("Test get redeemable SKU by id", async function () { + let res = await redeemableSkusService.get({ + redeemable_sku_id: redeemableSkuId + }).catch(function (err) { + console.log(JSON.stringify(err)); + assert.fail('Get redeemable SKU.'); + }); + assert.equal(res.success, true); + }); +} + +function listRedeemableSkus() { + it("Test list redeemable SKUs", async function () { + let res = await redeemableSkusService.getList({}).catch(function (err) { + console.log(JSON.stringify(err)); + assert.fail('List redeemable SKUs.'); + }); + assert.equal(res.success, true); + }); +} + + function testWebhookRequestSignature() { it ("Tests webhook request signature.", async function () { let version = "2", @@ -444,6 +498,10 @@ async function testcases() { await updateWebhook(); await deleteWebhook(); testWebhookRequestSignature(); + getRedemption(); + listRedemptions(); + getRedeemableSku(); + listRedeemableSkus() } testcases();