util.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. const async = require("async");
  3. const coreClass = require("../core");
  4. const config = require('config');
  5. module.exports = class extends coreClass {
  6. constructor(name, moduleManager) {
  7. super(name, moduleManager);
  8. this.dependsOn = ["mongo"];
  9. }
  10. initialize() {
  11. return new Promise(async (resolve, reject) => {
  12. this.setStage(1);
  13. this.mongoModule = this.moduleManager.modules["mongo"];
  14. this._autosuggestCache = {};
  15. this._autosuggestMap = {};
  16. this.accountSchemaModel = await this.mongoModule.model("accountSchema");
  17. this.accountModel = await this.mongoModule.model("account");
  18. async.waterfall([
  19. (next) => {
  20. this.accountSchemaModel.find({}, null, { sort: "-version" }, next);
  21. },
  22. (schemas, next) => {
  23. schemas.forEach(schema => {
  24. this._autosuggestMap[schema.version] = {};
  25. schema.fields.forEach(field => {
  26. field.fieldTypes.forEach(fieldType => {
  27. if (fieldType.type === "text" && fieldType.autosuggestGroup) {
  28. this._autosuggestMap[schema.version][`${field.fieldId}.${fieldType.fieldTypeId}`] = fieldType.autosuggestGroup;
  29. this._autosuggestCache[fieldType.autosuggestGroup] = [];
  30. }
  31. });
  32. });
  33. });
  34. this.accountModel.find({}, next);
  35. },
  36. (accounts, next) => {
  37. accounts.forEach(account => {
  38. Object.keys(this._autosuggestMap[account.version]).forEach(key => {
  39. let autosuggestGroup = this._autosuggestMap[account.version][key];
  40. let fieldId = key.split(".")[0];
  41. let fieldTypeId = key.split(".")[1];
  42. account.fields[fieldId].forEach(field => {
  43. if (this._autosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
  44. this._autosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
  45. });
  46. });
  47. });
  48. next();
  49. }
  50. ], (err) => {
  51. if (err) reject(new Error(err));
  52. else resolve();
  53. });
  54. resolve();
  55. })
  56. }
  57. get autosuggestCache() {
  58. return new Promise(async resolve => {
  59. try { await this._validateHook(); } catch { return; }
  60. resolve(this._autosuggestCache);
  61. });
  62. }
  63. get autosuggestMap() {
  64. return new Promise(async resolve => {
  65. try { await this._validateHook(); } catch { return; }
  66. resolve(this._autosuggestMap);
  67. });
  68. }
  69. async addAutosuggestAccount(account) {
  70. try { await this._validateHook(); } catch { return; }
  71. Object.keys(this._autosuggestMap[account.version]).forEach(key => {
  72. let autosuggestGroup = this._autosuggestMap[account.version][key];
  73. let fieldId = key.split(".")[0];
  74. let fieldTypeId = key.split(".")[1];
  75. account.fields[fieldId].forEach(field => {
  76. if (this._autosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
  77. this._autosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
  78. });
  79. });
  80. }
  81. }