Skip to content

Commit

Permalink
[calendar/messaging] fix google refresh token transaction (twentyhq#4989
Browse files Browse the repository at this point in the history
)

## Context
The full-sync job was enqueued within a transaction, which means it
could be executed before the transaction was commit and
connected-account was not created yet.
This PR re-arrange the code a bit to avoid this

cc @bosiraphael thx for flagging this!
  • Loading branch information
Weiko authored Apr 16, 2024
1 parent 5dbe775 commit 38745d8
Showing 1 changed file with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@ export class GoogleAPIsService {

const isCalendarEnabled =
this.environmentService.get('CALENDAR_PROVIDER_GOOGLE_ENABLED') &&
!!isCalendarEnabledFlag;
!!isCalendarEnabledFlag?.value;

await workspaceDataSource?.transaction(async (manager: EntityManager) => {
const connectedAccounts =
await this.connectedAccountRepository.getAllByHandleAndWorkspaceMemberId(
handle,
workspaceMemberId,
workspaceId,
manager,
);
const connectedAccounts =
await this.connectedAccountRepository.getAllByHandleAndWorkspaceMemberId(
handle,
workspaceMemberId,
workspaceId,
);

if (!connectedAccounts || connectedAccounts?.length === 0) {
const newConnectedAccountId = v4();
const existingAccountId = connectedAccounts?.[0]?.id;
const newOrExistingConnectedAccountId = existingAccountId ?? v4();

await workspaceDataSource?.transaction(async (manager: EntityManager) => {
if (!existingAccountId) {
await this.connectedAccountRepository.create(
{
id: newConnectedAccountId,
id: newOrExistingConnectedAccountId,
handle,
provider: ConnectedAccountProvider.GOOGLE,
accessToken: input.accessToken,
Expand All @@ -114,7 +114,7 @@ export class GoogleAPIsService {
await this.messageChannelRepository.create(
{
id: v4(),
connectedAccountId: newConnectedAccountId,
connectedAccountId: newOrExistingConnectedAccountId,
type: MessageChannelType.EMAIL,
handle,
visibility: MessageChannelVisibility.SHARE_EVERYTHING,
Expand All @@ -127,42 +127,36 @@ export class GoogleAPIsService {
await this.calendarChannelRepository.create(
{
id: v4(),
connectedAccountId: newConnectedAccountId,
connectedAccountId: newOrExistingConnectedAccountId,
handle,
visibility: CalendarChannelVisibility.SHARE_EVERYTHING,
},
workspaceId,
manager,
);
}

await this.enqueueSyncJobs(
newConnectedAccountId,
workspaceId,
isCalendarEnabled,
);
} else {
await this.connectedAccountRepository.updateAccessTokenAndRefreshToken(
input.accessToken,
input.refreshToken,
connectedAccounts[0].id,
newOrExistingConnectedAccountId,
workspaceId,
manager,
);

await this.messageChannelRepository.resetSync(
connectedAccounts[0].id,
newOrExistingConnectedAccountId,
workspaceId,
manager,
);

await this.enqueueSyncJobs(
connectedAccounts[0].id,
workspaceId,
isCalendarEnabled,
);
}
});

await this.enqueueSyncJobs(
newOrExistingConnectedAccountId,
workspaceId,
isCalendarEnabled,
);
}

private async enqueueSyncJobs(
Expand Down

0 comments on commit 38745d8

Please sign in to comment.