Skip to content

Commit ed26a0e

Browse files
author
Tim Anema
authored
Merge pull request #701 from kickbooster/add-batch-discount-code
Add support for Discount Code batch endpoints
2 parents 484be2b + b7233a1 commit ed26a0e

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
module ShopifyAPI
3+
class DiscountCodeBatch < Base
4+
init_prefix :price_rule
5+
6+
self.collection_name = 'batch'
7+
8+
def price_rule_id
9+
@prefix_options[:price_rule_id]
10+
end
11+
12+
def discount_code_job
13+
@discount_codes ||= begin
14+
if id
15+
path = self.class.api_version.construct_api_path("price_rules/#{price_rule_id}/batch/#{id}/discount_codes.json")
16+
discount_codes = ShopifyAPI::DiscountCode.find :all, from: path
17+
discount_codes.each do |code|
18+
errors = code.attributes['errors']
19+
errors.attributes.each do |key, values|
20+
values.each { |message| code.errors.add(key, message) }
21+
end
22+
end
23+
discount_codes
24+
end
25+
end
26+
end
27+
28+
def encode(options = {})
29+
send("to_#{self.class.format.extension}", options)
30+
end
31+
end
32+
end

test/discount_code_batch_test.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
require 'test_helper'
3+
4+
class DiscountCodeBatchTest < Test::Unit::TestCase
5+
def setup
6+
super
7+
fake 'price_rules/102586120/batch/989355119', body: load_fixture('discount_code_batch')
8+
end
9+
10+
def test_get_batch
11+
batch = ShopifyAPI::DiscountCodeBatch.find 989355119, params: { price_rule_id: 102586120 }
12+
13+
assert_equal 989355119, batch.id
14+
end
15+
16+
def test_get_batch_discount_codes
17+
fake 'price_rules/102586120/batch/989355119/discount_codes',
18+
method: :get,
19+
status: 200,
20+
body: load_fixture('discount_code_batch_discount_codes')
21+
22+
batch = ShopifyAPI::DiscountCodeBatch.find 989355119, params: { price_rule_id: 102586120 }
23+
discount_code_job = batch.discount_code_job
24+
25+
assert_equal 3, discount_code_job.count
26+
assert discount_code_job[2].errors.present?
27+
end
28+
29+
def test_create_batch
30+
fake 'price_rules/102586120/batch', method: :post, status: 201, body: load_fixture('discount_code_batch')
31+
batch = ShopifyAPI::DiscountCodeBatch.new
32+
batch.prefix_options[:price_rule_id] = 102586120
33+
discount_codes = [{ "code": "SUMMER1" }, { "code": "SUMMER2" }, { "code": "SUMMER3" }]
34+
batch.discount_codes = discount_codes
35+
batch.save
36+
37+
expected_body = { discount_codes: discount_codes }.to_json
38+
assert_equal expected_body, WebMock.last_request.body
39+
end
40+
end
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"discount_code_creation": {
3+
"id": 989355119,
4+
"price_rule_id": 102586120,
5+
"started_at": null,
6+
"completed_at": null,
7+
"created_at": "2018-07-05T13:04:29-04:00",
8+
"updated_at": "2018-07-05T13:04:29-04:00",
9+
"status": "queued",
10+
"codes_count": 3,
11+
"imported_count": 0,
12+
"failed_count": 0
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"discount_codes": [
3+
{
4+
"id": null,
5+
"code": "SUMMER1",
6+
"errors": {}
7+
},
8+
{
9+
"id": null,
10+
"code": "SUMMER2",
11+
"errors": {}
12+
},
13+
{
14+
"id": null,
15+
"code": "SUMMER2",
16+
"errors": {
17+
"code": ["must be unique"]
18+
}
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)