UNPKG

nestwhats

Version:

A whatsapp-web.js wrapper for NestJS to create WhatsApp bots

93 lines (92 loc) 3.72 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var ClientsRegistryService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClientsRegistryService = exports.ClientStatus = void 0; const common_1 = require("@nestjs/common"); const client_token_util_1 = require("./providers/client-token.util"); var ClientStatus; (function (ClientStatus) { ClientStatus["Initializing"] = "initializing"; ClientStatus["QrReceived"] = "qr_received"; ClientStatus["Authenticated"] = "authenticated"; ClientStatus["Ready"] = "ready"; ClientStatus["Disconnected"] = "disconnected"; })(ClientStatus || (exports.ClientStatus = ClientStatus = {})); let ClientsRegistryService = ClientsRegistryService_1 = class ClientsRegistryService { constructor() { this.logger = new common_1.Logger(ClientsRegistryService_1.name); this.registry = new Map(); this.changeListeners = new Set(); } add(entry) { const token = (0, client_token_util_1.getClientToken)(entry.name); if (this.registry.has(token)) { this.logger.warn(`Client "${entry.name}" is already registered and will be overwritten`); } this.registry.set(token, Object.assign(Object.assign({}, entry), { status: ClientStatus.Initializing, statusAt: Date.now() })); } updateStatus(name, status, qr) { const token = (0, client_token_util_1.getClientToken)(name); const entry = this.registry.get(token); if (!entry) return; entry.status = status; entry.statusAt = Date.now(); entry.qr = qr; this.notify(); } updateInfo(name, pushname, phone) { const token = (0, client_token_util_1.getClientToken)(name); const entry = this.registry.get(token); if (!entry) return; entry.pushname = pushname; entry.phone = phone; this.notify(); } subscribe(fn) { this.changeListeners.add(fn); return () => this.changeListeners.delete(fn); } notify() { for (const fn of this.changeListeners) fn(); } get(name) { var _a; return (_a = this.registry.get((0, client_token_util_1.getClientToken)(name))) === null || _a === void 0 ? void 0 : _a.client; } getEntry(name) { return this.registry.get((0, client_token_util_1.getClientToken)(name)); } getAll() { return [...this.registry.values()]; } getByStatus(status) { return [...this.registry.values()].filter((e) => e.status === status); } getNames() { return [...this.registry.values()].map((entry) => entry.name); } getSummary() { return [...this.registry.values()].map(({ name, prefix, status, statusAt, qr, pushname, phone }) => ({ name, prefix, status, statusAt, qr, pushname, phone, })); } }; exports.ClientsRegistryService = ClientsRegistryService; exports.ClientsRegistryService = ClientsRegistryService = ClientsRegistryService_1 = __decorate([ (0, common_1.Injectable)() ], ClientsRegistryService);