2020-09-10 16:00:21 +02:00
|
|
|
const userController = require("../api/controllers/user")
|
|
|
|
const recordController = require("../api/controllers/record")
|
|
|
|
const sgMail = require("@sendgrid/mail")
|
|
|
|
|
|
|
|
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
|
|
|
|
|
|
|
|
let BUILTIN_ACTIONS = {
|
2020-09-15 16:52:38 +02:00
|
|
|
CREATE_USER: async function(inputs) {
|
|
|
|
const { username, password, accessLevelId } = inputs
|
2020-09-10 16:00:21 +02:00
|
|
|
const ctx = {
|
2020-09-10 17:46:58 +02:00
|
|
|
user: {
|
2020-09-15 16:52:38 +02:00
|
|
|
instanceId: inputs.instanceId,
|
2020-09-10 16:00:21 +02:00
|
|
|
},
|
|
|
|
request: {
|
2020-09-10 17:46:58 +02:00
|
|
|
body: { username, password, accessLevelId },
|
2020-09-10 16:00:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-09-15 15:27:23 +02:00
|
|
|
await userController.create(ctx)
|
2020-09-10 16:00:21 +02:00
|
|
|
return {
|
2020-09-15 15:27:23 +02:00
|
|
|
response: ctx.body,
|
|
|
|
id: ctx.body._id,
|
|
|
|
revision: ctx.body._rev,
|
|
|
|
success: ctx.status === 200,
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return {
|
2020-09-15 15:27:23 +02:00
|
|
|
success: false,
|
|
|
|
response: err,
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-09-15 16:52:38 +02:00
|
|
|
SAVE_RECORD: async function(inputs) {
|
2020-09-10 16:00:21 +02:00
|
|
|
const ctx = {
|
|
|
|
params: {
|
2020-09-15 16:52:38 +02:00
|
|
|
instanceId: inputs.instanceId,
|
|
|
|
modelId: inputs.model._id,
|
2020-09-10 16:00:21 +02:00
|
|
|
},
|
|
|
|
request: {
|
2020-09-15 16:52:38 +02:00
|
|
|
body: inputs.record,
|
2020-09-10 16:00:21 +02:00
|
|
|
},
|
2020-09-15 16:52:38 +02:00
|
|
|
user: { instanceId: inputs.instanceId },
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await recordController.save(ctx)
|
|
|
|
return {
|
2020-09-15 15:27:23 +02:00
|
|
|
response: ctx.body,
|
|
|
|
id: ctx.body._id,
|
|
|
|
revision: ctx.body._rev,
|
|
|
|
success: ctx.status === 200,
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return {
|
2020-09-15 15:27:23 +02:00
|
|
|
success: false,
|
|
|
|
response: err,
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-09-15 16:52:38 +02:00
|
|
|
SEND_EMAIL: async function(inputs) {
|
2020-09-10 16:00:21 +02:00
|
|
|
const msg = {
|
2020-09-15 16:52:38 +02:00
|
|
|
to: inputs.to,
|
|
|
|
from: inputs.from,
|
|
|
|
subject: inputs.subject,
|
|
|
|
text: inputs.text,
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await sgMail.send(msg)
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return {
|
|
|
|
success: false,
|
2020-09-15 15:27:23 +02:00
|
|
|
response: err,
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-09-15 16:52:38 +02:00
|
|
|
DELETE_RECORD: async function(inputs) {
|
|
|
|
const { model, ...record } = inputs.record
|
2020-09-14 12:47:52 +02:00
|
|
|
// TODO: better logging of when actions are missed due to missing parameters
|
|
|
|
if (record.recordId == null || record.revId == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let ctx = {
|
|
|
|
params: {
|
|
|
|
modelId: model._id,
|
|
|
|
recordId: record.recordId,
|
|
|
|
revId: record.revId,
|
|
|
|
},
|
2020-09-15 16:52:38 +02:00
|
|
|
user: { instanceId: inputs.instanceId },
|
2020-09-14 12:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await recordController.destroy(ctx)
|
2020-09-15 15:27:23 +02:00
|
|
|
return {
|
|
|
|
response: ctx.body,
|
|
|
|
success: ctx.status === 200,
|
|
|
|
}
|
2020-09-14 12:47:52 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return {
|
2020-09-15 15:27:23 +02:00
|
|
|
success: false,
|
|
|
|
response: err,
|
2020-09-14 12:47:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.getAction = async function(actionName) {
|
|
|
|
if (BUILTIN_ACTIONS[actionName] != null) {
|
|
|
|
return BUILTIN_ACTIONS[actionName]
|
|
|
|
}
|
|
|
|
// TODO: load async actions here
|
|
|
|
}
|