@hed-hog/core 0.0.151 → 0.0.152

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/dist/dashboard/dashboard/dashboard.controller.d.ts +214 -9
  2. package/dist/dashboard/dashboard/dashboard.controller.d.ts.map +1 -1
  3. package/dist/dashboard/dashboard/dashboard.controller.js +35 -35
  4. package/dist/dashboard/dashboard/dashboard.controller.js.map +1 -1
  5. package/dist/dashboard/dashboard/dashboard.module.d.ts +1 -1
  6. package/dist/dashboard/dashboard/dashboard.module.d.ts.map +1 -1
  7. package/dist/dashboard/dashboard/dashboard.module.js +11 -6
  8. package/dist/dashboard/dashboard/dashboard.module.js.map +1 -1
  9. package/dist/dashboard/dashboard/dashboard.service.d.ts +217 -16
  10. package/dist/dashboard/dashboard/dashboard.service.d.ts.map +1 -1
  11. package/dist/dashboard/dashboard/dashboard.service.js +115 -28
  12. package/dist/dashboard/dashboard/dashboard.service.js.map +1 -1
  13. package/dist/dashboard/dashboard/dto/create.dto.d.ts +4 -2
  14. package/dist/dashboard/dashboard/dto/create.dto.d.ts.map +1 -1
  15. package/dist/dashboard/dashboard/dto/create.dto.js +10 -1
  16. package/dist/dashboard/dashboard/dto/create.dto.js.map +1 -1
  17. package/dist/dashboard/dashboard/dto/index.d.ts +5 -0
  18. package/dist/dashboard/dashboard/dto/index.d.ts.map +1 -0
  19. package/dist/dashboard/dashboard/dto/index.js +25 -0
  20. package/dist/dashboard/dashboard/dto/index.js.map +1 -0
  21. package/dist/dashboard/dashboard/dto/update.dto.d.ts +5 -4
  22. package/dist/dashboard/dashboard/dto/update.dto.d.ts.map +1 -1
  23. package/dist/dashboard/dashboard/dto/update.dto.js +27 -3
  24. package/dist/dashboard/dashboard/dto/update.dto.js.map +1 -1
  25. package/dist/dashboard/dashboard-component/dashboard-component.controller.d.ts +114 -9
  26. package/dist/dashboard/dashboard-component/dashboard-component.controller.d.ts.map +1 -1
  27. package/dist/dashboard/dashboard-component/dashboard-component.controller.js +35 -36
  28. package/dist/dashboard/dashboard-component/dashboard-component.controller.js.map +1 -1
  29. package/dist/dashboard/dashboard-component/dashboard-component.module.js +4 -3
  30. package/dist/dashboard/dashboard-component/dashboard-component.module.js.map +1 -1
  31. package/dist/dashboard/dashboard-component/dashboard-component.service.d.ts +116 -15
  32. package/dist/dashboard/dashboard-component/dashboard-component.service.d.ts.map +1 -1
  33. package/dist/dashboard/dashboard-component/dashboard-component.service.js +109 -22
  34. package/dist/dashboard/dashboard-component/dashboard-component.service.js.map +1 -1
  35. package/dist/dashboard/dashboard-component/dto/create.dto.d.ts +4 -2
  36. package/dist/dashboard/dashboard-component/dto/create.dto.d.ts.map +1 -1
  37. package/dist/dashboard/dashboard-component/dto/create.dto.js +10 -1
  38. package/dist/dashboard/dashboard-component/dto/create.dto.js.map +1 -1
  39. package/dist/dashboard/dashboard-component/dto/index.d.ts +5 -0
  40. package/dist/dashboard/dashboard-component/dto/index.d.ts.map +1 -0
  41. package/dist/dashboard/dashboard-component/dto/index.js +25 -0
  42. package/dist/dashboard/dashboard-component/dto/index.js.map +1 -0
  43. package/dist/dashboard/dashboard-component/dto/update.dto.d.ts +13 -4
  44. package/dist/dashboard/dashboard-component/dto/update.dto.d.ts.map +1 -1
  45. package/dist/dashboard/dashboard-component/dto/update.dto.js +67 -3
  46. package/dist/dashboard/dashboard-component/dto/update.dto.js.map +1 -1
  47. package/dist/dashboard/dashboard-core/dashboard-core.controller.d.ts +56 -0
  48. package/dist/dashboard/dashboard-core/dashboard-core.controller.d.ts.map +1 -1
  49. package/dist/dashboard/dashboard-core/dashboard-core.controller.js +65 -0
  50. package/dist/dashboard/dashboard-core/dashboard-core.controller.js.map +1 -1
  51. package/dist/dashboard/dashboard-core/dashboard-core.service.d.ts +52 -0
  52. package/dist/dashboard/dashboard-core/dashboard-core.service.d.ts.map +1 -1
  53. package/dist/dashboard/dashboard-core/dashboard-core.service.js +221 -0
  54. package/dist/dashboard/dashboard-core/dashboard-core.service.js.map +1 -1
  55. package/dist/dashboard/dashboard-item/dashboard-item.controller.d.ts +26 -29
  56. package/dist/dashboard/dashboard-item/dashboard-item.controller.d.ts.map +1 -1
  57. package/dist/dashboard/dashboard-item/dashboard-item.controller.js +24 -43
  58. package/dist/dashboard/dashboard-item/dashboard-item.controller.js.map +1 -1
  59. package/dist/dashboard/dashboard-item/dashboard-item.service.d.ts +30 -34
  60. package/dist/dashboard/dashboard-item/dashboard-item.service.d.ts.map +1 -1
  61. package/dist/dashboard/dashboard-item/dashboard-item.service.js +51 -35
  62. package/dist/dashboard/dashboard-item/dashboard-item.service.js.map +1 -1
  63. package/dist/dashboard/dashboard-item/dto/index.d.ts +5 -0
  64. package/dist/dashboard/dashboard-item/dto/index.d.ts.map +1 -0
  65. package/dist/dashboard/dashboard-item/dto/index.js +25 -0
  66. package/dist/dashboard/dashboard-item/dto/index.js.map +1 -0
  67. package/dist/dashboard/dashboard-item/dto/update.dto.d.ts +7 -4
  68. package/dist/dashboard/dashboard-item/dto/update.dto.d.ts.map +1 -1
  69. package/dist/dashboard/dashboard-item/dto/update.dto.js +42 -3
  70. package/dist/dashboard/dashboard-item/dto/update.dto.js.map +1 -1
  71. package/dist/dashboard/dashboard.module.js +1 -1
  72. package/dist/dashboard/dashboard.module.js.map +1 -1
  73. package/hedhog/data/route.yaml +44 -14
  74. package/hedhog/query/dashboard-seed.sql +131 -0
  75. package/package.json +1 -1
  76. package/src/dashboard/dashboard/dashboard.controller.ts +26 -23
  77. package/src/dashboard/dashboard/dashboard.module.ts +7 -2
  78. package/src/dashboard/dashboard/dashboard.service.ts +125 -44
  79. package/src/dashboard/dashboard/dto/create.dto.ts +12 -3
  80. package/src/dashboard/dashboard/dto/index.ts +7 -0
  81. package/src/dashboard/dashboard/dto/update.dto.ts +17 -3
  82. package/src/dashboard/dashboard-component/dashboard-component.controller.ts +22 -19
  83. package/src/dashboard/dashboard-component/dashboard-component.module.ts +3 -3
  84. package/src/dashboard/dashboard-component/dashboard-component.service.ts +128 -39
  85. package/src/dashboard/dashboard-component/dto/create.dto.ts +12 -3
  86. package/src/dashboard/dashboard-component/dto/index.ts +7 -0
  87. package/src/dashboard/dashboard-component/dto/update.dto.ts +49 -3
  88. package/src/dashboard/dashboard-core/dashboard-core.controller.ts +53 -2
  89. package/src/dashboard/dashboard-core/dashboard-core.service.ts +271 -0
  90. package/src/dashboard/dashboard-item/dashboard-item.controller.ts +17 -26
  91. package/src/dashboard/dashboard-item/dashboard-item.service.ts +55 -47
  92. package/src/dashboard/dashboard-item/dto/index.ts +7 -0
  93. package/src/dashboard/dashboard-item/dto/update.dto.ts +27 -3
  94. package/src/dashboard/dashboard.module.ts +2 -2
  95. package/src/language/en.json +4 -1
  96. package/src/language/pt.json +4 -1
