77-include_lib (" limiter_proto/include/limproto_limiter_thrift.hrl" ).
88-include_lib (" limiter_proto/include/limproto_context_payproc_thrift.hrl" ).
99
10- -type turnover_selector () :: dmsl_domain_thrift :'TurnoverLimitSelector ' ().
10+ -type turnover_terms_container () :: dmsl_domain_thrift :'PaymentsProvisionTerms' () | dmsl_domain_thrift : 'ShopConfig ' ().
1111-type turnover_limit () :: dmsl_domain_thrift :'TurnoverLimit' ().
1212-type invoice () :: dmsl_domain_thrift :'Invoice' ().
1313-type payment () :: dmsl_domain_thrift :'InvoicePayment' ().
2323
2424-export_type ([turnover_limit_value / 0 ]).
2525
26- -export ([get_turnover_limits /1 ]).
26+ -export ([get_turnover_limits /2 ]).
2727-export ([check_limits /5 ]).
2828-export ([check_shop_limits /5 ]).
2929-export ([hold_payment_limits /5 ]).
4242 terminal = TerminalRef
4343}).
4444
45- -spec get_turnover_limits (turnover_selector () | undefined ) -> [turnover_limit ()].
46- get_turnover_limits (undefined ) ->
45+ -define (party (PartyID ), # domain_Party {
46+ id = PartyID
47+ }).
48+
49+ -define (shop (ShopID ), # domain_Shop {
50+ id = ShopID
51+ }).
52+
53+ % % Very specific errors to crutch around
54+ -define (POSTING_PLAN_NOT_FOUND (ID ), # base_InvalidRequest {errors = [<<" Posting plan not found: " , ID /binary >>]}).
55+ -define (OPERATION_NOT_FOUND , {invalid_request , [<<" OperationNotFound" >>]}).
56+
57+ -spec get_turnover_limits (turnover_terms_container (), strict | lenient ) -> [turnover_limit ()].
58+
59+ get_turnover_limits (# domain_ShopConfig {turnover_limits = undefined }, _Mode ) ->
60+ [];
61+ get_turnover_limits (# domain_ShopConfig {turnover_limits = Limits }, Mode ) ->
62+ ordsets :to_list (filter_existing_turnover_limits (Limits , Mode ));
63+ get_turnover_limits (# domain_PaymentsProvisionTerms {turnover_limits = undefined }, _Mode ) ->
4764 [];
48- get_turnover_limits ({ value , Limits }) ->
49- Limits ;
50- get_turnover_limits (Ambiguous ) ->
65+ get_turnover_limits (# domain_PaymentsProvisionTerms { turnover_limits = { value , Limits }}, Mode ) ->
66+ filter_existing_turnover_limits ( Limits , Mode ) ;
67+ get_turnover_limits (# domain_PaymentsProvisionTerms { turnover_limits = Ambiguous }, _Mode ) ->
5168 error ({misconfiguration , {'Could not reduce selector to a value' , Ambiguous }}).
5269
70+ -define (LIMIT_NOT_FOUND (Revision , LimitID ),
71+ {object_not_found , {Revision , {limit_config , # domain_LimitConfigRef {id = LimitID }}}}
72+ ).
73+ filter_existing_turnover_limits (Limits , Mode ) ->
74+ % % When mode is strict and limit-config does not exist it raises a
75+ % % misconfiguration error.
76+ % % Otherwise it filters out non existent one.
77+ lists :filter (
78+ fun
79+ (# domain_TurnoverLimit {domain_revision = undefined }) ->
80+ true ;
81+ (# domain_TurnoverLimit {id = ID , domain_revision = Ver }) ->
82+ try
83+ _ = hg_domain :get (Ver , {limit_config , # domain_LimitConfigRef {id = ID }}),
84+ true
85+ catch
86+ error :? LIMIT_NOT_FOUND (_Revision , _LimitID ) when Mode =:= lenient ->
87+ false ;
88+ error :? LIMIT_NOT_FOUND (Revision , LimitID ) when Mode =:= strict ->
89+ error ({misconfiguration , {'Limit config not found' , {Revision , LimitID }}})
90+ end
91+ end ,
92+ Limits
93+ ).
94+
5395- spec get_limit_values ([turnover_limit ()], invoice (), payment (), route (), pos_integer ()) -> [turnover_limit_value ()].
5496get_limit_values (TurnoverLimits , Invoice , Payment , Route , Iter ) ->
5597 Context = gen_limit_context (Invoice , Payment , Route ),
@@ -277,13 +319,19 @@ rollback_payment_limits(TurnoverLimits, Invoice, Payment, Route, Iter, Flags) ->
277319 {LegacyTurnoverLimits , BatchTurnoverLimits } = split_turnover_limits_by_available_limiter_api (TurnoverLimits ),
278320 ok = legacy_rollback_payment_limits (Context , LegacyTurnoverLimits , Invoice , Payment , Route , Iter , Flags ),
279321 OperationIdSegments = make_route_operation_segments (Invoice , Payment , Route , Iter ),
280- ok = batch_rollback_limits (Context , BatchTurnoverLimits , OperationIdSegments ).
322+ ok = batch_rollback_limits (Context , BatchTurnoverLimits , OperationIdSegments , Flags ).
281323
282- batch_rollback_limits (_Context , [], _OperationIdSegments ) ->
324+ batch_rollback_limits (_Context , [], _OperationIdSegments , _Flags ) ->
283325 ok ;
284- batch_rollback_limits (Context , TurnoverLimits , OperationIdSegments ) ->
326+ batch_rollback_limits (Context , TurnoverLimits , OperationIdSegments , Flags ) ->
285327 {LimitRequest , _ } = prepare_limit_request (TurnoverLimits , OperationIdSegments ),
286- hg_limiter_client :rollback_batch (LimitRequest , Context ).
328+ IgnoreError = lists :member (ignore_not_found , Flags ) orelse lists :member (ignore_business_error , Flags ),
329+ try
330+ ok = hg_limiter_client :rollback_batch (LimitRequest , Context )
331+ catch
332+ error :(? OPERATION_NOT_FOUND ) when IgnoreError =:= true ->
333+ ok
334+ end .
287335
288336legacy_rollback_payment_limits (Context , TurnoverLimits , Invoice , Payment , Route , Iter , Flags ) ->
289337 ChangeIDs = [
@@ -308,7 +356,7 @@ rollback_shop_limits(TurnoverLimits, PartyConfigRef, ShopConfigRef, Invoice, Pay
308356 Context , LegacyTurnoverLimits , PartyConfigRef , ShopConfigRef , Invoice , Payment , Flags
309357 ),
310358 OperationIdSegments = make_shop_operation_segments (PartyConfigRef , ShopConfigRef , Invoice , Payment ),
311- ok = batch_rollback_limits (Context , BatchTurnoverLimits , OperationIdSegments ).
359+ ok = batch_rollback_limits (Context , BatchTurnoverLimits , OperationIdSegments , Flags ).
312360
313361legacy_rollback_shop_limits (Context , TurnoverLimits , PartyConfigRef , ShopConfigRef , Invoice , Payment , Flags ) ->
314362 ChangeIDs = [construct_shop_change_id (PartyConfigRef , ShopConfigRef , Invoice , Payment )],
@@ -321,7 +369,7 @@ rollback_refund_limits(TurnoverLimits, Invoice, Payment, Refund, Route) ->
321369 {LegacyTurnoverLimits , BatchTurnoverLimits } = split_turnover_limits_by_available_limiter_api (TurnoverLimits ),
322370 ok = legacy_rollback_refund_limits (Context , LegacyTurnoverLimits , Invoice , Payment , Refund ),
323371 OperationIdSegments = make_refund_operation_segments (Invoice , Payment , Refund ),
324- ok = batch_rollback_limits (Context , BatchTurnoverLimits , OperationIdSegments ).
372+ ok = batch_rollback_limits (Context , BatchTurnoverLimits , OperationIdSegments , [] ).
325373
326374legacy_rollback_refund_limits (Context , TurnoverLimits , Invoice , Payment , Refund ) ->
327375 ChangeIDs = [construct_refund_change_id (Invoice , Payment , Refund )],
@@ -348,9 +396,6 @@ process_changes(LimitChangesQueues, WithFun, Clock, Context, Flags) ->
348396 LimitChangesQueues
349397 ).
350398
351- % % Very specific error to crutch around
352- -define (POSTING_PLAN_NOT_FOUND (ID ), # base_InvalidRequest {errors = [<<" Posting plan not found: " , ID /binary >>]}).
353-
354399process_changes_try_wrap ([LimitChange ], WithFun , Clock , Context , Flags ) ->
355400 IgnoreNotFound = lists :member (ignore_not_found , Flags ),
356401 # limiter_LimitChange {change_id = ChangeID } = LimitChange ,
0 commit comments