@@ -29,6 +29,8 @@ export class CalendarController {
29
29
WorkspaceClient | Promise < WorkspaceClient >
30
30
> ( )
31
31
32
+ private readonly workspacesByEmail = new Map < string , string [ ] > ( )
33
+
32
34
private readonly syncedWorkspaces = new Set < string > ( )
33
35
34
36
private readonly tokens : Collection < Token >
@@ -95,6 +97,7 @@ export class CalendarController {
95
97
await limiter . add ( async ( ) => {
96
98
const workspace = await this . startWorkspace ( info . workspaceId , tokens )
97
99
await workspace . sync ( )
100
+ await workspace . close ( )
98
101
this . syncedWorkspaces . add ( info . workspaceId )
99
102
} )
100
103
const newProgress = Math . round ( ( i * 100 ) / infos . length )
@@ -114,6 +117,9 @@ export class CalendarController {
114
117
} , 60000 )
115
118
console . log ( 'init client' , token . workspace , token . userId )
116
119
await workspaceClient . createCalendarClient ( token , true )
120
+ const arr = this . workspacesByEmail . get ( token . email ) ?? [ ]
121
+ arr . push ( token . workspace )
122
+ this . workspacesByEmail . set ( token . email , arr )
117
123
clearTimeout ( timeout )
118
124
} catch ( err ) {
119
125
console . error ( `Couldn't create client for ${ workspace } ${ token . userId } ${ token . email } ` )
@@ -123,30 +129,25 @@ export class CalendarController {
123
129
}
124
130
125
131
async push ( email : string , mode : 'events' | 'calendar' , calendarId ?: string ) : Promise < void > {
126
- const tokens = await this . tokens . find ( { email, access_token : { $exists : true } } ) . toArray ( )
127
- const token = generateToken ( systemAccountEmail , { name : '' } )
128
- const workspaces = [ ...new Set ( tokens . map ( ( p ) => p . workspace ) . filter ( ( p ) => this . syncedWorkspaces . has ( p ) ) ) ]
132
+ const workspaces = this . workspacesByEmail . get ( email ) ?. filter ( ( p ) => this . syncedWorkspaces . has ( p ) ) ?? [ ]
129
133
if ( workspaces . length === 0 ) return
130
- const infos = await getWorkspacesInfo ( token , workspaces )
131
- for ( const token of tokens ) {
132
- const info = infos . find ( ( p ) => p . workspaceId === token . workspace )
133
- if ( info === undefined ) {
134
- continue
135
- }
136
- if ( isDeletingMode ( info . mode ) ) {
137
- await this . tokens . deleteOne ( { userId : token . userId , workspace : token . workspace } )
138
- continue
139
- }
140
- if ( ! isActiveMode ( info . mode ) ) {
141
- continue
134
+ for ( const workspace of workspaces ) {
135
+ const workspaceClient = await this . getWorkspaceClient ( workspace )
136
+ let calendar = workspaceClient . getCalendarClient ( email )
137
+ if ( calendar !== undefined ) {
138
+ if ( calendar instanceof Promise ) {
139
+ calendar = await calendar
140
+ }
141
+ } else {
142
+ const token = await this . tokens . findOne ( { email, workspace, access_token : { $exists : true } } )
143
+ if ( token == null ) continue
144
+ calendar = await workspaceClient . createCalendarClient ( token )
142
145
}
143
- const workspace = await this . getWorkspaceClient ( token . workspace )
144
- const calendarClient = await workspace . createCalendarClient ( token )
145
146
if ( mode === 'calendar' ) {
146
- await calendarClient . syncCalendars ( email )
147
+ await calendar . syncCalendars ( email )
147
148
}
148
149
if ( mode === 'events' && calendarId !== undefined ) {
149
- await calendarClient . sync ( calendarId , email )
150
+ await calendar . sync ( calendarId , email )
150
151
}
151
152
}
152
153
}
0 commit comments