From 9e3420df07c49438f2fc9c5fafb60e730f47ed89 Mon Sep 17 00:00:00 2001 From: Marie <51697796+ijreilly@users.noreply.github.com> Date: Tue, 21 May 2024 10:46:49 +0200 Subject: [PATCH] [fix] Re-introduce beforeUpdateOneObject hook (#5495) ... and disable name edition in object edition form. This feature will be introduced by #5491 --- .../SettingsDataModelObjectAboutForm.tsx | 4 +++- .../settings/data-model/SettingsObjectEdit.tsx | 1 + .../hooks/before-update-one-object.hook.ts | 4 +--- .../object-metadata/object-metadata.module.ts | 7 ++++++- .../object-metadata/object-metadata.resolver.ts | 15 ++++++++++++--- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/twenty-front/src/modules/settings/data-model/objects/forms/components/SettingsDataModelObjectAboutForm.tsx b/packages/twenty-front/src/modules/settings/data-model/objects/forms/components/SettingsDataModelObjectAboutForm.tsx index b7dfc9229ff77..11ddfc5b5787a 100644 --- a/packages/twenty-front/src/modules/settings/data-model/objects/forms/components/SettingsDataModelObjectAboutForm.tsx +++ b/packages/twenty-front/src/modules/settings/data-model/objects/forms/components/SettingsDataModelObjectAboutForm.tsx @@ -22,6 +22,7 @@ type SettingsDataModelObjectAboutFormValues = z.infer< type SettingsDataModelObjectAboutFormProps = { disabled?: boolean; + disableNameEdit?: boolean; objectMetadataItem?: ObjectMetadataItem; }; @@ -46,6 +47,7 @@ const StyledInputContainer = styled.div` export const SettingsDataModelObjectAboutForm = ({ disabled, + disableNameEdit, objectMetadataItem, }: SettingsDataModelObjectAboutFormProps) => { const { control } = useFormContext(); @@ -93,7 +95,7 @@ export const SettingsDataModelObjectAboutForm = ({ placeholder={placeholder} value={value} onChange={onChange} - disabled={disabled} + disabled={disabled || disableNameEdit} fullWidth /> )} diff --git a/packages/twenty-front/src/pages/settings/data-model/SettingsObjectEdit.tsx b/packages/twenty-front/src/pages/settings/data-model/SettingsObjectEdit.tsx index 5bfdfd9c9f3cd..d9460720af8de 100644 --- a/packages/twenty-front/src/pages/settings/data-model/SettingsObjectEdit.tsx +++ b/packages/twenty-front/src/pages/settings/data-model/SettingsObjectEdit.tsx @@ -133,6 +133,7 @@ export const SettingsObjectEdit = () => { /> diff --git a/packages/twenty-server/src/engine/metadata-modules/object-metadata/hooks/before-update-one-object.hook.ts b/packages/twenty-server/src/engine/metadata-modules/object-metadata/hooks/before-update-one-object.hook.ts index 74b18f8059c5b..b2cef2279cd60 100644 --- a/packages/twenty-server/src/engine/metadata-modules/object-metadata/hooks/before-update-one-object.hook.ts +++ b/packages/twenty-server/src/engine/metadata-modules/object-metadata/hooks/before-update-one-object.hook.ts @@ -30,10 +30,8 @@ export class BeforeUpdateOneObject // TODO: this logic could be moved to a policy guard async run( instance: UpdateOneInputType, - context: any, + workspaceId: string, ): Promise> { - const workspaceId = context?.req?.user?.workspace?.id; - if (!workspaceId) { throw new UnauthorizedException(); } diff --git a/packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.module.ts b/packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.module.ts index 20c15c9be270f..9637f05229a81 100644 --- a/packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.module.ts +++ b/packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.module.ts @@ -19,6 +19,7 @@ import { ObjectMetadataResolver } from 'src/engine/metadata-modules/object-metad import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module'; import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; import { WorkspaceCacheVersionModule } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.module'; +import { BeforeUpdateOneObject } from 'src/engine/metadata-modules/object-metadata/hooks/before-update-one-object.hook'; import { ObjectMetadataService } from './object-metadata.service'; import { ObjectMetadataEntity } from './object-metadata.entity'; @@ -65,7 +66,11 @@ import { ObjectMetadataDTO } from './dtos/object-metadata.dto'; ], }), ], - providers: [ObjectMetadataService, ObjectMetadataResolver], + providers: [ + ObjectMetadataService, + ObjectMetadataResolver, + BeforeUpdateOneObject, + ], exports: [ObjectMetadataService], }) export class ObjectMetadataModule {} diff --git a/packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.resolver.ts b/packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.resolver.ts index 67e6305bd4d04..ccb1b2a60643e 100644 --- a/packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.resolver.ts +++ b/packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.resolver.ts @@ -7,12 +7,19 @@ import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard'; import { ObjectMetadataDTO } from 'src/engine/metadata-modules/object-metadata/dtos/object-metadata.dto'; import { DeleteOneObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/delete-object.input'; import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service'; -import { UpdateOneObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/update-object.input'; +import { + UpdateObjectPayload, + UpdateOneObjectInput, +} from 'src/engine/metadata-modules/object-metadata/dtos/update-object.input'; +import { BeforeUpdateOneObject } from 'src/engine/metadata-modules/object-metadata/hooks/before-update-one-object.hook'; @UseGuards(JwtAuthGuard) @Resolver(() => ObjectMetadataDTO) export class ObjectMetadataResolver { - constructor(private readonly objectMetadataService: ObjectMetadataService) {} + constructor( + private readonly objectMetadataService: ObjectMetadataService, + private readonly beforeUpdateOneObject: BeforeUpdateOneObject, + ) {} @Mutation(() => ObjectMetadataDTO) deleteOneObject( @@ -23,10 +30,12 @@ export class ObjectMetadataResolver { } @Mutation(() => ObjectMetadataDTO) - updateOneObject( + async updateOneObject( @Args('input') input: UpdateOneObjectInput, @AuthWorkspace() { id: workspaceId }: Workspace, ) { + await this.beforeUpdateOneObject.run(input, workspaceId); + return this.objectMetadataService.updateOneObject(input, workspaceId); } }