Fix external patches

This commit is contained in:
Adria Navarro 2024-12-17 18:50:17 +01:00
parent 84a67bb346
commit d061f44eda
2 changed files with 29 additions and 6 deletions

View File

@ -52,10 +52,22 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
const table = await utils.getTableFromSource(source) const table = await utils.getTableFromSource(source)
const { _id, ...rowData } = ctx.request.body const { _id, ...rowData } = ctx.request.body
const dataToUpdate = await inputProcessing( const beforeRow = await sdk.rows.external.getRow(table._id!, _id, {
relationships: true,
})
let dataToUpdate = cloneDeep(beforeRow)
const allowedField = utils.getSourceFields(source)
for (const key of Object.keys(rowData)) {
if (!allowedField.includes(key)) continue
dataToUpdate[key] = rowData[key]
}
dataToUpdate = await inputProcessing(
ctx.user?._id, ctx.user?._id,
cloneDeep(source), cloneDeep(source),
rowData dataToUpdate
) )
const validateResult = await sdk.rows.utils.validate({ const validateResult = await sdk.rows.utils.validate({
@ -66,10 +78,6 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
throw { validation: validateResult.errors } throw { validation: validateResult.errors }
} }
const beforeRow = await sdk.rows.external.getRow(table._id!, _id, {
relationships: true,
})
const response = await handleRequest(Operation.UPDATE, source, { const response = await handleRequest(Operation.UPDATE, source, {
id: breakRowIdField(_id), id: breakRowIdField(_id),
row: dataToUpdate, row: dataToUpdate,

View File

@ -110,6 +110,21 @@ function fixBooleanFields(row: Row, table: Table) {
return row return row
} }
export function getSourceFields(source: Table | ViewV2): string[] {
const isView = sdk.views.isView(source)
if (isView) {
const fields = Object.keys(
helpers.views.basicFields(source, { visible: true })
)
return fields
}
const fields = Object.entries(source.schema)
.filter(([_, field]) => field.visible !== false)
.map(([columnName]) => columnName)
return fields
}
export async function sqlOutputProcessing( export async function sqlOutputProcessing(
rows: DatasourcePlusQueryResponse, rows: DatasourcePlusQueryResponse,
source: Table | ViewV2, source: Table | ViewV2,