Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/erlang-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: Erlang CI Checks
on:
push:
branches:
- 'master'
- 'epic/**'
- "master"
- "epic/**"
pull_request:
branches: ['**']
branches: ["**"]

jobs:
setup:
Expand All @@ -30,7 +30,7 @@ jobs:
run:
name: Run checks
needs: setup
uses: valitydev/erlang-workflows/.github/workflows/erlang-parallel-build.yml@v1.0.17
uses: valitydev/erlang-workflows/.github/workflows/erlang-parallel-build.yml@v1
with:
otp-version: ${{ needs.setup.outputs.otp-version }}
rebar-version: ${{ needs.setup.outputs.rebar-version }}
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
erlang 27.1.2
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ DEV_IMAGE_TAG = $(TEST_CONTAINER_NAME)-dev
DEV_IMAGE_ID = $(file < .image.dev)

DOCKER ?= docker
DOCKERCOMPOSE ?= docker-compose
DOCKERCOMPOSE ?= docker compose
DOCKERCOMPOSE_W_ENV = DEV_IMAGE_TAG=$(DEV_IMAGE_TAG) $(DOCKERCOMPOSE) -f compose.yaml -f compose.tracing.yaml
REBAR ?= rebar3
TEST_CONTAINER_NAME ?= testrunner
Expand Down
142 changes: 82 additions & 60 deletions apps/party_management/src/pm_domain.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

-module(pm_domain).

-include_lib("damsel/include/dmsl_domain_thrift.hrl").
-include_lib("damsel/include/dmsl_domain_conf_thrift.hrl").
-include_lib("damsel/include/dmsl_domain_conf_v2_thrift.hrl").

%%

Expand All @@ -18,14 +17,15 @@

-export([insert/1]).
-export([update/1]).
-export([cleanup/0]).
-export([cleanup/1]).

%%

-type revision() :: pos_integer().
-type ref() :: dmsl_domain_thrift:'Reference'().
-type object() :: dmsl_domain_thrift:'DomainObject'().
-type data() :: _.
-type commit_response() :: dmsl_domain_conf_v2_thrift:'CommitResponse'().

-export_type([revision/0]).
-export_type([ref/0]).
Expand All @@ -34,87 +34,109 @@

-spec head() -> revision().
head() ->
dmt_client:get_last_version().
dmt_client:get_latest_version().

-spec get(revision(), ref()) -> data() | no_return().
get(Revision, Ref) ->
get(Revision0, Ref) ->
Revision1 = maybe_migrate_version(Revision0),
try
extract_data(dmt_client:checkout_object(Revision, Ref))
extract_data(dmt_client:checkout_object(Ref, Revision1))
catch
throw:#domain_conf_ObjectNotFound{} ->
error({object_not_found, {Revision, Ref}})
throw:#domain_conf_v2_ObjectNotFound{} ->
error({object_not_found, {Revision1, Ref}})
end.

-spec find(revision(), ref()) -> data() | notfound.
find(Revision, Ref) ->
find(Revision0, Ref) ->
Revision1 = maybe_migrate_version(Revision0),
try
extract_data(dmt_client:checkout_object(Revision, Ref))
extract_data(dmt_client:checkout_object(Ref, Revision1))
catch
throw:#domain_conf_ObjectNotFound{} ->
throw:#domain_conf_v2_ObjectNotFound{} ->
notfound
end.

-spec exists(revision(), ref()) -> boolean().
exists(Revision, Ref) ->
exists(Revision0, Ref) ->
Revision1 = maybe_migrate_version(Revision0),
try
_ = dmt_client:checkout_object(Revision, Ref),
_ = dmt_client:checkout_object(Ref, Revision1),
true
catch
throw:#domain_conf_ObjectNotFound{} ->
throw:#domain_conf_v2_ObjectNotFound{} ->
false
end.

extract_data({_Tag, {_Name, _Ref, Data}}) ->
extract_data(#domain_conf_v2_VersionedObject{object = {_Tag, {_Name, _Ref, Data}}}) ->
Data.

-spec commit(revision(), dmt_client:commit()) -> revision() | no_return().
commit(Revision, Commit) ->
dmt_client:commit(Revision, Commit).
-spec commit(revision(), [dmt_client:operation()], binary()) -> commit_response() | no_return().
commit(Revision, Operations, AuthorID) ->
dmt_client:commit(Revision, Operations, AuthorID).

-spec insert(object() | [object()]) -> revision() | no_return().
insert(Object) when not is_list(Object) ->
insert([Object]);
-spec insert(object() | [object()]) -> {revision(), [ref()]} | no_return().
insert(Objects) ->
Commit = #'domain_conf_Commit'{
ops = [
{insert, #'domain_conf_InsertOp'{
object = Object
}}
|| Object <- Objects
]
},
commit(head(), Commit).
insert(Objects, generate_author()).

