Skip to content

Commit 20458f3

Browse files
fix: The problem of getting wrong ObjectID in some special scenarios (#65)
Fix issue [Change tracking will capture the wrong objectID in some special cases.](#60) Co-authored-by: I560824 <[email protected]>
1 parent 5b9b458 commit 20458f3

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

lib/entity-helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ async function getObjectId (reqData, entityName, fields, curObj) {
113113
path.shift()
114114
}
115115
field = path.join('_')
116-
let obj = _db_data[field] || req_data[field]
116+
let obj = current.name === entityName && req_data[field] ? req_data[field] : _db_data[field]
117117
if (obj) all.push(obj)
118118
} else {
119119
let e = entity.elements[field]

tests/bookshop/db/schema.cds

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ entity Report : cuid {
185185
}
186186

187187
entity Order : cuid {
188+
@title : '{i18n>title}'
189+
@changelog
190+
title : String;
191+
type : Association to one OrderType;
188192
report : Association to one Report;
189193
header : Composition of one OrderHeader;
190194
orderItems : Composition of many OrderItem
@@ -193,6 +197,12 @@ entity Order : cuid {
193197
status : String;
194198
}
195199

200+
entity OrderType : cuid {
201+
@title: '{i18n>title}'
202+
@changelog
203+
title : String;
204+
}
205+
196206
entity Customers : cuid {
197207
name : String;
198208
city : String;

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,31 @@ describe("change log draft disabled test", () => {
426426
delete cds.services.AdminService.entities.OrderItem["@changelog"];
427427
delete cds.services.AdminService.entities.Level2Object["@changelog"];
428428
delete cds.services.AdminService.entities.Level3Object["@changelog"];
429+
430+
cds.db.entities.Order["@changelog"] = [
431+
{ "=": "title" },
432+
{ "=": "type.title" },
433+
]
434+
await POST(
435+
`/odata/v4/admin/Order`,
436+
{
437+
ID: "0a41a187-a2ff-4df6-bd12-fae8996e7c44",
438+
title: "test Order title",
439+
},
440+
);
441+
442+
const createOrderChanges = await adminService.run(
443+
SELECT.from(ChangeView).where({
444+
entity: "sap.capire.bookshop.Order",
445+
attribute: "title",
446+
modification: "create",
447+
}),
448+
);
449+
450+
expect(createOrderChanges.length).to.equal(1);
451+
const createOrderChange = createOrderChanges[0];
452+
expect(createOrderChange.objectID).to.equal("test Order title");
453+
delete cds.db.entities.Order["@changelog"];
429454
});
430455

431456
it("8.2 Annotate fields from chained associated entities as displayed value (ERP4SMEPREPWORKAPPPLAT-1094 ERP4SMEPREPWORKAPPPLAT-4542)", async () => {

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,34 @@ describe("change log integration test", () => {
788788
cds.services.AdminService.entities.BookStores["@changelog"] = [{ "=": "name" }];
789789
});
790790

791-
it("7.1 Annotate fields from chained associated entities as objectID (ERP4SMEPREPWORKAPPPLAT-993 ERP4SMEPREPWORKAPPPLAT-4542)", async () => {
791+
it("7.1 Annotate fields from chained associated entities as objectID (ERP4SMEPREPWORKAPPPLAT-993)", async () => {
792+
cds.services.AdminService.entities.BookStores["@changelog"].push({ "=": "city.name" })
793+
794+
const createBookStoresAction = POST.bind({}, `/odata/v4/admin/BookStores`, {
795+
ID: "9d703c23-54a8-4eff-81c1-cdce6b6587c4",
796+
name: "new name",
797+
});
798+
await utils.apiAction(
799+
"admin",
800+
"BookStores",
801+
"9d703c23-54a8-4eff-81c1-cdce6b6587c4",
802+
"AdminService",
803+
createBookStoresAction,
804+
true
805+
);
806+
807+
const BookStoresChanges = await adminService.run(
808+
SELECT.from(ChangeView).where({
809+
entity: "sap.capire.bookshop.BookStores",
810+
attribute: "name",
811+
})
812+
);
813+
expect(BookStoresChanges.length).to.equal(1);
814+
const BookStoresChange = BookStoresChanges[0];
815+
expect(BookStoresChange.objectID).to.equal("new name");
816+
817+
delete cds.services.AdminService.entities.BookStores["@changelog"];
818+
792819
cds.services.AdminService.entities.Books["@changelog"] = [
793820
{ "=": "bookStore.lifecycleStatus.name" },
794821
{ "=": "bookStore.location" },

tests/integration/service-api.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ describe("change log integration test", () => {
7070
});
7171

7272
it("7.3 Annotate fields from chained associated entities as objectID (ERP4SMEPREPWORKAPPPLAT-4542)", async () => {
73+
cds.services.AdminService.entities.BookStores["@changelog"].push({ "=": "city.name" })
74+
75+
const bookStoreData = {
76+
ID: "9d703c23-54a8-4eff-81c1-cdce6b6587c4",
77+
name: "new name",
78+
};
79+
await adminService.run(INSERT.into(adminService.entities.BookStores).entries(bookStoreData));
80+
let createBookStoresChanges = await SELECT.from(ChangeView).where({
81+
entity: "sap.capire.bookshop.BookStores",
82+
attribute: "name",
83+
modification: "create",
84+
});
85+
expect(createBookStoresChanges.length).to.equal(1);
86+
const createBookStoresChange = createBookStoresChanges[0];
87+
expect(createBookStoresChange.objectID).to.equal("new name");
88+
89+
cds.services.AdminService.entities.BookStores["@changelog"].pop();
90+
7391
const level3EntityData = [
7492
{
7593
ID: "12ed5dd8-d45b-11ed-afa1-0242ac654321",

0 commit comments

Comments
 (0)