account.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.utilModule = this.moduleManager.modules["util"];
  15. this.accountSchema = await this.mongoModule.schema("account");
  16. this.accountModel = await this.mongoModule.model("account");
  17. resolve();
  18. })
  19. }
  20. async getAll() {
  21. return new Promise(async (resolve, reject) => {
  22. try { await this._validateHook(); } catch { return; }
  23. this.accountModel.find({}, (err, accounts) => {
  24. if (err) reject(new Error(err));
  25. else resolve(accounts);
  26. });
  27. });
  28. }
  29. async getById(accountId) {
  30. return new Promise(async (resolve, reject) => {
  31. try { await this._validateHook(); } catch { return; }
  32. this.accountModel.findById(accountId, (err, account) => {
  33. if (err) reject(new Error(err));
  34. else resolve(account);
  35. });
  36. });
  37. }
  38. async add(account) {
  39. return new Promise(async (resolve, reject) => {
  40. try { await this._validateHook(); } catch { return; }
  41. this.accountModel.create(account, (err) => {
  42. if (err) reject(new Error(err));
  43. else {
  44. this.utilModule.addAutosuggestAccount(account);
  45. resolve();
  46. }
  47. });
  48. });
  49. }
  50. async editById(accountId, account) {
  51. return new Promise(async (resolve, reject) => {
  52. try { await this._validateHook(); } catch { return; }
  53. this.accountModel.updateOne({ _id: accountId }, account, (err) => {
  54. if (err) reject(new Error(err));
  55. else {
  56. this.utilModule.addAutosuggestAccount(account);
  57. resolve();
  58. }
  59. });
  60. });
  61. }
  62. }