2021-03-03 18:52:41 +01:00
|
|
|
const rowController = require("../../../controllers/row")
|
|
|
|
const appController = require("../../../controllers/application")
|
2021-03-03 19:41:49 +01:00
|
|
|
const autoController = require("../../../controllers/automation")
|
2021-03-03 18:52:41 +01:00
|
|
|
const CouchDB = require("../../../../db")
|
|
|
|
|
|
|
|
function Request(appId, params) {
|
|
|
|
this.user = { appId }
|
|
|
|
this.params = params
|
|
|
|
}
|
|
|
|
|
2021-03-03 19:41:49 +01:00
|
|
|
exports.getAllTableRows = async config => {
|
|
|
|
const req = new Request(config.appId, { tableId: config.table._id })
|
2021-03-03 18:52:41 +01:00
|
|
|
await rowController.fetchTableRows(req)
|
|
|
|
return req.body
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.clearAllApps = async () => {
|
|
|
|
const req = {}
|
|
|
|
await appController.fetch(req)
|
|
|
|
const apps = req.body
|
|
|
|
if (!apps || apps.length <= 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for (let app of apps) {
|
|
|
|
const appId = app._id
|
|
|
|
await appController.delete(new Request(null, { appId }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 19:41:49 +01:00
|
|
|
exports.clearAllAutomations = async config => {
|
|
|
|
const automations = await config.getAllAutomations()
|
|
|
|
for (let auto of automations) {
|
|
|
|
await config.deleteAutomation(auto)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:52:41 +01:00
|
|
|
exports.createRequest = (request, method, url, body) => {
|
|
|
|
let req
|
|
|
|
|
|
|
|
if (method === "POST") req = request.post(url).send(body)
|
|
|
|
else if (method === "GET") req = request.get(url)
|
|
|
|
else if (method === "DELETE") req = request.delete(url)
|
|
|
|
else if (method === "PATCH") req = request.patch(url).send(body)
|
|
|
|
else if (method === "PUT") req = request.put(url).send(body)
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2021-03-03 19:41:49 +01:00
|
|
|
exports.checkBuilderEndpoint = async ({ config, method, url, body }) => {
|
2021-03-03 18:52:41 +01:00
|
|
|
const headers = await config.login()
|
|
|
|
await exports
|
2021-03-03 19:41:49 +01:00
|
|
|
.createRequest(config.request, method, url, body)
|
2021-03-03 18:52:41 +01:00
|
|
|
.set(headers)
|
|
|
|
.expect(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw DB insert utility.
|
|
|
|
*/
|
|
|
|
exports.insertDocument = async (databaseId, document) => {
|
|
|
|
const { id, ...documentFields } = document
|
|
|
|
return await new CouchDB(databaseId).put({ _id: id, ...documentFields })
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw DB delete utility.
|
|
|
|
*/
|
|
|
|
exports.destroyDocument = async (databaseId, documentId) => {
|
|
|
|
return await new CouchDB(databaseId).destroy(documentId)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw DB get utility.
|
|
|
|
*/
|
|
|
|
exports.getDocument = async (databaseId, documentId) => {
|
|
|
|
return await new CouchDB(databaseId).get(documentId)
|
|
|
|
}
|