Skip to content

Commit

Permalink
hotfix for lab (#9981)
Browse files Browse the repository at this point in the history
Issue:
When attempting to toggle a public feature flag that didn't exist in the
workspace's feature flags array, the LabService threw a
FeatureFlagException with code FEATURE_FLAG_NOT_FOUND. This prevented
users from enabling public feature flags for the first time in their
workspace.
Fix:
Modified the LabService to handle non-existent public feature flags by
creating them instead of throwing an error. When the flag doesn't exist,
the service now saves a new feature flag record with the provided key
and value, associates it with the workspace, and returns the newly
created flag.
  • Loading branch information
ehconitin authored Feb 3, 2025
1 parent 7a0f2f8 commit edeaeca
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 29 deletions.
10 changes: 7 additions & 3 deletions packages/twenty-front/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ export type Mutation = {
skipSyncEmailOnboardingStep: OnboardingStepSuccess;
track: Analytics;
updateBillingSubscription: BillingUpdateOutput;
updateLabPublicFeatureFlag: Scalars['Boolean'];
updateLabPublicFeatureFlag: FeatureFlag;
updateOneField: Field;
updateOneObject: Object;
updateOneServerlessFunction: ServerlessFunction;
Expand Down Expand Up @@ -2225,7 +2225,7 @@ export type UpdateLabPublicFeatureFlagMutationVariables = Exact<{
}>;


export type UpdateLabPublicFeatureFlagMutation = { __typename?: 'Mutation', updateLabPublicFeatureFlag: boolean };
export type UpdateLabPublicFeatureFlagMutation = { __typename?: 'Mutation', updateLabPublicFeatureFlag: { __typename?: 'FeatureFlag', id: any, key: FeatureFlagKey, value: boolean } };

export type CreateOidcIdentityProviderMutationVariables = Exact<{
input: SetupOidcSsoInput;
Expand Down Expand Up @@ -3884,7 +3884,11 @@ export type GetEnvironmentVariablesGroupedLazyQueryHookResult = ReturnType<typeo
export type GetEnvironmentVariablesGroupedQueryResult = Apollo.QueryResult<GetEnvironmentVariablesGroupedQuery, GetEnvironmentVariablesGroupedQueryVariables>;
export const UpdateLabPublicFeatureFlagDocument = gql`
mutation UpdateLabPublicFeatureFlag($input: UpdateLabPublicFeatureFlagInput!) {
updateLabPublicFeatureFlag(input: $input)
updateLabPublicFeatureFlag(input: $input) {
id
key
value
}
}
`;
export type UpdateLabPublicFeatureFlagMutationFn = Apollo.MutationFunction<UpdateLabPublicFeatureFlagMutation, UpdateLabPublicFeatureFlagMutationVariables>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export const UPDATE_LAB_PUBLIC_FEATURE_FLAG = gql`
mutation UpdateLabPublicFeatureFlag(
$input: UpdateLabPublicFeatureFlagInput!
) {
updateLabPublicFeatureFlag(input: $input)
updateLabPublicFeatureFlag(input: $input) {
id
key
value
}
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,29 @@ export const useLabPublicFeatureFlags = () => {
);
const labPublicFeatureFlags = useRecoilValue(labPublicFeatureFlagsState);

const [updateLabPublicFeatureFlag] = useUpdateLabPublicFeatureFlagMutation();
const [updateLabPublicFeatureFlag] = useUpdateLabPublicFeatureFlagMutation({
onCompleted: (data) => {
if (isDefined(currentWorkspace)) {
const updatedFlag = data.updateLabPublicFeatureFlag;

setCurrentWorkspace({
...currentWorkspace,
featureFlags: [
...(currentWorkspace.featureFlags?.filter(
(flag) => flag.key !== updatedFlag.key,
) ?? []),
{
...updatedFlag,
workspaceId: currentWorkspace.id,
},
],
});
}
},
onError: (error) => {
setError(error.message);
},
});

const handleLabPublicFeatureFlagUpdate = async (
publicFeatureFlag: FeatureFlagKey,
Expand All @@ -35,20 +57,8 @@ export const useLabPublicFeatureFlags = () => {
value,
},
},
onError: (error) => {
setError(error.message);
},
});

if (isDefined(response.data)) {
setCurrentWorkspace({
...currentWorkspace,
featureFlags: currentWorkspace.featureFlags?.map((flag) =>
flag.key === publicFeatureFlag ? { ...flag, value } : flag,
),
});
}

return !!response.data;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UseFilters, UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

import { AuthGraphqlApiExceptionFilter } from 'src/engine/core-modules/auth/filters/auth-graphql-api-exception.filter';
import { FeatureFlag } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { UpdateLabPublicFeatureFlagInput } from 'src/engine/core-modules/lab/dtos/update-lab-public-feature-flag.input';
import { LabService } from 'src/engine/core-modules/lab/services/lab.service';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
Expand All @@ -14,13 +15,11 @@ export class LabResolver {
constructor(private labService: LabService) {}

@UseGuards(WorkspaceAuthGuard)
@Mutation(() => Boolean)
@Mutation(() => FeatureFlag)
async updateLabPublicFeatureFlag(
@Args('input') input: UpdateLabPublicFeatureFlagInput,
@AuthWorkspace() workspace: Workspace,
): Promise<boolean> {
await this.labService.updateLabPublicFeatureFlag(workspace.id, input);

return true;
): Promise<FeatureFlag> {
return this.labService.updateLabPublicFeatureFlag(workspace.id, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class LabService {
async updateLabPublicFeatureFlag(
workspaceId: string,
payload: UpdateLabPublicFeatureFlagInput,
): Promise<void> {
): Promise<FeatureFlag> {
featureFlagValidator.assertIsFeatureFlagKey(
payload.publicFeatureFlag,
new FeatureFlagException(
Expand Down Expand Up @@ -62,15 +62,24 @@ export class LabService {
(flag) => flag.key === FeatureFlagKey[payload.publicFeatureFlag],
);

if (!existingFlag) {
throw new FeatureFlagException(
'Public feature flag not found',
FeatureFlagExceptionCode.FEATURE_FLAG_NOT_FOUND,
);
if (existingFlag) {
await this.featureFlagRepository.update(existingFlag.id, {
value: payload.value,
});

return { ...existingFlag, value: payload.value };
}

await this.featureFlagRepository.update(existingFlag.id, {
const newFlag = await this.featureFlagRepository.save({
key: FeatureFlagKey[payload.publicFeatureFlag],
value: payload.value,
workspaceId: workspace.id,
});

workspace.featureFlags = [...(workspace.featureFlags || []), newFlag];

await this.workspaceRepository.save(workspace);

return newFlag;
}
}

0 comments on commit edeaeca

Please sign in to comment.