patentapply_record.js 919 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const _ = require('lodash');
  3. const { ObjectId } = require('mongoose').Types;
  4. const addRecord = async (ctx, service, args) => {
  5. const arr = service.split('.');
  6. let s = ctx.service;
  7. for (const key of arr) {
  8. s = s[key];
  9. }
  10. const service_method = 'record';
  11. return await s[service_method](args);
  12. };
  13. module.exports = options => {
  14. return async function patentapply_record(ctx, next) {
  15. const { url, method } = ctx.request;
  16. await next();
  17. let id;
  18. if (method === 'POST') {
  19. // 将添加,修改拦截,给加上记录
  20. const arr = url.split('/');
  21. const last = _.last(arr);
  22. let word = '';
  23. if (ObjectId.isValid(last) && url.includes('update')) word = 'update';
  24. else {
  25. word = 'create';
  26. }
  27. id = _.get(ctx.body, 'data._id');
  28. const nd = await addRecord(ctx, options, { id, method: word });
  29. ctx.body.data = nd;
  30. }
  31. };
  32. };