|
| 1 | +import { Logger } from '@nestjs/common'; |
| 2 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 3 | + |
| 4 | +import chalk from 'chalk'; |
| 5 | +import { Command, CommandRunner, Option } from 'nest-commander'; |
| 6 | +import { Repository } from 'typeorm'; |
| 7 | + |
| 8 | +import { |
| 9 | + KeyValuePair, |
| 10 | + KeyValuePairType, |
| 11 | +} from 'src/engine/core-modules/key-value-pair/key-value-pair.entity'; |
| 12 | +import { |
| 13 | + Workspace, |
| 14 | + WorkspaceActivationStatus, |
| 15 | +} from 'src/engine/core-modules/workspace/workspace.entity'; |
| 16 | +import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service'; |
| 17 | +import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; |
| 18 | +import { CalendarChannelSyncStatus } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity'; |
| 19 | +import { AccountsToReconnectService } from 'src/modules/connected-account/services/accounts-to-reconnect.service'; |
| 20 | +import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity'; |
| 21 | +import { AccountsToReconnectKeys } from 'src/modules/connected-account/types/accounts-to-reconnect-key-value.type'; |
| 22 | +import { MessageChannelSyncStatus } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity'; |
| 23 | + |
| 24 | +interface SetUserVarsAccountsToReconnectCommandOptions { |
| 25 | + workspaceId?: string; |
| 26 | +} |
| 27 | + |
| 28 | +@Command({ |
| 29 | + name: 'upgrade-0.23:set-user-vars-accounts-to-reconnect', |
| 30 | + description: 'Set user vars accounts to reconnect', |
| 31 | +}) |
| 32 | +export class SetUserVarsAccountsToReconnectCommand extends CommandRunner { |
| 33 | + private readonly logger = new Logger( |
| 34 | + SetUserVarsAccountsToReconnectCommand.name, |
| 35 | + ); |
| 36 | + constructor( |
| 37 | + private readonly workspaceCacheVersionService: WorkspaceCacheVersionService, |
| 38 | + private readonly twentyORMGlobalManager: TwentyORMGlobalManager, |
| 39 | + private readonly accountsToReconnectService: AccountsToReconnectService, |
| 40 | + @InjectRepository(KeyValuePair, 'core') |
| 41 | + private readonly keyValuePairRepository: Repository<KeyValuePair>, |
| 42 | + @InjectRepository(Workspace, 'core') |
| 43 | + private readonly workspaceRepository: Repository<Workspace>, |
| 44 | + ) { |
| 45 | + super(); |
| 46 | + } |
| 47 | + |
| 48 | + @Option({ |
| 49 | + flags: '-w, --workspace-id [workspace_id]', |
| 50 | + description: 'workspace id. Command runs on all workspaces if not provided', |
| 51 | + required: false, |
| 52 | + }) |
| 53 | + parseWorkspaceId(value: string): string { |
| 54 | + return value; |
| 55 | + } |
| 56 | + |
| 57 | + async run( |
| 58 | + _passedParam: string[], |
| 59 | + options: SetUserVarsAccountsToReconnectCommandOptions, |
| 60 | + ): Promise<void> { |
| 61 | + let activeWorkspaceIds: string[] = []; |
| 62 | + |
| 63 | + if (options.workspaceId) { |
| 64 | + activeWorkspaceIds = [options.workspaceId]; |
| 65 | + } else { |
| 66 | + const activeWorkspaces = await this.workspaceRepository.find({ |
| 67 | + where: { |
| 68 | + activationStatus: WorkspaceActivationStatus.ACTIVE, |
| 69 | + ...(options.workspaceId && { id: options.workspaceId }), |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + activeWorkspaceIds = activeWorkspaces.map((workspace) => workspace.id); |
| 74 | + } |
| 75 | + |
| 76 | + if (!activeWorkspaceIds.length) { |
| 77 | + this.logger.log(chalk.yellow('No workspace found')); |
| 78 | + |
| 79 | + return; |
| 80 | + } else { |
| 81 | + this.logger.log( |
| 82 | + chalk.green( |
| 83 | + `Running command on ${activeWorkspaceIds.length} workspaces`, |
| 84 | + ), |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + // Remove all deprecated user vars |
| 89 | + await this.keyValuePairRepository.delete({ |
| 90 | + type: KeyValuePairType.USER_VAR, |
| 91 | + key: 'ACCOUNTS_TO_RECONNECT', |
| 92 | + }); |
| 93 | + |
| 94 | + for (const workspaceId of activeWorkspaceIds) { |
| 95 | + try { |
| 96 | + const connectedAccountRepository = |
| 97 | + await this.twentyORMGlobalManager.getRepositoryForWorkspace<ConnectedAccountWorkspaceEntity>( |
| 98 | + workspaceId, |
| 99 | + 'connectedAccount', |
| 100 | + ); |
| 101 | + |
| 102 | + try { |
| 103 | + const connectedAccountsInFailedInsufficientPermissions = |
| 104 | + await connectedAccountRepository.find({ |
| 105 | + select: { |
| 106 | + id: true, |
| 107 | + accountOwner: { |
| 108 | + userId: true, |
| 109 | + }, |
| 110 | + }, |
| 111 | + where: [ |
| 112 | + { |
| 113 | + messageChannels: { |
| 114 | + syncStatus: |
| 115 | + MessageChannelSyncStatus.FAILED_INSUFFICIENT_PERMISSIONS, |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + calendarChannels: { |
| 120 | + syncStatus: |
| 121 | + CalendarChannelSyncStatus.FAILED_INSUFFICIENT_PERMISSIONS, |
| 122 | + }, |
| 123 | + }, |
| 124 | + ], |
| 125 | + relations: { |
| 126 | + accountOwner: true, |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + for (const connectedAccount of connectedAccountsInFailedInsufficientPermissions) { |
| 131 | + try { |
| 132 | + await this.accountsToReconnectService.addAccountToReconnectByKey( |
| 133 | + AccountsToReconnectKeys.ACCOUNTS_TO_RECONNECT_INSUFFICIENT_PERMISSIONS, |
| 134 | + connectedAccount.accountOwner.userId, |
| 135 | + workspaceId, |
| 136 | + connectedAccount.id, |
| 137 | + ); |
| 138 | + } catch (error) { |
| 139 | + this.logger.error( |
| 140 | + `Failed to add account to reconnect for workspace ${workspaceId}: ${error.message}`, |
| 141 | + ); |
| 142 | + throw error; |
| 143 | + } |
| 144 | + } |
| 145 | + } catch (error) { |
| 146 | + this.logger.log( |
| 147 | + chalk.red(`Running command on workspace ${workspaceId} failed`), |
| 148 | + ); |
| 149 | + throw error; |
| 150 | + } |
| 151 | + |
| 152 | + await this.workspaceCacheVersionService.incrementVersion(workspaceId); |
| 153 | + |
| 154 | + this.logger.log( |
| 155 | + chalk.green(`Running command on workspace ${workspaceId} done`), |
| 156 | + ); |
| 157 | + } catch (error) { |
| 158 | + this.logger.error( |
| 159 | + `Migration failed for workspace ${workspaceId}: ${error.message}`, |
| 160 | + ); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + this.logger.log(chalk.green(`Command completed!`)); |
| 165 | + } |
| 166 | +} |
0 commit comments