|
|
@ -11,6 +11,7 @@ const { |
|
|
inputProcessing, |
|
|
inputProcessing, |
|
|
outputProcessing, |
|
|
outputProcessing, |
|
|
processAutoColumn, |
|
|
processAutoColumn, |
|
|
|
|
|
cleanupAttachments, |
|
|
} = require("../../../utilities/rowProcessor") |
|
|
} = require("../../../utilities/rowProcessor") |
|
|
const { FieldTypes } = require("../../../constants") |
|
|
const { FieldTypes } = require("../../../constants") |
|
|
const { isEqual } = require("lodash") |
|
|
const { isEqual } = require("lodash") |
|
|
@ -25,6 +26,7 @@ const { |
|
|
getFromDesignDoc, |
|
|
getFromDesignDoc, |
|
|
getFromMemoryDoc, |
|
|
getFromMemoryDoc, |
|
|
} = require("../view/utils") |
|
|
} = require("../view/utils") |
|
|
|
|
|
const { cloneDeep } = require("lodash/fp") |
|
|
|
|
|
|
|
|
const CALCULATION_TYPES = { |
|
|
const CALCULATION_TYPES = { |
|
|
SUM: "sum", |
|
|
SUM: "sum", |
|
|
@ -109,14 +111,14 @@ exports.patch = async ctx => { |
|
|
const inputs = ctx.request.body |
|
|
const inputs = ctx.request.body |
|
|
const tableId = inputs.tableId |
|
|
const tableId = inputs.tableId |
|
|
const isUserTable = tableId === InternalTables.USER_METADATA |
|
|
const isUserTable = tableId === InternalTables.USER_METADATA |
|
|
let dbRow |
|
|
let oldRow |
|
|
try { |
|
|
try { |
|
|
dbRow = await db.get(inputs._id) |
|
|
oldRow = await db.get(inputs._id) |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
if (isUserTable) { |
|
|
if (isUserTable) { |
|
|
// don't include the rev, it'll be the global rev
|
|
|
// don't include the rev, it'll be the global rev
|
|
|
// this time
|
|
|
// this time
|
|
|
dbRow = { |
|
|
oldRow = { |
|
|
_id: inputs._id, |
|
|
_id: inputs._id, |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
@ -125,13 +127,14 @@ exports.patch = async ctx => { |
|
|
} |
|
|
} |
|
|
let dbTable = await db.get(tableId) |
|
|
let dbTable = await db.get(tableId) |
|
|
// need to build up full patch fields before coerce
|
|
|
// need to build up full patch fields before coerce
|
|
|
|
|
|
let combinedRow = cloneDeep(oldRow) |
|
|
for (let key of Object.keys(inputs)) { |
|
|
for (let key of Object.keys(inputs)) { |
|
|
if (!dbTable.schema[key]) continue |
|
|
if (!dbTable.schema[key]) continue |
|
|
dbRow[key] = inputs[key] |
|
|
combinedRow[key] = inputs[key] |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// this returns the table and row incase they have been updated
|
|
|
// this returns the table and row incase they have been updated
|
|
|
let { table, row } = inputProcessing(ctx.user, dbTable, dbRow) |
|
|
let { table, row } = inputProcessing(ctx.user, dbTable, combinedRow) |
|
|
const validateResult = await validate({ |
|
|
const validateResult = await validate({ |
|
|
row, |
|
|
row, |
|
|
table, |
|
|
table, |
|
|
@ -149,6 +152,8 @@ exports.patch = async ctx => { |
|
|
tableId: row.tableId, |
|
|
tableId: row.tableId, |
|
|
table, |
|
|
table, |
|
|
}) |
|
|
}) |
|
|
|
|
|
// check if any attachments removed
|
|
|
|
|
|
await cleanupAttachments(appId, table, { oldRow, row }) |
|
|
|
|
|
|
|
|
if (isUserTable) { |
|
|
if (isUserTable) { |
|
|
// the row has been updated, need to put it into the ctx
|
|
|
// the row has been updated, need to put it into the ctx
|
|
|
@ -295,6 +300,8 @@ exports.destroy = async function (ctx) { |
|
|
row, |
|
|
row, |
|
|
tableId: row.tableId, |
|
|
tableId: row.tableId, |
|
|
}) |
|
|
}) |
|
|
|
|
|
// remove any attachments that were on the row from object storage
|
|
|
|
|
|
await cleanupAttachments(appId, table, { row }) |
|
|
|
|
|
|
|
|
let response |
|
|
let response |
|
|
if (ctx.params.tableId === InternalTables.USER_METADATA) { |
|
|
if (ctx.params.tableId === InternalTables.USER_METADATA) { |
|
|
@ -341,6 +348,8 @@ exports.bulkDestroy = async ctx => { |
|
|
} else { |
|
|
} else { |
|
|
await db.bulkDocs(rows.map(row => ({ ...row, _deleted: true }))) |
|
|
await db.bulkDocs(rows.map(row => ({ ...row, _deleted: true }))) |
|
|
} |
|
|
} |
|
|
|
|
|
// remove any attachments that were on the rows from object storage
|
|
|
|
|
|
await cleanupAttachments(appId, table, { rows }) |
|
|
await Promise.all(updates) |
|
|
await Promise.all(updates) |
|
|
return { response: { ok: true }, rows } |
|
|
return { response: { ok: true }, rows } |
|
|
} |
|
|
} |
|
|
|