fix public feature flag update (#10887)

## Context
upsert from typeorm does not seem to return keys that are not updated,
I'm reverting back to find/save since upsert is not consistent
This commit is contained in:
Weiko
2025-03-14 15:01:06 +01:00
committed by GitHub
parent fe4b47b781
commit 9883472d55
3 changed files with 37 additions and 35 deletions

View File

@ -25,6 +25,8 @@ describe('FeatureFlagService', () => {
findOneBy: jest.fn(),
find: jest.fn(),
upsert: jest.fn(),
findOne: jest.fn(),
save: jest.fn(),
};
const workspaceId = 'workspace-id';
@ -179,9 +181,7 @@ describe('FeatureFlagService', () => {
workspaceId,
};
mockFeatureFlagRepository.upsert.mockResolvedValue({
generatedMaps: [mockFeatureFlag],
});
mockFeatureFlagRepository.save.mockResolvedValue(mockFeatureFlag);
(
featureFlagValidator.assertIsFeatureFlagKey as jest.Mock
@ -196,17 +196,11 @@ describe('FeatureFlagService', () => {
// Assert
expect(result).toEqual(mockFeatureFlag);
expect(mockFeatureFlagRepository.upsert).toHaveBeenCalledWith(
{
key: FeatureFlagKey[featureFlag],
value,
workspaceId,
},
{
conflictPaths: ['workspaceId', 'key'],
skipUpdateIfNoValuesChanged: true,
},
);
expect(mockFeatureFlagRepository.save).toHaveBeenCalledWith({
key: FeatureFlagKey[featureFlag],
value,
workspaceId,
});
});
it('should throw an exception when feature flag key is invalid', async () => {

View File

@ -82,16 +82,6 @@ export class FeatureFlagService {
value: boolean;
shouldBePublic?: boolean;
}): Promise<FeatureFlag> {
if (shouldBePublic) {
publicFeatureFlagValidator.assertIsPublicFeatureFlag(
featureFlag,
new FeatureFlagException(
'Invalid feature flag key, flag is not public',
FeatureFlagExceptionCode.INVALID_FEATURE_FLAG_KEY,
),
);
}
featureFlagValidator.assertIsFeatureFlagKey(
featureFlag,
new FeatureFlagException(
@ -100,18 +90,36 @@ export class FeatureFlagService {
),
);
const upsertResult = await this.featureFlagRepository.upsert(
{
key: FeatureFlagKey[featureFlag],
value,
const featureFlagKey = FeatureFlagKey[featureFlag];
if (shouldBePublic) {
publicFeatureFlagValidator.assertIsPublicFeatureFlag(
featureFlagKey,
new FeatureFlagException(
'Invalid feature flag key, flag is not public',
FeatureFlagExceptionCode.INVALID_FEATURE_FLAG_KEY,
),
);
}
const existingFeatureFlag = await this.featureFlagRepository.findOne({
where: {
key: featureFlagKey,
workspaceId: workspaceId,
},
{
conflictPaths: ['workspaceId', 'key'],
skipUpdateIfNoValuesChanged: true,
},
);
});
return upsertResult.generatedMaps[0] as FeatureFlag;
const featureFlagToSave = existingFeatureFlag
? {
...existingFeatureFlag,
value,
}
: {
key: featureFlagKey,
value,
workspaceId: workspaceId,
};
return await this.featureFlagRepository.save(featureFlagToSave);
}
}

View File

@ -472,7 +472,7 @@ describe('workspace permissions', () => {
`,
variables: {
input: {
publicFeatureFlag: 'TestFeature',
publicFeatureFlag: 'IsStripeIntegrationEnabled',
value: true,
},
},