-spec insert(object() | [object()], binary()) -> {revision(), [ref()]} | no_return().
insert(Object, AuthorID) when not is_list(Object) ->
insert([Object], AuthorID);
insert(Objects, AuthorID) ->
Commit = [
{insert, #domain_conf_v2_InsertOp{
object = {Type, Object},
force_ref = {Type, ForceRef}
}}
|| {Type, {_ObjectName, ForceRef, Object}} <- Objects
],
#domain_conf_v2_CommitResponse{version = Version, new_objects = NewObjects} =
commit(head(), Commit, AuthorID),
NewObjectsIDs = [
{Tag, Ref}
|| {Tag, {_ON, Ref, _Obj}} <- ordsets:to_list(NewObjects)
],
{Version, NewObjectsIDs}.

-spec update(object() | [object()]) -> revision() | no_return().
update(NewObject) when not is_list(NewObject) ->
update([NewObject]);
update(NewObjects) ->
update(NewObject, generate_author()).

-spec update(object() | [object()], binary()) -> revision() | no_return().
update(NewObject, AuthorID) when not is_list(NewObject) ->
update([NewObject], AuthorID);
update(NewObjects, AuthorID) ->
Revision = head(),
Commit = #'domain_conf_Commit'{
ops = [
{update, #'domain_conf_UpdateOp'{
old_object = {Tag, {ObjectName, Ref, OldData}},
new_object = NewObject
}}
|| NewObject = {Tag, {ObjectName, Ref, _Data}} <- NewObjects,
OldData <- [get(Revision, {Tag, Ref})]
]
},
commit(Revision, Commit).

-spec remove([object()]) -> revision() | no_return().
remove(Objects) ->
Commit = #'domain_conf_Commit'{
ops = [
{remove, #'domain_conf_RemoveOp'{
object = Object
}}
|| Object <- Objects
]
},
commit(head(), Commit).

-spec cleanup() -> revision() | no_return().
cleanup() ->
#'domain_conf_Snapshot'{domain = Domain} = dmt_client:checkout(latest),
remove(maps:values(Domain)).
Commit = [
{update, #domain_conf_v2_UpdateOp{object = NewObject}}
|| NewObject = {_Tag, {_ObjectName, _Ref, _Data}} <- NewObjects
],
#domain_conf_v2_CommitResponse{version = Version} = commit(Revision, Commit, AuthorID),
Version.

-spec remove([object()], binary()) -> revision() | no_return().
remove(Objects, AuthorID) ->
Commit = [
{remove, #domain_conf_v2_RemoveOp{
ref = Ref
}}
|| Ref <- Objects
],
#domain_conf_v2_CommitResponse{version = Version} = commit(head(), Commit, AuthorID),
Version.

-spec cleanup([ref()]) -> revision() | no_return().
cleanup(Refs) ->
remove(Refs, generate_author()).

generate_author() ->
Random = genlib:unique(),
Params = #domain_conf_v2_AuthorParams{email = Random, name = Random},
#domain_conf_v2_Author{id = Id} = dmt_client:author_create(Params, #{}),
Id.

maybe_migrate_version(N) when is_number(N) ->
N;
maybe_migrate_version({version, N}) ->
N;
maybe_migrate_version({head, _}) ->
head().
59 changes: 42 additions & 17 deletions apps/party_management/test/pm_claim_committer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ all() ->
-spec init_per_suite(config()) -> config().
init_per_suite(C) ->
{Apps, _Ret} = pm_ct_helper:start_apps([woody, scoper, dmt_client, party_management]),
_ = pm_domain:insert(construct_domain_fixture()),
PartyID = erlang:list_to_binary([?MODULE_STRING, ".", erlang:integer_to_list(erlang:system_time())]),
{_Rev, ObjIds} = pm_domain:insert(construct_domain_fixture()),
PartyID = erlang:list_to_binary([
?MODULE_STRING, ".", erlang:integer_to_list(erlang:system_time())
]),
ApiClient = pm_ct_helper:create_client(),
[{apps, Apps}, {party_id, PartyID}, {api_client, ApiClient} | C].
[{apps, Apps}, {party_id, PartyID}, {api_client, ApiClient}, {objects_ids, ObjIds} | C].

-spec end_per_suite(config()) -> _.
end_per_suite(C) ->
_ = pm_domain:cleanup(),
_ = pm_domain:cleanup(cfg(objects_ids, C)),
[application:stop(App) || App <- cfg(apps, C)].

