From a81626005c7b65c1321b462d861ca64dac977561 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 17 May 2024 15:55:27 +0200 Subject: [PATCH] Save timeonly on external db --- .../api/controllers/row/ExternalRequest.ts | 120 ++++++++++-------- 1 file changed, 64 insertions(+), 56 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index bd92413851..823330d601 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -1,3 +1,4 @@ +import dayjs from "dayjs" import { AutoFieldSubType, AutoReason, @@ -285,65 +286,72 @@ export class ExternalRequest { // parse floats/numbers if (field.type === FieldType.NUMBER && !isNaN(parseFloat(row[key]))) { newRow[key] = parseFloat(row[key]) - } - // if its not a link then just copy it over - if (field.type !== FieldType.LINK) { - newRow[key] = row[key] - continue - } - const { tableName: linkTableName } = breakExternalTableId(field?.tableId) - // table has to exist for many to many - if (!linkTableName || !this.tables[linkTableName]) { - continue - } - const linkTable = this.tables[linkTableName] - // @ts-ignore - const linkTablePrimary = linkTable.primary[0] - // one to many - if (isOneSide(field)) { - let id = row[key][0] - if (id) { - if (typeof row[key] === "string") { - id = decodeURIComponent(row[key]).match(/\[(.*?)\]/)?.[1] - } - newRow[field.foreignKey || linkTablePrimary] = breakRowIdField(id)[0] - } else { - // Removing from both new and row, as we don't know if it has already been processed - row[field.foreignKey || linkTablePrimary] = null - newRow[field.foreignKey || linkTablePrimary] = null + } else if (field.type === FieldType.LINK) { + const { tableName: linkTableName } = breakExternalTableId( + field?.tableId + ) + // table has to exist for many to many + if (!linkTableName || !this.tables[linkTableName]) { + continue } - } - // many to many - else if (isManyToMany(field)) { - // we're not inserting a doc, will be a bunch of update calls - const otherKey: string = field.throughFrom || linkTablePrimary - const thisKey: string = field.throughTo || tablePrimary - for (const relationship of row[key]) { - manyRelationships.push({ - tableId: field.through || field.tableId, - isUpdate: false, - key: otherKey, - [otherKey]: breakRowIdField(relationship)[0], - // leave the ID for enrichment later - [thisKey]: `{{ literal ${tablePrimary} }}`, - }) - } - } - // many to one - else { - const thisKey: string = "id" + const linkTable = this.tables[linkTableName] // @ts-ignore - const otherKey: string = field.fieldName - for (const relationship of row[key]) { - manyRelationships.push({ - tableId: field.tableId, - isUpdate: true, - key: otherKey, - [thisKey]: breakRowIdField(relationship)[0], - // leave the ID for enrichment later - [otherKey]: `{{ literal ${tablePrimary} }}`, - }) + const linkTablePrimary = linkTable.primary[0] + // one to many + if (isOneSide(field)) { + let id = row[key][0] + if (id) { + if (typeof row[key] === "string") { + id = decodeURIComponent(row[key]).match(/\[(.*?)\]/)?.[1] + } + newRow[field.foreignKey || linkTablePrimary] = + breakRowIdField(id)[0] + } else { + // Removing from both new and row, as we don't know if it has already been processed + row[field.foreignKey || linkTablePrimary] = null + newRow[field.foreignKey || linkTablePrimary] = null + } } + // many to many + else if (isManyToMany(field)) { + // we're not inserting a doc, will be a bunch of update calls + const otherKey: string = field.throughFrom || linkTablePrimary + const thisKey: string = field.throughTo || tablePrimary + for (const relationship of row[key]) { + manyRelationships.push({ + tableId: field.through || field.tableId, + isUpdate: false, + key: otherKey, + [otherKey]: breakRowIdField(relationship)[0], + // leave the ID for enrichment later + [thisKey]: `{{ literal ${tablePrimary} }}`, + }) + } + } + // many to one + else { + const thisKey: string = "id" + // @ts-ignore + const otherKey: string = field.fieldName + for (const relationship of row[key]) { + manyRelationships.push({ + tableId: field.tableId, + isUpdate: true, + key: otherKey, + [thisKey]: breakRowIdField(relationship)[0], + // leave the ID for enrichment later + [otherKey]: `{{ literal ${tablePrimary} }}`, + }) + } + } + } else if ( + field.type === FieldType.DATETIME && + field.timeOnly && + row[key] + ) { + newRow[key] = dayjs(row[key]).format("HH:mm") + } else { + newRow[key] = row[key] } } // we return the relationships that may need to be created in the through table