util.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. resolve();
  52. })
  53. }
  54. get autosuggestCache() {
  55. return new Promise(async resolve => {
  56. try { await this._validateHook(); } catch { return; }
  57. resolve(this._autosuggestCache);
  58. });
  59. }
  60. get autosuggestMap() {
  61. return new Promise(async resolve => {
  62. try { await this._validateHook(); } catch { return; }
  63. resolve(this._autosuggestMap);
  64. });
  65. }
  66. async addAutosuggestAccount(account) {
  67. try { await this._validateHook(); } catch { return; }
  68. Object.keys(this._autosuggestMap).forEach(key => {
  69. let autosuggestGroup = this._autosuggestMap[key];
  70. let fieldId = key.split(".")[0];
  71. let fieldTypeId = key.split(".")[1];
  72. account.fields[fieldId].forEach(field => {
  73. if (this._autosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
  74. this._autosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
  75. });
  76. });
  77. }
  78. }