patentapply_record.js 1013 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. let id;
  17. if (method === 'POST') {
  18. const data = _.get(ctx.body, 'data');
  19. if (data && data.id)id = data._id;
  20. }
  21. await next();
  22. if (method === 'POST') {
  23. // 将添加,修改拦截,给加上记录
  24. const arr = url.split('/');
  25. const last = _.last(arr);
  26. let word = '';
  27. if (ObjectId.isValid(last)) word = 'update';
  28. else {
  29. word = 'create';
  30. }
  31. id = _.get(ctx.body, 'data._id');
  32. const nd = await addRecord(ctx, options, { id, method: word });
  33. ctx.body.data = nd;
  34. }
  35. };
  36. };