%%% Tests
Expand Down Expand Up @@ -197,8 +199,12 @@ contract_adjustment_creation(C) ->
PartyID = cfg(party_id, C),
ContractID = ?REAL_CONTRACT_ID1,
ID = <<"ADJ1">>,
AdjustmentParams = #claimmgmt_ContractAdjustmentParams{template = #domain_ContractTemplateRef{id = 2}},
Modifications = [?cm_contract_modification(ContractID, ?cm_adjustment_creation(ID, AdjustmentParams))],
AdjustmentParams = #claimmgmt_ContractAdjustmentParams{
template = #domain_ContractTemplateRef{id = 2}
},
Modifications = [
?cm_contract_modification(ContractID, ?cm_adjustment_creation(ID, AdjustmentParams))
],
Claim = claim(Modifications, PartyID),
ok = accept_claim(Claim, C),
ok = commit_claim(Claim, C),
Expand Down Expand Up @@ -304,14 +310,16 @@ shop_complex_modification(C) ->
id = <<"ID">>,
upper_boundary = 10000,
%% Only needs to be set when TurnoverLimit is in dominant config, otherwise skip it
domain_revision = dmt_client:get_last_version()
domain_revision = dmt_client:get_latest_version()
}
]),
Modifications = [
?cm_shop_modification(ShopID, {category_modification, NewCategory}),
?cm_shop_modification(ShopID, {details_modification, NewDetails}),
?cm_shop_modification(ShopID, {location_modification, NewLocation}),
?cm_shop_modification(ShopID, {cash_register_modification_unit, CashRegisterModificationUnit}),
?cm_shop_modification(
ShopID, {cash_register_modification_unit, CashRegisterModificationUnit}
),
?cm_shop_modification(ShopID, {turnover_limits_modification, TurnoverLimits})
],
Claim = claim(Modifications, PartyID),
Expand All @@ -336,10 +344,15 @@ invalid_cash_register_modification(C) ->
description = <<"Updated shop description.">>
},
AnotherShopID = <<"Totaly not the valid one">>,
Mod = ?cm_shop_modification(AnotherShopID, {cash_register_modification_unit, CashRegisterModificationUnit}),
Mod = ?cm_shop_modification(
AnotherShopID, {cash_register_modification_unit, CashRegisterModificationUnit}
),
Modifications = [?cm_shop_modification(?REAL_SHOP_ID, {details_modification, NewDetails}), Mod],
Claim = claim(Modifications, PartyID),
{exception, ?cm_invalid_party_changeset(?cm_invalid_shop_not_exists(AnotherShopID), [{party_modification, Mod}])} =
{exception,
?cm_invalid_party_changeset(?cm_invalid_shop_not_exists(AnotherShopID), [
{party_modification, Mod}
])} =
accept_claim(Claim, C).

-spec shop_contract_modification(config()) -> _.
Expand Down Expand Up @@ -380,7 +393,9 @@ contractor_already_exists(C) ->
Mod = ?cm_contractor_creation(ContractorID, ContractorParams),
Claim = claim([Mod], PartyID),
{exception,
?cm_invalid_party_changeset(?cm_invalid_contractor_already_exists(ContractorID), [{party_modification, Mod}])} =
?cm_invalid_party_changeset(?cm_invalid_contractor_already_exists(ContractorID), [
{party_modification, Mod}
])} =
accept_claim(Claim, C).

-spec contract_already_exists(config()) -> _.
Expand All @@ -391,7 +406,9 @@ contract_already_exists(C) ->
Mod = ?cm_contract_creation(ContractID, ContractParams),
Claim = claim([Mod], PartyID),
{exception,
?cm_invalid_party_changeset(?cm_invalid_contract_already_exists(ContractID), [{party_modification, Mod}])} =
?cm_invalid_party_changeset(?cm_invalid_contract_already_exists(ContractID), [
{party_modification, Mod}
])} =
accept_claim(Claim, C).

-spec contract_already_terminated(config()) -> _.
Expand All @@ -402,9 +419,11 @@ contract_already_terminated(C) ->
Mod = ?cm_contract_modification(ContractID, {termination, Reason}),
Claim = claim([Mod], PartyID),
{exception,
?cm_invalid_party_changeset(?cm_invalid_contract_invalid_status_terminated(ContractID, _), [
{party_modification, Mod}
])} =
?cm_invalid_party_changeset(
?cm_invalid_contract_invalid_status_terminated(ContractID, _), [
{party_modification, Mod}
]
)} =
accept_claim(Claim, C).

-spec shop_already_exists(config()) -> _.
Expand All @@ -428,7 +447,10 @@ shop_already_exists(C) ->
?cm_shop_account_creation(ShopID, ?cur(<<"RUB">>))
],
Claim = claim(Modifications, PartyID),
{exception, ?cm_invalid_party_changeset(?cm_invalid_shop_already_exists(ShopID), [{party_modification, Mod}])} =
{exception,
?cm_invalid_party_changeset(?cm_invalid_shop_already_exists(ShopID), [
{party_modification, Mod}
])} =
accept_claim(Claim, C).

-spec wallet_account_creation(config()) -> _.
Expand Down Expand Up @@ -492,7 +514,10 @@ claim(PartyModifications, PartyID) ->
id = id(),
party_id = PartyID,
status = {pending, #claimmgmt_ClaimPending{}},
changeset = [?cm_party_modification(id(), ts(), Mod, UserInfo) || Mod <- PartyModifications],
changeset = [
?cm_party_modification(id(), ts(), Mod, UserInfo)
|| Mod <- PartyModifications
],
revision = 1,
created_at = ts()
}.
Expand Down
Loading
Loading