123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 'use strict';
- const async = require("async");
- const coreClass = require("../core");
- const config = require('config');
- module.exports = class extends coreClass {
- constructor(name, moduleManager) {
- super(name, moduleManager);
- this.dependsOn = ["mongo"];
- }
- initialize() {
- return new Promise(async (resolve, reject) => {
- this.setStage(1);
- this.mongoModule = this.moduleManager.modules["mongo"];
- this._autosuggestCache = {};
- this._autosuggestMap = {};
- this.accountSchemaModel = await this.mongoModule.model("accountSchema");
- this.accountModel = await this.mongoModule.model("account");
- async.waterfall([
- (next) => {
- this.accountSchemaModel.find({}, null, { sort: "-version" }, next);
- },
- (schemas, next) => {
- schemas.forEach(schema => {
- this._autosuggestMap[schema.version] = {};
- schema.fields.forEach(field => {
- field.fieldTypes.forEach(fieldType => {
- if (fieldType.type === "text" && fieldType.autosuggestGroup) {
- this._autosuggestMap[schema.version][`${field.fieldId}.${fieldType.fieldTypeId}`] = fieldType.autosuggestGroup;
- this._autosuggestCache[fieldType.autosuggestGroup] = [];
- }
- });
- });
- });
- this.accountModel.find({}, next);
- },
- (accounts, next) => {
- accounts.forEach(account => {
- Object.keys(this._autosuggestMap[account.version]).forEach(key => {
- let autosuggestGroup = this._autosuggestMap[account.version][key];
- let fieldId = key.split(".")[0];
- let fieldTypeId = key.split(".")[1];
- account.fields[fieldId].forEach(field => {
- if (this._autosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
- this._autosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
- });
- });
- });
- next();
- }
- ], (err) => {
- if (err) reject(new Error(err));
- else resolve();
- });
- resolve();
- })
- }
- get autosuggestCache() {
- return new Promise(async resolve => {
- try { await this._validateHook(); } catch { return; }
- resolve(this._autosuggestCache);
- });
- }
- get autosuggestMap() {
- return new Promise(async resolve => {
- try { await this._validateHook(); } catch { return; }
- resolve(this._autosuggestMap);
- });
- }
- async addAutosuggestAccount(account) {
- try { await this._validateHook(); } catch { return; }
- Object.keys(this._autosuggestMap[account.version]).forEach(key => {
- let autosuggestGroup = this._autosuggestMap[account.version][key];
- let fieldId = key.split(".")[0];
- let fieldTypeId = key.split(".")[1];
- account.fields[fieldId].forEach(field => {
- if (this._autosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
- this._autosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
- });
- });
- }
- }
|