@@ -1,4 +1,50 @@
1
- import { PartialType } from '@nestjs/mapped-types';
2
- import { CreateDTO } from './create.dto';
1
+ import { getLocaleText } from '@hed-hog/api-locale';
2
+ import { IsBoolean, IsNotEmpty, IsNumber, IsObject, IsOptional, IsString } from 'class-validator';
3
3
 
4
- export class UpdateDTO extends PartialType(CreateDTO) {}
4
+ export class UpdateDTO {
5
+ @IsOptional()
6
+ @IsString({ message: (args) => getLocaleText('validation.stringRequired', args.value) })
7
+ slug?: string;
8
+
9
+ @IsOptional()
10
+ @IsString({ message: (args) => getLocaleText('validation.stringRequired', args.value) })
11
+ path?: string;
12
+
13
+ @IsOptional()
14
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
15
+ min_width?: number;
16
+
17
+ @IsOptional()
18
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
19
+ max_width?: number;
20
+
21
+ @IsOptional()
22
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
23
+ min_height?: number;
24
+
25
+ @IsOptional()
26
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
27
+ max_height?: number;
28
+
29
+ @IsOptional()
30
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
31
+ width?: number;
32
+
33
+ @IsOptional()
34
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
35
+ height?: number;
36
+
37
+ @IsOptional()
38
+ @IsBoolean({ message: (args) => getLocaleText('validation.booleanRequired', args.value) })
39
+ is_resizable?: boolean;
40
+
41
+ @IsOptional()
42
+ @IsObject({
43
+ message: (args) =>
44
+ getLocaleText('validation.localeMustBeObject', args.value),
45
+ })
46
+ @IsNotEmpty({
47
+ message: (args) => getLocaleText('validation.localeRequired', args.value),
48
+ })
49
+ locale?: Record<string, { name: string }>;
50
+ }
@@ -1,7 +1,8 @@
1
- import { Role } from '@hed-hog/api';
1
+ import { Role, User } from '@hed-hog/api';
2
2
  import { Locale } from '@hed-hog/api-locale';
