@hedhog/admin 0.48.10 → 0.48.11

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 (82) hide show
  1. package/dist/admin.module.d.ts.map +1 -1
  2. package/dist/admin.module.js +3 -1
  3. package/dist/admin.module.js.map +1 -1
  4. package/dist/index.d.ts +1 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +1 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/setting/dto/create.dto.d.ts +9 -0
  9. package/dist/setting/dto/create.dto.d.ts.map +1 -0
  10. package/dist/setting/dto/create.dto.js +40 -0
  11. package/dist/setting/dto/create.dto.js.map +1 -0
  12. package/dist/setting/dto/delete.dto.d.ts +4 -0
  13. package/dist/setting/dto/delete.dto.d.ts.map +1 -0
  14. package/dist/setting/dto/delete.dto.js +23 -0
  15. package/dist/setting/dto/delete.dto.js.map +1 -0
  16. package/dist/setting/dto/setting-user.dto.d.ts +4 -0
  17. package/dist/setting/dto/setting-user.dto.d.ts.map +1 -0
  18. package/dist/setting/dto/setting-user.dto.js +21 -0
  19. package/dist/setting/dto/setting-user.dto.js.map +1 -0
  20. package/dist/setting/dto/setting.dto.d.ts +9 -0
  21. package/dist/setting/dto/setting.dto.d.ts.map +1 -0
  22. package/dist/setting/dto/setting.dto.js +34 -0
  23. package/dist/setting/dto/setting.dto.js.map +1 -0
  24. package/dist/setting/dto/update.dto.d.ts +6 -0
  25. package/dist/setting/dto/update.dto.d.ts.map +1 -0
  26. package/dist/setting/dto/update.dto.js +9 -0
  27. package/dist/setting/dto/update.dto.js.map +1 -0
  28. package/dist/setting/setting.controller.d.ts +102 -0
  29. package/dist/setting/setting.controller.d.ts.map +1 -0
  30. package/dist/setting/setting.controller.js +160 -0
  31. package/dist/setting/setting.controller.js.map +1 -0
  32. package/dist/setting/setting.module.d.ts +3 -0
  33. package/dist/setting/setting.module.d.ts.map +1 -0
  34. package/dist/setting/setting.module.js +24 -0
  35. package/dist/setting/setting.module.js.map +1 -0
  36. package/dist/setting/setting.service.d.ts +103 -0
  37. package/dist/setting/setting.service.d.ts.map +1 -0
  38. package/dist/setting/setting.service.js +333 -0
  39. package/dist/setting/setting.service.js.map +1 -0
  40. package/frontend/dashboard/components/create-panel.tsx.ejs +63 -63
  41. package/frontend/dashboard/components/dashboard.screen.tsx.ejs +116 -116
  42. package/frontend/dashboard/components/update-panel.tsx.ejs +80 -80
  43. package/frontend/dashboard/locales/en/dashboard.dashboard.json +11 -11
  44. package/frontend/dashboard/locales/pt/dashboard.dashboard.json +11 -11
  45. package/frontend/dashboard/react-query/handlers.ts.ejs +28 -28
  46. package/frontend/dashboard/react-query/requests.ts.ejs +56 -56
  47. package/frontend/dashboard-component/components/create-panel.tsx.ejs +130 -130
  48. package/frontend/dashboard-component/components/update-panel.tsx.ejs +164 -164
  49. package/frontend/dashboard-component/locales/en/dashboard.dashboard-component.json +11 -11
  50. package/frontend/dashboard-component/locales/pt/dashboard.dashboard-component.json +11 -11
  51. package/frontend/dashboard-component/react-query/handlers.ts.ejs +28 -28
  52. package/frontend/dashboard-component/react-query/requests.ts.ejs +61 -61
  53. package/frontend/dashboard-item/components/create-panel.tsx.ejs +108 -108
  54. package/frontend/dashboard-item/components/update-panel.tsx.ejs +141 -141
  55. package/frontend/dashboard-item/locales/en/dashboard.dashboard-item.json +11 -11
  56. package/frontend/dashboard-item/locales/pt/dashboard.dashboard-item.json +11 -11
  57. package/frontend/dashboard-item/react-query/handlers.ts.ejs +28 -28
  58. package/frontend/dashboard-item/react-query/requests.ts.ejs +58 -58
  59. package/frontend/dashboard-user/components/create-panel.tsx.ejs +108 -108
  60. package/frontend/dashboard-user/components/update-panel.tsx.ejs +137 -137
  61. package/frontend/dashboard-user/locales/en/dashboard.dashboard-user.json +11 -11
  62. package/frontend/dashboard-user/locales/pt/dashboard.dashboard-user.json +11 -11
  63. package/frontend/dashboard-user/react-query/handlers.ts.ejs +28 -28
  64. package/frontend/dashboard-user/react-query/requests.ts.ejs +58 -58
  65. package/hedhog.yaml +244 -0
  66. package/package.json +1 -1
  67. package/src/admin.module.ts +3 -1
  68. package/src/dashboard/dashboard-component/dashboard-component.controller.ts +55 -55
  69. package/src/dashboard/dashboard-component/dto/create.dto.ts +36 -36
  70. package/src/dashboard/dashboard-component/dto/update.dto.ts +4 -4
  71. package/src/dashboard/dashboard-item/dto/update.dto.ts +4 -4
  72. package/src/dashboard/dashboard-user/dto/update.dto.ts +4 -4
  73. package/src/dashboard/index.ts +1 -1
  74. package/src/index.ts +2 -0
  75. package/src/setting/dto/create.dto.ts +21 -0
  76. package/src/setting/dto/delete.dto.ts +8 -0
  77. package/src/setting/dto/setting-user.dto.ts +6 -0
  78. package/src/setting/dto/setting.dto.ts +17 -0
  79. package/src/setting/dto/update.dto.ts +4 -0
  80. package/src/setting/setting.controller.ts +105 -0
  81. package/src/setting/setting.module.ts +11 -0
  82. package/src/setting/setting.service.ts +400 -0
