[calendar/messaging] fix google refresh token transaction (#4989)
## 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!
This commit is contained in:
@ -84,23 +84,23 @@ export class GoogleAPIsService {
|
|||||||
|
|
||||||
const isCalendarEnabled =
|
const isCalendarEnabled =
|
||||||
this.environmentService.get('CALENDAR_PROVIDER_GOOGLE_ENABLED') &&
|
this.environmentService.get('CALENDAR_PROVIDER_GOOGLE_ENABLED') &&
|
||||||
!!isCalendarEnabledFlag;
|
!!isCalendarEnabledFlag?.value;
|
||||||
|
|
||||||
|
const connectedAccounts =
|
||||||
|
await this.connectedAccountRepository.getAllByHandleAndWorkspaceMemberId(
|
||||||
|
handle,
|
||||||
|
workspaceMemberId,
|
||||||
|
workspaceId,
|
||||||
|
);
|
||||||
|
|
||||||
|
const existingAccountId = connectedAccounts?.[0]?.id;
|
||||||
|
const newOrExistingConnectedAccountId = existingAccountId ?? v4();
|
||||||
|
|
||||||
await workspaceDataSource?.transaction(async (manager: EntityManager) => {
|
await workspaceDataSource?.transaction(async (manager: EntityManager) => {
|
||||||
const connectedAccounts =
|
if (!existingAccountId) {
|
||||||
await this.connectedAccountRepository.getAllByHandleAndWorkspaceMemberId(
|
|
||||||
handle,
|
|
||||||
workspaceMemberId,
|
|
||||||
workspaceId,
|
|
||||||
manager,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!connectedAccounts || connectedAccounts?.length === 0) {
|
|
||||||
const newConnectedAccountId = v4();
|
|
||||||
|
|
||||||
await this.connectedAccountRepository.create(
|
await this.connectedAccountRepository.create(
|
||||||
{
|
{
|
||||||
id: newConnectedAccountId,
|
id: newOrExistingConnectedAccountId,
|
||||||
handle,
|
handle,
|
||||||
provider: ConnectedAccountProvider.GOOGLE,
|
provider: ConnectedAccountProvider.GOOGLE,
|
||||||
accessToken: input.accessToken,
|
accessToken: input.accessToken,
|
||||||
@ -114,7 +114,7 @@ export class GoogleAPIsService {
|
|||||||
await this.messageChannelRepository.create(
|
await this.messageChannelRepository.create(
|
||||||
{
|
{
|
||||||
id: v4(),
|
id: v4(),
|
||||||
connectedAccountId: newConnectedAccountId,
|
connectedAccountId: newOrExistingConnectedAccountId,
|
||||||
type: MessageChannelType.EMAIL,
|
type: MessageChannelType.EMAIL,
|
||||||
handle,
|
handle,
|
||||||
visibility: MessageChannelVisibility.SHARE_EVERYTHING,
|
visibility: MessageChannelVisibility.SHARE_EVERYTHING,
|
||||||
@ -127,7 +127,7 @@ export class GoogleAPIsService {
|
|||||||
await this.calendarChannelRepository.create(
|
await this.calendarChannelRepository.create(
|
||||||
{
|
{
|
||||||
id: v4(),
|
id: v4(),
|
||||||
connectedAccountId: newConnectedAccountId,
|
connectedAccountId: newOrExistingConnectedAccountId,
|
||||||
handle,
|
handle,
|
||||||
visibility: CalendarChannelVisibility.SHARE_EVERYTHING,
|
visibility: CalendarChannelVisibility.SHARE_EVERYTHING,
|
||||||
},
|
},
|
||||||
@ -135,34 +135,28 @@ export class GoogleAPIsService {
|
|||||||
manager,
|
manager,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.enqueueSyncJobs(
|
|
||||||
newConnectedAccountId,
|
|
||||||
workspaceId,
|
|
||||||
isCalendarEnabled,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
await this.connectedAccountRepository.updateAccessTokenAndRefreshToken(
|
await this.connectedAccountRepository.updateAccessTokenAndRefreshToken(
|
||||||
input.accessToken,
|
input.accessToken,
|
||||||
input.refreshToken,
|
input.refreshToken,
|
||||||
connectedAccounts[0].id,
|
newOrExistingConnectedAccountId,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
manager,
|
manager,
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.messageChannelRepository.resetSync(
|
await this.messageChannelRepository.resetSync(
|
||||||
connectedAccounts[0].id,
|
newOrExistingConnectedAccountId,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
manager,
|
manager,
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.enqueueSyncJobs(
|
|
||||||
connectedAccounts[0].id,
|
|
||||||
workspaceId,
|
|
||||||
isCalendarEnabled,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await this.enqueueSyncJobs(
|
||||||
|
newOrExistingConnectedAccountId,
|
||||||
|
workspaceId,
|
||||||
|
isCalendarEnabled,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async enqueueSyncJobs(
|
private async enqueueSyncJobs(
|
||||||
|
|||||||
Reference in New Issue
Block a user