Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Add select/include pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 16, 2020
1 parent 42b73a2 commit c5c54c4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/errors/errors.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const EMAIL_DELETE_PRIMARY = '400016: Cannot delete primary email';
export const CANNOT_UPDATE_ROLE_SOLE_OWNER =
'400017: Cannot change the role of the only owner';
export const INVALID_DOMAIN = '400018: Invalid domain';
export const SELECT_INCLUDE_PIPE_FORMAT = '400014: Invalid query format';

export const EMAIL_USER_CONFLICT =
'409001: User with this email already exists';
Expand Down
5 changes: 4 additions & 1 deletion src/modules/groups/groups.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { AuditLog } from '../audit-logs/audit-log.decorator';
import { Scopes } from '../auth/scope.decorator';
import { ReplaceGroupDto, UpdateGroupDto } from './groups.dto';
import { GroupsService } from './groups.service';
import { SelectIncludePipe } from 'src/pipes/select-include.pipe';

@Controller('groups')
export class GroupController {
Expand Down Expand Up @@ -46,8 +47,10 @@ export class GroupController {
@Scopes('group-{groupId}:read-info')
async get(
@Param('groupId', ParseIntPipe) id: number,
@Query('select', SelectIncludePipe) select?: Record<string, boolean>,
@Query('include', SelectIncludePipe) include?: Record<string, boolean>,
): Promise<Expose<groups>> {
return this.groupsService.getGroup(Number(id));
return this.groupsService.getGroup(Number(id), { select, include });
}

@Patch(':groupId')
Expand Down
15 changes: 13 additions & 2 deletions src/modules/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,21 @@ export class GroupsService {
return groups.map((user) => this.prisma.expose<groups>(user));
}

async getGroup(id: number): Promise<Expose<groups>> {
async getGroup(
id: number,
{
select,
include,
}: {
select?: Record<string, boolean>;
include?: Record<string, boolean>;
},
): Promise<Expose<groups>> {
const group = await this.prisma.groups.findOne({
where: { id },
});
select,
include,
} as any);
if (!group) throw new NotFoundException(GROUP_NOT_FOUND);
return this.prisma.expose<groups>(group);
}
Expand Down
23 changes: 23 additions & 0 deletions src/pipes/select-include.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
import { SELECT_INCLUDE_PIPE_FORMAT } from '../errors/errors.constants';
import { object } from 'dot-object';

/**
* Convert a string like "id,createdAt,user.name,user.id"
* => { id: true, createdAt: true, user: { name: true, id: true } }
*/
@Injectable()
export class SelectIncludePipe implements PipeTransform {
transform(value: string): Record<string, boolean> | undefined {
if (value == null) return undefined;
try {
const testRecord: Record<string, boolean> = {};
value.split(',').forEach((i) => {
if (/^[a-z0-9\.]+$/i.test(i.trim())) testRecord[i.trim()] = true;
});
return object(testRecord) as Record<string, boolean>;
} catch (_) {
throw new BadRequestException(SELECT_INCLUDE_PIPE_FORMAT);
}
}
}

0 comments on commit c5c54c4

Please sign in to comment.