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(),
|
findOneBy: jest.fn(),
|
||||||
find: jest.fn(),
|
find: jest.fn(),
|
||||||
upsert: jest.fn(),
|
upsert: jest.fn(),
|
||||||
|
findOne: jest.fn(),
|
||||||
|
save: jest.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const workspaceId = 'workspace-id';
|
const workspaceId = 'workspace-id';
|
||||||
@ -179,9 +181,7 @@ describe('FeatureFlagService', () => {
|
|||||||
workspaceId,
|
workspaceId,
|
||||||
};
|
};
|
||||||
|
|
||||||
mockFeatureFlagRepository.upsert.mockResolvedValue({
|
mockFeatureFlagRepository.save.mockResolvedValue(mockFeatureFlag);
|
||||||
generatedMaps: [mockFeatureFlag],
|
|
||||||
});
|
|
||||||
|
|
||||||
(
|
(
|
||||||
featureFlagValidator.assertIsFeatureFlagKey as jest.Mock
|
featureFlagValidator.assertIsFeatureFlagKey as jest.Mock
|
||||||
@ -196,17 +196,11 @@ describe('FeatureFlagService', () => {
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(result).toEqual(mockFeatureFlag);
|
expect(result).toEqual(mockFeatureFlag);
|
||||||
expect(mockFeatureFlagRepository.upsert).toHaveBeenCalledWith(
|
expect(mockFeatureFlagRepository.save).toHaveBeenCalledWith({
|
||||||
{
|
key: FeatureFlagKey[featureFlag],
|
||||||
key: FeatureFlagKey[featureFlag],
|
value,
|
||||||
value,
|
workspaceId,
|
||||||
workspaceId,
|
});
|
||||||
},
|
|
||||||
{
|
|
||||||
conflictPaths: ['workspaceId', 'key'],
|
|
||||||
skipUpdateIfNoValuesChanged: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an exception when feature flag key is invalid', async () => {
|
it('should throw an exception when feature flag key is invalid', async () => {
|
||||||
|
|||||||
@ -82,16 +82,6 @@ export class FeatureFlagService {
|
|||||||
value: boolean;
|
value: boolean;
|
||||||
shouldBePublic?: boolean;
|
shouldBePublic?: boolean;
|
||||||
}): Promise<FeatureFlag> {
|
}): Promise<FeatureFlag> {
|
||||||
if (shouldBePublic) {
|
|
||||||
publicFeatureFlagValidator.assertIsPublicFeatureFlag(
|
|
||||||
featureFlag,
|
|
||||||
new FeatureFlagException(
|
|
||||||
'Invalid feature flag key, flag is not public',
|
|
||||||
FeatureFlagExceptionCode.INVALID_FEATURE_FLAG_KEY,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
featureFlagValidator.assertIsFeatureFlagKey(
|
featureFlagValidator.assertIsFeatureFlagKey(
|
||||||
featureFlag,
|
featureFlag,
|
||||||
new FeatureFlagException(
|
new FeatureFlagException(
|
||||||
@ -100,18 +90,36 @@ export class FeatureFlagService {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
const upsertResult = await this.featureFlagRepository.upsert(
|
const featureFlagKey = FeatureFlagKey[featureFlag];
|
||||||
{
|
|
||||||
key: FeatureFlagKey[featureFlag],
|
if (shouldBePublic) {
|
||||||
value,
|
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,
|
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: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
publicFeatureFlag: 'TestFeature',
|
publicFeatureFlag: 'IsStripeIntegrationEnabled',
|
||||||
value: true,
|
value: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user