Skip to content

Commit 4bf9050

Browse files
fix: When a custom action is triggered on a child entity, the corresponding changelog is captured. (#56)
This PR is to solve the problem that when the custom action of the child entity is triggered, the corresponding change log will not be correctly captured. Co-authored-by: I560824 <[email protected]>
1 parent 728f932 commit 4bf9050

10 files changed

+103
-3
lines changed

lib/change-log.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ async function track_changes (req) {
359359
if (!changes) return
360360

361361
await _formatChangeLog(changes, req)
362-
if (isComposition && !isDraftEnabled) {
362+
if (isComposition) {
363363
[ target, entityKey ] = await _prepareChangeLogForComposition(target, entityKey, changes, this)
364364
}
365365
const dbEntity = getDBEntity(target)

tests/bookshop/db/codelists.cds

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using {sap.common.CodeList as CodeList} from '@sap/cds/common';
2+
3+
namespace sap.capire.bookshop;
4+
5+
entity PaymentAgreementStatusCodes : CodeList {
6+
key code : String(10);
7+
}
8+
9+
entity ActivationStatusCode : CodeList {
10+
key code : String(20);
11+
}

tests/bookshop/db/common/codeLists.cds

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ entity LifecycleStatusCodes : CodeList {
1010
entity BookTypeCodes : CodeList {
1111
key code : String(3);
1212
}
13+
14+
entity ActivationStatusCode : CodeList {
15+
key code : String(20);
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
code;name
2+
INACTIVE;Inactive
3+
ACTIVE;Active
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
code;name
2+
EXPIRED;Expired
3+
VALID;Valid
4+
VALIDLATER;Valid Later
5+
INACTIVE;Inactive

tests/bookshop/db/schema.cds

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ using {
99
sap.capire.common.types.LifecycleStatusCode as LifecycleStatusCode,
1010
sap.capire.common.types.BookTypeCodes as BookTypeCodes,
1111
} from './common/types.cds';
12+
using {sap.capire.bookshop.ActivationStatusCode} from './codelists';
13+
using {sap.capire.bookshop.PaymentAgreementStatusCodes as PaymentAgreementStatusCodes} from './codelists';
1214

1315
namespace sap.capire.bookshop;
1416

@@ -77,6 +79,10 @@ entity Volumns : managed, cuid {
7779
@title : '{i18n>volumns.sequence}'
7880
sequence : Integer;
7981
book : Association to one Books;
82+
@title : '{i18n>Status}'
83+
@changelog : [ActivationStatus.name]
84+
ActivationStatus : Association to one ActivationStatusCode;
85+
PaymentAgreementStatus : Association to one PaymentAgreementStatusCodes on PaymentAgreementStatus.code = ActivationStatus.code;
8086
}
8187

8288
@title : '{i18n>bookStoreRegistry.objectTitle}'
@@ -140,6 +146,9 @@ entity OrderItem : cuid {
140146
entity OrderItemNote : cuid {
141147
orderItem : Association to one OrderItem;
142148
content : String;
149+
@title : '{i18n>Status}'
150+
ActivationStatus : Association to one ActivationStatusCode;
151+
PaymentAgreementStatus : Association to one PaymentAgreementStatusCodes on PaymentAgreementStatus.code = ActivationStatus.code;
143152
}
144153

145154
entity City : cuid {

tests/bookshop/srv/admin-service.cds

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using {sap.capire.bookshop as my} from '../db/schema';
2+
using {sap.capire.bookshop.PaymentAgreementStatusCodes as PaymentAgreementStatusCodes} from '../db/codelists';
23

34
service AdminService {
45
@odata.draft.enabled
@@ -8,8 +9,19 @@ service AdminService {
89
entity Report as projection on my.Report;
910
entity Order as projection on my.Order;
1011
entity OrderItem as projection on my.OrderItem;
11-
entity OrderItemNote as projection on my.OrderItemNote;
12-
entity Volumns as projection on my.Volumns;
12+
13+
entity OrderItemNote as projection on my.OrderItemNote actions {
14+
@cds.odata.bindingparameter.name: 'self'
15+
@Common.SideEffects : {TargetEntities: [self]}
16+
action activate();
17+
};
18+
19+
entity Volumns as projection on my.Volumns actions {
20+
@cds.odata.bindingparameter.name: 'self'
21+
@Common.SideEffects : {TargetEntities: [self]}
22+
action activate();
23+
};
24+
1325
entity Customers as projection on my.Customers;
1426
}
1527

@@ -82,6 +94,7 @@ annotate AdminService.OrderItem with {
8294

8395
annotate AdminService.OrderItemNote with {
8496
content @changelog;
97+
ActivationStatus @changelog : [ActivationStatus.name];
8598
}
8699

87100
annotate AdminService.Customers with {

tests/bookshop/srv/admin-service.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,23 @@ module.exports = cds.service.impl(async (srv) => {
55
const newBookStores = req.data;
66
newBookStores.lifecycleStatus_code = "IP";
77
});
8+
9+
const onActivateVolumns = async (req) => {
10+
const entity = req.entity;
11+
const entityID = req._params[req._params.length - 1].ID;
12+
await UPDATE.entity(entity)
13+
.where({ ID: entityID })
14+
.set({ ActivationStatus_code: "VALID" });
15+
};
16+
17+
const onActivateOrderItemNote = async (req) => {
18+
const entity = req.entity;
19+
const entityID = req._params[req._params.length - 1];
20+
await UPDATE.entity(entity)
21+
.where({ ID: entityID })
22+
.set({ ActivationStatus_code: "VALID" });
23+
};
24+
25+
srv.on("activate", "Volumns", onActivateVolumns);
26+
srv.on("activate", "OrderItemNote", onActivateOrderItemNote);
827
});

tests/integration/fiori-draft-disabled.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,22 @@ describe("change log draft disabled test", () => {
368368
expect(headerChange.parentObjectID).to.equal("Post");
369369
delete cds.services.AdminService.entities.Order["@changelog"];
370370
});
371+
372+
it("11.2 The change log should be captured when a child entity in draft-disabled mode triggers a custom action (ERP4SMEPREPWORKAPPPLAT-6211)", async () => {
373+
await POST(
374+
`/odata/v4/admin/Order(ID=0a41a187-a2ff-4df6-bd12-fae8996e6e31)/orderItems(ID=9a61178f-bfb3-4c17-8d17-c6b4a63e0097)/notes(ID=a40a9fd8-573d-4f41-1111-fa8ea0d8b1bc)/AdminService.activate`,
375+
{
376+
ActivationStatus_code: "VALID",
377+
},
378+
);
379+
let changes = await SELECT.from(ChangeView).where({
380+
entity: "sap.capire.bookshop.OrderItemNote",
381+
attribute: "ActivationStatus",
382+
});
383+
expect(changes.length).to.equal(1);
384+
expect(changes[0].valueChangedFrom).to.equal("");
385+
expect(changes[0].valueChangedTo).to.equal("VALID");
386+
expect(changes[0].entityKey).to.equal("0a41a187-a2ff-4df6-bd12-fae8996e6e31");
387+
expect(changes[0].parentKey).to.equal("9a61178f-bfb3-4c17-8d17-c6b4a63e0097");
388+
});
371389
});

tests/integration/fiori-draft-enabled.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,4 +1082,22 @@ describe("change log integration test", () => {
10821082
expect(registryChange.parentKey).to.equal("8aaed432-8336-4b0d-be7e-3ef1ce7f13ea");
10831083
expect(registryChange.parentObjectID).to.equal("City Lights Books");
10841084
});
1085+
1086+
it("11.1 The change log should be captured when a child entity in draft-enabled mode triggers a custom action (ERP4SMEPREPWORKAPPPLAT-6211)", async () => {
1087+
await POST(
1088+
`/odata/v4/admin/BookStores(ID=64625905-c234-4d0d-9bc1-283ee8946770,IsActiveEntity=true)/books(ID=9d703c23-54a8-4eff-81c1-cdce6b8376b1,IsActiveEntity=true)/volumns(ID=dd1fdd7d-da2a-4600-940b-0baf2946c9bf,IsActiveEntity=true)/AdminService.activate`,
1089+
{
1090+
ActivationStatus_code: "VALID",
1091+
},
1092+
);
1093+
let changes = await SELECT.from(ChangeView).where({
1094+
entity: "sap.capire.bookshop.Volumns",
1095+
attribute: "ActivationStatus",
1096+
});
1097+
expect(changes.length).to.equal(1);
1098+
expect(changes[0].valueChangedFrom).to.equal("");
1099+
expect(changes[0].valueChangedTo).to.equal("VALID");
1100+
expect(changes[0].entityKey).to.equal("64625905-c234-4d0d-9bc1-283ee8946770");
1101+
expect(changes[0].parentKey).to.equal("9d703c23-54a8-4eff-81c1-cdce6b8376b1");
1102+
});
10851103
});

0 commit comments

Comments
 (0)