fix delete functionality
This commit is contained in:
parent
91e27804e5
commit
579166082d
|
@ -63,71 +63,11 @@ exports.patch = async function (ctx) {
|
|||
}
|
||||
|
||||
exports.save = async function (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)
|
||||
if (ctx.request.body.type === 'delete') {
|
||||
await bulkDelete(ctx)
|
||||
} else {
|
||||
await saveRecords(ctx)
|
||||
}
|
||||
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) {
|
||||
|
@ -224,23 +164,6 @@ exports.save = async function (ctx) {
|
|||
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) {
|
||||
const errors = await validate({
|
||||
instanceId: ctx.user.instanceId,
|
||||
|
@ -328,6 +251,83 @@ exports.save = async function (ctx) {
|
|||
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 = {
|
||||
link: {
|
||||
"": [],
|
||||
|
|
Loading…
Reference in New Issue