|
1 | 1 | import { prismaRepository } from '@api/server.module'; |
2 | 2 | import { CacheService } from '@api/services/cache.service'; |
3 | 3 | import { CacheConf, configService } from '@config/env.config'; |
| 4 | +import { Logger } from '@config/logger.config'; |
4 | 5 | import { INSTANCE_DIR } from '@config/path.config'; |
5 | 6 | import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys'; |
6 | 7 | import fs from 'fs/promises'; |
@@ -73,12 +74,15 @@ async function fileExists(file: string): Promise<any> { |
73 | 74 | } |
74 | 75 | } |
75 | 76 |
|
| 77 | +const logger = new Logger('useMultiFileAuthStatePrisma'); |
| 78 | + |
76 | 79 | export default async function useMultiFileAuthStatePrisma( |
77 | 80 | sessionId: string, |
78 | 81 | cache: CacheService, |
79 | 82 | ): Promise<{ |
80 | 83 | state: AuthenticationState; |
81 | 84 | saveCreds: () => Promise<void>; |
| 85 | + removeCreds: () => Promise<void>; |
82 | 86 | }> { |
83 | 87 | const localFolder = path.join(INSTANCE_DIR, sessionId); |
84 | 88 | const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json'); |
@@ -142,6 +146,26 @@ export default async function useMultiFileAuthStatePrisma( |
142 | 146 | } |
143 | 147 | } |
144 | 148 |
|
| 149 | + async function removeCreds(): Promise<any> { |
| 150 | + const cacheConfig = configService.get<CacheConf>('CACHE'); |
| 151 | + |
| 152 | + // Redis |
| 153 | + try { |
| 154 | + if (cacheConfig.REDIS.ENABLED) { |
| 155 | + await cache.delete(sessionId); |
| 156 | + logger.info({ action: 'redis.delete', sessionId }); |
| 157 | + |
| 158 | + return; |
| 159 | + } |
| 160 | + } catch (err) { |
| 161 | + logger.warn({ action: 'redis.delete', sessionId, err }); |
| 162 | + } |
| 163 | + |
| 164 | + logger.info({ action: 'auth.key.delete', sessionId }); |
| 165 | + |
| 166 | + await deleteAuthKey(sessionId); |
| 167 | + } |
| 168 | + |
145 | 169 | let creds = await readData('creds'); |
146 | 170 | if (!creds) { |
147 | 171 | creds = initAuthCreds(); |
@@ -183,5 +207,7 @@ export default async function useMultiFileAuthStatePrisma( |
183 | 207 | saveCreds: () => { |
184 | 208 | return writeData(creds, 'creds'); |
185 | 209 | }, |
| 210 | + |
| 211 | + removeCreds, |
186 | 212 | }; |
187 | 213 | } |
0 commit comments