Skip to content

Commit 344f0d6

Browse files
committed
feat(quotes): camelCase billing_items, loosen payload
1 parent 4da72d6 commit 344f0d6

10 files changed

Lines changed: 186 additions & 176 deletions

File tree

app/services/quote_versions/validators/one_off/business_validator.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,26 @@ def validate_add_on_existence(add_on, index)
5555

5656
def validate_datetimes(add_on, index)
5757
%w[payload overrides].each do |section|
58-
from = add_on.dig(section, "from_datetime")
59-
to = add_on.dig(section, "to_datetime")
58+
from = add_on.dig(section, "fromDatetime")
59+
to = add_on.dig(section, "toDatetime")
6060
next if from.nil? && to.nil?
6161

6262
if from.nil?
63-
add_error(field: add_on_field(index, "#{section}.from_datetime"), error_code: "value_is_mandatory")
63+
add_error(field: add_on_field(index, "#{section}.fromDatetime"), error_code: "value_is_mandatory")
6464
elsif to.nil?
65-
add_error(field: add_on_field(index, "#{section}.to_datetime"), error_code: "value_is_mandatory")
65+
add_error(field: add_on_field(index, "#{section}.toDatetime"), error_code: "value_is_mandatory")
6666
elsif Time.zone.parse(from) > Time.zone.parse(to)
67-
add_error(field: add_on_field(index, "#{section}.from_datetime"), error_code: "invalid_date_range")
67+
add_error(field: add_on_field(index, "#{section}.fromDatetime"), error_code: "invalid_date_range")
6868
end
6969
end
7070
end
7171

7272
def add_ons
73-
billing_items["add_ons"] || []
73+
billing_items["addOns"] || []
7474
end
7575

7676
def add_on_field(index, suffix)
77-
:"billing_items.add_ons.#{index}.#{suffix}"
77+
:"billing_items.addOns.#{index}.#{suffix}"
7878
end
7979

8080
def known_add_on_ids

