20260722222510
This commit is contained in:
1 parent
d04fad24ed
commit
59b3ccfb1c
36 files changed
+2753
-3064
No files matched your search
@@ -0,0 +1,55 @@
|
||||
import { nextIdApi } from "@/services/api";
|
||||
|
||||
export function cloneData(value) {
|
||||
return JSON.parse(JSON.stringify(value));
|
||||
}
|
||||
|
||||
export function diffRows(current, previous) {
|
||||
const previousById = new Map(previous.map((item) => [String(item.b_id), item]));
|
||||
const currentById = new Map(current.map((item) => [String(item.b_id), item]));
|
||||
return {
|
||||
inserts: current.filter((item) => !previousById.has(String(item.b_id))),
|
||||
updates: current.filter((item) => {
|
||||
const old = previousById.get(String(item.b_id));
|
||||
return old && JSON.stringify(item) !== JSON.stringify(old);
|
||||
}),
|
||||
deletes: previous
|
||||
.filter((item) => !currentById.has(String(item.b_id)))
|
||||
.map((item) => ({ b_id: item.b_id })),
|
||||
};
|
||||
}
|
||||
|
||||
export async function allocateTemporaryIds(rowGroups) {
|
||||
const temporaryIds = [
|
||||
...new Set(
|
||||
rowGroups
|
||||
.flat()
|
||||
.map((row) => String(row?.b_id ?? ""))
|
||||
.filter((id) => /^-\d+$/.test(id)),
|
||||
),
|
||||
];
|
||||
const generated = await Promise.all(temporaryIds.map(() => nextIdApi()));
|
||||
const mapping = new Map(temporaryIds.map((id, index) => [id, String(generated[index].data)]));
|
||||
rowGroups.flat().forEach((row) => {
|
||||
const id = mapping.get(String(row?.b_id));
|
||||
if (id) row.b_id = id;
|
||||
});
|
||||
return mapping;
|
||||
}
|
||||
|
||||
export function remapForeignKeys(rows, fields, mapping) {
|
||||
rows.forEach((row) => {
|
||||
fields.forEach((field) => {
|
||||
const value = mapping.get(String(row[field]));
|
||||
if (value) row[field] = value;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function tableChange(table, changes) {
|
||||
return { table, key_field: "b_id", ...changes };
|
||||
}
|
||||
|
||||
export function hasChanges(request) {
|
||||
return request.inserts?.length || request.updates?.length || request.deletes?.length;
|
||||
}
|
||||
Reference in new issue
Block a user