2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2018-02-13 11:54:57 +00:00
|
|
|
asyncHandler(fn) {
|
|
|
|
return (req, res, next) => Promise.resolve(fn(req, res, next))
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
// handle error
|
|
|
|
res.status(err.status_code || 500).send({error: err.message});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
setup(app) {
|
|
|
|
// get list
|
2018-02-13 11:54:57 +00:00
|
|
|
app.get('/api/resource/:doctype', this.asyncHandler(async function(request, response) {
|
2018-02-12 12:01:31 +00:00
|
|
|
for (let key of ['fields', 'filters']) {
|
2018-01-12 12:25:07 +00:00
|
|
|
if (request.query[key]) {
|
|
|
|
request.query[key] = JSON.parse(request.query[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 06:46:38 +00:00
|
|
|
let data = await frappe.db.getAll({
|
2018-01-12 12:25:07 +00:00
|
|
|
doctype: request.params.doctype,
|
2018-01-23 12:26:40 +00:00
|
|
|
fields: request.query.fields,
|
2018-01-12 12:25:07 +00:00
|
|
|
filters: request.query.filters,
|
|
|
|
start: request.query.start || 0,
|
|
|
|
limit: request.query.limit || 20,
|
|
|
|
order_by: request.query.order_by,
|
|
|
|
order: request.query.order
|
|
|
|
});
|
|
|
|
|
|
|
|
return response.json(data);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// create
|
2018-02-13 11:54:57 +00:00
|
|
|
app.post('/api/resource/:doctype', this.asyncHandler(async function(request, response) {
|
2018-02-08 09:38:47 +00:00
|
|
|
let data = request.body;
|
2018-01-12 12:25:07 +00:00
|
|
|
data.doctype = request.params.doctype;
|
2018-02-08 12:28:51 +00:00
|
|
|
let doc = frappe.newDoc(data);
|
2018-01-12 12:25:07 +00:00
|
|
|
await doc.insert();
|
|
|
|
await frappe.db.commit();
|
2018-02-08 11:45:32 +00:00
|
|
|
return response.json(doc.getValidDict());
|
2018-01-12 12:25:07 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
// update
|
2018-02-13 11:54:57 +00:00
|
|
|
app.put('/api/resource/:doctype/:name', this.asyncHandler(async function(request, response) {
|
2018-02-08 09:38:47 +00:00
|
|
|
let data = request.body;
|
2018-02-08 12:28:51 +00:00
|
|
|
let doc = await frappe.getDoc(request.params.doctype, request.params.name);
|
2018-01-12 12:25:07 +00:00
|
|
|
Object.assign(doc, data);
|
|
|
|
await doc.update();
|
|
|
|
await frappe.db.commit();
|
2018-02-08 11:45:32 +00:00
|
|
|
return response.json(doc.getValidDict());
|
2018-01-12 12:25:07 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
// get document
|
2018-02-13 11:54:57 +00:00
|
|
|
app.get('/api/resource/:doctype/:name', this.asyncHandler(async function(request, response) {
|
2018-02-08 12:28:51 +00:00
|
|
|
let doc = await frappe.getDoc(request.params.doctype, request.params.name);
|
2018-02-08 11:45:32 +00:00
|
|
|
return response.json(doc.getValidDict());
|
2018-01-12 12:25:07 +00:00
|
|
|
}));
|
|
|
|
|
2018-01-23 13:18:37 +00:00
|
|
|
// get value
|
2018-02-13 11:54:57 +00:00
|
|
|
app.get('/api/resource/:doctype/:name/:fieldname', this.asyncHandler(async function(request, response) {
|
2018-02-08 09:38:47 +00:00
|
|
|
let value = await frappe.db.getValue(request.params.doctype, request.params.name, request.params.fieldname);
|
2018-01-23 13:18:37 +00:00
|
|
|
return response.json({value: value});
|
|
|
|
}));
|
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
// delete
|
2018-02-13 11:54:57 +00:00
|
|
|
app.delete('/api/resource/:doctype/:name', this.asyncHandler(async function(request, response) {
|
2018-02-08 12:28:51 +00:00
|
|
|
let doc = await frappe.getDoc(request.params.doctype, request.params.name)
|
2018-01-12 12:25:07 +00:00
|
|
|
await doc.delete();
|
|
|
|
return response.json({});
|
|
|
|
}));
|
2018-02-08 09:38:47 +00:00
|
|
|
|
|
|
|
// delete many
|
2018-02-13 11:54:57 +00:00
|
|
|
app.delete('/api/resource/:doctype', this.asyncHandler(async function(request, response) {
|
2018-02-08 09:38:47 +00:00
|
|
|
let names = request.body;
|
|
|
|
for (let name of names) {
|
2018-02-08 12:28:51 +00:00
|
|
|
let doc = await frappe.getDoc(request.params.doctype, name);
|
2018-02-08 09:38:47 +00:00
|
|
|
await doc.delete();
|
|
|
|
}
|
|
|
|
return response.json({});
|
|
|
|
}));
|
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
};
|