app/services/quote_versions/validators/one_off/schema.rb

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,27 @@ module Schema
99
"additionalProperties" => {"not" => {}, "x-error" => "unsupported_key"},
1010
"x-error" => {"type" => "invalid_type", "required" => "value_is_mandatory"},
1111
"properties" => {
12-
"add_ons" => {
12+
"addOns" => {
1313
"type" => "array",
1414
"x-error" => {"type" => "invalid_type", "minItems" => "invalid_count"},
1515
"items" => {
1616
"type" => "object",
1717
"additionalProperties" => {"not" => {}, "x-error" => "unsupported_key"},
1818
"x-error" => {"type" => "invalid_type", "required" => "value_is_mandatory"},
19-
"required" => %w[id local_id],
19+
"required" => %w[id localId payload],
2020
"properties" => {
2121
"id" => {
2222
"type" => "string",
2323
"format" => "uuid",
2424
"x-error" => {"type" => "invalid_type", "format" => "invalid_format"}
2525
},
26-
"local_id" => {
26+
"localId" => {
2727
"type" => "string",
2828
"minLength" => 1,
2929
"x-error" => {"type" => "invalid_type", "minLength" => "invalid_value"}
3030
},
3131
"payload" => {
3232
"type" => "object",
33-
"additionalProperties" => {"not" => {}, "x-error" => "unsupported_key"},
3433
"x-error" => {"type" => "invalid_type", "required" => "value_is_mandatory"},
3534
"properties" => {
3635
"code" => {
@@ -43,26 +42,22 @@ module Schema
4342
"exclusiveMinimum" => 0,
4443
"x-error" => {"type" => "invalid_type", "exclusiveMinimum" => "invalid_value"}
4544
},
46-
"unit_amount_cents" => {
45+
"unitAmountCents" => {
4746
"type" => "integer",
4847
"minimum" => 0,
4948
"x-error" => {"type" => "invalid_type", "minimum" => "invalid_value"}
5049
},
51-
"total_amount_cents" => {
50+
"totalAmountCents" => {
5251
"type" => "integer",
5352
"minimum" => 0,
5453
"x-error" => {"type" => "invalid_type", "minimum" => "invalid_value"}
5554
},
56-
"invoice_display_name" => {
57-
"type" => %w[string null],
58-
"x-error" => {"type" => "invalid_type"}
59-
},
60-
"from_datetime" => {
55+
"fromDatetime" => {
6156
"type" => %w[string null],
6257
"format" => "date-time",
6358
"x-error" => {"type" => "invalid_type", "format" => "invalid_format"}
6459
},
65-
"to_datetime" => {
60+
"toDatetime" => {
6661
"type" => %w[string null],
6762
"format" => "date-time",
6863
"x-error" => {"type" => "invalid_type", "format" => "invalid_format"}
@@ -84,27 +79,27 @@ module Schema
8479
"exclusiveMinimum" => 0,
8580
"x-error" => {"type" => "invalid_type", "exclusiveMinimum" => "invalid_value"}
8681
},
87-
"unit_amount_cents" => {
82+
"unitAmountCents" => {
8883
"type" => "integer",
8984
"minimum" => 0,
9085
"x-error" => {"type" => "invalid_type", "minimum" => "invalid_value"}
9186
},
92-
"total_amount_cents" => {
87+
"totalAmountCents" => {
9388
"type" => "integer",
9489
"minimum" => 0,
9590
"x-error" => {"type" => "invalid_type", "minimum" => "invalid_value"}
9691
},
97-
"invoice_display_name" => {
92+
"invoiceDisplayName" => {
9893
"type" => "string",
9994
"minLength" => 1,
10095
"x-error" => {"type" => "invalid_type", "minLength" => "invalid_value"}
10196
},
102-
"from_datetime" => {
97+
"fromDatetime" => {
10398
"type" => %w[string null],
10499
"format" => "date-time",
105100
"x-error" => {"type" => "invalid_type", "format" => "invalid_format"}
106101
},
107-
"to_datetime" => {
102+
"toDatetime" => {
108103
"type" => %w[string null],
109104
"format" => "date-time",
110105
"x-error" => {"type" => "invalid_type", "format" => "invalid_format"}
@@ -118,13 +113,12 @@ module Schema
118113
}.freeze
119114

120115
APPROVE_DEFINITION = UPDATE_DEFINITION.deep_dup.tap do |schema|
121-
schema["required"] = ["add_ons"]
116+
schema["required"] = ["addOns"]
122117

123-
add_ons = schema["properties"]["add_ons"]
118+
add_ons = schema["properties"]["addOns"]
124119
add_ons["minItems"] = 1
125-
add_ons["items"]["required"] += ["payload"]
126120
add_ons["items"]["properties"]["payload"]["required"] =
127-
%w[code units unit_amount_cents total_amount_cents]
121+
%w[code units unitAmountCents totalAmountCents]
128122
end.freeze
129123

130124
SCHEMERS = {

spec/factories/quote_versions.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
currency { "EUR" }
2121
billing_items do
2222
{
23-
"add_ons" => [
23+
"addOns" => [
2424
{
2525
"id" => add_on.id,
26-
"local_id" => SecureRandom.uuid,
26+
"localId" => SecureRandom.uuid,
2727
"payload" => {
2828
"code" => add_on.code,
2929
"units" => 1,
30-
"unit_amount_cents" => 10_000,
31-
"total_amount_cents" => 10_000
30+
"unitAmountCents" => 10_000,
31+
"totalAmountCents" => 10_000
3232
}
3333
}
3434
]

spec/services/quote_versions/approve_service_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
it "does not approve the quote version" do
119119
expect(result).not_to be_success
120120
expect(result.error).to be_a(BaseService::ValidationFailure)
121-
expect(result.error.messages).to eq({"billing_items.add_ons": ["value_is_mandatory"]})
121+
expect(result.error.messages).to eq({"billing_items.addOns": ["value_is_mandatory"]})
122122

123123
expect(quote_version.reload.approved?).to eq(false)
124124
end

spec/services/quote_versions/create_service_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@
8989
let(:create_params) do
9090
{
9191
billing_items: {
92-
"add_ons" => [
92+
"addOns" => [
9393
{
9494
"id" => add_on.id,
95-
"local_id" => "3d08b2df-4e4c-4d58-b415-a525c1663735",
95+
"localId" => "3d08b2df-4e4c-4d58-b415-a525c1663735",
9696
"payload" => {
9797
"code" => add_on.code,
9898
"units" => 1,
99-
"unit_amount_cents" => 10_000,
100-
"total_amount_cents" => 10_000
99+
"unitAmountCents" => 10_000,
100+
"totalAmountCents" => 10_000
101101
}
102102
}
103103
]
@@ -113,13 +113,13 @@
113113

114114
context "when the payload is invalid" do
115115
let(:create_params) do
116-
{billing_items: {"add_ons" => [{"id" => "not-a-uuid", "local_id" => "l1"}]}, currency: "EUR"}
116+
{billing_items: {"addOns" => [{"id" => "not-a-uuid", "localId" => "l1", "payload" => {}}]}, currency: "EUR"}
117117
end
118118

119119
it "returns a validation failure and does not create the version" do
120120
expect(result).not_to be_success
121121
expect(result.error).to be_a(BaseService::ValidationFailure)
122-
expect(result.error.messages).to eq({"billing_items.add_ons.0.id": ["invalid_format"]})
122+
expect(result.error.messages).to eq({"billing_items.addOns.0.id": ["invalid_format"]})
123123
expect(quote.versions.count).to eq(0)
124124
end
125125
end

spec/services/quote_versions/update_service_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@
4545
let(:update_params) { {billing_items:, currency: "EUR"} }
4646
let(:billing_items) do
4747
{
48-
"add_ons" => [
48+
"addOns" => [
4949
{
5050
"id" => add_on.id,
51-
"local_id" => "3d08b2df-4e4c-4d58-b415-a525c1663735",
51+
"localId" => "3d08b2df-4e4c-4d58-b415-a525c1663735",
5252
"payload" => {
5353
"code" => add_on.code,
5454
"units" => 2,
55-
"unit_amount_cents" => 10_000,
56-
"total_amount_cents" => 20_000
55+
"unitAmountCents" => 10_000,
56+
"totalAmountCents" => 20_000
5757
}
5858
}
5959
]
@@ -67,16 +67,16 @@
6767

6868
context "when the payload is invalid" do
6969
let(:billing_items) do
70-
{"add_ons" => [{"id" => "not-a-uuid", "local_id" => "3d08b2df", "payload" => {"units" => 0}}]}
70+
{"addOns" => [{"id" => "not-a-uuid", "localId" => "3d08b2df", "payload" => {"units" => 0}}]}
7171
end
7272

7373
it "returns a validation failure and does not persist the changes" do
7474
expect(result).not_to be_success
7575
expect(result.error).to be_a(BaseService::ValidationFailure)
7676
expect(result.error.messages).to eq(
7777
{
78-
"billing_items.add_ons.0.id": ["invalid_format"],
79-
"billing_items.add_ons.0.payload.units": ["invalid_value"]
78+
"billing_items.addOns.0.id": ["invalid_format"],
79+
"billing_items.addOns.0.payload.units": ["invalid_value"]
8080
}
8181
)
8282

0 commit comments

Comments
 (0)