patentapply_record.js 881 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. let id;
  17. if (method === 'POST') {
  18. const { data } = ctx.body;
  19. 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 word = 'create';
  29. await addRecord(ctx, id, word, options);
  30. }
  31. };
  32. };