@@ -50,6 +50,7 @@ import { WAMonitoringService } from '../services/monitor.service';
5050import { WAStartupService } from '../services/whatsapp.service' ;
5151import { Logger } from '../../config/logger.config' ;
5252import { RedisCache } from '../../db/redis.client' ;
53+ import { Request } from 'express' ;
5354
5455export class InstanceController {
5556 constructor (
@@ -63,28 +64,36 @@ export class InstanceController {
6364
6465 private readonly logger = new Logger ( InstanceController . name ) ;
6566
66- public async createInstance ( { instanceName } : InstanceDto ) {
67- const instance = new WAStartupService (
68- this . configService ,
69- this . eventEmitter ,
70- this . repository ,
71- this . cache ,
72- ) ;
73- instance . instanceName = instanceName ;
74- this . waMonitor . waInstances [ instance . instanceName ] = instance ;
75- this . waMonitor . delInstanceTime ( instance . instanceName ) ;
76-
77- const hash = await this . authService . generateHash ( {
78- instanceName : instance . instanceName ,
79- } ) ;
67+ public async createInstance ( { instanceName } : InstanceDto , req : Request ) {
68+ try {
69+ const instance = new WAStartupService (
70+ this . configService ,
71+ this . eventEmitter ,
72+ this . repository ,
73+ this . cache ,
74+ ) ;
75+ instance . instanceName = instanceName ;
76+ this . waMonitor . waInstances [ instance . instanceName ] = instance ;
77+ this . waMonitor . delInstanceTime ( instance . instanceName ) ;
8078
81- return {
82- instance : {
79+ const hash = await this . authService . generateHash ( {
8380 instanceName : instance . instanceName ,
84- status : 'created' ,
85- } ,
86- hash,
87- } ;
81+ } ) ;
82+
83+ req . session [ instance . instanceName ] = Buffer . from ( JSON . stringify ( hash ) ) . toString (
84+ 'base64' ,
85+ ) ;
86+
87+ return {
88+ instance : {
89+ instanceName : instance . instanceName ,
90+ status : 'created' ,
91+ } ,
92+ hash,
93+ } ;
94+ } catch ( error ) {
95+ this . logger . error ( error ) ;
96+ }
8897 }
8998
9099 public async connectToWhatsapp ( { instanceName } : InstanceDto ) {
@@ -146,7 +155,11 @@ export class InstanceController {
146155 }
147156 }
148157
149- public async refreshToken ( _ : InstanceDto , oldToken : OldToken ) {
150- return await this . authService . refreshToken ( oldToken ) ;
158+ public async refreshToken ( instance : InstanceDto , oldToken : OldToken , req : Request ) {
159+ const token = await this . authService . refreshToken ( oldToken ) ;
160+
161+ req . session [ instance . instanceName ] = Buffer . from ( JSON . stringify ( token ) ) . toString (
162+ 'base64' ,
163+ ) ;
151164 }
152165}
0 commit comments