-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
6382 create a command to add a uservar in the key value pair table for every account which needs to reconnect #6553
Merged
Weiko
merged 13 commits into
main
from
6382-create-a-command-to-add-a-uservar-in-the-key-value-pair-table-for-every-account-which-needs-to-reconnect
Aug 7, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9991ab9
create command
bosiraphael 16f0726
create addAccountToReconnectByKey
bosiraphael 43e6faa
remove unecessary transaction in SetWorkspaceActivationStatusCommand
bosiraphael 21ad7b3
remove transaction
bosiraphael c562bb2
delete all deprecated user vars
bosiraphael d546193
add command to 0.23 upgrade command
bosiraphael e03b41b
modify command name
bosiraphael 1086770
fix imports
bosiraphael 9cd8695
remove log
bosiraphael 36c97fe
Merge main
bosiraphael 5c6a573
remove duplicated provider
bosiraphael 00929ff
replace deprecated function
bosiraphael 97969d3
remove datasource check
bosiraphael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: UpgradeTo0_23Command is duplicated in providers array