1 import { isObject } from "../common/is-object"
2
3 interface ItemRecord extends Record<string, any> {
4 id: string
5 }
6
7 export function convertItemResponseToUpdateRequest(
8 item: ItemRecord,
9 selects: string[],
10 relations: string[],
11 fromManyRelationships: boolean = false
12 ): ItemRecord {
13 const newItem: ItemRecord = {
14 id: item.id,
15 }
16
17 // If item is a child of a many relationship, we just need to pass in the id of the item
18 if (fromManyRelationships) {
19 return newItem
20 }
21
22 for (const [key, value] of Object.entries(item)) {
23 if (relations.includes(key)) {
24 const relation = item[key]
25
26 // If the relationship is an object, its either a one to one or many to one relationship
27 // We typically don't update the parent from the child relationship, we can skip this for now.
28 // This can be focused on solely for one to one relationships
29 if (isObject(relation)) {
30 // If "id" is the only one in the object, underscorize the relation. This is assuming that
31 // the relationship itself was changed to another item and now we need to revert it to the old item.
32 if (Object.keys(relation).length === 1 && "id" in relation) {
33 newItem[`${key}_id`] = relation.id
34 }
35
36 // If attributes of the relation have been updated, we can assume that this
37 // was an update operation on the relation. We revert what was updated.
38 if (Object.keys(relation).length > 1) {
39 // The ID can be figured out from the relationship, we can delete the ID here
40 if ("id" in relation) {
41 delete relation.id
42 }
43
44 // we just need the selects for the relation, filter it out and remove the parent scope
45 const filteredSelects = selects
46 .filter((s) => s.startsWith(key) && !s.includes("id"))
47 .map(shiftFirstPath)
48
49 // Add the filtered selects to the sanitized object
50 for (const filteredSelect of filteredSelects) {
51 newItem[key] = newItem[key] || {}
52 newItem[key][filteredSelect] = relation[filteredSelect]
53 }
54 }
55
56 continue
57 }
58
59 // If the relation is an array, we can expect this to be a one to many or many to many
60 // relationships. Recursively call the function until all relations are converted
61 if (Array.isArray(relation)) {
62 const newRelationsArray: ItemRecord[] = []
63
64 for (const rel of relation) {
65 // Scope selects and relations to ones that are relevant to the current relation
66 const filteredRelations = relations
67 .filter((r) => r.startsWith(key))
68 .map(shiftFirstPath)
69
70 const filteredSelects = selects
71 .filter((s) => s.startsWith(key))
72 .map(shiftFirstPath)
73
74 newRelationsArray.push(
75 convertItemResponseToUpdateRequest(
76 rel,
77 filteredSelects,
78 filteredRelations,
79 true
80 )
81 )
82 }
83
84 newItem[key] = newRelationsArray
85 }
86 }
87
88 // if the key exists in the selects, we add them to the new sanitized array.
89 // sanitisation is done because MikroORM adds relationship attributes and other default attributes
90 // which we do not want to add to the update request
91 if (selects.includes(key) && !fromManyRelationships) {
92 newItem[key] = value
93 }
94 }
95
96 return newItem
97 }
98
99 function shiftFirstPath(select) {
100 const selectArray = select.split(".")
101 selectArray.shift()
102
103 return selectArray.join(".")
104 }