3
- import { Controller, Get, Param } from '@nestjs/common';
3
+ import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
4
4
  import { DashboardCoreService } from './dashboard-core.service';
5
+
5
6
  @Role()
6
7
  @Controller('dashboard-core')
7
8
  export class DashboardCoreController {
@@ -11,4 +12,54 @@ export class DashboardCoreController {
11
12
  fromSlug(@Param('slug') slug: string, @Locale() locale) {
12
13
  return this.dashboardCoreService.fromSlug(slug, locale);
13
14
  }
15
+
16
+ @Get('stats/overview')
17
+ getStatistics() {
18
+ return this.dashboardCoreService.getStatistics();
19
+ }
20
+
21
+ @Get('stats/user-growth')
22
+ getUserGrowth() {
23
+ return this.dashboardCoreService.getUserGrowth();
24
+ }
25
+
26
+ @Get('layout/:slug')
27
+ getUserLayout(@User() user, @Param('slug') slug: string) {
28
+ return this.dashboardCoreService.getUserLayout(user.id, slug);
29
+ }
30
+
31
+ @Post('layout/:slug')
32
+ saveUserLayout(
33
+ @User() user,
34
+ @Param('slug') slug: string,
35
+ @Body() body: { layout: Array<{ i: string; x: number; y: number; w: number; h: number }> },
36
+ ) {
37
+ return this.dashboardCoreService.saveUserLayout(user.id, slug, body.layout);
38
+ }
39
+
40
+ @Post('widget/:slug')
41
+ addWidget(
42
+ @User() user,
43
+ @Param('slug') slug: string,
44
+ @Body() body: { componentSlug: string },
45
+ ) {
46
+ return this.dashboardCoreService.addWidgetToUserDashboard(
47
+ user.id,
48
+ slug,
49
+ body.componentSlug,
50
+ );
51
+ }
52
+
53
+ @Delete('widget/:slug/:widgetId')
54
+ removeWidget(
55
+ @User() user,
56
+ @Param('slug') slug: string,
57
+ @Param('widgetId') widgetId: string,
58
+ ) {
59
+ return this.dashboardCoreService.removeWidgetFromUserDashboard(
60
+ user.id,
61
+ slug,
62
+ widgetId,
63
+ );
64
+ }
14
65
  }
@@ -24,4 +24,275 @@ export class DashboardCoreService {
24
24
  },
25
25
  });
