Skip to content

Commit

Permalink
feat(api): add filter for getting flags
Browse files Browse the repository at this point in the history
  • Loading branch information
manekenpix committed Mar 27, 2024
1 parent 5b73059 commit 328ef34
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 20 deletions.
1 change: 1 addition & 0 deletions api/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './utils';
15 changes: 15 additions & 0 deletions api/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const cleanObject = <T>(e: Partial<T>) => {
const keys = Object.keys(e);
let res = {};

keys.forEach((key) => {
if (e[key as keyof typeof e] !== undefined) {
res = {
...res,
[key]: e[key as keyof typeof e],
};
}
});

return res;
};
6 changes: 4 additions & 2 deletions api/src/flag/flag.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
Patch,
UseGuards,
Param,
Query,
Delete,
HttpCode,
HttpStatus,
ParseUUIDPipe,
} from '@nestjs/common';
import FlagService from './flag.service';
import FlagDto from './flag.dts';
import QueryDto from './query.dts';
import FlagGuard from './flag.guard';

@Controller('flags')
Expand All @@ -21,8 +23,8 @@ class FlagController {

@Get('/')
@HttpCode(HttpStatus.OK)
async getFlags(): Promise<FlagDto[]> {
return this.flagsService.get();
async getFlags(@Query() query: QueryDto): Promise<FlagDto[]> {
return this.flagsService.get(query);
}

@Get('/:id')
Expand Down
34 changes: 18 additions & 16 deletions api/src/flag/flag.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { InjectModel } from '@nestjs/mongoose';
import { Flag, FlagDocument } from './schemas/flag.schema';
import { Model, Types } from 'mongoose';
import FlagDto from './flag.dts';
import QueryDto from './query.dts';
import { cleanObject } from '../common';

@Injectable()
class FlagRepository {
Expand All @@ -11,29 +13,15 @@ class FlagRepository {
private db: Model<FlagDocument>,
) {}

private formatFlags(flags: FlagDocument[]): FlagDto[] {
return flags.map(
({ _id, name, type, value, environment, project, isEnabled }) => ({
id: _id as Types.ObjectId,
name,
type,
value,
environment,
project,
isEnabled,
}),
);
}

async create(flag: FlagDto): Promise<FlagDto> {
const newFlag = new this.db(flag);
const f = await newFlag.save();

return this.formatFlags([f])[0];
}

async get(): Promise<FlagDto[]> {
const fs = await this.db.find();
async get(q: QueryDto): Promise<FlagDto[]> {
const fs = await this.db.find(cleanObject<QueryDto>(q));

return this.formatFlags(fs);
}
Expand All @@ -55,6 +43,20 @@ class FlagRepository {
async delete(id: string): Promise<void> {
await this.db.findByIdAndRemove(id).orFail();
}

private formatFlags(flags: FlagDocument[]): FlagDto[] {
return flags.map(
({ _id, name, type, value, environment, project, isEnabled }) => ({
id: _id as Types.ObjectId,
name,
type,
value,
environment,
project,
isEnabled,
}),
);
}
}

export default FlagRepository;
5 changes: 3 additions & 2 deletions api/src/flag/flag.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import FlagDto from './flag.dts';
import QueryDto from './query.dts';
import FlagRepository from './flag.repository';

@Injectable()
Expand All @@ -8,8 +9,8 @@ class FlagService {

constructor(private repository: FlagRepository) {}

async get(): Promise<FlagDto[]> {
return await this.repository.get();
async get(q: QueryDto): Promise<FlagDto[]> {
return await this.repository.get(q);
}

async getById(id: string): Promise<FlagDto> {
Expand Down
17 changes: 17 additions & 0 deletions api/src/flag/query.dts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IsNotEmpty, IsIn, IsString, IsOptional } from 'class-validator';
import { FlagEnv } from './schemas/flag.schema';

export default class QueryDto {
@IsString()
@IsOptional()
name?: string;

@IsIn(['staging', 'production'])
@IsOptional()
environment?: FlagEnv;

@IsNotEmpty()
@IsString()
@IsOptional()
project?: string;
}

0 comments on commit 328ef34

Please sign in to comment.