ledger_sync 1.0.10 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +36 -20
  3. data/README.md +105 -66
  4. data/ledger_sync.gemspec +1 -0
  5. data/lib/ledger_sync.rb +23 -4
  6. data/lib/ledger_sync/adaptors/operation.rb +28 -28
  7. data/lib/ledger_sync/adaptors/quickbooks_online/account/mapping.rb +325 -0
  8. data/lib/ledger_sync/adaptors/quickbooks_online/account/operations/create.rb +54 -0
  9. data/lib/ledger_sync/adaptors/quickbooks_online/account/operations/find.rb +38 -0
  10. data/lib/ledger_sync/adaptors/quickbooks_online/account/operations/update.rb +61 -0
  11. data/lib/ledger_sync/adaptors/quickbooks_online/account/searcher.rb +67 -0
  12. data/lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb +18 -16
  13. data/lib/ledger_sync/adaptors/quickbooks_online/bill/operations/create.rb +65 -0
  14. data/lib/ledger_sync/adaptors/quickbooks_online/bill/operations/find.rb +37 -0
  15. data/lib/ledger_sync/adaptors/quickbooks_online/bill/operations/update.rb +69 -0
  16. data/lib/ledger_sync/adaptors/quickbooks_online/bill/searcher.rb +28 -0
  17. data/lib/ledger_sync/adaptors/quickbooks_online/customer/operations/create.rb +6 -6
  18. data/lib/ledger_sync/adaptors/quickbooks_online/customer/operations/find.rb +1 -3
  19. data/lib/ledger_sync/adaptors/quickbooks_online/customer/operations/update.rb +6 -9
  20. data/lib/ledger_sync/adaptors/quickbooks_online/customer/searcher.rb +2 -38
  21. data/lib/ledger_sync/adaptors/quickbooks_online/deposit/operations/create.rb +61 -0
  22. data/lib/ledger_sync/adaptors/quickbooks_online/deposit/operations/find.rb +36 -0
  23. data/lib/ledger_sync/adaptors/quickbooks_online/deposit/operations/update.rb +65 -0
  24. data/lib/ledger_sync/adaptors/quickbooks_online/deposit/searcher.rb +28 -0
  25. data/lib/ledger_sync/adaptors/quickbooks_online/expense/mapping.rb +15 -0
  26. data/lib/ledger_sync/adaptors/quickbooks_online/expense/operations/create.rb +68 -0
  27. data/lib/ledger_sync/adaptors/quickbooks_online/expense/operations/find.rb +39 -0
  28. data/lib/ledger_sync/adaptors/quickbooks_online/expense/operations/update.rb +72 -0
  29. data/lib/ledger_sync/adaptors/quickbooks_online/expense/searcher.rb +28 -0
  30. data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/mapping.rb +14 -0
  31. data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/operations/create.rb +56 -0
  32. data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/operations/find.rb +34 -0
  33. data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/operations/update.rb +60 -0
  34. data/lib/ledger_sync/adaptors/quickbooks_online/journal_entry/searcher.rb +28 -0
  35. data/lib/ledger_sync/adaptors/quickbooks_online/operation.rb +30 -0
  36. data/lib/ledger_sync/adaptors/quickbooks_online/payment/operations/create.rb +8 -22
  37. data/lib/ledger_sync/adaptors/quickbooks_online/payment/operations/find.rb +0 -2
  38. data/lib/ledger_sync/adaptors/quickbooks_online/payment/operations/update.rb +6 -22
  39. data/lib/ledger_sync/adaptors/quickbooks_online/searcher.rb +45 -0
  40. data/lib/ledger_sync/adaptors/quickbooks_online/transfer/operations/create.rb +52 -0
  41. data/lib/ledger_sync/adaptors/quickbooks_online/transfer/operations/find.rb +36 -0
  42. data/lib/ledger_sync/adaptors/quickbooks_online/transfer/operations/update.rb +57 -0
  43. data/lib/ledger_sync/adaptors/quickbooks_online/vendor/operations/create.rb +46 -0
  44. data/lib/ledger_sync/adaptors/quickbooks_online/vendor/operations/find.rb +36 -0
  45. data/lib/ledger_sync/adaptors/quickbooks_online/vendor/operations/update.rb +51 -0
  46. data/lib/ledger_sync/adaptors/quickbooks_online/vendor/searcher.rb +64 -0
  47. data/lib/ledger_sync/adaptors/searcher.rb +3 -1
  48. data/lib/ledger_sync/adaptors/test/account/operations/create.rb +53 -0
  49. data/lib/ledger_sync/adaptors/test/account/operations/find.rb +38 -0
  50. data/lib/ledger_sync/adaptors/test/account/operations/invalid.rb +25 -0
  51. data/lib/ledger_sync/adaptors/test/account/operations/update.rb +50 -0
  52. data/lib/ledger_sync/adaptors/test/account/operations/valid.rb +31 -0
  53. data/lib/ledger_sync/adaptors/test/account/searcher.rb +40 -0
  54. data/lib/ledger_sync/adaptors/test/adaptor.rb +4 -4
  55. data/lib/ledger_sync/adaptors/test/customer/operations/create.rb +4 -6
  56. data/lib/ledger_sync/adaptors/test/customer/operations/find.rb +0 -2
  57. data/lib/ledger_sync/adaptors/test/customer/operations/update.rb +4 -6
  58. data/lib/ledger_sync/adaptors/test/customer/searcher.rb +2 -2
  59. data/lib/ledger_sync/adaptors/test/expense/operations/create.rb +54 -0
  60. data/lib/ledger_sync/adaptors/test/expense/operations/find.rb +39 -0
  61. data/lib/ledger_sync/adaptors/test/expense/operations/update.rb +57 -0
  62. data/lib/ledger_sync/adaptors/test/payment/operations/create.rb +4 -20
  63. data/lib/ledger_sync/adaptors/test/payment/operations/find.rb +0 -2
  64. data/lib/ledger_sync/adaptors/test/payment/operations/update.rb +4 -20
  65. data/lib/ledger_sync/adaptors/test/transfer/operations/create.rb +45 -0
  66. data/lib/ledger_sync/adaptors/test/transfer/operations/find.rb +36 -0
  67. data/lib/ledger_sync/adaptors/test/transfer/operations/update.rb +48 -0
  68. data/lib/ledger_sync/adaptors/test/vendor/operations/create.rb +47 -0
  69. data/lib/ledger_sync/adaptors/test/vendor/operations/find.rb +34 -0
  70. data/lib/ledger_sync/adaptors/test/vendor/operations/invalid.rb +21 -0
  71. data/lib/ledger_sync/adaptors/test/vendor/operations/update.rb +44 -0
  72. data/lib/ledger_sync/adaptors/test/vendor/operations/valid.rb +27 -0
  73. data/lib/ledger_sync/adaptors/test/vendor/searcher.rb +41 -0
  74. data/lib/ledger_sync/error/resource_errors.rb +24 -0
  75. data/lib/ledger_sync/resource.rb +24 -64
  76. data/lib/ledger_sync/resource_attribute.rb +50 -0
  77. data/lib/ledger_sync/resource_attribute/dirty_mixin.rb +49 -0
  78. data/lib/ledger_sync/resource_attribute/mixin.rb +90 -0
  79. data/lib/ledger_sync/resource_attribute/reference.rb +9 -0
  80. data/lib/ledger_sync/resource_attribute/reference/many.rb +34 -0
  81. data/lib/ledger_sync/resource_attribute/reference/one.rb +33 -0
  82. data/lib/ledger_sync/resource_attribute_set.rb +58 -0
  83. data/lib/ledger_sync/resources/account.rb +14 -0
  84. data/lib/ledger_sync/resources/bill.rb +17 -0
  85. data/lib/ledger_sync/resources/bill_line_item.rb +11 -0
  86. data/lib/ledger_sync/resources/customer.rb +5 -4
  87. data/lib/ledger_sync/resources/deposit.rb +16 -0
  88. data/lib/ledger_sync/resources/deposit_line_item.rb +11 -0
  89. data/lib/ledger_sync/resources/expense.rb +19 -0
  90. data/lib/ledger_sync/resources/expense_line_item.rb +11 -0
  91. data/lib/ledger_sync/resources/journal_entry.rb +13 -0
  92. data/lib/ledger_sync/resources/journal_entry_line_item.rb +12 -0
  93. data/lib/ledger_sync/resources/payment.rb +5 -3
  94. data/lib/ledger_sync/resources/transfer.rb +15 -0
  95. data/lib/ledger_sync/resources/vendor.rb +12 -0
  96. data/lib/ledger_sync/result.rb +0 -24
  97. data/lib/ledger_sync/type/boolean.rb +17 -0
  98. data/lib/ledger_sync/type/date.rb +17 -0
  99. data/lib/ledger_sync/type/float.rb +17 -0
  100. data/lib/ledger_sync/type/integer.rb +17 -0
  101. data/lib/ledger_sync/type/reference_many.rb +27 -0
  102. data/lib/ledger_sync/type/reference_one.rb +26 -0
  103. data/lib/ledger_sync/type/string.rb +17 -0
  104. data/lib/ledger_sync/type/value.rb +12 -0
  105. data/lib/ledger_sync/type/value_mixin.rb +19 -0
  106. data/lib/ledger_sync/util/hash_helpers.rb +16 -1
  107. data/lib/ledger_sync/util/resources_builder.rb +36 -9
  108. data/lib/ledger_sync/version.rb +1 -1
  109. metadata +92 -7
  110. data/lib/ledger_sync/adaptors/quickbooks_online/customer/operations/upsert.rb +0 -42
  111. data/lib/ledger_sync/adaptors/quickbooks_online/payment/operations/upsert.rb +0 -53
  112. data/lib/ledger_sync/adaptors/test/customer/operations/upsert.rb +0 -42
  113. data/lib/ledger_sync/adaptors/test/payment/operations/upsert.rb +0 -53
  114. data/lib/ledger_sync/sync.rb +0 -108
  115. data/lib/ledger_sync/util/coordinator.rb +0 -72
