-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathapi-key.controller.ts
More file actions
101 lines (95 loc) · 3.54 KB
/
Copy pathapi-key.controller.ts
File metadata and controls
101 lines (95 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { Body, Controller, Delete, Get, Inject, Injectable, Post, UseInterceptors } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { UseCaseType } from '../../common/data-injection.tokens.js';
import { SlugUuid } from '../../decorators/slug-uuid.decorator.js';
import { UserId } from '../../decorators/user-id.decorator.js';
import { InTransactionEnum } from '../../enums/in-transaction.enum.js';
import { SentryInterceptor } from '../../interceptors/sentry.interceptor.js';
import { CreateApiKeyDto } from './application/dto/create-api-key.dto.js';
import { CreatedApiKeyDto } from './application/dto/created-api-key.dto.js';
import { FoundApiKeyDto } from './application/dto/found-api-key.dto.js';
import { ICreateApiKey, IDeleteApiKey, IGetApiKey, IGetApiKeys } from './use-cases/api-key-use-cases.interface.js';
import { buildCreatedApiKeyDto } from './utils/build-created-api-key.dto.js';
import { buildFoundApiKeyDto } from './utils/build-found-api-key.dto.js';
import { Timeout } from '../../decorators/timeout.decorator.js';
@UseInterceptors(SentryInterceptor)
@Timeout()
@Controller()
@ApiBearerAuth()
@ApiTags('Api Key')
@Injectable()
export class ApiKeyController {
constructor(
@Inject(UseCaseType.CREATE_API_KEY)
private readonly createApiKeyUseCase: ICreateApiKey,
@Inject(UseCaseType.GET_API_KEYS)
private readonly getApiKeysUseCase: IGetApiKeys,
@Inject(UseCaseType.GET_API_KEY)
private readonly getApiKeyUseCase: IGetApiKey,
@Inject(UseCaseType.DELETE_API_KEY)
private readonly deleteApiKeyUseCase: IDeleteApiKey,
) {}
@ApiOperation({ summary: 'Create new API key' })
@ApiResponse({
status: 200,
description: 'Api key created.',
type: CreatedApiKeyDto,
})
@Post('/apikey')
public async createApiKey(@UserId() userId: string, @Body() apiKeyData: CreateApiKeyDto): Promise<CreatedApiKeyDto> {
const apiKey = await this.createApiKeyUseCase.execute(
{
userId,
title: apiKeyData.title,
},
InTransactionEnum.ON,
);
return buildCreatedApiKeyDto(apiKey);
}
@ApiOperation({ summary: 'Get all user api keys' })
@ApiResponse({
status: 200,
description: 'Get all user api keys.',
type: FoundApiKeyDto,
isArray: true,
})
@Get('/apikeys')
public async getApiKeys(@UserId() userId: string): Promise<Array<FoundApiKeyDto>> {
const foundApiKeys = await this.getApiKeysUseCase.execute(userId);
return foundApiKeys.map((apiKey) => buildFoundApiKeyDto(apiKey));
}
@ApiOperation({ summary: 'Get api key by id' })
@ApiResponse({
status: 200,
description: 'Get api key by id.',
type: FoundApiKeyDto,
})
@Get('/apikey/:apiKeyId')
public async getApiKey(@UserId() userId: string, @SlugUuid('apiKeyId') apiKeyId: string): Promise<FoundApiKeyDto> {
const foundApiKey = await this.getApiKeyUseCase.execute({ userId, apiKeyId });
return buildFoundApiKeyDto(foundApiKey);
}
@ApiOperation({ summary: 'Delete api key by id' })
@ApiResponse({
status: 200,
description: 'Get api key by id.',
type: FoundApiKeyDto,
})
@Delete('/apikey/:apiKeyId')
public async deleteApiKey(@UserId() userId: string, @SlugUuid('apiKeyId') apiKeyId: string): Promise<FoundApiKeyDto> {
const deletedApiKey = await this.deleteApiKeyUseCase.execute({ userId, apiKeyId }, InTransactionEnum.ON);
return buildFoundApiKeyDto(deletedApiKey);
}
@ApiOperation({ summary: 'Check api key' })
@ApiResponse({
status: 200,
description: 'Api key is valid.',
})
@Get('/check/apikey')
public async checkApiKey(): Promise<any> {
return {
result: true,
message: 'Api key is valid',
};
}
}