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:
@ -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 () => {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -472,7 +472,7 @@ describe('workspace permissions', () => {
|
||||
`,
|
||||
variables: {
|
||||
input: {
|
||||
publicFeatureFlag: 'TestFeature',
|
||||
publicFeatureFlag: 'IsStripeIntegrationEnabled',
|
||||
value: true,
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user