util.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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", limit: 1 }, next);
  21. },
  22. (schemas, next) => {
  23. schemas[0].fields.forEach(field => {
  24. field.fieldTypes.forEach(fieldType => {
  25. if (fieldType.type === "text" && fieldType.autosuggestGroup) {
  26. this._autosuggestMap[`${field.fieldId}.${fieldType.fieldTypeId}`] = fieldType.autosuggestGroup;
  27. this._autosuggestCache[fieldType.autosuggestGroup] = [];
  28. }
  29. });
  30. });
  31. this.accountModel.find({}, next);
  32. },
  33. (accounts, next) => {
  34. accounts.forEach(account => {
  35. Object.keys(this._autosuggestMap).forEach(key => {
  36. let autosuggestGroup = this._autosuggestMap[key];
  37. let fieldId = key.split(".")[0];
  38. let fieldTypeId = key.split(".")[1];
  39. account.fields[fieldId].forEach(field => {
  40. if (this._autosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
  41. this._autosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
  42. });
  43. });
  44. });
  45. next();
  46. }
  47. ], (err) => {
  48. if (err) reject(new Error(err));
  49. else resolve();
  50. });
  51. })
  52. }
  53. get autosuggestCache() {
  54. return new Promise(async resolve => {
  55. try { await this._validateHook(); } catch { return; }
  56. resolve(this._autosuggestCache);
  57. });
  58. }
  59. get autosuggestMap() {
  60. return new Promise(async resolve => {
  61. try { await this._validateHook(); } catch { return; }
  62. resolve(this._autosuggestMap);
  63. });
  64. }
  65. async addAutosuggestAccount(account) {
  66. try { await this._validateHook(); } catch { return; }
  67. Object.keys(this._autosuggestMap).forEach(key => {
  68. let autosuggestGroup = this._autosuggestMap[key];
  69. let fieldId = key.split(".")[0];
  70. let fieldTypeId = key.split(".")[1];
  71. account.fields[fieldId].forEach(field => {
  72. if (this._autosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
  73. this._autosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
  74. });
  75. });
  76. }
  77. }