-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdashboard-widgets.controller.ts
More file actions
152 lines (148 loc) · 5.35 KB
/
Copy pathdashboard-widgets.controller.ts
File metadata and controls
152 lines (148 loc) · 5.35 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
Body,
Controller,
Delete,
Inject,
Injectable,
Param,
Post,
Put,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { UseCaseType } from '../../../common/data-injection.tokens.js';
import { MasterPassword } from '../../../decorators/master-password.decorator.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 { ConnectionEditGuard } from '../../../guards/connection-edit.guard.js';
import { SentryInterceptor } from '../../../interceptors/sentry.interceptor.js';
import { CreateDashboardWidgetDs } from './data-structures/create-dashboard-widget.ds.js';
import { DeleteDashboardWidgetDs } from './data-structures/delete-dashboard-widget.ds.js';
import { UpdateDashboardWidgetDs } from './data-structures/update-dashboard-widget.ds.js';
import { CreateDashboardWidgetDto } from './dto/create-dashboard-widget.dto.js';
import { FoundDashboardWidgetDto } from './dto/found-dashboard-widget.dto.js';
import { UpdateDashboardWidgetDto } from './dto/update-dashboard-widget.dto.js';
import {
ICreateDashboardWidget,
IDeleteDashboardWidget,
IUpdateDashboardWidget,
} from './use-cases/dashboard-widget-use-cases.interface.js';
@UseInterceptors(SentryInterceptor)
@Controller()
@ApiBearerAuth()
@ApiTags('Dashboard Widgets')
@Injectable()
export class DashboardWidgetController {
constructor(
@Inject(UseCaseType.CREATE_DASHBOARD_WIDGET)
private readonly createDashboardWidgetUseCase: ICreateDashboardWidget,
@Inject(UseCaseType.UPDATE_DASHBOARD_WIDGET)
private readonly updateDashboardWidgetUseCase: IUpdateDashboardWidget,
@Inject(UseCaseType.DELETE_DASHBOARD_WIDGET)
private readonly deleteDashboardWidgetUseCase: IDeleteDashboardWidget,
) {}
@ApiOperation({ summary: 'Create a new widget in a dashboard' })
@ApiResponse({
status: 201,
description: 'Widget created.',
type: FoundDashboardWidgetDto,
})
@ApiBody({ type: CreateDashboardWidgetDto })
@ApiParam({ name: 'dashboardId', required: true })
@ApiParam({ name: 'connectionId', required: true })
@UseGuards(ConnectionEditGuard)
@Post('/dashboard/:dashboardId/widget/:connectionId')
async createWidget(
@SlugUuid('connectionId') connectionId: string,
@Param('dashboardId') dashboardId: string,
@MasterPassword() masterPwd: string,
@UserId() userId: string,
@Body() createDto: CreateDashboardWidgetDto,
): Promise<FoundDashboardWidgetDto> {
const inputData: CreateDashboardWidgetDs = {
dashboardId,
connectionId,
masterPassword: masterPwd,
userId,
widget_type: createDto.widget_type,
chart_type: createDto.chart_type,
name: createDto.name,
description: createDto.description,
position_x: createDto.position_x,
position_y: createDto.position_y,
width: createDto.width,
height: createDto.height,
widget_options: createDto.widget_options,
query_id: createDto.query_id,
};
return await this.createDashboardWidgetUseCase.execute(inputData, InTransactionEnum.ON);
}
@ApiOperation({ summary: 'Update a widget in a dashboard' })
@ApiResponse({
status: 200,
description: 'Widget updated.',
type: FoundDashboardWidgetDto,
})
@ApiBody({ type: UpdateDashboardWidgetDto })
@ApiParam({ name: 'dashboardId', required: true })
@ApiParam({ name: 'widgetId', required: true })
@ApiParam({ name: 'connectionId', required: true })
@UseGuards(ConnectionEditGuard)
@Put('/dashboard/:dashboardId/widget/:widgetId/:connectionId')
async updateWidget(
@SlugUuid('connectionId') connectionId: string,
@Param('dashboardId') dashboardId: string,
@Param('widgetId') widgetId: string,
@MasterPassword() masterPwd: string,
@UserId() userId: string,
@Body() updateDto: UpdateDashboardWidgetDto,
): Promise<FoundDashboardWidgetDto> {
const inputData: UpdateDashboardWidgetDs = {
widgetId,
dashboardId,
connectionId,
masterPassword: masterPwd,
userId,
widget_type: updateDto.widget_type,
chart_type: updateDto.chart_type,
name: updateDto.name,
description: updateDto.description,
position_x: updateDto.position_x,
position_y: updateDto.position_y,
width: updateDto.width,
height: updateDto.height,
widget_options: updateDto.widget_options,
query_id: updateDto.query_id,
};
return await this.updateDashboardWidgetUseCase.execute(inputData, InTransactionEnum.ON);
}
@ApiOperation({ summary: 'Delete a widget from a dashboard' })
@ApiResponse({
status: 200,
description: 'Widget deleted.',
type: FoundDashboardWidgetDto,
})
@ApiParam({ name: 'dashboardId', required: true })
@ApiParam({ name: 'widgetId', required: true })
@ApiParam({ name: 'connectionId', required: true })
@UseGuards(ConnectionEditGuard)
@Delete('/dashboard/:dashboardId/widget/:widgetId/:connectionId')
async deleteWidget(
@SlugUuid('connectionId') connectionId: string,
@Param('dashboardId') dashboardId: string,
@Param('widgetId') widgetId: string,
@MasterPassword() masterPwd: string,
@UserId() userId: string,
): Promise<FoundDashboardWidgetDto> {
const inputData: DeleteDashboardWidgetDs = {
widgetId,
dashboardId,
connectionId,
masterPassword: masterPwd,
userId,
};
return await this.deleteDashboardWidgetUseCase.execute(inputData, InTransactionEnum.ON);
}
}