-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolveNestedObjects.ts
153 lines (136 loc) · 5.58 KB
/
resolveNestedObjects.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* `resolveNestedObjects` middleware
*/
import { Strapi } from "@strapi/strapi";
export default (config, { strapi }: { strapi: Strapi }) => {
return async (ctx, next) => {
const { method, originalUrl } = ctx.request;
const parentObj = ctx.request.body.data;
const slashCount = (originalUrl.match(/\//g) || []).length;
if (method === "POST" && originalUrl.includes("/api/")) {
if (slashCount === 2) {
try {
await strapi.db.transaction(async (transaction) => {
try {
const split = originalUrl.split("/");
const contentType = split[2].slice(0, -1);
// Recursive function to create nested objects and update IDs
const createNestedObjects = async (data) => {
for (const key in data) {
if (typeof data[key] === "object" && data[key] !== null) {
if (Array.isArray(data[key])) {
for (let i = 0; i < data[key].length; i++) {
const nestedObject = data[key][i];
await createNestedObjects(nestedObject);
const createdNestedObject = await strapi
.service(`api::${key}.${key}`)
.create({ data: nestedObject });
data[key][i] = createdNestedObject.id;
}
} else {
const nestedObject = data[key];
await createNestedObjects(nestedObject);
const createdNestedObject = await strapi
.service(`api::${key}.${key}`)
.create({
data: nestedObject,
});
data[key] = createdNestedObject.id;
}
}
}
};
// Create nested objects and update IDs
await createNestedObjects(parentObj);
await strapi
.service(`api::${contentType}.${contentType}`)
.create({
data: parentObj,
});
await transaction.commit();
// Respond with a success message
ctx.status = 200;
ctx.body = {
message: "Object and nested objects created successfully.",
};
} catch (error) {
await transaction.rollback(); // Rollback the transaction if there's an error
ctx.status = 500;
ctx.body = { data: null, error };
}
});
} catch (error) {
// Handle errors and provide a suitable response
ctx.status = 500;
ctx.body = "Internal Server Error";
}
} else {
ctx.status = 500;
ctx.body = "Internal Server Error";
}
} else if (method === "PUT" && originalUrl.includes("/api/")) {
if (slashCount === 3) {
try {
await strapi.db.transaction(async (transaction) => {
try {
// Recursive function to create nested objects and update IDs
const updateNestedObjects = async (data) => {
for (const key in data) {
if (typeof data[key] === "object" && data[key] !== null) {
if (Array.isArray(data[key])) {
for (let i = 0; i < data[key].length; i++) {
const nestedObject = data[key][i];
await updateNestedObjects(nestedObject);
const updatedNestedObject = await strapi
.service(`api::${key}.${key}`)
.update(nestedObject.id, { data: nestedObject });
data[key][i] = updatedNestedObject.id;
}
} else {
const nestedObject = data[key];
await updateNestedObjects(nestedObject);
if (nestedObject.id) {
const updatedNestedObject = await strapi
.service(`api::${key}.${key}`)
.update(nestedObject.id, {
data: nestedObject,
});
data[key] = updatedNestedObject.id;
} else {
const updatedNestedObject = await strapi
.service(`api::${key}.${key}`)
.create({
data: nestedObject,
});
data[key] = updatedNestedObject.id;
}
}
}
}
};
// Update nested objects and update IDs
await updateNestedObjects(parentObj);
// Update the request body with resolved IDs
ctx.request.body.data = parentObj;
await transaction.commit();
await next();
} catch (error) {
console.log(error);
await transaction.rollback(); // Rollback the transaction if there's an error
ctx.status = 500;
ctx.body = { data: null, error };
}
});
} catch (error) {
// Handle errors and provide a suitable response
console.error(error);
ctx.status = 500;
ctx.body = "Internal Server Error";
}
} else {
ctx.status = 500;
ctx.body = "Internal Server Errore";
}
} else await next();
};
};