@@ -0,0 +1,17 @@
1
+ import { Type } from 'class-transformer';
2
+ import { IsArray, IsString, ValidateNested } from 'class-validator';
3
+
4
+ class Setting {
5
+ @IsString()
6
+ slug: string;
7
+
8
+ @IsString()
9
+ value: string;
10
+ }
11
+
12
+ export class SettingDTO {
13
+ @IsArray()
14
+ @ValidateNested({ each: true })
15
+ @Type(() => Setting)
16
+ setting: Setting[];
17
+ }
@@ -0,0 +1,4 @@
1
+ import { PartialType } from '@nestjs/mapped-types';
2
+ import { CreateDTO } from './create.dto';
3
+
4
+ export class UpdateDTO extends PartialType(CreateDTO) {}
@@ -0,0 +1,105 @@
1
+ import { Public, Role, User } from '@hedhog/core';
2
+ import { Locale } from '@hedhog/locale';
3
+ import { Pagination } from '@hedhog/pagination';
4
+ import {
5
+ Body,
6
+ Controller,
7
+ Delete,
8
+ forwardRef,
9
+ Get,
10
+ Inject,
11
+ Param,
12
+ ParseIntPipe,
13
+ Patch,
14
+ Post,
15
+ Put,
16
+ } from '@nestjs/common';
17
+ import { CreateDTO } from './dto/create.dto';
18
+ import { DeleteDTO } from './dto/delete.dto';
19
+ import { SettingUserDTO } from './dto/setting-user.dto';
20
+ import { SettingDTO } from './dto/setting.dto';
21
+ import { UpdateDTO } from './dto/update.dto';
22
+ import { SettingService } from './setting.service';
23
+
24
+ @Role()
25
+ @Controller('setting')
26
+ export class SettingsController {
27
+ constructor(
28
+ @Inject(forwardRef(() => SettingService))
29
+ private readonly settingService: SettingService,
30
+ ) {}
31
+
32
+ @Get('group/:slug')
33
+ async getSettingFromGroup(
34
+ @Pagination() paginationParams,
35
+ @Locale() locale,
36
+ @Param('slug') slug: string,
37
+ ) {
38
+ return this.settingService.getSettingFromGroup(
39
+ locale,
40
+ paginationParams,
41
+ slug,
42
+ );
43
+ }
44
+
45
+ @Public()
46
+ @Get('group')
47
+ async listSettingGroups(@Pagination() paginationParams, @Locale() locale) {
48
+ return this.settingService.listSettingGroups(locale, paginationParams);
49
+ }
50
+
51
+ @Get('/user')
52
+ async getUserSettings(@User() { id }) {
53
+ return this.settingService.getUserSettings(id);
54
+ }
55
+
56
+ @Get()
57
+ async listSettings(@Pagination() paginationParams, @Locale() locale) {
58
+ return this.settingService.listSettings(locale, paginationParams);
59
+ }
60
+
61
+ @Get(':settingId')
62
+ async get(@Param('settingId', ParseIntPipe) settingId: number) {
63
+ return this.settingService.get(settingId);
64
+ }
65
+
66
+ @Post()
67
+ async create(@Body() data: CreateDTO) {
68
+ return this.settingService.create(data);
69
+ }
70
+
71
+ @Put('user/:slug')
72
+ async updateUserFromSlug(
73
+ @Param('slug') slug: string,
74
+ @Body() { value }: SettingUserDTO,
75
+ @User() { id },
76
+ ) {
77
+ return this.settingService.setSettingUserValue(id, slug, value);
78
+ }
79
+
80
+ @Put(':slug')
81
+ async updateFromSlug(@Param('slug') slug: string, @Body() data: UpdateDTO) {
82
+ return this.settingService.updateFromSlug(slug, data);
83
+ }
84
+
85
+ @Put()
86
+ async setManySettings(@Body() data: SettingDTO) {
87
+ return this.settingService.setManySettings(data);
88
+ }
89
+
90
+ @Patch(':settingId')
91
+ async update(
92
+ @Param('settingId', ParseIntPipe) settingId: number,
93
+ @Body() data: UpdateDTO,
94
+ ) {
95
+ return this.settingService.update({
96
+ id: settingId,
97
+ data,
98
+ });
99
+ }
100
+
101
+ @Delete()
102
+ async delete(@Body() data: DeleteDTO) {
103
+ return this.settingService.delete(data);
104
+ }
105
+ }
@@ -0,0 +1,11 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { SettingsController } from './setting.controller';
3
+ import { SettingService } from './setting.service';
4
+
5
+ @Module({
6
+ imports: [],
7
+ controllers: [SettingsController],
8
+ providers: [SettingService],
9
+ exports: [SettingService],
10
+ })
11
+ export class SettingModule {}
@@ -0,0 +1,400 @@
1
+ import path = require('path');
2
+ import { itemTranslations } from '@hedhog/core';
3
+ import { PaginationDTO, PaginationService } from '@hedhog/pagination';
4
+ import { PrismaService } from '@hedhog/prisma';
5
+ import {
6
+ BadRequestException,
7
+ Inject,
8
+ Injectable,
9
+ forwardRef,
10
+ } from '@nestjs/common';
11
+ import { CreateDTO } from './dto/create.dto';
12
+ import { DeleteDTO } from './dto/delete.dto';
13
+ import { SettingDTO } from './dto/setting.dto';
14
+ import { UpdateDTO } from './dto/update.dto';
15
+
16
+ @Injectable()
17
+ export class SettingService {
18
+ constructor(
19
+ @Inject(forwardRef(() => PrismaService))
20
+ private readonly prismaService: PrismaService,
21
+ @Inject(forwardRef(() => PaginationService))
22
+ private readonly paginationService: PaginationService,
23
+ ) {}
24
+
25
+ async setManySettings(data: SettingDTO) {
26
+ const transaction = [];
27
+
28
+ for (const { slug, value } of data.setting) {
29
+ transaction.push(
30
+ this.prismaService.setting.updateMany({
31
+ where: {
32
+ slug,
33
+ },
34
+ data: {
35
+ value,
36
+ },
37
+ }),
38
+ );
39
+ }
40
+
41
+ await this.prismaService.$transaction(transaction);
42
+ return { success: true };
43
+ }
44
+
45
+ async getSettingFromGroup(locale: any, paginationParams: any, slug: string) {
46
+ const fields = ['slug', 'value'];
47
+
48
+ paginationParams.pageSize = 100;
49
+
50
+ const OR: any[] = this.prismaService.createInsensitiveSearch(
51
+ fields,
52
+ paginationParams,
53
+ );
54
+
55
+ const result = await this.paginationService.paginate(
56
+ this.prismaService.setting,
57
+ paginationParams,
58
+ {
59
+ where: {
60
+ AND: {
61
+ setting_group: {
62
+ slug,
63
+ },
64
+ OR,
65
+ },
66
+ },
67
+ include: {
68
+ setting_group: {
69
+ include: {
70
+ setting_group_locale: {
71
+ where: {
72
+ locale: {
73
+ code: locale,
74
+ },
75
+ },
76
+ select: {
77
+ name: true,
78
+ description: true,
79
+ },
80
+ },
81
+ },
82
+ },
83
+ setting_locale: {
84
+ where: {
85
+ locale: {
86
+ code: locale,
87
+ },
88
+ },
89
+ select: {
90
+ name: true,
91
+ description: true,
92
+ },
93
+ },
94
+ },
95
+ },
96
+ 'setting_locale',
97
+ );
98
+
99
+ result.data = result.data.map((setting: any) => {
100
+ setting.setting_group = itemTranslations(
101
+ 'setting_group_locale',
102
+ setting.setting_group,
103
+ );
104
+ return setting;
105
+ });
106
+
107
+ return result;
108
+ }
109
+
110
+ async listSettingGroups(locale: string, paginationParams: PaginationDTO) {
111
+ const fields = ['slug', 'icon'];
112
+
113
+ paginationParams.pageSize = 100;
114
+
115
+ const OR: any[] = this.prismaService.createInsensitiveSearch(
116
+ fields,
117
+ paginationParams,
118
+ );
119
+
120
+ const result = await this.paginationService.paginate(
121
+ this.prismaService.setting_group,
122
+ paginationParams,
123
+ {
124
+ where: {
125
+ OR,
126
+ },
127
+ include: {
128
+ setting_group_locale: {
129
+ where: {
130
+ locale: {
131
+ code: locale,
132
+ },
133
+ },
134
+ select: {
135
+ name: true,
136
+ description: true,
137
+ },
138
+ },
139
+ },
140
+ },
141
+ 'setting_group_locale',
142
+ );
143
+
144
+ return result;
145
+ }
146
+
147
+ async listSettings(locale: string, paginationParams: PaginationDTO) {
148
+ const fields = ['slug', 'value'];
149
+
150
+ const OR: any[] = this.prismaService.createInsensitiveSearch(
151
+ fields,
152
+ paginationParams,
153
+ );
154
+
155
+ const result = await this.paginationService.paginate(
156
+ this.prismaService.setting,
157
+ paginationParams,
158
+ {
159
+ where: {
160
+ OR,
161
+ },
162
+ include: {
163
+ setting_group: {
164
+ include: {
165
+ setting_group_locale: {
166
+ where: {
167
+ locale: {
168
+ code: locale,
169
+ },
170
+ },
171
+ select: {
172
+ name: true,
173
+ description: true,
174
+ },
175
+ },
176
+ },
177
+ },
178
+ setting_locale: {
179
+ where: {
180
+ locale: {
181
+ code: locale,
182
+ },
183
+ },
184
+ select: {
185
+ name: true,
186
+ description: true,
187
+ },
188
+ },
189
+ },
190
+ },
191
+ 'setting_locale',
192
+ );
193
+
194
+ result.data = result.data.map((setting: any) => {
195
+ setting.setting_group = itemTranslations(
196
+ 'setting_group_locale',
197
+ setting.setting_group,
198
+ );
199
+ return setting;
200
+ });
201
+
202
+ return result;
203
+ }
204
+
205
+ async get(settingId: number) {
206
+ return this.prismaService.setting.findUnique({
207
+ where: { id: settingId },
208
+ });
209
+ }
210
+
211
+ async create(data: CreateDTO) {
212
+ return this.prismaService.setting.create({
213
+ data: {
214
+ slug: data.slug,
215
+ type: data.type as any,
216
+ value: data.value,
217
+ user_override: data.user_override,
218
+ setting_group: {
219
+ connect: {
220
+ id: data.group_id,
221
+ },
222
+ },
223
+ },
224
+ });
225
+ }
226
+
227
+ async update({ id, data }: { id: number; data: UpdateDTO }) {
228
+ return this.prismaService.setting.update({
229
+ where: { id },
230
+ data: {
231
+ slug: data.slug,
232
+ type: data.type as any,
233
+ value: data.value,
234
+ user_override: data.user_override,
235
+ setting_group: {
236
+ connect: {
237
+ id: data.group_id,
238
+ },
239
+ },
240
+ },
241
+ });
242
+ }
243
+
244
+ async updateFromSlug(slug: string, data: UpdateDTO) {
245
+ const { id } = await this.prismaService.setting.findFirst({
246
+ where: {
247
+ slug,
248
+ },
249
+ });
250
+
251
+ if (!id) {
252
+ throw new BadRequestException(`Setting with slug ${slug} not found.`);
253
+ }
254
+
255
+ return this.update({
256
+ id,
257
+ data,
258
+ });
259
+ }
260
+
261
+ async delete({ ids }: DeleteDTO) {
262
+ if (ids == undefined || ids == null) {
263
+ throw new BadRequestException(
264
+ `You must select at least one setting to delete.`,
265
+ );
266
+ }
267
+
268
+ return this.prismaService.setting.deleteMany({
269
+ where: {
270
+ id: {
271
+ in: ids,
272
+ },
273
+ },
274
+ });
275
+ }
276
+
277
+ async setSettingUserValue(user_id: number, slug: string, value: string) {
278
+ const user = await this.prismaService.user.findUnique({
279
+ where: {
280
+ id: user_id,
281
+ },
282
+ select: {
283
+ id: true,
284
+ },
285
+ });
286
+
287
+ if (!user) {
288
+ throw new BadRequestException(`User with id ${user_id} not found.`);
289
+ }
290
+
291
+ const setting = await this.prismaService.setting.findFirst({
292
+ where: {
293
+ slug,
294
+ user_override: true,
295
+ },
296
+ });
297
+
298
+ if (!setting) {
299
+ throw new BadRequestException(
300
+ `Setting with slug ${slug} not found or user can not override.`,
301
+ );
302
+ }
303
+
304
+ return await this.prismaService.setting_user.upsert({
305
+ where: {
306
+ user_id_setting_id: {
307
+ setting_id: setting.id,
308
+ user_id: user.id,
309
+ },
310
+ },
311
+ create: {
312
+ setting_id: setting.id,
313
+ value,
314
+ user_id: user.id,
315
+ },
316
+ update: {
317
+ value,
318
+ },
319
+ select: {
320
+ setting_id: true,
321
+ user_id: true,
322
+ value: true,
323
+ },
324
+ });
325
+ }
326
+
327
+ async getSettingValues(slug: string | string[]) {
328
+ slug = Array.isArray(slug) ? slug : [slug];
329
+
330
+ let setting = await this.prismaService.setting.findMany({
331
+ where: {
332
+ slug: {
333
+ in: slug,
334
+ },
335
+ },
336
+ select: {
337
+ id: true,
338
+ value: true,
339
+ slug: true,
340
+ type: true,
341
+ user_override: true,
342
+ },
343
+ });
344
+
345
+ const slugUserOverride = setting.filter((setting) => setting.user_override);
346
+
347
+ const settingUser = await this.prismaService.setting_user.findMany({
348
+ where: {
349
+ setting_id: {
350
+ in: slugUserOverride.map((setting) => setting.id),
351
+ },
352
+ },
353
+ select: {
354
+ value: true,
355
+ setting_id: true,
356
+ },
357
+ });
358
+
359
+ const data: Record<string, any> = {};
360
+
361
+ setting.forEach((setting) => {
362
+ switch (setting.type) {
363
+ case 'boolean':
364
+ data[setting.slug] = setting.value === 'true';
365
+ break;
366
+ case 'number':
367
+ data[setting.slug] = Number(setting.value);
368
+ break;
369
+ case 'array':
370
+ case 'json':
371
+ data[setting.slug] = JSON.parse(setting.value);
372
+ break;
373
+ default:
374
+ data[setting.slug] = setting.value;
375
+ }
376
+ });
377
+
378
+ settingUser.forEach((setting) => {
379
+ data[slugUserOverride.find((s) => s.id === setting.setting_id).slug] =
380
+ setting.value;
381
+ });
382
+
383
+ return data;
384
+ }
385
+
386
+ async getUserSettings(user_id: number) {
387
+ return this.prismaService.setting_user.findMany({
388
+ where: {
389
+ user_id,
390
+ },
391
+ include: {
392
+ setting: {
393
+ select: {
394
+ slug: true,
395
+ },
396
+ },
397
+ },
398
+ });
399
+ }
400
+ }