From 045822d65e8bccc0ef9e3dfe775cc6bbebae09f6 Mon Sep 17 00:00:00 2001 From: adrinr Date: Fri, 24 Feb 2023 11:24:29 +0100 Subject: [PATCH] Dry --- .../api/controllers/row/ExternalRequest.ts | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 45940abff8..f60d1b83ab 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -154,11 +154,12 @@ function generateIdForRow( // build id array let idParts = [] for (let field of primary) { - // need to handle table name + field or just field, depending on if relationships used - let fieldValue = row[`${table.name}.${field}`] - if (!fieldValue && !isLinked) { - fieldValue = row[field] - } + let fieldValue = extractFieldValue({ + row, + tableName: table.name, + fieldName: field, + isLinked, + }) if (fieldValue) { idParts.push(fieldValue) } @@ -181,6 +182,25 @@ function getEndpoint(tableId: string | undefined, operation: string) { } } +// need to handle table name + field or just field, depending on if relationships used +function extractFieldValue({ + row, + tableName, + fieldName, + isLinked, +}: { + row: Row + tableName: string + fieldName: string + isLinked: boolean +}) { + let value = row[`${tableName}.${fieldName}`] + if (value == null && !isLinked) { + value = row[fieldName] + } + return value +} + function basicProcessing({ row, table, @@ -195,8 +215,13 @@ function basicProcessing({ for (let field of Object.values(table.schema)) { const fieldName = field.name - const pathValue = row[`${table.name}.${fieldName}`] - const value = pathValue != null || isLinked ? pathValue : row[fieldName] + const value = extractFieldValue({ + row, + tableName: table.name, + fieldName, + isLinked, + }) + // all responses include "select col as table.col" so that overlaps are handled if (value != null) { thisRow[fieldName] = value