-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
New variable relation filter syntax + upgrade command #9765
Open
eliasylonen
wants to merge
6
commits into
twentyhq:main
Choose a base branch
from
eliasylonen:feat/variable-filters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42c8359
Relation filter value upgrade command draft
ad-elias e112f10
JSON formatting of simple relation filter value
ad-elias 05b4767
Fix formatting
ad-elias 3d50a3d
New relation filter value format
ad-elias e0abee6
Fix upgrade command
ad-elias 688d44b
Merge branch 'main' into feat/variable-filters
lucasbordeau 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
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
2 changes: 1 addition & 1 deletion
2
.../object-record/object-filter-dropdown/constants/CurrentWorkspaceMemberSelectableItemId.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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export const CURRENT_WORKSPACE_MEMBER_SELECTABLE_ITEM_ID = | ||
'CURRENT_WORKSPACE_MEMBER'; | ||
'{{CURRENT_WORKSPACE_MEMBER}}'; |
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
4 changes: 0 additions & 4 deletions
4
packages/twenty-front/src/modules/views/view-filter-value/types/RelationFilterValue.ts
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
...front/src/modules/views/view-filter-value/validation-schemas/relationFilterValueSchema.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,27 @@ | ||
import { z } from 'zod'; | ||
|
||
const relationFilterValueItemSchema = z.union([ | ||
z.string().uuid(), | ||
z.literal('{{CURRENT_WORKSPACE_MEMBER}}'), | ||
]); | ||
|
||
const relationFilterValueArraySchema = z.array(relationFilterValueItemSchema); | ||
|
||
export const relationFilterValueSchema = z | ||
.string() | ||
.transform((value, ctx) => { | ||
if (value === '') { | ||
return []; | ||
} | ||
|
||
try { | ||
return JSON.parse(value); | ||
} catch (error) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: (error as Error).message, | ||
}); | ||
return z.NEVER; | ||
} | ||
}) | ||
.pipe(relationFilterValueArraySchema); |
11 changes: 0 additions & 11 deletions
11
...src/modules/views/view-filter-value/validation-schemas/simpleRelationFilterValueSchema.ts
This file was deleted.
Oops, something went wrong.
153 changes: 153 additions & 0 deletions
153
...ase/commands/upgrade-version/0-42/0-42-standardize-variable-view-filter-syntax.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,153 @@ | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import chalk from 'chalk'; | ||
import { Command } from 'nest-commander'; | ||
import { FieldMetadataType } from 'twenty-shared'; | ||
import { In, Repository } from 'typeorm'; | ||
import { z } from 'zod'; | ||
|
||
import { | ||
ActiveWorkspacesCommandOptions, | ||
ActiveWorkspacesCommandRunner, | ||
} from 'src/database/commands/active-workspaces.command'; | ||
import { isCommandLogger } from 'src/database/commands/logger'; | ||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; | ||
import { WorkspaceMetadataVersionService } from 'src/engine/metadata-modules/workspace-metadata-version/services/workspace-metadata-version.service'; | ||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; | ||
import { WorkspaceMigrationRunnerService } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service'; | ||
|
||
const relationFilterValueSchemaObject = z.object({ | ||
isCurrentWorkspaceMemberSelected: z.boolean().optional(), | ||
selectedRecordIds: z.array(z.string()), | ||
}); | ||
|
||
const jsonRelationFilterValueSchema = z | ||
.string() | ||
.transform((value, ctx) => { | ||
try { | ||
return JSON.parse(value); | ||
} catch (error) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: (error as Error).message, | ||
}); | ||
|
||
return z.NEVER; | ||
} | ||
}) | ||
.pipe(relationFilterValueSchemaObject); | ||
|
||
@Command({ | ||
name: 'upgrade-0.42:standardize-variable-view-filter-syntax', | ||
description: 'Standardize variable view filter syntax', | ||
}) | ||
export class StandardizeVariableViewFilterSyntaxCommand extends ActiveWorkspacesCommandRunner { | ||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
protected readonly workspaceRepository: Repository<Workspace>, | ||
@InjectRepository(FieldMetadataEntity, 'metadata') | ||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>, | ||
private readonly workspaceMigrationRunnerService: WorkspaceMigrationRunnerService, | ||
private readonly workspaceMetadataVersionService: WorkspaceMetadataVersionService, | ||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, | ||
) { | ||
super(workspaceRepository); | ||
} | ||
|
||
async executeActiveWorkspacesCommand( | ||
_passedParam: string[], | ||
options: ActiveWorkspacesCommandOptions, | ||
workspaceIds: string[], | ||
): Promise<void> { | ||
this.logger.log( | ||
'Running command to standardize view filter syntax for relation filters', | ||
); | ||
|
||
if (isCommandLogger(this.logger)) { | ||
this.logger.setVerbose(options.verbose ?? false); | ||
} | ||
|
||
let workspaceIterator = 1; | ||
|
||
for (const workspaceId of workspaceIds) { | ||
this.logger.log( | ||
`Running command for workspace ${workspaceId} ${workspaceIterator}/${workspaceIds.length}`, | ||
); | ||
|
||
try { | ||
const viewFilterRepository = | ||
await this.twentyORMGlobalManager.getRepositoryForWorkspace( | ||
workspaceId, | ||
'viewFilter', | ||
false, | ||
); | ||
|
||
const relationFieldMetadata = await this.fieldMetadataRepository.find({ | ||
where: { | ||
workspaceId, | ||
type: FieldMetadataType.RELATION, | ||
}, | ||
}); | ||
|
||
const relationFieldMetadataIds = relationFieldMetadata.map( | ||
(fieldMetadata) => fieldMetadata.id, | ||
); | ||
|
||
const relationViewFilters = await viewFilterRepository.find({ | ||
where: { | ||
fieldMetadataId: In(relationFieldMetadataIds), | ||
}, | ||
}); | ||
|
||
for (const relationViewFilter of relationViewFilters) { | ||
await viewFilterRepository.update(relationViewFilter.id, { | ||
...relationViewFilter, | ||
value: this.convertRelationViewFilterValue( | ||
relationViewFilter.value, | ||
), | ||
}); | ||
} | ||
|
||
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations( | ||
workspaceId, | ||
); | ||
|
||
await this.workspaceMetadataVersionService.incrementMetadataVersion( | ||
workspaceId, | ||
); | ||
|
||
workspaceIterator++; | ||
this.logger.log( | ||
chalk.green(`Command completed for workspace ${workspaceId}.`), | ||
); | ||
} catch (error) { | ||
this.logger.error( | ||
`Error running command for workspace ${workspaceId}: ${error}`, | ||
); | ||
} | ||
} | ||
this.logger.log(chalk.green(`Command completed!`)); | ||
} | ||
|
||
private convertRelationViewFilterValue(value: string): string { | ||
const jsonRelationFilterValueResult = | ||
jsonRelationFilterValueSchema.safeParse(value); | ||
|
||
if (!jsonRelationFilterValueResult.success) { | ||
return value; | ||
} | ||
|
||
const { selectedRecordIds, isCurrentWorkspaceMemberSelected } = | ||
jsonRelationFilterValueResult.data; | ||
|
||
if (isCurrentWorkspaceMemberSelected) { | ||
return JSON.stringify([ | ||
...selectedRecordIds, | ||
'{{CURRENT_WORKSPACE_MEMBER}}', | ||
]); | ||
} | ||
|
||
return JSON.stringify(selectedRecordIds); | ||
} | ||
} |
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.
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.
I was initially hoping we could do something a bit more elegant that replaces {{ }} at a higher level for all filter types, but it's maybe more pragmatic to go for that! Fine with me. cc @charlesBochet again