fix delete functionality

This commit is contained in:
kevmodrome 2020-10-08 14:45:50 +02:00
parent 480a73997d
commit da32029c31
1 changed files with 285 additions and 285 deletions

View File

@ -63,71 +63,11 @@ exports.patch = async function (ctx) {
} }
exports.save = async function (ctx) { exports.save = async function (ctx) {
const instanceId = ctx.user.instanceId if (ctx.request.body.type === 'delete') {
const db = new CouchDB(instanceId) await bulkDelete(ctx)
let record = ctx.request.body } else {
record.modelId = ctx.params.modelId await saveRecords(ctx)
if (!record._rev && !record._id) {
record._id = generateRecordID(record.modelId)
} }
exports.save = async function (ctx) {
const db = new CouchDB(ctx.user.instanceId)
let record = ctx.request.body
record.modelId = ctx.params.modelId
if (!record._rev && !record._id) {
record._id = generateRecordID(record.modelId)
}
const model = await db.get(record.modelId)
record = coerceRecordValues(record, model)
const validateResult = await validate({
record,
model,
})
if (!validateResult.valid) {
ctx.status = 400
ctx.body = {
status: 400,
errors: validateResult.errors,
}
return
}
const existingRecord = record._rev && (await db.get(record._id))
// make sure link records are up to date
record = await linkRecords.updateLinks({
instanceId,
eventType: linkRecords.EventType.RECORD_SAVE,
record,
modelId: record.modelId,
model,
})
if (existingRecord) {
const response = await db.put(record)
record._rev = response.rev
record.type = "record"
ctx.body = record
ctx.status = 200
ctx.message = `${model.name} updated successfully.`
return
}
record.type = "record"
const response = await db.post(record)
record._rev = response.rev
ctx.eventEmitter &&
ctx.eventEmitter.emitRecord(`record:save`, instanceId, record, model)
ctx.body = record
ctx.status = 200
ctx.message = `${model.name} created successfully`
} }
exports.fetchView = async function (ctx) { exports.fetchView = async function (ctx) {
@ -224,23 +164,6 @@ exports.save = async function (ctx) {
ctx.eventEmitter.emitRecord(`record:delete`, instanceId, record) ctx.eventEmitter.emitRecord(`record:delete`, instanceId, record)
} }
async function bulkDelete(ctx) {
const { records } = ctx.request.body
const db = new CouchDB(ctx.user.instanceId)
await db.bulkDocs(
records.map(record => ({ ...record, _deleted: true }), (err, res) => {
if (err) {
ctx.status = 500
} else {
records.forEach(record => {
emitEvent(`record:delete`, ctx, record)
})
ctx.status = 200
}
}))
}
exports.validate = async function (ctx) { exports.validate = async function (ctx) {
const errors = await validate({ const errors = await validate({
instanceId: ctx.user.instanceId, instanceId: ctx.user.instanceId,
@ -328,6 +251,83 @@ exports.save = async function (ctx) {
return record return record
} }
async function bulkDelete(ctx) {
const { records } = ctx.request.body
const db = new CouchDB(ctx.user.instanceId)
await db.bulkDocs(
records.map(record => ({ ...record, _deleted: true }), (err, res) => {
if (err) {
ctx.status = 500
} else {
records.forEach(record => {
emitEvent(`record:delete`, ctx, record)
})
ctx.status = 200
}
}))
}
async function saveRecords(ctx) {
const instanceId = ctx.user.instanceId
const db = new CouchDB(instanceId)
let record = ctx.request.body
record.modelId = ctx.params.modelId
if (!record._rev && !record._id) {
record._id = generateRecordID(record.modelId)
}
const model = await db.get(record.modelId)
record = coerceRecordValues(record, model)
const validateResult = await validate({
record,
model,
})
if (!validateResult.valid) {
ctx.status = 400
ctx.body = {
status: 400,
errors: validateResult.errors,
}
return
}
const existingRecord = record._rev && (await db.get(record._id))
// make sure link records are up to date
record = await linkRecords.updateLinks({
instanceId,
eventType: linkRecords.EventType.RECORD_SAVE,
record,
modelId: record.modelId,
model,
})
if (existingRecord) {
const response = await db.put(record)
record._rev = response.rev
record.type = "record"
ctx.body = record
ctx.status = 200
ctx.message = `${model.name} updated successfully.`
return
}
record.type = "record"
const response = await db.post(record)
record._rev = response.rev
ctx.eventEmitter &&
ctx.eventEmitter.emitRecord(`record:save`, instanceId, record, model)
ctx.body = record
ctx.status = 200
ctx.message = `${model.name} created successfully`
}
const TYPE_TRANSFORM_MAP = { const TYPE_TRANSFORM_MAP = {
link: { link: {
"": [], "": [],