forest_admin_datasource_mambu_payments 1.33.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 (68) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/Rakefile +6 -0
  4. data/forest_admin_datasource_mambu_payments.gemspec +37 -0
  5. data/lib/forest_admin_datasource_mambu_payments/client/reads.rb +66 -0
  6. data/lib/forest_admin_datasource_mambu_payments/client/writes.rb +42 -0
  7. data/lib/forest_admin_datasource_mambu_payments/client.rb +166 -0
  8. data/lib/forest_admin_datasource_mambu_payments/collections/account_holder.rb +64 -0
  9. data/lib/forest_admin_datasource_mambu_payments/collections/balance.rb +75 -0
  10. data/lib/forest_admin_datasource_mambu_payments/collections/base_collection.rb +254 -0
  11. data/lib/forest_admin_datasource_mambu_payments/collections/claim.rb +98 -0
  12. data/lib/forest_admin_datasource_mambu_payments/collections/connected_account.rb +103 -0
  13. data/lib/forest_admin_datasource_mambu_payments/collections/direct_debit_mandate.rb +125 -0
  14. data/lib/forest_admin_datasource_mambu_payments/collections/event.rb +133 -0
  15. data/lib/forest_admin_datasource_mambu_payments/collections/expected_payment.rb +132 -0
  16. data/lib/forest_admin_datasource_mambu_payments/collections/external_account.rb +121 -0
  17. data/lib/forest_admin_datasource_mambu_payments/collections/file.rb +89 -0
  18. data/lib/forest_admin_datasource_mambu_payments/collections/incoming_payment.rb +120 -0
  19. data/lib/forest_admin_datasource_mambu_payments/collections/internal_account.rb +136 -0
  20. data/lib/forest_admin_datasource_mambu_payments/collections/payee_verification_request.rb +88 -0
  21. data/lib/forest_admin_datasource_mambu_payments/collections/payment_capture.rb +136 -0
  22. data/lib/forest_admin_datasource_mambu_payments/collections/payment_order.rb +132 -0
  23. data/lib/forest_admin_datasource_mambu_payments/collections/reconciliation.rb +93 -0
  24. data/lib/forest_admin_datasource_mambu_payments/collections/return.rb +132 -0
  25. data/lib/forest_admin_datasource_mambu_payments/collections/transaction.rb +113 -0
  26. data/lib/forest_admin_datasource_mambu_payments/configuration.rb +36 -0
  27. data/lib/forest_admin_datasource_mambu_payments/datasource.rb +35 -0
  28. data/lib/forest_admin_datasource_mambu_payments/plugins/disable_search.rb +31 -0
  29. data/lib/forest_admin_datasource_mambu_payments/plugins/helpers.rb +94 -0
  30. data/lib/forest_admin_datasource_mambu_payments/plugins/messages.rb +30 -0
  31. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/holder_link_plugin.rb +56 -0
  32. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_account_holder_to_direct_debit_mandates.rb +14 -0
  33. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_account_holder_to_incoming_payments.rb +14 -0
  34. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_external_account_to_direct_debit_mandates.rb +13 -0
  35. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_external_account_to_incoming_payments.rb +13 -0
  36. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_external_account_to_payment_orders.rb +13 -0
  37. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_incoming_payment_to_events.rb +13 -0
  38. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_incoming_payment_to_expected_payments.rb +21 -0
  39. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_incoming_payment_to_returns.rb +12 -0
  40. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_incoming_payment_to_transactions.rb +20 -0
  41. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_internal_account_to_balances.rb +17 -0
  42. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_internal_account_to_incoming_payments.rb +13 -0
  43. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_internal_account_to_payment_orders.rb +17 -0
  44. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_payment_order_to_events.rb +13 -0
  45. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_payment_order_to_receiving_account_holder.rb +15 -0
  46. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_payment_order_to_returns.rb +12 -0
  47. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/link_payment_order_to_transactions.rb +51 -0
  48. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/one_to_many_link_plugin.rb +35 -0
  49. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/pivot_resolution.rb +73 -0
  50. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_connected_account_filter.rb +38 -0
  51. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_cross_reconciliation_filter.rb +55 -0
  52. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_holder_filter.rb +32 -0
  53. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_link_plugin.rb +64 -0
  54. data/lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_reconciliation_filter.rb +39 -0
  55. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/approve_payment_order.rb +56 -0
  56. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/cancel_payment_order.rb +66 -0
  57. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/create_account_holder.rb +44 -0
  58. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/create_external_account.rb +54 -0
  59. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/create_internal_account.rb +58 -0
  60. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/create_payment_order.rb +66 -0
  61. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/trigger_payee_verification.rb +58 -0
  62. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/update_account_holder.rb +67 -0
  63. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/update_external_account.rb +75 -0
  64. data/lib/forest_admin_datasource_mambu_payments/plugins/smart_actions/update_internal_account.rb +75 -0
  65. data/lib/forest_admin_datasource_mambu_payments/query/condition_tree_translator.rb +115 -0
  66. data/lib/forest_admin_datasource_mambu_payments/version.rb +3 -0
  67. data/lib/forest_admin_datasource_mambu_payments.rb +44 -0
  68. metadata +170 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 75e0d0f5bf5d786f1b1b546d6f9b6c2f1ba9a84ea34af18c886c3dff870bb2bb
