Skip to content

Commit

Permalink
[api] Add DiscountCodeCreationJob resource
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Nov 13, 2019
1 parent 83dd2d1 commit a654ed6
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ parameters needed to fetch the next and previous page of results.
- `list(priceRuleId)`
- `lookup(params)`
- `update(priceRuleId, id, params)`
- discountCodeCreationJob
- `discountCodes(priceRuleId, discountCodeCreationJobId)`
- `create(priceRuleId, params)`
- `get(priceRuleId, discountCodeCreationJobId)`
- dispute
- `get(id)`
- `list([params])`
Expand Down
59 changes: 59 additions & 0 deletions resources/discount-code-creation-job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const assign = require('lodash/assign');
const pick = require('lodash/pick');

const baseChild = require('../mixins/base-child');

/**
* Creates an DiscountCodeCreation instance.
*
* @param {Shopify} shopify Reference to the Shopify instance
* @constructor
* @public
*/
function DiscountCodeCreationJob(shopify) {
this.shopify = shopify;

this.parentName = 'price_rules';
this.name = 'batch';
this.key = 'discount_code_creation';
}

assign(DiscountCodeCreationJob.prototype, pick(baseChild, ['buildUrl', 'get']));

/**
* Creates a discount code creation job.
*
* @param {Number} priceRuleId Price rule ID
* @param {Array} params Array of discount codes
* @return {Promise} Promise that resolves with the result
* @public
*/
DiscountCodeCreationJob.prototype.create = function create(
priceRuleId,
params
) {
const url = this.buildUrl(priceRuleId);
return this.shopify.request(url, 'POST', undefined, {
discount_codes: params
}).then((data) => data[this.key]);
};

/**
* Retrieves a list of discount codes for a discount code creation job.
*
* @param {Number} priceRuleId Price rule ID
* @param {Number} batchId Discount code creation job ID
* @return {Promise} Promise that resolves with the result
* @public
*/
DiscountCodeCreationJob.prototype.discountCodes = function discountCodes(
priceRuleId,
batchId
) {
const url = this.buildUrl(priceRuleId, `${batchId}/discount_codes`);
return this.shopify.request(url, 'GET', 'discount_codes');
};

module.exports = DiscountCodeCreationJob;
1 change: 1 addition & 0 deletions resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const map = {
customerAddress: 'customer-address',
customerSavedSearch: 'customer-saved-search',
discountCode: 'discount-code',
discountCodeCreationJob: 'discount-code-creation-job',
dispute: 'dispute',
draftOrder: 'draft-order',
event: 'event',
Expand Down
54 changes: 54 additions & 0 deletions test/discount-code-creation-job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
describe('Shopify#discountCodeCreationJob', () => {
'use strict';

const expect = require('chai').expect;

const fixtures = require('./fixtures/discount-code-creation-job');
const common = require('./common');

const shopify = common.shopify;
const scope = common.scope;

afterEach(() => expect(scope.isDone()).to.be.true);

it('creates a new discount code creation job', () => {
const input = fixtures.req.create;
const output = fixtures.res.create;

scope
.post('/admin/price_rules/507328175/batch.json', input)
.reply(201, output);

return shopify.discountCodeCreationJob
.create(507328175, input.discount_codes)
.then((data) =>
expect(data).to.deep.equal(output.discount_code_creation)
);
});

it('gets a single discount code creation job by its ID', () => {
const output = fixtures.res.get;

scope
.get('/admin/price_rules/507328175/batch/173232803.json')
.reply(200, output);

return shopify.discountCodeCreationJob
.get(507328175, 173232803)
.then((data) =>
expect(data).to.deep.equal(output.discount_code_creation)
);
});

it('gets a list of discount codes for a discount code creation job', () => {
const output = fixtures.res.discountCodes;

scope
.get('/admin/price_rules/507328175/batch/173232803/discount_codes.json')
.reply(200, output);

return shopify.discountCodeCreationJob
.discountCodes(507328175, 173232803)
.then((data) => expect(data).to.deep.equal(output.discount_codes));
});
});
4 changes: 4 additions & 0 deletions test/fixtures/discount-code-creation-job/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

exports.req = require('./req');
exports.res = require('./res');
13 changes: 13 additions & 0 deletions test/fixtures/discount-code-creation-job/req/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"discount_codes": [
{
"code": "SUMMER1"
},
{
"code": "SUMMER2"
},
{
"code": "SUMMER3"
}
]
}
3 changes: 3 additions & 0 deletions test/fixtures/discount-code-creation-job/req/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.create = require('./create');
14 changes: 14 additions & 0 deletions test/fixtures/discount-code-creation-job/res/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"discount_code_creation": {
"id": 989355120,
"price_rule_id": 507328175,
"started_at": null,
"completed_at": null,
"created_at": "2019-10-16T16:18:10-04:00",
"updated_at": "2019-10-16T16:18:10-04:00",
"status": "queued",
"codes_count": 3,
"imported_count": 0,
"failed_count": 0
}
}
19 changes: 19 additions & 0 deletions test/fixtures/discount-code-creation-job/res/discountCodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"discount_codes": [
{
"id": null,
"code": "foo",
"errors": {}
},
{
"id": null,
"code": "",
"errors": {}
},
{
"id": null,
"code": "bar",
"errors": {}
}
]
}
14 changes: 14 additions & 0 deletions test/fixtures/discount-code-creation-job/res/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"discount_code_creation": {
"id": 173232803,
"price_rule_id": 507328175,
"started_at": null,
"completed_at": null,
"created_at": "2019-10-16T16:17:53-04:00",
"updated_at": "2019-10-16T16:17:53-04:00",
"status": "queued",
"codes_count": 3,
"imported_count": 0,
"failed_count": 0
}
}
5 changes: 5 additions & 0 deletions test/fixtures/discount-code-creation-job/res/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.discountCodes = require('./discountCodes');
exports.create = require('./create');
exports.get = require('./get');

0 comments on commit a654ed6

Please sign in to comment.