-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] Add DiscountCodeCreationJob resource
- Loading branch information
Showing
11 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
|
||
exports.req = require('./req'); | ||
exports.res = require('./res'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"discount_codes": [ | ||
{ | ||
"code": "SUMMER1" | ||
}, | ||
{ | ||
"code": "SUMMER2" | ||
}, | ||
{ | ||
"code": "SUMMER3" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
exports.create = require('./create'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
test/fixtures/discount-code-creation-job/res/discountCodes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |