forked from bloom-housing/bloom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: feature flag consumption (bloom-housing#4489) (#820)
* feat: feature flag controller service and tests 4459 * feat: associate jurisdictions tests * feat: permissions and permission tests * feat: make naming random * feat: edge case coverage * feat: refine uuid array validation * feat: remove unused import * feat: controller cleanup
- Loading branch information
Showing
24 changed files
with
1,737 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Prisma } from '@prisma/client'; | ||
import { randomBoolean } from './boolean-generator'; | ||
import { randomAdjective, randomName } from './word-generator'; | ||
|
||
export const featureFlagFactory = ( | ||
name = randomName(), | ||
active = randomBoolean(), | ||
description = `${randomAdjective()} feature flag`, | ||
jurisdictionIds?: string[], | ||
): Prisma.FeatureFlagsCreateInput => ({ | ||
name: name, | ||
description: description, | ||
active: active, | ||
jurisdictions: jurisdictionIds | ||
? { | ||
connect: jurisdictionIds.map((jurisdiction) => { | ||
return { | ||
id: jurisdiction, | ||
}; | ||
}), | ||
} | ||
: undefined, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
ParseUUIDPipe, | ||
Post, | ||
Put, | ||
UseGuards, | ||
UsePipes, | ||
ValidationPipe, | ||
} from '@nestjs/common'; | ||
import { | ||
ApiExtraModels, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { FeatureFlagService } from '../services/feature-flag.service'; | ||
import { FeatureFlag } from '../dtos/feature-flags/feature-flag.dto'; | ||
import { FeatureFlagAssociate } from '../dtos/feature-flags/feature-flag-associate.dto'; | ||
import { FeatureFlagCreate } from '../dtos/feature-flags/feature-flag-create.dto'; | ||
import { FeatureFlagUpdate } from '../dtos/feature-flags/feature-flag-update.dto'; | ||
import { defaultValidationPipeOptions } from '../utilities/default-validation-pipe-options'; | ||
import { IdDTO } from '../dtos/shared/id.dto'; | ||
import { SuccessDTO } from '../dtos/shared/success.dto'; | ||
import { PermissionTypeDecorator } from '../decorators/permission-type.decorator'; | ||
import { OptionalAuthGuard } from '../guards/optional.guard'; | ||
import { PermissionGuard } from '../guards/permission.guard'; | ||
import { ApiKeyGuard } from '../guards/api-key.guard'; | ||
|
||
@Controller('featureFlags') | ||
@ApiTags('featureFlags') | ||
@UsePipes(new ValidationPipe(defaultValidationPipeOptions)) | ||
@ApiExtraModels( | ||
FeatureFlagAssociate, | ||
FeatureFlagCreate, | ||
FeatureFlagUpdate, | ||
IdDTO, | ||
) | ||
@PermissionTypeDecorator('featureFlags') | ||
@UseGuards(ApiKeyGuard, OptionalAuthGuard, PermissionGuard) | ||
export class FeatureFlagController { | ||
constructor(private readonly featureFlagService: FeatureFlagService) {} | ||
|
||
@Get() | ||
@ApiOperation({ summary: 'List of feature flags', operationId: 'list' }) | ||
@ApiOkResponse({ type: FeatureFlag, isArray: true }) | ||
async list(): Promise<FeatureFlag[]> { | ||
return await this.featureFlagService.list(); | ||
} | ||
|
||
@Post() | ||
@ApiOperation({ | ||
summary: 'Create a feature flag', | ||
operationId: 'create', | ||
}) | ||
@ApiOkResponse({ type: FeatureFlag }) | ||
async create(@Body() featureFlag: FeatureFlagCreate): Promise<FeatureFlag> { | ||
return await this.featureFlagService.create(featureFlag); | ||
} | ||
|
||
@Put() | ||
@ApiOperation({ | ||
summary: 'Update a feature flag', | ||
operationId: 'update', | ||
}) | ||
@ApiOkResponse({ type: FeatureFlag }) | ||
async update(@Body() featureFlag: FeatureFlagUpdate): Promise<FeatureFlag> { | ||
return await this.featureFlagService.update(featureFlag); | ||
} | ||
|
||
@Delete() | ||
@ApiOperation({ | ||
summary: 'Delete a feature flag by id', | ||
operationId: 'delete', | ||
}) | ||
@ApiOkResponse({ type: SuccessDTO }) | ||
async delete(@Body() dto: IdDTO): Promise<SuccessDTO> { | ||
return await this.featureFlagService.delete(dto.id); | ||
} | ||
|
||
@Put(`associateJurisdictions`) | ||
@ApiOperation({ | ||
summary: 'Associate and disassociate jurisdictions with a feature flag', | ||
operationId: 'associateJurisdictions', | ||
}) | ||
@ApiOkResponse({ type: FeatureFlag }) | ||
async associateJurisdictions( | ||
@Body() featureFlagAssociate: FeatureFlagAssociate, | ||
): Promise<FeatureFlag> { | ||
return await this.featureFlagService.associateJurisdictions( | ||
featureFlagAssociate, | ||
); | ||
} | ||
|
||
@Get(`:featureFlagId`) | ||
@ApiOperation({ | ||
summary: 'Get a feature flag by id', | ||
operationId: 'retrieve', | ||
}) | ||
@ApiOkResponse({ type: FeatureFlag }) | ||
async retrieve( | ||
@Param('featureFlagId', new ParseUUIDPipe({ version: '4' })) | ||
featureFlagId: string, | ||
): Promise<FeatureFlag> { | ||
return this.featureFlagService.findOne(featureFlagId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Expose } from 'class-transformer'; | ||
import { IsArray, IsDefined, IsString, IsUUID } from 'class-validator'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { ValidationsGroupsEnum } from '../../enums/shared/validation-groups-enum'; | ||
|
||
export class FeatureFlagAssociate { | ||
@Expose() | ||
@IsString({ groups: [ValidationsGroupsEnum.default] }) | ||
@IsUUID(4, { groups: [ValidationsGroupsEnum.default] }) | ||
@IsDefined({ groups: [ValidationsGroupsEnum.default] }) | ||
@ApiProperty() | ||
id: string; | ||
|
||
@Expose() | ||
@IsArray({ groups: [ValidationsGroupsEnum.default] }) | ||
@IsUUID(4, { | ||
groups: [ValidationsGroupsEnum.default], | ||
each: true, | ||
}) | ||
@ApiProperty() | ||
associate: string[]; | ||
|
||
@Expose() | ||
@IsArray({ groups: [ValidationsGroupsEnum.default] }) | ||
@IsUUID(4, { | ||
groups: [ValidationsGroupsEnum.default], | ||
each: true, | ||
}) | ||
@ApiProperty() | ||
remove: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { OmitType } from '@nestjs/swagger'; | ||
import { FeatureFlagUpdate } from './feature-flag-update.dto'; | ||
|
||
export class FeatureFlagCreate extends OmitType(FeatureFlagUpdate, ['id']) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { OmitType } from '@nestjs/swagger'; | ||
import { FeatureFlag } from './feature-flag.dto'; | ||
|
||
export class FeatureFlagUpdate extends OmitType(FeatureFlag, [ | ||
'createdAt', | ||
'updatedAt', | ||
'jurisdictions', | ||
]) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Expose, Type } from 'class-transformer'; | ||
import { | ||
IsBoolean, | ||
IsDefined, | ||
IsString, | ||
MaxLength, | ||
ValidateNested, | ||
} from 'class-validator'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { AbstractDTO } from '../shared/abstract.dto'; | ||
import { IdDTO } from '../shared/id.dto'; | ||
import { ValidationsGroupsEnum } from '../../enums/shared/validation-groups-enum'; | ||
|
||
export class FeatureFlag extends AbstractDTO { | ||
@Expose() | ||
@IsString({ groups: [ValidationsGroupsEnum.default] }) | ||
@MaxLength(256, { groups: [ValidationsGroupsEnum.default] }) | ||
@IsDefined({ groups: [ValidationsGroupsEnum.default] }) | ||
@ApiProperty() | ||
name: string; | ||
|
||
@Expose() | ||
@IsString({ groups: [ValidationsGroupsEnum.default] }) | ||
@IsDefined({ groups: [ValidationsGroupsEnum.default] }) | ||
@ApiProperty() | ||
description: string; | ||
|
||
@Expose() | ||
@IsBoolean({ groups: [ValidationsGroupsEnum.default] }) | ||
@IsDefined({ groups: [ValidationsGroupsEnum.default] }) | ||
@ApiProperty() | ||
active: boolean; | ||
|
||
@Expose() | ||
@ValidateNested({ groups: [ValidationsGroupsEnum.default], each: true }) | ||
@Type(() => IdDTO) | ||
@ApiProperty({ type: IdDTO, isArray: true }) | ||
jurisdictions: IdDTO[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { FeatureFlagController } from '../controllers/feature-flag.controller'; | ||
import { FeatureFlagService } from '../services/feature-flag.service'; | ||
import { JurisdictionModule } from './jurisdiction.module'; | ||
import { PermissionModule } from './permission.module'; | ||
import { PrismaModule } from './prisma.module'; | ||
|
||
@Module({ | ||
imports: [JurisdictionModule, PermissionModule, PrismaModule], | ||
controllers: [FeatureFlagController], | ||
providers: [FeatureFlagService], | ||
exports: [FeatureFlagService], | ||
}) | ||
export class FeatureFlagModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.