26
26
  }
27
+
28
+ async getStatistics() {
29
+ const now = new Date();
30
+ const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
31
+ const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
32
+
33
+ // Total de usuários
34
+ const totalUsers = await this.prismaService.user.count();
35
+
36
+ // Usuários criados nos últimos 7 dias
37
+ const recentUsers = await this.prismaService.user.count({
38
+ where: {
39
+ created_at: {
40
+ gte: sevenDaysAgo,
41
+ },
42
+ },
43
+ });
44
+
45
+ // Usuários ativos (criados nos últimos 30 dias como aproximação)
46
+ const activeUsers = await this.prismaService.user.count({
47
+ where: {
48
+ created_at: {
49
+ gte: thirtyDaysAgo,
50
+ },
51
+ },
52
+ });
53
+
54
+ // Total de roles
55
+ const totalRoles = await this.prismaService.role.count();
56
+
57
+ // Total de menus
58
+ const totalMenus = await this.prismaService.menu.count();
59
+
60
+ // Total de rotas
61
+ const totalRoutes = await this.prismaService.route.count();
62
+
63
+ return {
64
+ totalUsers,
65
+ recentUsers,
66
+ activeUsers,
67
+ totalRoles,
68
+ totalMenus,
69
+ totalRoutes,
70
+ };
71
+ }
72
+
73
+ async getUserGrowth() {
74
+ const now = new Date();
75
+ const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
76
+
77
+ // Contagem de usuários criados por dia nos últimos 30 dias
78
+ const usersByDay = await this.prismaService.$queryRaw<
79
+ Array<{ date: Date; count: bigint }>
80
+ >`
81
+ SELECT DATE("created_at") as date, COUNT(*) as count
82
+ FROM "user"
83
+ WHERE "created_at" >= ${thirtyDaysAgo}
84
+ GROUP BY DATE("created_at")
85
+ ORDER BY date ASC
86
+ `;
87
+
88
+ return usersByDay.map((item) => ({
89
+ date: item.date,
90
+ count: Number(item.count),
91
+ }));
92
+ }
93
+
94
+ async getUserLayout(userId: number, slug: string) {
95
+ // Busca os itens do dashboard para este slug
96
+ const dashboardItems = await this.prismaService.dashboard_item.findMany({
97
+ where: {
98
+ dashboard: {
99
+ slug,
100
+ },
101
+ },
102
+ include: {
103
+ dashboard_user: {
104
+ where: {
105
+ user_id: userId,
106
+ },
107
+ },
108
+ dashboard_component: true,
109
+ },
110
+ });
111
+
112
+ return dashboardItems.map((item) => {
113
+ const userLayout = item.dashboard_user[0];
114
+ return {
115
+ i: `widget-${item.id}`,
116
+ component_id: item.component_id,
117
+ slug: item.dashboard_component.slug,
118
+ x: userLayout?.x_axis ?? item.x_axis ?? 0,
119
+ y: userLayout?.y_axis ?? item.y_axis ?? 0,
120
+ w: userLayout?.width ?? item.width ?? 2,
121
+ h: userLayout?.height ?? item.height ?? 2,
122
+ minW: item.dashboard_component.min_width,
123
+ maxW: item.dashboard_component.max_width,
124
+ minH: item.dashboard_component.min_height,
125
+ maxH: item.dashboard_component.max_height,
126
+ isResizable: item.dashboard_component.is_resizable ?? true,
127
+ };
128
+ });
129
+ }
130
+
131
+ async saveUserLayout(
132
+ userId: number,
133
+ slug: string,
134
+ layout: Array<{
135
+ i: string;
136
+ x: number;
137
+ y: number;
138
+ w: number;
139
+ h: number;
140
+ }>,
141
+ ) {
142
+ // Busca o dashboard
143
+ const dashboard = await this.prismaService.dashboard.findFirst({
144
+ where: { slug },
145
+ });
146
+
147
+ if (!dashboard) {
148
+ throw new Error(`Dashboard with slug '${slug}' not found`);
149
+ }
150
+
151
+ // Para cada item do layout, salva ou atualiza o dashboard_user
152
+ for (const item of layout) {
153
+ // Extrai o ID do item do identificador
154
+ const itemId = parseInt(item.i.replace('widget-', ''));
155
+
156
+ // Verifica se o item existe
157
+ const dashboardItem = await this.prismaService.dashboard_item.findFirst({
158
+ where: {
159
+ id: itemId,
160
+ dashboard_id: dashboard.id,
161
+ },
162
+ });
163
+
164
+ if (!dashboardItem) {
165
+ continue;
166
+ }
167
+
168
+ // Busca se já existe um dashboard_user para este item e usuário
169
+ const existingUserLayout =
170
+ await this.prismaService.dashboard_user.findFirst({
171
+ where: {
172
+ item_id: itemId,
173
+ user_id: userId,
174
+ },
175
+ });
176
+
177
+ if (existingUserLayout) {
178
+ // Atualiza o layout existente
179
+ await this.prismaService.dashboard_user.update({
180
+ where: {
181
+ id: existingUserLayout.id,
182
+ },
183
+ data: {
184
+ x_axis: item.x,
185
+ y_axis: item.y,
186
+ width: item.w,
187
+ height: item.h,
188
+ },
189
+ });
190
+ } else {
191
+ // Cria um novo layout de usuário
192
+ await this.prismaService.dashboard_user.create({
193
+ data: {
194
+ item_id: itemId,
195
+ user_id: userId,
196
+ x_axis: item.x,
197
+ y_axis: item.y,
198
+ width: item.w,
199
+ height: item.h,
200
+ },
201
+ });
202
+ }
203
+ }
204
+
205
+ return { success: true };
206
+ }
207
+
208
+ async addWidgetToUserDashboard(
209
+ userId: number,
210
+ slug: string,
211
+ componentSlug: string,
212
+ ) {
213
+ // Busca o dashboard
214
+ const dashboard = await this.prismaService.dashboard.findFirst({
215
+ where: { slug },
216
+ });
217
+
218
+ if (!dashboard) {
219
+ throw new Error(`Dashboard with slug '${slug}' not found`);
220
+ }
221
+
222
+ // Busca o componente
223
+ const component = await this.prismaService.dashboard_component.findFirst({
224
+ where: { slug: componentSlug },
225
+ });
226
+
227
+ if (!component) {
228
+ throw new Error(`Component with slug '${componentSlug}' not found`);
229
+ }
230
+
231
+ // Verifica se já existe um item para este componente no dashboard
232
+ let dashboardItem = await this.prismaService.dashboard_item.findFirst({
233
+ where: {
234
+ dashboard_id: dashboard.id,
235
+ component_id: component.id,
236
+ },
237
+ });
238
+
239
+ // Se não existe, cria um novo item
240
+ if (!dashboardItem) {
241
+ dashboardItem = await this.prismaService.dashboard_item.create({
242
+ data: {
243
+ dashboard_id: dashboard.id,
244
+ component_id: component.id,
245
+ width: component.width,
246
+ height: component.height,
247
+ x_axis: 0,
248
+ y_axis: 0,
249
+ },
250
+ });
251
+ }
252
+
253
+ // Cria um registro de dashboard_user para este usuário
254
+ const userLayout = await this.prismaService.dashboard_user.create({
255
+ data: {
256
+ item_id: dashboardItem.id,
257
+ user_id: userId,
258
+ width: component.width,
259
+ height: component.height,
260
+ x_axis: 0,
261
+ y_axis: 999, // Coloca no final
262
+ },
263
+ });
264
+
265
+ return {
266
+ i: `widget-${dashboardItem.id}`,
267
+ component_id: component.id,
268
+ slug: component.slug,
269
+ x: userLayout.x_axis,
270
+ y: userLayout.y_axis,
271
+ w: userLayout.width,
272
+ h: userLayout.height,
273
+ minW: component.min_width,
274
+ maxW: component.max_width,
275
+ minH: component.min_height,
276
+ maxH: component.max_height,
277
+ isResizable: component.is_resizable ?? true,
278
+ };
279
+ }
280
+
281
+ async removeWidgetFromUserDashboard(
282
+ userId: number,
283
+ slug: string,
284
+ widgetId: string,
285
+ ) {
286
+ const itemId = parseInt(widgetId.replace('widget-', ''));
287
+
288
+ // Remove o dashboard_user
289
+ await this.prismaService.dashboard_user.deleteMany({
290
+ where: {
291
+ item_id: itemId,
292
+ user_id: userId,
293
+ },
294
+ });
295
+
296
+ return { success: true };
297
+ }
27
298
  }
