Skip to content

Commit 6c6436d

Browse files
authored
Fix calendar (#9620)
Signed-off-by: Denis Bykhov <[email protected]>
1 parent 1dba8ff commit 6c6436d

File tree

8 files changed

+27
-23
lines changed

8 files changed

+27
-23
lines changed

services/calendar/pod-calendar/src/auth.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import { calendar_v3, google } from 'googleapis'
3232
import { encode64 } from './base64'
3333
import { getClient } from './client'
3434
import { addUserByEmail, removeUserByEmail, setSyncHistory } from './kvsUtils'
35+
import { lock } from './mutex'
3536
import { IncomingSyncManager } from './sync'
3637
import { CALENDAR_INTEGRATION, GoogleEmail, SCOPES, State, Token, User } from './types'
37-
import { getGoogleClient, getWorkspaceToken, removeIntegrationSecret } from './utils'
38+
import { getGoogleClient, removeIntegrationSecret } from './utils'
3839
import { WatchController } from './watch'
39-
import { lock } from './mutex'
4040

4141
interface AuthResult {
4242
success: boolean
@@ -71,7 +71,7 @@ export class AuthController {
7171
async () => {
7272
const mutex = await lock(`${state.workspace}:${state.userId}`)
7373
try {
74-
const client = await getClient(getWorkspaceToken(state.workspace))
74+
const client = await getClient(state.workspace)
7575
const txOp = new TxOperations(client, state.userId)
7676
const controller = new AuthController(ctx, accountClient, txOp, state)
7777
await controller.process(code)
@@ -96,7 +96,7 @@ export class AuthController {
9696
async () => {
9797
const mutex = await lock(`${workspace}:${userId}`)
9898
try {
99-
const client = await getClient(getWorkspaceToken(workspace))
99+
const client = await getClient(workspace)
100100
const txOp = new TxOperations(client, core.account.System)
101101
const controller = new AuthController(ctx, accountClient, txOp, {
102102
userId,
@@ -321,8 +321,8 @@ export class AuthController {
321321
return authUrl
322322
}
323323

324-
static async getUserId (account: AccountUuid, token: string): Promise<PersonId> {
325-
const client = await getClient(token)
324+
static async getUserId (account: AccountUuid, workspace: WorkspaceUuid, token: string): Promise<PersonId> {
325+
const client = await getClient(workspace, token)
326326
const person = await client.findOne(contact.class.Person, { personUuid: account })
327327
if (person === undefined) {
328328
throw new Error('Person not found')

services/calendar/pod-calendar/src/client.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@
1414
//
1515

1616
import client from '@hcengineering/client'
17-
import { type Client } from '@hcengineering/core'
17+
import { WorkspaceUuid, type Client } from '@hcengineering/core'
1818
import { setMetadata } from '@hcengineering/platform'
1919
import { createClient, getTransactorEndpoint } from '@hcengineering/server-client'
20+
import { getWorkspaceToken } from './utils'
2021

21-
export async function getClient (token: string): Promise<Client> {
22-
const endpoint = await getTransactorEndpoint(token, 'external')
22+
const endpoints = new Map<WorkspaceUuid, string>()
23+
24+
export async function getClient (
25+
workspace: WorkspaceUuid,
26+
token: string = getWorkspaceToken(workspace)
27+
): Promise<Client> {
28+
const endpoint = endpoints.get(workspace) ?? (await getTransactorEndpoint(token, 'external'))
29+
endpoints.set(workspace, endpoint)
2330
setMetadata(client.metadata.FilterModel, 'client')
2431
return await createClient(endpoint, token)
2532
}

services/calendar/pod-calendar/src/kvsUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ export async function removeUserByEmail (user: User, email: GoogleEmail): Promis
9393
export async function cleanUserByEmail (): Promise<void> {
9494
const client = getKvsClient()
9595
const keys = await client.listKeys(`${CALENDAR_INTEGRATION}:users:`)
96-
if (keys == null) return
97-
for (const key in keys) {
96+
if (keys?.keys == null) return
97+
for (const key of keys.keys) {
9898
await client.deleteKey(key)
9999
}
100100
}

services/calendar/pod-calendar/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const main = async (): Promise<void> => {
8585
const redirectURL = req.query.redirectURL as string
8686

8787
const { account, workspace } = decodeToken(token)
88-
const userId = await AuthController.getUserId(account, token)
88+
const userId = await AuthController.getUserId(account, workspace, token)
8989
const url = AuthController.getAuthUrl(redirectURL, workspace, userId, token)
9090
res.send(url)
9191
} catch (err) {
@@ -126,7 +126,7 @@ export const main = async (): Promise<void> => {
126126

127127
const value = req.query.value as GoogleEmail
128128
const { account, workspace } = decodeToken(token)
129-
const userId = await AuthController.getUserId(account, token)
129+
const userId = await AuthController.getUserId(account, workspace, token)
130130
await AuthController.signout(ctx, accountClient, userId, workspace, value)
131131
} catch (err) {
132132
ctx.error('signout', { message: (err as any).message })

services/calendar/pod-calendar/src/outcomingClient.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { OAuth2Client } from 'google-auth-library'
2323
import { calendar_v3 } from 'googleapis'
2424
import { getClient } from './client'
2525
import { removeUserByEmail, setSyncHistory } from './kvsUtils'
26+
import { lock, synced } from './mutex'
2627
import { getRateLimitter, RateLimiter } from './rateLimiter'
2728
import { CALENDAR_INTEGRATION, Token } from './types'
2829
import {
@@ -31,13 +32,11 @@ import {
3132
getGoogleClient,
3233
getMixinFields,
3334
getTimezone,
34-
getWorkspaceToken,
3535
parseEventDate,
3636
parseRecurrenceStrings,
3737
removeIntegrationSecret,
3838
setCredentials
3939
} from './utils'
40-
import { lock, synced } from './mutex'
4140

4241
export class OutcomingClient {
4342
private readonly calendar: calendar_v3.Calendar
@@ -403,7 +402,7 @@ export class OutcomingClient {
403402
): Promise<void> {
404403
if (event.access === 'owner' || event.access === 'writer') {
405404
const mutex = await lock(`outcoming:${workspace}`)
406-
const client = await getClient(getWorkspaceToken(workspace))
405+
const client = await getClient(workspace)
407406
const txOp = new TxOperations(client, core.account.System)
408407
try {
409408
const user = await getTokenByEvent(accountClient, txOp, event, workspace)

services/calendar/pod-calendar/src/pushHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { getClient } from './client'
1919
import { getUserByEmail, removeUserByEmail } from './kvsUtils'
2020
import { IncomingSyncManager } from './sync'
2121
import { CALENDAR_INTEGRATION, GoogleEmail, Token } from './types'
22-
import { getGoogleClient, getWorkspaceToken, removeIntegrationSecret, setCredentials } from './utils'
22+
import { getGoogleClient, removeIntegrationSecret, setCredentials } from './utils'
2323

2424
export class PushHandler {
2525
constructor (
@@ -33,7 +33,7 @@ export class PushHandler {
3333
{},
3434
async () => {
3535
try {
36-
const client = await getClient(getWorkspaceToken(token.workspace))
36+
const client = await getClient(token.workspace)
3737
const res = getGoogleClient()
3838
const authSuccess = await setCredentials(res.auth, token)
3939
if (!authSuccess) {

services/calendar/pod-calendar/src/sync.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import { getRateLimitter, RateLimiter } from './rateLimiter'
5555
import { CALENDAR_INTEGRATION, GoogleEmail, Token, User } from './types'
5656
import {
5757
getGoogleClient,
58-
getWorkspaceToken,
5958
parseEventDate,
6059
parseRecurrenceStrings,
6160
removeIntegrationSecret,
@@ -98,7 +97,7 @@ export class IncomingSyncManager {
9897
}
9998

10099
static async sync (ctx: MeasureContext, accountClient: AccountClient, user: Token, email: GoogleEmail): Promise<void> {
101-
const client = await getClient(getWorkspaceToken(user.workspace))
100+
const client = await getClient(user.workspace)
102101
const txOp = new TxOperations(client, user.userId)
103102
const google = getGoogleClient()
104103
const mutex = await lock(`${user.workspace}:${user.userId}:${email}`)

services/calendar/pod-calendar/src/workspaceClient.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ import { CalendarClient } from './calendar'
2828
import { getClient } from './client'
2929
import config from './config'
3030
import { addUserByEmail, getSyncHistory, setSyncHistory } from './kvsUtils'
31+
import { synced } from './mutex'
3132
import { IncomingSyncManager } from './sync'
3233
import { getWorkspaceTokens } from './tokens'
3334
import { GoogleEmail, Token } from './types'
34-
import { getWorkspaceToken } from './utils'
35-
import { synced } from './mutex'
3635

3736
export class WorkspaceClient {
3837
private readonly clients = new Map<GoogleEmail, CalendarClient>()
@@ -49,7 +48,7 @@ export class WorkspaceClient {
4948
) {}
5049

5150
static async run (ctx: MeasureContext, accountClient: AccountClient, workspace: WorkspaceUuid): Promise<void> {
52-
const client = await getClient(getWorkspaceToken(workspace))
51+
const client = await getClient(workspace)
5352
const txOp = new TxOperations(client, core.account.System)
5453
const instance = new WorkspaceClient(ctx, accountClient, txOp, workspace)
5554

0 commit comments

Comments
 (0)