|
@@ -2,14 +2,14 @@
|
|
const _ = require('lodash');
|
|
const _ = require('lodash');
|
|
const { ObjectId } = require('mongoose').Types;
|
|
const { ObjectId } = require('mongoose').Types;
|
|
|
|
|
|
-const addRecord = async (ctx, service, ...args) => {
|
|
|
|
|
|
+const addRecord = async (ctx, service, args) => {
|
|
const arr = service.split('.');
|
|
const arr = service.split('.');
|
|
let s = ctx.service;
|
|
let s = ctx.service;
|
|
for (const key of arr) {
|
|
for (const key of arr) {
|
|
s = s[key];
|
|
s = s[key];
|
|
}
|
|
}
|
|
const service_method = 'record';
|
|
const service_method = 'record';
|
|
- return await s[service_method](...args);
|
|
|
|
|
|
+ return await s[service_method](args);
|
|
};
|
|
};
|
|
|
|
|
|
module.exports = options => {
|
|
module.exports = options => {
|
|
@@ -17,8 +17,8 @@ module.exports = options => {
|
|
const { url, method } = ctx.request;
|
|
const { url, method } = ctx.request;
|
|
let id;
|
|
let id;
|
|
if (method === 'POST') {
|
|
if (method === 'POST') {
|
|
- const { data } = ctx.body;
|
|
|
|
- id = data._id;
|
|
|
|
|
|
+ const data = _.get(ctx.body, 'data');
|
|
|
|
+ if (data && data.id)id = data._id;
|
|
}
|
|
}
|
|
await next();
|
|
await next();
|
|
if (method === 'POST') {
|
|
if (method === 'POST') {
|
|
@@ -27,8 +27,12 @@ module.exports = options => {
|
|
const last = _.last(arr);
|
|
const last = _.last(arr);
|
|
let word = '';
|
|
let word = '';
|
|
if (ObjectId.isValid(last)) word = 'update';
|
|
if (ObjectId.isValid(last)) word = 'update';
|
|
- else word = 'create';
|
|
|
|
- await addRecord(ctx, id, word, options);
|
|
|
|
|
|
+ else {
|
|
|
|
+ word = 'create';
|
|
|
|
+ id = _.get(ctx.body, 'data._id');
|
|
|
|
+ }
|
|
|
|
+ const nd = await addRecord(ctx, options, { id, method: word });
|
|
|
|
+ ctx.body.data = nd;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
};
|
|
};
|