@@ -1,5 +1,6 @@
1
- import { Pagination } from '@hed-hog/api-pagination';
1
+ import { Role } from '@hed-hog/api';
2
2
  import { Locale } from '@hed-hog/api-locale';
3
+ import { Pagination } from '@hed-hog/api-pagination';
3
4
  import {
4
5
  Body,
5
6
  Controller,
@@ -8,14 +9,12 @@ import {
8
9
  Inject,
9
10
  Param,
10
11
  ParseIntPipe,
11
- Patch,
12
12
  Post,
13
- forwardRef,
13
+ Query,
14
+ forwardRef
14
15
  } from '@nestjs/common';
15
- import { CreateDTO } from './dto/create.dto';
16
- import { UpdateDTO } from './dto/update.dto';
17
16
  import { DashboardItemService } from './dashboard-item.service';
18
- import { Role, DeleteDTO } from '@hed-hog/api';
17
+ import { CreateDashboardItemDTO } from './dto';
19
18
 
20
19
  @Role()
21
20
  @Controller('dashboard-item')
@@ -26,30 +25,22 @@ export class DashboardItemController {
26
25
  ) {}
27
26
 
28
27
  @Get()
29
- async list(@Pagination() paginationParams) {
30
- return this.dashboardItemService.list(paginationParams);
31
- }
32
-
33
- @Get(':id')
34
- async get(@Param('id', ParseIntPipe) id: number) {
35
- return this.dashboardItemService.get(id);
28
+ getAllDashboardItems(
29
+ @Pagination() paginationParams,
30
+ @Locale() locale: string,
31
+ @Query('dashboardId') dashboardId?: string,
32
+ ) {
33
+ const id = dashboardId ? parseInt(dashboardId) : undefined;
34
+ return this.dashboardItemService.getAllDashboardItems(paginationParams, locale, id);
36
35
  }
37
36
 
38
37
  @Post()
39
- async create(@Body() data: CreateDTO) {
40
- return this.dashboardItemService.create(data);
41
- }
42
-
43
- @Patch(':id')
44
- async update(@Param('id', ParseIntPipe) id: number, @Body() data: UpdateDTO) {
45
- return this.dashboardItemService.update({
46
- id,
47
- data,
48
- });
38
+ createDashboardItem(@Body() data: CreateDashboardItemDTO, @Locale() locale: string) {
39
+ return this.dashboardItemService.createDashboardItem(data, locale);
49
40
  }
50
41
 
51
- @Delete()
52
- async delete(@Body() data: DeleteDTO) {
53
- return this.dashboardItemService.delete(data);
42
+ @Delete(':id')
43
+ deleteDashboardItem(@Param('id', ParseIntPipe) id: number, @Locale() locale: string) {
44
+ return this.dashboardItemService.deleteDashboardItem(id, locale);
54
45
  }
55
46
  }
@@ -1,14 +1,13 @@
1
- import { DeleteDTO } from '@hed-hog/api';
2
- import { PaginationDTO, PaginationService } from '@hed-hog/api-pagination';
1
+ import { getLocaleText, LocaleService } from '@hed-hog/api-locale';
2
+ import { PaginationService } from '@hed-hog/api-pagination';
3
3
  import { PrismaService } from '@hed-hog/api-prisma';
4
4
  import {
5
- BadRequestException,
6
- Inject,
7
- Injectable,
8
- forwardRef,
5
+ forwardRef,
6
+ Inject,
7
+ Injectable,
8
+ NotFoundException
9
9
  } from '@nestjs/common';
10
- import { CreateDTO } from './dto/create.dto';
11
- import { UpdateDTO } from './dto/update.dto';
10
+ import { CreateDashboardItemDTO } from './dto';
12
11
 
13
12
  @Injectable()
14
13
  export class DashboardItemService {
@@ -17,66 +16,75 @@ export class DashboardItemService {
17
16
  private readonly prismaService: PrismaService,
18
17
  @Inject(forwardRef(() => PaginationService))
19
18
  private readonly paginationService: PaginationService,
19
+ @Inject(forwardRef(() => LocaleService))
20
+ private readonly localeService: LocaleService,
20
21
  ) {}
21
22
 
22
- async list(paginationParams: PaginationDTO) {
23
- const fields = [];
24
- const OR: any[] = this.prismaService.createInsensitiveSearch(
25
- fields,
26
- paginationParams,
27
- );
28
-
29
- if (paginationParams.search && !isNaN(+paginationParams.search)) {
30
- OR.push({ id: { equals: +paginationParams.search } });
31
- }
32
-
23
+ async getAllDashboardItems(paginationParams, locale: string, dashboardId?: number) {
33
24
  return this.paginationService.paginate(
34
25
  this.prismaService.dashboard_item,
35
26
  paginationParams,
36
27
  {
37
- where: {
38
- OR,
39
- },
28
+ where: dashboardId ? { dashboard_id: dashboardId } : undefined,
40
29
  include: {
41
- dashboard_component: true,
42
- dashboard: true,
30
+ dashboard: {
31
+ include: {
32
+ dashboard_locale: {
33
+ include: {
34
+ locale: true,
35
+ },
36
+ },
37
+ },
38
+ },
39
+ dashboard_component: {
40
+ include: {
41
+ dashboard_component_locale: {
42
+ include: {
43
+ locale: true,
44
+ },
45
+ },
46
+ },
47
+ },
48
+ },
49
+ orderBy: {
50
+ id: 'desc',
43
51
  },
44
52
  },
53
+ 'dashboardItem',
45
54
  );
46
55
  }
47
56
 
48
- async get(id: number) {
49
- return this.prismaService.dashboard_item.findUnique({
50
- where: { id: id },
51
- });
52
- }
53
-
54
- async create(data: CreateDTO) {
57
+ async createDashboardItem(data: CreateDashboardItemDTO, locale: string) {
55
58
  return this.prismaService.dashboard_item.create({
56
- data,
59
+ data: {
60
+ dashboard_id: data.dashboard_id,
61
+ component_id: data.component_id,
62
+ width: data.width,
63
+ height: data.height,
64
+ x_axis: data.x_axis,
65
+ y_axis: data.y_axis,
66
+ },
67
+ include: {
68
+ dashboard: true,
69
+ dashboard_component: true,
70
+ },
57
71
  });
58
72
  }
59
73
 
60
- async update({ id, data }: { id: number; data: UpdateDTO }) {
61
- return this.prismaService.dashboard_item.update({
62
- where: { id: id },
63
- data,
74
+ async deleteDashboardItem(id: number, locale: string) {
75
+ const item = await this.prismaService.dashboard_item.findUnique({
76
+ where: { id },
64
77
  });
65
- }
66
78
 
67
- async delete({ ids }: DeleteDTO): Promise<{count:number}> {
68
- if (ids == undefined || ids == null) {
69
- throw new BadRequestException(
70
- 'You must select at least one item to delete.',
79
+ if (!item) {
80
+ throw new NotFoundException(
81
+ getLocaleText('dashboardItemNotFound', locale, 'Dashboard item not found'),
71
82
  );
72
83
  }
73
84
 
74
- return this.prismaService.dashboard_item.deleteMany({
75
- where: {
76
- id: {
77
- in: ids,
78
- },
79
- },
85
+ await this.prismaService.dashboard_item.delete({
86
+ where: { id },
80
87
  });
88
+ return { success: true };
81
89
  }
82
90
  }
@@ -0,0 +1,7 @@
1
+ export * from './create.dto';
2
+ export * from './update.dto';
3
+
4
+ // Aliases para compatibilidade
5
+ export { CreateDTO as CreateDashboardItemDTO } from './create.dto';
6
+ export { UpdateDTO as UpdateDashboardItemDTO } from './update.dto';
7
+
@@ -1,4 +1,28 @@
1
- import { PartialType } from '@nestjs/mapped-types';
2
- import { CreateDTO } from './create.dto';
1
+ import { getLocaleText } from '@hed-hog/api-locale';
2
+ import { IsNumber, IsOptional } from 'class-validator';
3
3
 
4
- export class UpdateDTO extends PartialType(CreateDTO) {}
4
+ export class UpdateDTO {
5
+ @IsOptional()
6
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
7
+ component_id?: number;
8
+
9
+ @IsOptional()
10
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
11
+ dashboard_id?: number;
12
+
13
+ @IsOptional()
14
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
15
+ width?: number;
16
+
17
+ @IsOptional()
18
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
19
+ height?: number;
20
+
21
+ @IsOptional()
22
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
23
+ x_axis?: number;
24
+
25
+ @IsOptional()
26
+ @IsNumber({}, { message: (args) => getLocaleText('validation.numberRequired', args.value) })
27
+ y_axis?: number;
28
+ }
@@ -5,13 +5,13 @@ import { DashboardComponentModule } from './dashboard-component/dashboard-compon
5
5
  import { DashboardCoreModule } from './dashboard-core/dashboard-core.module';
6
6
  import { DashboardItemModule } from './dashboard-item/dashboard-item.module';
7
7
  import { DashboardUserModule } from './dashboard-user/dashboard-user.module';
8
- import { DashboardModule2 } from './dashboard/dashboard.module';
8
+ import { DashboardCrudModule } from './dashboard/dashboard.module';
9
9
 
10
10
  @Module({
11
11
  imports: [
12
12
  forwardRef(() => LocaleModule),
13
13
  forwardRef(() => PrismaModule),
14
- forwardRef(() => DashboardModule2),
14
+ forwardRef(() => DashboardCrudModule),
15
15
  forwardRef(() => DashboardComponentModule),
16
16
  forwardRef(() => DashboardItemModule),
17
17
  forwardRef(() => DashboardUserModule),
@@ -167,5 +167,8 @@
167
167
  },
168
168
  "mail_sent_not_found": "Mail sent record not found.",
169
169
  "validationSslugMustBeString": "Slug must be a string.",
170
- "sessionRevoked": "Session has been revoked or expired."
170
+ "sessionRevoked": "Session has been revoked or expired.",
171
+ "dashboardNotFound": "Dashboard not found.",
172
+ "dashboardComponentNotFound": "Dashboard component not found.",
173
+ "dashboardItemNotFound": "Dashboard item not found."
171
174
  }
@@ -167,5 +167,8 @@
167
167
  },
168
168
  "mail_sent_not_found": "Registro de e-mail enviado não encontrado.",
169
169
  "validationSslugMustBeString": "O slug deve ser uma string.",
170
- "sessionRevoked": "Sessão foi revogada ou expirada."
170
+ "sessionRevoked": "Sessão foi revogada ou expirou.",
171
+ "dashboardNotFound": "Dashboard não encontrado.",
172
+ "dashboardComponentNotFound": "Componente de dashboard não encontrado.",
173
+ "dashboardItemNotFound": "Item de dashboard não encontrado."
171
174
  }