4
+ data.tar.gz: a20371a99995fbca040eab5fe38eef71867bb5b093bdbaad20e2d542ff735cf7
5
+ SHA512:
6
+ metadata.gz: d23380a9dc6ce5487ec104f2f2ca57b1c65c9239ce16ebf2c8845c290134904d363d318868de4252be95b82934b7ad601df5b71efce60cfb8290f0312f1760d6
7
+ data.tar.gz: 7fc354ae457585278d86cf4d1244a8c22028df25fa9928a512bfd9aa6f385c4cae289395f27643329c37c23ec2b36481fb3099e24a222981fa7a0f5b84ee59f7
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,37 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
3
+
4
+ require_relative 'lib/forest_admin_datasource_mambu_payments/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'forest_admin_datasource_mambu_payments'
8
+ spec.version = ForestAdminDatasourceMambuPayments::VERSION
9
+ spec.authors = ['Forest Admin']
10
+ spec.email = ['contact@forestadmin.com']
11
+ spec.homepage = 'https://www.forestadmin.com'
12
+ spec.summary = 'Mambu Payments (Numeral) datasource for Forest Admin Ruby agent.'
13
+ spec.description = 'Surface Mambu Payments connected accounts, payment orders, ' \
14
+ 'transactions and balances as Forest Admin collections.'
15
+ spec.license = 'GPL-3.0'
16
+ spec.required_ruby_version = '>= 3.0.0'
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/ForestAdmin/agent-ruby'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/ForestAdmin/agent-ruby/blob/main/CHANGELOG.md'
21
+ spec.metadata['rubygems_mfa_required'] = 'false'
22
+
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) ||
26
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
27
+ end
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_dependency 'activesupport', '>= 6.1'
34
+ spec.add_dependency 'faraday', '~> 2.0'
35
+ spec.add_dependency 'faraday-retry', '~> 2.0'
36
+ spec.add_dependency 'zeitwerk', '~> 2.3'
37
+ end
@@ -0,0 +1,66 @@
1
+ module ForestAdminDatasourceMambuPayments
2
+ class Client
3
+ module Reads
4
+ def list_connected_accounts(**params) = list_resource('connected_accounts', params)
5
+ def find_connected_account(id) = get_resource('connected_accounts', id)
6
+
7
+ def list_payment_orders(**params) = list_resource('payment_orders', params)
8
+ def find_payment_order(id) = get_resource('payment_orders', id)
9
+
10
+ def list_transactions(**params) = list_resource('transactions', params)
11
+ def find_transaction(id) = get_resource('transactions', id)
12
+
13
+ def list_balances(**params) = list_resource('balances', params)
14
+ def find_balance(id) = get_resource('balances', id)
15
+
16
+ def list_account_holders(**params) = list_resource('account_holders', params)
17
+ def find_account_holder(id) = get_resource('account_holders', id)
18
+
19
+ def list_external_accounts(**params) = list_resource('external_accounts', params)
20
+ def find_external_account(id) = get_resource('external_accounts', id)
21
+
22
+ def list_internal_accounts(**params) = list_resource('internal_accounts', params)
23
+ def find_internal_account(id) = get_resource('internal_accounts', id)
24
+
25
+ def list_incoming_payments(**params) = list_resource('incoming_payments', params)
26
+ def find_incoming_payment(id) = get_resource('incoming_payments', id)
27
+
28
+ def list_direct_debit_mandates(**params) = list_resource('direct_debit_mandates', params)
29
+ def find_direct_debit_mandate(id) = get_resource('direct_debit_mandates', id)
30
+
31
+ def list_expected_payments(**params) = list_resource('expected_payments', params)
32
+ def find_expected_payment(id) = get_resource('expected_payments', id)
33
+
34
+ def list_events(**params) = list_resource('events', params)
35
+ def find_event(id) = get_resource('events', id)
36
+
37
+ def list_files(**params) = list_resource('files', params)
38
+ def find_file(id) = get_resource('files', id)
39
+
40
+ def list_returns(**params) = list_resource('returns', params)
41
+ def find_return(id) = get_resource('returns', id)
42
+
43
+ # Claims are arrived-from-the-network resources (created via the sandbox
44
+ # simulator or by the counterparty bank). No POST/PATCH/DELETE here:
45
+ # accept/reject are lifecycle actions and would belong in a plugin.
46
+ def list_claims(**params) = list_resource('claims', params)
47
+ def find_claim(id) = get_resource('claims', id)
48
+
49
+ def list_reconciliations(**params) = list_resource('reconciliations', params)
50
+ def find_reconciliation(id) = get_resource('reconciliations', id)
51
+
52
+ # Payment captures are emitted by PSPs (or registered manually via API
53
+ # to reconcile reporting files). create/update/cancel exist on the
54
+ # Numeral API but are lifecycle operations deferred to a future plugin.
55
+ def list_payment_captures(**params) = list_resource('payment_captures', params)
56
+ def find_payment_capture(id) = get_resource('payment_captures', id)
57
+
58
+ # Payee verification requests are emitted by Numeral when an outgoing
59
+ # verification is sent (via the TriggerPayeeVerification plugin) or
60
+ # when an incoming verification arrives from the network. send /
61
+ # simulate are exposed as smart actions, not collection writes.
62
+ def list_payee_verification_requests(**params) = list_resource('payee_verification_requests', params)
63
+ def find_payee_verification_request(id) = get_resource('payee_verification_requests', id)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,42 @@
1
+ module ForestAdminDatasourceMambuPayments
2
+ class Client
3
+ module Writes
4
+ def create_payment_order(attrs) = post_resource('payment_orders', attrs)
5
+ def update_payment_order(id, attrs) = patch_resource('payment_orders', id, attrs)
6
+ def delete_payment_order(id) = delete_resource('payment_orders', id)
7
+ def approve_payment_order(id, attrs = {}) = post_action_resource('payment_orders', id, 'approve', attrs)
8
+ def cancel_payment_order(id, attrs = {}) = post_action_resource('payment_orders', id, 'cancel', attrs)
9
+
10
+ def create_account_holder(attrs) = post_resource('account_holders', attrs)
11
+ def update_account_holder(id, attrs) = patch_resource('account_holders', id, attrs)
12
+ def delete_account_holder(id) = delete_resource('account_holders', id)
13
+
14
+ def create_external_account(attrs) = post_resource('external_accounts', attrs)
15
+ def update_external_account(id, attrs) = patch_resource('external_accounts', id, attrs)
16
+ def delete_external_account(id) = delete_resource('external_accounts', id)
17
+ def verify_external_account(id, attrs = {}) = post_action_resource('external_accounts', id, 'verify', attrs)
18
+
19
+ def create_internal_account(attrs) = post_resource('internal_accounts', attrs)
20
+ def update_internal_account(id, attrs) = patch_resource('internal_accounts', id, attrs)
21
+ def delete_internal_account(id) = delete_resource('internal_accounts', id)
22
+
23
+ def create_direct_debit_mandate(attrs) = post_resource('direct_debit_mandates', attrs)
24
+ def update_direct_debit_mandate(id, attrs) = patch_resource('direct_debit_mandates', id, attrs)
25
+ def delete_direct_debit_mandate(id) = delete_resource('direct_debit_mandates', id)
26
+
27
+ def create_expected_payment(attrs) = post_resource('expected_payments', attrs)
28
+ def update_expected_payment(id, attrs) = patch_resource('expected_payments', id, attrs)
29
+ def delete_expected_payment(id) = delete_resource('expected_payments', id)
30
+
31
+ # Numeral has no DELETE on /returns; the lifecycle "cancel"/"approve" are
32
+ # exposed as side-effect actions and would belong in a plugin, not here.
33
+ def create_return(attrs) = post_resource('returns', attrs)
34
+ def update_return(id, attrs) = patch_resource('returns', id, attrs)
35
+
36
+ # Numeral has no DELETE on /reconciliations either; cancel is a lifecycle
37
+ # action and is deferred to a future plugin.
38
+ def create_reconciliation(attrs) = post_resource('reconciliations', attrs)
39
+ def update_reconciliation(id, attrs) = patch_resource('reconciliations', id, attrs)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,166 @@
1
+ module ForestAdminDatasourceMambuPayments
2
+ # rubocop:disable Metrics/ClassLength
3
+ class Client
4
+ include Reads
5
+ include Writes
6
+
7
+ MAX_PER_PAGE = 100
8
+
9
+ def initialize(configuration)
10
+ @configuration = configuration
11
+ end
12
+
13
+ private
14
+
15
+ def list_resource(path, params = {})
16
+ must_succeed("list(#{path})") do
17
+ body = connection.get(path, normalize_params(params)).body
18
+ extract_records(body, path)
19
+ end
20
+ end
21
+
22
+ def get_resource(path, id)
23
+ extract_record(connection.get("#{path}/#{id}").body)
24
+ rescue Faraday::ResourceNotFound
25
+ nil
26
+ rescue Faraday::Error => e
27
+ raise api_error("get(#{path}/#{id})", e)
28
+ rescue StandardError => e
29
+ raise APIError, "Mambu Payments API call failed: get(#{path}/#{id}): #{e.class}: #{e.message}"
30
+ end
31
+
32
+ def post_resource(path, attributes)
33
+ must_succeed("create(#{path})") do
34
+ extract_record(connection.post(path, attributes).body)
35
+ end
36
+ end
37
+
38
+ def patch_resource(path, id, attributes)
39
+ must_succeed("update(#{path}/#{id})") do
40
+ extract_record(connection.patch("#{path}/#{id}", attributes).body)
41
+ end
42
+ end
43
+
44
+ # POST .../:id/:action — Numeral exposes side-effect endpoints (approve,
45
+ # cancel, verify) as sub-paths returning the updated resource.
46
+ def post_action_resource(path, id, action, attributes = {})
47
+ must_succeed("#{action}(#{path}/#{id})") do
48
+ extract_record(connection.post("#{path}/#{id}/#{action}", attributes).body)
49
+ end
50
+ end
51
+
52
+ # Numeral list responses are typically wrapped (e.g. { "data": [...] } or
53
+ # { "connected_accounts": [...] }) but we accept a raw array too. Falls back
54
+ # to the first array-valued field so we don't silently coerce a wrapper hash
55
+ # into an array of [key, value] pairs.
56
+ def extract_records(body, path)
57
+ return body if body.is_a?(Array)
58
+ return [] unless body.is_a?(Hash)
59
+
60
+ wrapped = body['data'] || body[path] || body['records'] || body['items']
61
+ wrapped.is_a?(Array) ? wrapped : fallback_records(body, path)
62
+ end
63
+
64
+ # Last resort when the wrapper key is unknown: return the first array-valued
65
+ # field, logging that we guessed so an unexpected envelope is visible.
66
+ def fallback_records(body, path)
67
+ fallback = body.values.find { |v| v.is_a?(Array) }
68
+ return [] unless fallback
69
+
70
+ ForestAdminDatasourceMambuPayments.logger.warn(
71
+ "[forest_admin_datasource_mambu_payments] list(#{path}) used wrapper-key fallback; " \
72
+ "body keys=#{body.keys.inspect}"
73
+ )
74
+ fallback
75
+ end
76
+
77
+ def extract_record(body)
78
+ return nil if body.nil?
79
+ return body['data'] if body.is_a?(Hash) && body['data'].is_a?(Hash)
80
+
81
+ body
82
+ end
83
+
84
+ def delete_resource(path, id)
85
+ must_succeed("delete(#{path}/#{id})") do
86
+ connection.delete("#{path}/#{id}")
87
+ true
88
+ end
89
+ end
90
+
91
+ def normalize_params(params)
92
+ params.compact.transform_values { |v| v.is_a?(Array) ? v.join(',') : v }
93
+ end
94
+
95
+ def must_succeed(operation)
96
+ yield
97
+ rescue Faraday::Error => e
98
+ raise api_error(operation, e)
99
+ rescue StandardError => e
100
+ raise APIError, "Mambu Payments API call failed: #{operation}: #{e.class}: #{e.message}"
101
+ end
102
+
103
+ # Builds an APIError that preserves the HTTP status and the API's own error
104
+ # body. Numeral returns structured validation errors (e.g. on a 422), which
105
+ # smart actions surface to the operator instead of a generic failure string.
106
+ def api_error(operation, error)
107
+ response = error.respond_to?(:response) ? error.response : nil
108
+ status = response.is_a?(Hash) ? response[:status] : nil
109
+ body = response.is_a?(Hash) ? response[:body] : nil
110
+ detail = error_detail(status, body) || "#{error.class}: #{error.message}"
111
+ APIError.new("Mambu Payments API call failed: #{operation}: #{detail}", status: status, body: parse_body(body))
112
+ end
113
+
114
+ def error_detail(status, body)
115
+ return nil unless status
116
+
117
+ ["HTTP #{status}", error_message(parse_body(body))].compact.join(' ').strip
118
+ end
119
+
120
+ # Pulls the human-readable message out of the common Numeral error shapes
121
+ # ({ "error": { "message": ... } }, { "errors": [...] }, { "message": ... }).
122
+ def error_message(parsed)
123
+ return parsed.to_s[0, 500] unless parsed.is_a?(Hash)
124
+
125
+ message = parsed.dig('error', 'message') || parsed['message'] || parsed['detail'] || join_errors(parsed['errors'])
126
+ (message.to_s.empty? ? parsed.to_json : message)[0, 500]
127
+ end
128
+
129
+ def join_errors(errors)
130
+ Array(errors).filter_map { |e| e.is_a?(Hash) ? (e['message'] || e['detail']) : e }.join('; ')
131
+ end
132
+
133
+ def parse_body(body)
134
+ return body unless body.is_a?(String) && !body.empty?
135
+
136
+ JSON.parse(body)
137
+ rescue JSON::ParserError
138
+ body
139
+ end
140
+
141
+ def best_effort(operation, default:)
142
+ yield
143
+ rescue StandardError => e
144
+ ForestAdminDatasourceMambuPayments.logger.warn(
145
+ "[forest_admin_datasource_mambu_payments] #{operation} failed; degrading: #{e.class}: #{e.message}"
146
+ )
147
+ default
148
+ end
149
+
150
+ def connection
151
+ @connection ||= Faraday.new(url: @configuration.url) do |f|
152
+ f.request :json
153
+ f.request :retry, max: 3, interval: 0.2, backoff_factor: 2,
154
+ retry_statuses: [429, 502, 503, 504]
155
+ f.response :json
156
+ f.response :raise_error
157
+ f.headers['x-api-key'] = @configuration.api_key
158
+ f.headers['Accept'] = 'application/json'
159
+ f.headers['User-Agent'] = "forest_admin_datasource_mambu_payments/#{VERSION}"
160
+ f.options.open_timeout = @configuration.open_timeout
161
+ f.options.timeout = @configuration.timeout
162
+ end
163
+ end
164
+ end
165
+ # rubocop:enable Metrics/ClassLength
166
+ end
@@ -0,0 +1,64 @@
1
+ module ForestAdminDatasourceMambuPayments
2
+ module Collections
3
+ class AccountHolder < BaseCollection
4
+ OneToManySchema = ForestAdminDatasourceToolkit::Schema::Relations::OneToManySchema
5
+
6
+ client_resource :account_holder
7
+
8
+ def initialize(datasource)
9
+ super(datasource, 'MambuAccountHolder')
10
+ define_schema
11
+ define_relations
12
+ reconcile_filter_operators!
13
+ end
14
+
15
+ def create(_caller, data)
16
+ serialize(datasource.client.create_account_holder(build_payload(data)))
17
+ end
18
+
19
+ def update(caller, filter, patch)
20
+ payload = build_payload(patch)
21
+ ids_for(caller, filter).each { |id| datasource.client.update_account_holder(id, payload) }
22
+ end
23
+
24
+ def delete(caller, filter)
25
+ ids_for(caller, filter).each { |id| datasource.client.delete_account_holder(id) }
26
+ end
27
+
28
+ def serialize(record)
29
+ a = attrs_of(record)
30
+ {
31
+ 'id' => a['id'],
32
+ 'object' => a['object'],
33
+ 'name' => a['name'],
34
+ 'metadata' => a['metadata'],
35
+ 'disabled_at' => a['disabled_at'],
36
+ 'created_at' => a['created_at']
37
+ }
38
+ end
39
+
40
+ private
41
+
42
+ def define_schema
43
+ add_field('id', ColumnSchema.new(column_type: 'String', is_primary_key: true,
44
+ is_read_only: true, is_sortable: true))
45
+ add_field('object', ColumnSchema.new(column_type: 'String', is_read_only: true, is_sortable: false))
46
+ add_field('name', ColumnSchema.new(column_type: 'String', is_read_only: false, is_sortable: true))
47
+ add_field('metadata', ColumnSchema.new(column_type: 'Json', is_read_only: false, is_sortable: false))
48
+ add_field('disabled_at', ColumnSchema.new(column_type: 'Date', is_read_only: true, is_sortable: true))
49
+ add_field('created_at', ColumnSchema.new(column_type: 'Date', is_read_only: true, is_sortable: true))
50
+ end
51
+
52
+ def define_relations
53
+ add_field('external_accounts', OneToManySchema.new(
54
+ foreign_collection: 'MambuExternalAccount',
55
+ origin_key: 'account_holder_id', origin_key_target: 'id'
56
+ ))
57
+ add_field('internal_accounts', OneToManySchema.new(
58
+ foreign_collection: 'MambuInternalAccount',
59
+ origin_key: 'account_holder_id', origin_key_target: 'id'
60
+ ))
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,75 @@
1
+ module ForestAdminDatasourceMambuPayments
2
+ module Collections
3
+ class Balance < BaseCollection
4
+ ManyToOneSchema = ForestAdminDatasourceToolkit::Schema::Relations::ManyToOneSchema
5
+
6
+ ENUM_DIRECTION = %w[debit credit].freeze
7
+
8
+ client_resource :balance
9
+
10
+ def initialize(datasource)
11
+ super(datasource, 'MambuBalance')
12
+ define_schema
13
+ define_relations
14
+ reconcile_filter_operators!
15
+ end
16
+
17
+ def serialize(record)
18
+ a = attrs_of(record)
19
+ {
20
+ 'id' => a['id'],
21
+ 'object' => a['object'],
22
+ 'connected_account_id' => a['connected_account_id'],
23
+ 'type' => a['type'],
24
+ 'direction' => a['direction'],
25
+ 'amount' => a['amount'],
26
+ 'currency' => a['currency'],
27
+ 'date' => a['date'],
28
+ 'bank_data' => a['bank_data'],
29
+ 'created_at' => a['created_at']
30
+ }
31
+ end
32
+
33
+ protected
34
+
35
+ def collection_filters
36
+ {
37
+ 'connected_account_id' => { ops: [Operators::EQUAL, Operators::IN] }
38
+ }
39
+ end
40
+
41
+ def many_to_one_embeds
42
+ [
43
+ { foreign_key: 'connected_account_id', relation_name: 'connected_account',
44
+ collection: 'MambuConnectedAccount' }
45
+ ]
46
+ end
47
+
48
+ private
49
+
50
+ def define_schema
51
+ add_field('id', ColumnSchema.new(column_type: 'String', is_primary_key: true,
52
+ is_read_only: true, is_sortable: true))
53
+ add_field('object', ColumnSchema.new(column_type: 'String', is_read_only: true, is_sortable: false))
54
+ add_field('connected_account_id', ColumnSchema.new(column_type: 'String',
55
+ is_read_only: true, is_sortable: true))
56
+ add_field('type', ColumnSchema.new(column_type: 'String', is_read_only: true, is_sortable: true))
57
+ add_field('direction', ColumnSchema.new(column_type: 'Enum', enum_values: ENUM_DIRECTION,
58
+ is_read_only: true, is_sortable: false))
59
+ add_field('amount', ColumnSchema.new(column_type: 'Number', is_read_only: true, is_sortable: false))
60
+ add_field('currency', ColumnSchema.new(column_type: 'String', is_read_only: true, is_sortable: false))
61
+ add_field('date', ColumnSchema.new(column_type: 'Date', is_read_only: true, is_sortable: true))
62
+ add_field('bank_data', ColumnSchema.new(column_type: 'Json', is_read_only: true, is_sortable: false))
63
+ add_field('created_at', ColumnSchema.new(column_type: 'Date', is_read_only: true, is_sortable: true))
64
+ end
65
+
66
+ def define_relations
67
+ add_field('connected_account', ManyToOneSchema.new(
68
+ foreign_collection: 'MambuConnectedAccount',
69
+ foreign_key: 'connected_account_id',
70
+ foreign_key_target: 'id'
71
+ ))
72
+ end
73
+ end
74
+ end
75
+ end