Skip to content

Commit

Permalink
[Feature] client가 등록한 authorites를 확인할 수 있는 api 만들기
Browse files Browse the repository at this point in the history
Fixes #54
  • Loading branch information
siwonpada committed Dec 27, 2024
1 parent 6deea75 commit 4ed11fd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/client/client.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Body,
Controller,
Delete,
Get,
Param,
Post,
UseGuards,
Expand All @@ -27,13 +28,30 @@ import { ClientGuard } from './guard/client.guard';
import { GetClient } from './decorator/getClient.decorator';
import { Client } from '@prisma/client';
import { AuthorityDto } from './dto/req/authority.dto';
import { ExpandedClientResDto } from './dto/res/expandedClientRes.dto';

@ApiTags('client')
@Controller('client')
@UsePipes(new ValidationPipe({ transform: true }))
export class ClientController {
constructor(private readonly clientService: ClientService) {}

@ApiOperation({
summary: 'Get clients',
description: 'Get clients with name and uuid',
})
@ApiOkResponse({
description: 'The clients have been successfully fetched.',
type: [ExpandedClientResDto],
})
@ApiInternalServerErrorResponse({
description: 'Unknown errors',
})
@Get(':uuid')
async getClient(@Param('uuid') uuid: string): Promise<ExpandedClientResDto> {
return this.clientService.getClient(uuid);
}

@ApiOperation({
summary: 'Register a new client',
description:
Expand Down
13 changes: 13 additions & 0 deletions src/client/client.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@nestjs/common';
import { Client } from '@prisma/client';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
import { ExpandedClient } from './types/ExpandedClient.type';

@Injectable()
@Loggable()
Expand Down Expand Up @@ -54,6 +55,18 @@ export class ClientRepository {
});
}

/**
* Find a client by uuid with authorities. if the client is not found, return null
* @param uuid the uuid of the client to find
* @returns Founded client or null
*/
async findByUuidWithAuthority(uuid: string): Promise<ExpandedClient | null> {
return this.prismaService.client.findUnique({
where: { uuid },
include: { ExternalAuthority: true },
});
}

/**
* Delete a client by uuid
* @param uuid the uuid of the client to delete
Expand Down
16 changes: 16 additions & 0 deletions src/client/client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ConfigService } from '@nestjs/config';
import { firstValueFrom } from 'rxjs';
import { HttpService } from '@nestjs/axios';
import { Loggable } from '@lib/logger/decorator/loggable';
import { ExpandedClient } from './types/ExpandedClient.type';

@Injectable()
@Loggable()
Expand All @@ -23,6 +24,21 @@ export class ClientService {
this.configService.getOrThrow<string>('SLACK_WEBHOOK_URL');
}

/**
* get client
* @param uuid uuid of the client to get
* @returns client
*/
async getClient(uuid: string): Promise<ExpandedClient> {
const client = await this.clientRepository.findByUuidWithAuthority(uuid);
if (!client) {
this.logger.debug(`client not found`);
throw new ForbiddenException('client not found');
}

return client;
}

/**
* generate a password and create a new client and grant the request through slack
* @param param0 dto object containing the client name
Expand Down
16 changes: 16 additions & 0 deletions src/client/dto/res/expandedClientRes.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { ClientResDto } from './clientRes.dto';
import { ExternalAuthority } from '@prisma/client';

class AuthorityResDto implements ExternalAuthority {
@ApiProperty()
clientUuid: string;

@ApiProperty()
authority: string;
}

export class ExpandedClientResDto extends ClientResDto {
@ApiProperty()
ExternalAuthority: AuthorityResDto[];
}
7 changes: 7 additions & 0 deletions src/client/types/ExpandedClient.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Prisma } from '@prisma/client';

export type ExpandedClient = Prisma.ClientGetPayload<{
include: {
ExternalAuthority: true;
};
}>;

0 comments on commit 4ed11fd

Please sign in to comment.