Skip to content

Commit f23ebf1

Browse files
committed
feat: Added verbose logs
1 parent d66b751 commit f23ebf1

File tree

7 files changed

+422
-7
lines changed

7 files changed

+422
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Added organization name in vcard
88
* Added email in vcard
99
* Added url in vcard
10+
* Added verbose logs
1011

1112
### Fixed
1213

src/db/db.connect.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ import mongoose from 'mongoose';
22
import { configService, Database } from '../config/env.config';
33
import { Logger } from '../config/logger.config';
44

5-
const logger = new Logger('Db Connection');
5+
const logger = new Logger('MongoDB');
66

77
const db = configService.get<Database>('DATABASE');
88
export const dbserver = (() => {
99
if (db.ENABLED) {
10+
logger.verbose('connecting');
1011
const dbs = mongoose.createConnection(db.CONNECTION.URI, {
1112
dbName: db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api',
1213
});
14+
logger.verbose('connected in ' + db.CONNECTION.URI);
1315
logger.info('ON - dbName: ' + dbs['$dbName']);
16+
1417
process.on('beforeExit', () => {
18+
logger.verbose('instance destroyed');
1519
dbserver.destroy(true, (error) => logger.error(error));
1620
});
1721

src/whatsapp/services/auth.service.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ export class AuthService {
4343
{ expiresIn: jwtOpts.EXPIRIN_IN, encoding: 'utf8', subject: 'g-t' },
4444
);
4545

46+
this.logger.verbose('JWT token created: ' + token);
47+
4648
const auth = await this.repository.auth.create({ jwt: token }, instance.instanceName);
4749

50+
this.logger.verbose('JWT token saved in database');
51+
4852
if (auth['error']) {
4953
this.logger.error({
5054
localError: AuthService.name + '.jwt',
@@ -59,8 +63,14 @@ export class AuthService {
5963
private async apikey(instance: InstanceDto, token?: string) {
6064
const apikey = token ? token : v4().toUpperCase();
6165

66+
this.logger.verbose(
67+
token ? 'APIKEY defined: ' + apikey : 'APIKEY created: ' + apikey,
68+
);
69+
6270
const auth = await this.repository.auth.create({ apikey }, instance.instanceName);
6371

72+
this.logger.verbose('APIKEY saved in database');
73+
6474
if (auth['error']) {
6575
this.logger.error({
6676
localError: AuthService.name + '.apikey',
@@ -75,54 +85,77 @@ export class AuthService {
7585
public async checkDuplicateToken(token: string) {
7686
const instances = await this.waMonitor.instanceInfo();
7787

88+
this.logger.verbose('checking duplicate token');
89+
7890
const instance = instances.find((instance) => instance.instance.apikey === token);
7991

8092
if (instance) {
8193
throw new BadRequestException('Token already exists');
8294
}
8395

96+
this.logger.verbose('available token');
97+
8498
return true;
8599
}
86100

87101
public async generateHash(instance: InstanceDto, token?: string) {
88102
const options = this.configService.get<Auth>('AUTHENTICATION');
103+
104+
this.logger.verbose(
105+
'generating hash ' + options.TYPE + ' to instance: ' + instance.instanceName,
106+
);
107+
89108
return (await this[options.TYPE](instance, token)) as
90109
| { jwt: string }
91110
| { apikey: string };
92111
}
93112

94113
public async refreshToken({ oldToken }: OldToken) {
114+
this.logger.verbose('refreshing token');
115+
95116
if (!isJWT(oldToken)) {
96117
throw new BadRequestException('Invalid "oldToken"');
97118
}
98119

99120
try {
100121
const jwtOpts = this.configService.get<Auth>('AUTHENTICATION').JWT;
122+
123+
this.logger.verbose('checking oldToken');
124+
101125
const decode = verify(oldToken, jwtOpts.SECRET, {
102126
ignoreExpiration: true,
103127
}) as Pick<JwtPayload, 'apiName' | 'instanceName' | 'tokenId'>;
104128

129+
this.logger.verbose('checking token in database');
130+
105131
const tokenStore = await this.repository.auth.find(decode.instanceName);
106132

107133
const decodeTokenStore = verify(tokenStore.jwt, jwtOpts.SECRET, {
108134
ignoreExpiration: true,
109135
}) as Pick<JwtPayload, 'apiName' | 'instanceName' | 'tokenId'>;
110136

137+
this.logger.verbose('checking tokenId');
138+
111139
if (decode.tokenId !== decodeTokenStore.tokenId) {
112140
throw new BadRequestException('Invalid "oldToken"');
113141
}
114142

143+
this.logger.verbose('generating new token');
144+
115145
const token = {
116146
jwt: (await this.jwt({ instanceName: decode.instanceName })).jwt,
117147
instanceName: decode.instanceName,
118148
};
119149

120150
try {
151+
this.logger.verbose('checking webhook');
121152
const webhook = await this.repository.webhook.find(decode.instanceName);
122153
if (
123154
webhook?.enabled &&
124155
this.configService.get<Webhook>('WEBHOOK').EVENTS.NEW_JWT_TOKEN
125156
) {
157+
this.logger.verbose('sending webhook');
158+
126159
const httpService = axios.create({ baseURL: webhook.url });
127160
await httpService.post(
128161
'',
@@ -138,6 +171,8 @@ export class AuthService {
138171
this.logger.error(error);
139172
}
140173

174+
this.logger.verbose('token refreshed');
175+
141176
return token;
142177
} catch (error) {
143178
this.logger.error({

src/whatsapp/services/monitor.service.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export class WAMonitoringService {
2424
private readonly repository: RepositoryBroker,
2525
private readonly cache: RedisCache,
2626
) {
27+
this.logger.verbose('instance created');
28+
2729
this.removeInstance();
2830
this.noConnection();
2931
this.delInstanceFiles();
@@ -47,6 +49,10 @@ export class WAMonitoringService {
4749
public delInstanceTime(instance: string) {
4850
const time = this.configService.get<DelInstance>('DEL_INSTANCE');
4951
if (typeof time === 'number' && time > 0) {
52+
this.logger.verbose(
53+
`Instance "${instance}" don't have connection, will be removed in ${time} minutes`,
54+
);
55+
5056
setTimeout(async () => {
5157
if (this.waInstances[instance]?.connectionStatus?.state !== 'open') {
5258
if (this.waInstances[instance]?.connectionStatus?.state === 'connecting') {
@@ -66,6 +72,7 @@ export class WAMonitoringService {
6672
}
6773

6874
public async instanceInfo(instanceName?: string) {
75+
this.logger.verbose('get instance info');
6976
if (instanceName && !this.waInstances[instanceName]) {
7077
throw new NotFoundException(`Instance "${instanceName}" not found`);
7178
}
@@ -74,9 +81,14 @@ export class WAMonitoringService {
7481

7582
for await (const [key, value] of Object.entries(this.waInstances)) {
7683
if (value) {
84+
this.logger.verbose('get instance info: ' + key);
7785
if (value.connectionStatus.state === 'open') {
86+
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
7887
let apikey: string;
7988
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
89+
this.logger.verbose(
90+
'instance: ' + key + ' - hash exposed in fetch instances',
91+
);
8092
const tokenStore = await this.repository.auth.find(key);
8193
apikey = tokenStore.apikey || 'Apikey not found';
8294

@@ -91,6 +103,9 @@ export class WAMonitoringService {
91103
},
92104
});
93105
} else {
106+
this.logger.verbose(
107+
'instance: ' + key + ' - hash not exposed in fetch instances',
108+
);
94109
instances.push({
95110
instance: {
96111
instanceName: key,
@@ -102,8 +117,14 @@ export class WAMonitoringService {
102117
});
103118
}
104119
} else {
120+
this.logger.verbose(
121+
'instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state,
122+
);
105123
let apikey: string;
106124
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
125+
this.logger.verbose(
126+
'instance: ' + key + ' - hash exposed in fetch instances',
127+
);
107128
const tokenStore = await this.repository.auth.find(key);
108129
apikey = tokenStore.apikey || 'Apikey not found';
109130

@@ -115,6 +136,9 @@ export class WAMonitoringService {
115136
},
116137
});
117138
} else {
139+
this.logger.verbose(
140+
'instance: ' + key + ' - hash not exposed in fetch instances',
141+
);
118142
instances.push({
119143
instance: {
120144
instanceName: key,
@@ -126,10 +150,13 @@ export class WAMonitoringService {
126150
}
127151
}
128152

153+
this.logger.verbose('return instance info: ' + instances.length);
154+
129155
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
130156
}
131157

132158
private delInstanceFiles() {
159+
this.logger.verbose('cron to delete instance files started');
133160
setInterval(async () => {
134161
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
135162
const collections = await this.dbInstance.collections();
@@ -141,6 +168,7 @@ export class WAMonitoringService {
141168
{ _id: { $regex: /^session-.*/ } },
142169
],
143170
});
171+
this.logger.verbose('instance files deleted: ' + name);
144172
});
145173
} else if (this.redis.ENABLED) {
146174
} else {
@@ -158,14 +186,17 @@ export class WAMonitoringService {
158186
});
159187
}
160188
});
189+
this.logger.verbose('instance files deleted: ' + dirent.name);
161190
}
162191
}
163192
}
164193
}, 3600 * 1000 * 2);
165194
}
166195

167196
public async cleaningUp(instanceName: string) {
197+
this.logger.verbose('cleaning up instance: ' + instanceName);
168198
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
199+
this.logger.verbose('cleaning up instance in database: ' + instanceName);
169200
await this.repository.dbServer.connect();
170201
const collections: any[] = await this.dbInstance.collections();
171202
if (collections.length > 0) {
@@ -175,14 +206,18 @@ export class WAMonitoringService {
175206
}
176207

177208
if (this.redis.ENABLED) {
209+
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
178210
this.cache.reference = instanceName;
179211
await this.cache.delAll();
180212
return;
181213
}
214+
215+
this.logger.verbose('cleaning up instance in files: ' + instanceName);
182216
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
183217
}
184218

185219
public async loadInstance() {
220+
this.logger.verbose('load instances');
186221
const set = async (name: string) => {
187222
const instance = new WAStartupService(
188223
this.configService,
@@ -191,38 +226,50 @@ export class WAMonitoringService {
191226
this.cache,
192227
);
193228
instance.instanceName = name;
229+
this.logger.verbose('instance loaded: ' + name);
230+
194231
await instance.connectToWhatsapp();
232+
this.logger.verbose('connectToWhatsapp: ' + name);
233+
195234
this.waInstances[name] = instance;
196235
};
197236

198237
try {
199238
if (this.redis.ENABLED) {
239+
this.logger.verbose('redis enabled');
200240
await this.cache.connect(this.redis as Redis);
201241
const keys = await this.cache.instanceKeys();
202242
if (keys?.length > 0) {
243+
this.logger.verbose('reading instance keys and setting instances');
203244
keys.forEach(async (k) => await set(k.split(':')[1]));
204245
} else {
246+
this.logger.verbose('no instance keys found');
205247
initInstance();
206248
}
207249
return;
208250
}
209251

210252
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
253+
this.logger.verbose('database enabled');
211254
await this.repository.dbServer.connect();
212255
const collections: any[] = await this.dbInstance.collections();
213256
if (collections.length > 0) {
257+
this.logger.verbose('reading collections and setting instances');
214258
collections.forEach(
215259
async (coll) => await set(coll.namespace.replace(/^[\w-]+\./, '')),
216260
);
217261
} else {
262+
this.logger.verbose('no collections found');
218263
initInstance();
219264
}
220265
return;
221266
}
222267

268+
this.logger.verbose('store in files enabled');
223269
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
224270
for await (const dirent of dir) {
225271
if (dirent.isDirectory()) {
272+
this.logger.verbose('reading instance files and setting instances');
226273
const files = readdirSync(join(INSTANCE_DIR, dirent.name), {
227274
encoding: 'utf-8',
228275
});
@@ -233,6 +280,7 @@ export class WAMonitoringService {
233280

234281
await set(dirent.name);
235282
} else {
283+
this.logger.verbose('no instance files found');
236284
initInstance();
237285
}
238286
}
@@ -243,18 +291,23 @@ export class WAMonitoringService {
243291

244292
private removeInstance() {
245293
this.eventEmitter.on('remove.instance', async (instanceName: string) => {
294+
this.logger.verbose('remove instance: ' + instanceName);
246295
try {
296+
this.logger.verbose('instance: ' + instanceName + ' - removing from memory');
247297
this.waInstances[instanceName] = undefined;
248298
} catch {}
249299

250300
try {
301+
this.logger.verbose('request cleaning up instance: ' + instanceName);
251302
this.cleaningUp(instanceName);
252303
} finally {
253304
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
254305
}
255306
});
256307
this.eventEmitter.on('logout.instance', async (instanceName: string) => {
308+
this.logger.verbose('logout instance: ' + instanceName);
257309
try {
310+
this.logger.verbose('request cleaning up instance: ' + instanceName);
258311
this.cleaningUp(instanceName);
259312
} finally {
260313
this.logger.warn(`Instance "${instanceName}" - LOGOUT`);
@@ -263,9 +316,13 @@ export class WAMonitoringService {
263316
}
264317

265318
private noConnection() {
319+
this.logger.verbose('checking instances without connection');
266320
this.eventEmitter.on('no.connection', async (instanceName) => {
267321
try {
322+
this.logger.verbose('instance: ' + instanceName + ' - removing from memory');
268323
this.waInstances[instanceName] = undefined;
324+
325+
this.logger.verbose('request cleaning up instance: ' + instanceName);
269326
this.cleaningUp(instanceName);
270327
} catch (error) {
271328
this.logger.error({

0 commit comments

Comments
 (0)