-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
6382 create a command to add a uservar in the key value pair table fo…
…r every account which needs to reconnect (#6553) Closes #6382 Create SetUserVarsAccountsToReconnectCommand. This command loops on all workspaces and: - deletes all user vars with deprecated key `ACCOUNTS_TO_RECONNECT` - creates a key value pair of type `USER_VAR` with a key of `ACCOUNTS_TO_RECONNECT_INSUFFICIENT_PERMISSIONS` for all connect accounts with a message channel or calendar channel with status `FAILED_INSUFFICIENT_PERMISSIONS`
- Loading branch information
1 parent
2abb6ad
commit 5a72b94
Showing
9 changed files
with
220 additions
and
62 deletions.
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
...atabase/commands/upgrade-version/0-23/0-23-set-user-vars-accounts-to-reconnect.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import chalk from 'chalk'; | ||
import { Command, CommandRunner, Option } from 'nest-commander'; | ||
import { Repository } from 'typeorm'; | ||
|
||
import { | ||
KeyValuePair, | ||
KeyValuePairType, | ||
} from 'src/engine/core-modules/key-value-pair/key-value-pair.entity'; | ||
import { | ||
Workspace, | ||
WorkspaceActivationStatus, | ||
} from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service'; | ||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; | ||
import { CalendarChannelSyncStatus } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity'; | ||
import { AccountsToReconnectService } from 'src/modules/connected-account/services/accounts-to-reconnect.service'; | ||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity'; | ||
import { AccountsToReconnectKeys } from 'src/modules/connected-account/types/accounts-to-reconnect-key-value.type'; | ||
import { MessageChannelSyncStatus } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity'; | ||
|
||
interface SetUserVarsAccountsToReconnectCommandOptions { | ||
workspaceId?: string; | ||
} | ||
|
||
@Command({ | ||
name: 'upgrade-0.23:set-user-vars-accounts-to-reconnect', | ||
description: 'Set user vars accounts to reconnect', | ||
}) | ||
export class SetUserVarsAccountsToReconnectCommand extends CommandRunner { | ||
private readonly logger = new Logger( | ||
SetUserVarsAccountsToReconnectCommand.name, | ||
); | ||
constructor( | ||
private readonly workspaceCacheVersionService: WorkspaceCacheVersionService, | ||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, | ||
private readonly accountsToReconnectService: AccountsToReconnectService, | ||
@InjectRepository(KeyValuePair, 'core') | ||
private readonly keyValuePairRepository: Repository<KeyValuePair>, | ||
@InjectRepository(Workspace, 'core') | ||
private readonly workspaceRepository: Repository<Workspace>, | ||
) { | ||
super(); | ||
} | ||
|
||
@Option({ | ||
flags: '-w, --workspace-id [workspace_id]', | ||
description: 'workspace id. Command runs on all workspaces if not provided', | ||
required: false, | ||
}) | ||
parseWorkspaceId(value: string): string { | ||
return value; | ||
} | ||
|
||
async run( | ||
_passedParam: string[], | ||
options: SetUserVarsAccountsToReconnectCommandOptions, | ||
): Promise<void> { | ||
let activeWorkspaceIds: string[] = []; | ||
|
||
if (options.workspaceId) { | ||
activeWorkspaceIds = [options.workspaceId]; | ||
} else { | ||
const activeWorkspaces = await this.workspaceRepository.find({ | ||
where: { | ||
activationStatus: WorkspaceActivationStatus.ACTIVE, | ||
...(options.workspaceId && { id: options.workspaceId }), | ||
}, | ||
}); | ||
|
||
activeWorkspaceIds = activeWorkspaces.map((workspace) => workspace.id); | ||
} | ||
|
||
if (!activeWorkspaceIds.length) { | ||
this.logger.log(chalk.yellow('No workspace found')); | ||
|
||
return; | ||
} else { | ||
this.logger.log( | ||
chalk.green( | ||
`Running command on ${activeWorkspaceIds.length} workspaces`, | ||
), | ||
); | ||
} | ||
|
||
// Remove all deprecated user vars | ||
await this.keyValuePairRepository.delete({ | ||
type: KeyValuePairType.USER_VAR, | ||
key: 'ACCOUNTS_TO_RECONNECT', | ||
}); | ||
|
||
for (const workspaceId of activeWorkspaceIds) { | ||
try { | ||
const connectedAccountRepository = | ||
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ConnectedAccountWorkspaceEntity>( | ||
workspaceId, | ||
'connectedAccount', | ||
); | ||
|
||
try { | ||
const connectedAccountsInFailedInsufficientPermissions = | ||
await connectedAccountRepository.find({ | ||
select: { | ||
id: true, | ||
accountOwner: { | ||
userId: true, | ||
}, | ||
}, | ||
where: [ | ||
{ | ||
messageChannels: { | ||
syncStatus: | ||
MessageChannelSyncStatus.FAILED_INSUFFICIENT_PERMISSIONS, | ||
}, | ||
}, | ||
{ | ||
calendarChannels: { | ||
syncStatus: | ||
CalendarChannelSyncStatus.FAILED_INSUFFICIENT_PERMISSIONS, | ||
}, | ||
}, | ||
], | ||
relations: { | ||
accountOwner: true, | ||
}, | ||
}); | ||
|
||
for (const connectedAccount of connectedAccountsInFailedInsufficientPermissions) { | ||
try { | ||
await this.accountsToReconnectService.addAccountToReconnectByKey( | ||
AccountsToReconnectKeys.ACCOUNTS_TO_RECONNECT_INSUFFICIENT_PERMISSIONS, | ||
connectedAccount.accountOwner.userId, | ||
workspaceId, | ||
connectedAccount.id, | ||
); | ||
} catch (error) { | ||
this.logger.error( | ||
`Failed to add account to reconnect for workspace ${workspaceId}: ${error.message}`, | ||
); | ||
throw error; | ||
} | ||
} | ||
} catch (error) { | ||
this.logger.log( | ||
chalk.red(`Running command on workspace ${workspaceId} failed`), | ||
); | ||
throw error; | ||
} | ||
|
||
await this.workspaceCacheVersionService.incrementVersion(workspaceId); | ||
|
||
this.logger.log( | ||
chalk.green(`Running command on workspace ${workspaceId} done`), | ||
); | ||
} catch (error) { | ||
this.logger.error( | ||
`Migration failed for workspace ${workspaceId}: ${error.message}`, | ||
); | ||
} | ||
} | ||
|
||
this.logger.log(chalk.green(`Command completed!`)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.