@@ -1,42 +0,0 @@
1
- module LedgerSync
2
- module Adaptors
3
- module QuickBooksOnline
4
- module Customer
5
- module Operations
6
- class Upsert < Operation::Upsert
7
- class Contract < LedgerSync::Adaptors::Contract
8
- params do
9
- optional(:ledger_id).maybe(:string)
10
- optional(:email).maybe(:string)
11
- required(:name).filled(:string)
12
- optional(:phone_number).maybe(:string)
13
- end
14
- end
15
-
16
- private
17
-
18
- def build
19
- op = if qbo_customer?
20
- Update.new(adaptor: adaptor, resource: resource)
21
- else
22
- Create.new(adaptor: adaptor, resource: resource)
23
- end
24
- add_root_operation(op)
25
- end
26
-
27
- def find_result
28
- @find_result ||= Find.new(
29
- adaptor: adaptor,
30
- resource: resource
31
- ).perform
32
- end
33
-
34
- def qbo_customer?
35
- find_result.success?
36
- end
37
- end
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,53 +0,0 @@
1
- module LedgerSync
2
- module Adaptors
3
- module QuickBooksOnline
4
- module Payment
5
- module Operations
6
- class Upsert < Operation::Upsert
7
- class Contract < LedgerSync::Adaptors::Contract
8
- schema do
9
- required(:ledger_id).maybe(:string)
10
- required(:amount).filled(:integer)
11
- required(:currency).filled(:string)
12
- required(:customer).hash(Types::Reference)
13
- end
14
- end
15
-
16
- private
17
-
18
- def build
19
- op = if qbo_payment?
20
- Update.new(adaptor: adaptor, resource: resource)
21
- else
22
- Create.new(adaptor: adaptor, resource: resource)
23
- end
24
-
25
- build_customer_operation
26
- add_root_operation(op)
27
- end
28
-
29
- def build_customer_operation
30
- customer = Customer::Operations::Upsert.new(
31
- adaptor: adaptor,
32
- resource: resource.customer
33
- )
34
-
35
- add_before_operation(customer)
36
- end
37
-
38
- def find_result
39
- @find_result ||= Find.new(
40
- adaptor: adaptor,
41
- resource: resource
42
- ).perform
43
- end
44
-
45
- def qbo_payment?
46
- find_result.success?
47
- end
48
- end
49
- end
50
- end
51
- end
52
- end
53
- end
@@ -1,42 +0,0 @@
1
- module LedgerSync
2
- module Adaptors
3
- module Test
4
- module Customer
5
- module Operations
6
- class Upsert < Operation::Upsert
7
- class Contract < LedgerSync::Adaptors::Contract
8
- params do
9
- required(:ledger_id).maybe(:string)
10
- optional(:email).maybe(:string)
11
- required(:name).filled(:string)
12
- optional(:phone_number).maybe(:string)
13
- end
14
- end
15
-
16
- private
17
-
18
- def build
19
- op = if customer_exists_in_ledger?
20
- Update.new(adaptor: adaptor, resource: resource)
21
- else
22
- Create.new(adaptor: adaptor, resource: resource)
23
- end
24
- add_root_operation(op)
25
- end
26
-
27
- def find_result
28
- @find_result ||= Find.new(
29
- adaptor: adaptor,
30
- resource: resource
31
- ).perform
32
- end
33
-
34
- def customer_exists_in_ledger?
35
- find_result.success?
36
- end
37
- end
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,53 +0,0 @@
1
- module LedgerSync
2
- module Adaptors
3
- module Test
4
- module Payment
5
- module Operations
6
- class Upsert < Operation::Upsert
7
- class Contract < LedgerSync::Adaptors::Contract
8
- schema do
9
- required(:ledger_id).maybe(:string)
10
- required(:amount).value(:integer)
11
- required(:currency).value(:string)
12
- required(:customer).value(Types::Reference)
13
- end
14
- end
15
-
16
- private
17
-
18
- def build
19
- op = if qbo_payment?
20
- Update.new(adaptor: adaptor, resource: resource)
21
- else
22
- Create.new(adaptor: adaptor, resource: resource)
23
- end
24
-
25
- build_customer_operation
26
- add_root_operation(op)
27
- end
28
-
29
- def build_customer_operation
30
- customer = Customer::Operations::Upsert.new(
31
- adaptor: adaptor,
32
- resource: resource.customer
33
- )
34
-
35
- add_before_operation(customer)
36
- end
37
-
38
- def find_result
39
- @find_result ||= Find.new(
40
- adaptor: adaptor,
41
- resource: resource
42
- ).perform
43
- end
44
-
45
- def qbo_payment?
46
- find_result.success?
47
- end
48
- end
49
- end
50
- end
51
- end
52
- end
53
- end
@@ -1,108 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module LedgerSync
4
- class Sync
5
- include LedgerSync::Util::StringHelpers
6
- include SimplySerializable::Mixin
7
- include Validatable
8
-
9
- attr_reader :adaptor,
10
- :method,
11
- :resources_data,
12
- :resource_external_id,
13
- :resource_type
14
-
15
- serialize only: %i[
16
- adaptor
17
- method
18
- operation
19
- operations
20
- resource
21
- resource_external_id
22
- resource_type
23
- resources
24
- resources_data
25
- ]
26
-
27
- def initialize(
28
- adaptor:,
29
- method:,
30
- resources_data:,
31
- resource_external_id:,
32
- resource_type:
33
- )
34
- @adaptor = adaptor
35
- @resource_external_id = resource_external_id
36
- @resource_type = resource_type
37
- @method = method
38
- @resources_data = Util::HashHelpers.deep_symbolize_keys(resources_data)
39
- end
40
-
41
- def perform
42
- @perform ||= begin
43
- raise 'Missing adaptor' if adaptor.nil?
44
- raise 'Missing operation' if operation.nil?
45
-
46
- @result = validate
47
- .and_then { performer.perform }
48
- .on_success { return SyncResult.Success(sync: self) }
49
- .on_failure { return SyncResult.Failure(sync: self) }
50
- end
51
- end
52
-
53
- def coordinator
54
- @coordinator ||= Util::Coordinator.new(
55
- operation: operation
56
- )
57
- end
58
-
59
- def operations
60
- @operations ||= coordinator.operations
61
- end
62
-
63
- def operation
64
- @operation ||= Adaptors::Operation.klass_from(
65
- adaptor: adaptor,
66
- method: method,
67
- object: resource_type
68
- ).new(
69
- adaptor: adaptor,
70
- resource: resource
71
- )
72
- end
73
-
74
- def performer
75
- @performer ||= Util::Performer.new(operations: operations)
76
- end
77
-
78
- def resource
79
- @resource ||= resources_builder.resource
80
- end
81
-
82
- def resources
83
- @resources ||= resources_builder.resources
84
- end
85
-
86
- def resources_builder
87
- @resources_builder ||= Util::ResourcesBuilder.new(
88
- data: resources_data,
89
- root_resource_external_id: resource_external_id,
90
- root_resource_type: resource_type
91
- )
92
- end
93
-
94
- def result
95
- raise Error::SyncError::NotPerformedError.new(sync: self) unless @perform.present?
96
-
97
- @result
98
- end
99
-
100
- private
101
-
102
- def validate
103
- operations.inject(Result.Success) do |result, op|
104
- result.and_then { op.validate }
105
- end
106
- end
107
- end
108
- end
@@ -1,72 +0,0 @@
1
- module LedgerSync
2
- module Util
3
- class Coordinator
4
- attr_reader :operation
5
-
6
- def initialize(operation:)
7
-
8
- @operation = operation
9
- end
10
-
11
- def operations
12
- @operations ||= begin
13
- flattened_operation = self.class.flatten_operation(
14
- operation
15
- )
16
-
17
- converted_operations = self.class.convert_downstream_creates_to_upserts(
18
- flattened_operation
19
- )
20
-
21
- de_duped = self.class.de_dup(converted_operations)
22
-
23
- de_duped
24
- end
25
- end
26
-
27
- # If we have already seen a create for the same object
28
- def self.convert_downstream_creates_to_upserts(ops)
29
- cache = {}
30
- ops.flat_map do |op|
31
- cache_key = "#{op.class.name}/#{op.resource.external_id}"
32
- if op.create? && cache.key?(cache_key)
33
- flatten_operation(op.convert_to_update)
34
- else
35
- cache[cache_key] = nil
36
- op
37
- end
38
- end
39
- end
40
-
41
- def self.de_dup(ops)
42
- @de_dup_cache = {}
43
-
44
- ops.select do |op|
45
- cache_key = "#{op.class.name}/#{op.resource.external_id}/#{op.fingerprint}"
46
-
47
- if @de_dup_cache.key?(cache_key)
48
- false
49
- else
50
- @de_dup_cache[cache_key] = true
51
- end
52
- end
53
- end
54
-
55
- # To build the ordered operations, start at root operation,
56
- # perform all before_operations in order, perform root,
57
- # then perform all after_operations in order,
58
- # recursively doing the same for all operations.
59
- def self.flatten_operation(op)
60
- op.prepare
61
-
62
- raise "root_operation required: #{op.inspect}" if op.root_operation.nil?
63
-
64
- [
65
- op.before_operations.map { |e| flatten_operation(e) },
66
- op.root_operation,
67
- op.after_operations.map { |e| flatten_operation(e) }
68
- ].flatten
69
- end
70
- end
71
- end
72
- end