Skip to content

Commit 5bac42d

Browse files
committed
Reduce function complexity from 20 to 15 allowed
1 parent 6fcbaf2 commit 5bac42d

1 file changed

Lines changed: 37 additions & 22 deletions

File tree

packages/server/api/src/app/database/migrations/1741945618000-ReplaceSelectOptionsIdsWithNames.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,40 +121,55 @@ async function updateJsonObject(
121121
obj: any,
122122
tablesServerContext: TablesServerContext,
123123
): Promise<any> {
124-
if (!obj || typeof obj !== 'object') return obj;
124+
if (!obj || typeof obj !== 'object') {
125+
return obj;
126+
}
125127

126-
if (obj.input?.tableName) {
127-
const fields = await getFieldsFromCache(
128-
obj.input.tableName,
129-
tablesServerContext,
130-
);
128+
await updateInputFields(obj, tablesServerContext);
129+
await processObjectKeys(obj, tablesServerContext);
131130

132-
if (!fields) {
133-
return obj;
134-
}
131+
return obj;
132+
}
133+
134+
async function updateInputFields(
135+
obj: any,
136+
tablesServerContext: TablesServerContext,
137+
): Promise<void> {
138+
if (!obj.input?.tableName) {
139+
return;
140+
}
135141

136-
if (obj.input.fieldsProperties?.fieldsProperties) {
137-
for (const field of obj.input.fieldsProperties.fieldsProperties) {
138-
if (!field.fieldName) {
139-
continue;
140-
}
142+
const fields = await getFieldsFromCache(
143+
obj.input.tableName,
144+
tablesServerContext,
145+
);
141146

142-
updateFieldValue(field, fields.get(field.fieldName));
143-
}
147+
if (!fields || !obj.input.fieldsProperties?.fieldsProperties) {
148+
return;
149+
}
150+
151+
for (const field of obj.input.fieldsProperties.fieldsProperties) {
152+
if (field.fieldName) {
153+
updateFieldValue(field, fields.get(field.fieldName));
144154
}
145155
}
156+
}
146157

158+
async function processObjectKeys(
159+
obj: any,
160+
tablesServerContext: TablesServerContext,
161+
): Promise<void> {
147162
for (const key of Object.keys(obj)) {
148163
if (Array.isArray(obj[key])) {
149-
for (const item of obj[key]) {
150-
obj[key] = await updateJsonObject(item, tablesServerContext);
151-
}
152-
} else if (typeof obj[key] === 'object') {
164+
obj[key] = await Promise.all(
165+
obj[key].map((item: any) =>
166+
updateJsonObject(item, tablesServerContext),
167+
),
168+
);
169+
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
153170
obj[key] = await updateJsonObject(obj[key], tablesServerContext);
154171
}
155172
}
156-
157-
return obj;
158173
}
159174

160175
function updateFieldValue(field: any, fieldMap?: Map<number, string>) {

0 commit comments

Comments
 (0)