2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-08-18 15:54:17 +00:00
|
|
|
const path = require('path');
|
|
|
|
const multer = require('multer');
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
setup(app) {
|
|
|
|
// get list
|
2018-03-26 12:18:07 +00:00
|
|
|
app.get('/api/resource/:doctype', frappe.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-03-26 12:18:07 +00:00
|
|
|
app.post('/api/resource/:doctype', frappe.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-03-26 12:18:07 +00:00
|
|
|
app.put('/api/resource/:doctype/:name', frappe.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
|
|
|
}));
|
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
const upload = multer({
|
|
|
|
storage: multer.diskStorage({
|
|
|
|
destination: (req, file, cb) => {
|
|
|
|
cb(null, frappe.conf.staticPath)
|
|
|
|
},
|
|
|
|
filename: (req, file, cb) => {
|
|
|
|
const filename = file.originalname.split('.')[0];
|
|
|
|
const extension = path.extname(file.originalname);
|
|
|
|
const now = Date.now();
|
|
|
|
cb(null, filename + '-' + now + extension);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/api/upload/:doctype/:name/:fieldname', upload.array('files', 10), frappe.asyncHandler(async function(request, response) {
|
|
|
|
const files = request.files;
|
|
|
|
const { doctype, name, fieldname } = request.params;
|
|
|
|
|
|
|
|
let fileDocs = [];
|
|
|
|
for (let file of files) {
|
|
|
|
const doc = frappe.newDoc({
|
|
|
|
doctype: 'File',
|
|
|
|
name: path.join('/', file.path),
|
|
|
|
filename: file.originalname,
|
|
|
|
mimetype: file.mimetype,
|
|
|
|
size: file.size,
|
|
|
|
referenceDoctype: doctype,
|
|
|
|
referenceName: name,
|
|
|
|
referenceFieldname: fieldname
|
|
|
|
});
|
|
|
|
await doc.insert();
|
|
|
|
|
|
|
|
await frappe.db.setValue(doctype, name, fieldname, doc.name);
|
|
|
|
|
|
|
|
fileDocs.push(doc.getValidDict());
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.json(fileDocs);
|
|
|
|
}));
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
// get document
|
2018-03-26 12:18:07 +00:00
|
|
|
app.get('/api/resource/:doctype/:name', frappe.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-03-26 12:18:07 +00:00
|
|
|
app.get('/api/resource/:doctype/:name/:fieldname', frappe.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-03-26 12:18:07 +00:00
|
|
|
app.delete('/api/resource/:doctype/:name', frappe.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-03-26 12:18:07 +00:00
|
|
|
app.delete('/api/resource/:doctype', frappe.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
|
|
|
}
|
|
|
|
};
|