Skip to content

[FIX] point_of_sale: get method and orm_serialization #4774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master-pos_ref_syncing_team-branch
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export class Base extends WithLazyGetterTrap {
* This method is called when the instance is created or updated
* @param {*} _vals
*/
setup(_vals) {}
setup(_vals) {
this._dirty = typeof this.id !== "number";
}

/**
* This method is invoked only during instance creation to preserve the state across updates.
Expand Down Expand Up @@ -77,7 +79,28 @@ export class Base extends WithLazyGetterTrap {
return { ...this.uiState };
}

_markDirty() {
if (this.models._loadingData || this._dirty) {
return;
}

this._dirty = true;
this.model.getParentFields().forEach((field) => {
this[field.name]?._markDirty?.();
});
}

backLink(link) {
return this.model.backLink(this, link);
}

getId() {
// Returns integer id if exist or return uuid
const id = this.id;
if (typeof id === "number" || !this.uuid) {
return id;
}
const idUpdates = JSON.parse(localStorage.getItem("idUpdates")) || {};
return idUpdates[this.uuid] || this.uuid;
}
}
26 changes: 23 additions & 3 deletions addons/point_of_sale/static/src/app/models/related_models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,29 @@ export function createRelatedModels(modelDefs, modelClasses = {}, opts = {}) {
return result;
}

getUuidFromId(id) {
if (typeof id === "string") {
return id;
}
const modelUuids = this.map((rec) => rec.uuid);
const idUpdates = JSON.parse(localStorage.getItem("idUpdates")) || {};
const entry = Object.entries(idUpdates).find(
([_uuid, _id]) => modelUuids.includes(_uuid) && _id === id
);
if (entry) {
return entry[0];
}
return false;
}

read(value) {
const id = /^\d+$/.test(value) ? parseInt(value) : value; // In case of ID came from an input
return this[STORE_SYMBOL].getById(this.name, id);
const record = this[STORE_SYMBOL].getById(this.name, id);
const uuid = record || this.fields["uuid"] ? this.getUuidFromId(id) : null;
if (record || !uuid) {
return record;
}
return this[STORE_SYMBOL].get(this.name, "uuid", uuid);
}

readFirst() {
Expand Down Expand Up @@ -450,7 +470,7 @@ export function createRelatedModels(modelDefs, modelClasses = {}, opts = {}) {
}

_delete(record, opts = {}) {
const id = record.id;
const id = record.getId();
const ownFields = getFields(this.name);
const handleCommand = (inverse, field, record, backend = false) => {
if (inverse && !inverse.dummy && typeof id === "number") {
Expand All @@ -459,7 +479,7 @@ export function createRelatedModels(modelDefs, modelClasses = {}, opts = {}) {
const oldVal = map.get(inverse.name);
map.set(inverse.name, [
...(oldVal || []),
{ id: record.id, parentId: record[field.name].id },
{ id: record.id, parentId: record[field.name].id, getId: record.getId() },
]);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ const deepSerialization = (
continue;
}

if (typeof childRecord.id !== "number") {
if (typeof childRecord.getId() !== "number") {
toCreate.push(childRecord);
} else if (childRecord._dirty) {
toUpdate.push(childRecord);
childRecord._dirty = false;
}
serialized[relatedModel][childRecord.uuid] = childRecord.uuid;
}
Expand All @@ -67,7 +70,7 @@ const deepSerialization = (
...(result[fieldName] || []),
...toUpdate.map((childRecord) => [
1,
childRecord.id,
childRecord.getId(),
recursiveSerialize(childRecord, field.inverse_name),
]),
...toCreate.map((childRecord) => [
Expand All @@ -93,15 +96,15 @@ const deepSerialization = (
if (modelCommands.unlink.has(fieldName) || modelCommands.delete.has(fieldName)) {
result[fieldName] = result[fieldName] || [];
const processRecords = (records, cmdCode) => {
for (const { id, parentId } of records) {
for (const { id, parentId, getId } of records) {
const isAlreadyDeleted = serialized[relatedModel]?.["_deleted_" + id];
if (parentId === record.id && !isAlreadyDeleted) {
const isCascadeDelete =
record.models[relatedModel]?.fields[field.inverse_name]?.ondelete;
if (isCascadeDelete) {
serialized[relatedModel]["_deleted_" + id] = true;
}
result[fieldName].push([cmdCode, id]);
result[fieldName].push([cmdCode, getId]);
}
}
};
Expand Down Expand Up @@ -164,6 +167,8 @@ const deepSerialization = (
res[key] = getValue();
}

record._dirty = false;

// Cleanup: remove empty entries from uuidMapping.
for (const key in uuidMapping) {
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export class AggregatedUpdates {
[...fields].map((fieldName) => [fieldName, record[fieldName]])
),
});
record._markDirty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,31 +161,31 @@ describe("ORM serialization", () => {
}

// Server results with ids
models.connectNewData({
"pos.order": [{ ...order.raw, id: 1, lines: [11, 12] }],
"pos.order.line": [
{ ...line1.raw, id: "b", order_id: 1 },
{ ...line2.raw, id: "c", order_id: 1 },
],
});
// debugger
const idUpdates = {
[order.uuid]: 1,
[line1.uuid]: 11,
[line2.uuid]: 12,
};
localStorage.setItem("idUpdates", JSON.stringify(idUpdates));

line1.quantity = 99;
// {
// const result = models.serializeForORM(order);
// expect(result.lines.length).toBe(1);
// expect(result.lines[0][0]).toBe(1);
// expect(result.lines[0][1]).toBe(11);
// expect(result.lines[0][2].quantity).toBe(99);
// }

// // Delete line
// line1.delete();
// {
// const result = models.serializeForORM(order);
// expect(result.lines.length).toBe(1);
// expect(result.lines[0][0]).toBe(3);
// expect(result.lines[0][1]).toBe(11);
// }
{
const result = models.serializeForORM(order);
expect(result.lines.length).toBe(1);
expect(result.lines[0][0]).toBe(1);
expect(result.lines[0][1]).toBe(11);
expect(result.lines[0][2].quantity).toBe(99);
}

// Delete line
line1.delete();
{
const result = models.serializeForORM(order);
expect(result.lines.length).toBe(1);
expect(result.lines[0][0]).toBe(3);
expect(result.lines[0][1]).toBe(11);
}
});

test("serialization of non-dynamic model relationships", () => {
Expand Down Expand Up @@ -287,14 +287,14 @@ describe("ORM serialization", () => {
parentLine = models["pos.order.line"].getBy("uuid", parentLine.uuid);

line1.quantity = 99;
// {
// const result = models.serializeForORM(order);
// expect(result.lines.length).toBe(1);
// expect(result.lines[0][0]).toBe(1);
// expect(result.lines[0][1]).toBe(11);
// expect(result.lines[0][2].quantity).toBe(99);
// expect(result.relations_uuid_mapping).toBe(undefined);
// }
{
const result = models.serializeForORM(order);
expect(result.lines.length).toBe(1);
expect(result.lines[0][0]).toBe(1);
expect(result.lines[0][1]).toBe(11);
expect(result.lines[0][2].quantity).toBe(99);
expect(result.relations_uuid_mapping).toBe(undefined);
}
});

test("recursive relationship with group of lines", () => {
Expand Down Expand Up @@ -402,46 +402,46 @@ describe("ORM serialization", () => {
expect(order.groups[0].lines.length).toBe(1);
expect(order.groups[1].lines.length).toBe(1);

// {
// const result = models.serializeForORM(order);
// expect(result.groups[0].lines).toBeEmpty();
// expect(result.groups[1].lines).toBeEmpty();
// expect(result.lines.length).toBe(1);
// expect(result.lines[0][0]).toBe(1);
// expect(result.lines[0][1]).toBe(111);
// expect(result.lines[0][2].group_id).toBe(group2.id);
// expect(result.relations_uuid_mapping).toBe(undefined);
// }

// // Delete line
// line1.delete();
// //update the group, to be sure the lines are empty
// group1.index = 3;
// group2.index = 4;
// {
// const result = models.serializeForORM(order);
// expect(result.groups.length).toBe(2);
// expect(result.groups[0][0]).toBe(1);
// expect(result.groups[0][1]).toBe(group1.id);
// expect(result.groups[0][2].index).toBe(3);
// expect(result.groups[0][2].lines).toBeEmpty();
// expect(result.groups[1][0]).toBe(1);
// expect(result.groups[1][1]).toBe(group2.id);
// expect(result.groups[1][2].index).toBe(4);
// expect(result.groups[1][2].lines).toBeEmpty();

// expect(result.lines.length).toBe(1);
// expect(result.lines[0][0]).toBe(3);
// expect(result.lines[0][1]).toBe(110);
// expect(result.relations_uuid_mapping).toBe(undefined);
// }

// {
// //All update/delete have been cleared
// const result = models.serializeForORM(order);
// expect(result.groups).toBeEmpty();
// expect(result.lines).toBeEmpty();
// }
{
const result = models.serializeForORM(order);
expect(result.groups[0].lines).toBeEmpty();
expect(result.groups[1].lines).toBeEmpty();
expect(result.lines.length).toBe(1);
expect(result.lines[0][0]).toBe(1);
expect(result.lines[0][1]).toBe(111);
expect(result.lines[0][2].group_id).toBe(group2.id);
expect(result.relations_uuid_mapping).toBe(undefined);
}

// Delete line
line1.delete();
//update the group, to be sure the lines are empty
group1.index = 3;
group2.index = 4;
{
const result = models.serializeForORM(order);
expect(result.groups.length).toBe(2);
expect(result.groups[0][0]).toBe(1);
expect(result.groups[0][1]).toBe(group1.id);
expect(result.groups[0][2].index).toBe(3);
expect(result.groups[0][2].lines).toBeEmpty();
expect(result.groups[1][0]).toBe(1);
expect(result.groups[1][1]).toBe(group2.id);
expect(result.groups[1][2].index).toBe(4);
expect(result.groups[1][2].lines).toBeEmpty();

expect(result.lines.length).toBe(1);
expect(result.lines[0][0]).toBe(3);
expect(result.lines[0][1]).toBe(110);
expect(result.relations_uuid_mapping).toBe(undefined);
}

{
//All update/delete have been cleared
const result = models.serializeForORM(order);
expect(result.groups).toBeEmpty();
expect(result.lines).toBeEmpty();
}
});

test("grouped lines and nested lines", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ describe("Related Model", () => {
order.sampleData = "test";

expect(order.lines.length).toBe(1);
const oldLineId = order.lines[0].id;
expect(typeof oldOrderId).toBe("string");
expect(models["pos.order"].get(oldOrderId)).not.toBeEmpty();

Expand All @@ -694,11 +693,9 @@ describe("Related Model", () => {
expect(order.lines[0].name).toBe("Line 1111");

//Find by ids, uuids
expect(models["pos.order"].get(oldOrderId)).toBe(undefined);
expect(models["pos.order"].get(order.id)).toBe(order);
expect(models["pos.order"].getBy("uuid", order.uuid)).toBe(order);

expect(models["pos.order.line"].get(oldLineId)).toBe(undefined);
expect(models["pos.order.line"].get(line.id)).toBe(line);
expect(models["pos.order.line"].getBy("uuid", line.uuid)).toBe(line);
});
Expand Down