@hedhog/admin 0.0.108 → 0.0.110

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. package/dist/admin.module.d.ts.map +1 -1
  2. package/dist/admin.module.js +1 -6
  3. package/dist/admin.module.js.map +1 -1
  4. package/dist/auth/auth.controller.d.ts.map +1 -1
  5. package/dist/auth/auth.controller.js +6 -8
  6. package/dist/auth/auth.controller.js.map +1 -1
  7. package/dist/auth/guards/auth.guard.js +2 -2
  8. package/dist/auth/guards/auth.guard.js.map +1 -1
  9. package/dist/index.d.ts +0 -9
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +0 -11
  12. package/dist/index.js.map +1 -1
  13. package/dist/menu/menu.controller.d.ts.map +1 -1
  14. package/dist/menu/menu.controller.js +4 -5
  15. package/dist/menu/menu.controller.js.map +1 -1
  16. package/dist/role/dto/create.dto.d.ts +1 -1
  17. package/dist/role/dto/create.dto.d.ts.map +1 -1
  18. package/dist/role/dto/create.dto.js +2 -2
  19. package/dist/role/dto/create.dto.js.map +1 -1
  20. package/dist/role/guards/role.guard.d.ts.map +1 -1
  21. package/dist/role/guards/role.guard.js +3 -4
  22. package/dist/role/guards/role.guard.js.map +1 -1
  23. package/dist/role/role.controller.js +3 -3
  24. package/dist/role/role.controller.js.map +1 -1
  25. package/dist/role/role.module.js +1 -1
  26. package/dist/role/role.module.js.map +1 -1
  27. package/dist/role/role.service.d.ts +1 -1
  28. package/dist/role/role.service.d.ts.map +1 -1
  29. package/dist/role/role.service.js +1 -1
  30. package/dist/role/role.service.js.map +1 -1
  31. package/dist/route/route.controller.js +3 -3
  32. package/dist/route/route.controller.js.map +1 -1
  33. package/dist/screen/screen.controller.js +3 -3
  34. package/dist/screen/screen.controller.js.map +1 -1
  35. package/dist/user/user.controller.js +2 -2
  36. package/dist/user/user.controller.js.map +1 -1
  37. package/hedhog.yaml +1 -328
  38. package/package.json +8 -6
  39. package/src/admin.module.ts +1 -6
  40. package/src/auth/auth.controller.ts +1 -3
  41. package/src/auth/guards/auth.guard.ts +1 -1
  42. package/src/index.ts +0 -14
  43. package/src/menu/menu.controller.ts +2 -3
  44. package/src/role/dto/create.dto.ts +1 -1
  45. package/src/role/guards/role.guard.ts +1 -2
  46. package/src/role/role.controller.ts +2 -2
  47. package/src/role/role.module.ts +1 -1
  48. package/src/role/role.service.ts +1 -1
  49. package/src/route/route.controller.ts +2 -2
  50. package/src/screen/screen.controller.ts +2 -2
  51. package/src/user/user.controller.ts +1 -1
  52. package/dist/auth/decorators/public.decorator.d.ts +0 -3
  53. package/dist/auth/decorators/public.decorator.d.ts.map +0 -1
  54. package/dist/auth/decorators/public.decorator.js +0 -8
  55. package/dist/auth/decorators/public.decorator.js.map +0 -1
  56. package/dist/auth/decorators/user.decorator.d.ts +0 -2
  57. package/dist/auth/decorators/user.decorator.d.ts.map +0 -1
  58. package/dist/auth/decorators/user.decorator.js +0 -12
  59. package/dist/auth/decorators/user.decorator.js.map +0 -1
  60. package/dist/dto/with-locale.dto.d.ts +0 -4
  61. package/dist/dto/with-locale.dto.d.ts.map +0 -1
  62. package/dist/dto/with-locale.dto.js +0 -23
  63. package/dist/dto/with-locale.dto.js.map +0 -1
  64. package/dist/locale/dto/create.dto.d.ts +0 -6
  65. package/dist/locale/dto/create.dto.d.ts.map +0 -1
  66. package/dist/locale/dto/create.dto.js +0 -29
  67. package/dist/locale/dto/create.dto.js.map +0 -1
  68. package/dist/locale/dto/delete.dto.d.ts +0 -4
  69. package/dist/locale/dto/delete.dto.d.ts.map +0 -1
  70. package/dist/locale/dto/delete.dto.js +0 -23
  71. package/dist/locale/dto/delete.dto.js.map +0 -1
  72. package/dist/locale/dto/set-enabled.dto.d.ts +0 -4
  73. package/dist/locale/dto/set-enabled.dto.d.ts.map +0 -1
  74. package/dist/locale/dto/set-enabled.dto.js +0 -24
  75. package/dist/locale/dto/set-enabled.dto.js.map +0 -1
  76. package/dist/locale/dto/update.dto.d.ts +0 -6
  77. package/dist/locale/dto/update.dto.d.ts.map +0 -1
  78. package/dist/locale/dto/update.dto.js +0 -32
  79. package/dist/locale/dto/update.dto.js.map +0 -1
  80. package/dist/locale/index.d.ts +0 -5
  81. package/dist/locale/index.d.ts.map +0 -1
  82. package/dist/locale/index.js +0 -21
  83. package/dist/locale/index.js.map +0 -1
  84. package/dist/locale/locale.controller.d.ts +0 -18
  85. package/dist/locale/locale.controller.d.ts.map +0 -1
  86. package/dist/locale/locale.controller.js +0 -127
  87. package/dist/locale/locale.controller.js.map +0 -1
  88. package/dist/locale/locale.decorator.d.ts +0 -2
  89. package/dist/locale/locale.decorator.d.ts.map +0 -1
  90. package/dist/locale/locale.decorator.js +0 -9
  91. package/dist/locale/locale.decorator.js.map +0 -1
  92. package/dist/locale/locale.middleware.d.ts +0 -10
  93. package/dist/locale/locale.middleware.d.ts.map +0 -1
  94. package/dist/locale/locale.middleware.js +0 -45
  95. package/dist/locale/locale.middleware.js.map +0 -1
  96. package/dist/locale/locale.module.d.ts +0 -5
  97. package/dist/locale/locale.module.d.ts.map +0 -1
  98. package/dist/locale/locale.module.js +0 -35
  99. package/dist/locale/locale.module.js.map +0 -1
  100. package/dist/locale/locale.service.d.ts +0 -32
  101. package/dist/locale/locale.service.d.ts.map +0 -1
  102. package/dist/locale/locale.service.js +0 -290
  103. package/dist/locale/locale.service.js.map +0 -1
  104. package/dist/locale/locale.service.spec.d.ts +0 -2
  105. package/dist/locale/locale.service.spec.d.ts.map +0 -1
  106. package/dist/locale/locale.service.spec.js +0 -174
  107. package/dist/locale/locale.service.spec.js.map +0 -1
  108. package/dist/role/decorators/role.decorator.d.ts +0 -3
  109. package/dist/role/decorators/role.decorator.d.ts.map +0 -1
  110. package/dist/role/decorators/role.decorator.js +0 -10
  111. package/dist/role/decorators/role.decorator.js.map +0 -1
  112. package/dist/setting/dto/create.dto.d.ts +0 -3
  113. package/dist/setting/dto/create.dto.d.ts.map +0 -1
  114. package/dist/setting/dto/create.dto.js +0 -7
  115. package/dist/setting/dto/create.dto.js.map +0 -1
  116. package/dist/setting/dto/setting-user.dto.d.ts +0 -4
  117. package/dist/setting/dto/setting-user.dto.d.ts.map +0 -1
  118. package/dist/setting/dto/setting-user.dto.js +0 -21
  119. package/dist/setting/dto/setting-user.dto.js.map +0 -1
  120. package/dist/setting/dto/setting.dto.d.ts +0 -9
  121. package/dist/setting/dto/setting.dto.d.ts.map +0 -1
  122. package/dist/setting/dto/setting.dto.js +0 -34
  123. package/dist/setting/dto/setting.dto.js.map +0 -1
  124. package/dist/setting/dto/update.dto.d.ts +0 -4
  125. package/dist/setting/dto/update.dto.d.ts.map +0 -1
  126. package/dist/setting/dto/update.dto.js +0 -8
  127. package/dist/setting/dto/update.dto.js.map +0 -1
  128. package/dist/setting/setting.controller.d.ts +0 -25
  129. package/dist/setting/setting.controller.d.ts.map +0 -1
  130. package/dist/setting/setting.controller.js +0 -150
  131. package/dist/setting/setting.controller.js.map +0 -1
  132. package/dist/setting/setting.module.d.ts +0 -3
  133. package/dist/setting/setting.module.d.ts.map +0 -1
  134. package/dist/setting/setting.module.js +0 -31
  135. package/dist/setting/setting.module.js.map +0 -1
  136. package/dist/setting/setting.service.d.ts +0 -31
  137. package/dist/setting/setting.service.d.ts.map +0 -1
  138. package/dist/setting/setting.service.js +0 -314
  139. package/dist/setting/setting.service.js.map +0 -1
  140. package/dist/setting/setting.service.spec.d.ts +0 -2
  141. package/dist/setting/setting.service.spec.d.ts.map +0 -1
  142. package/dist/setting/setting.service.spec.js +0 -162
  143. package/dist/setting/setting.service.spec.js.map +0 -1
  144. package/src/auth/decorators/public.decorator.ts +0 -4
  145. package/src/auth/decorators/user.decorator.ts +0 -17
  146. package/src/dto/with-locale.dto.ts +0 -8
  147. package/src/locale/dto/create.dto.ts +0 -12
  148. package/src/locale/dto/delete.dto.ts +0 -8
  149. package/src/locale/dto/set-enabled.dto.ts +0 -9
  150. package/src/locale/dto/update.dto.ts +0 -15
  151. package/src/locale/index.ts +0 -4
  152. package/src/locale/locale.controller.ts +0 -79
  153. package/src/locale/locale.decorator.ts +0 -8
  154. package/src/locale/locale.middleware.ts +0 -34
  155. package/src/locale/locale.module.ts +0 -23
  156. package/src/locale/locale.service.spec.ts +0 -193
  157. package/src/locale/locale.service.ts +0 -364
  158. package/src/role/decorators/role.decorator.ts +0 -7
  159. package/src/setting/dto/create.dto.ts +0 -1
  160. package/src/setting/dto/setting-user.dto.ts +0 -6
  161. package/src/setting/dto/setting.dto.ts +0 -17
  162. package/src/setting/dto/update.dto.ts +0 -3
  163. package/src/setting/setting.controller.ts +0 -100
  164. package/src/setting/setting.module.ts +0 -18
  165. package/src/setting/setting.service.spec.ts +0 -183
  166. package/src/setting/setting.service.ts +0 -384
@@ -1,364 +0,0 @@
1
- import { PaginationDTO, PaginationService } from '@hedhog/pagination';
2
- import { PrismaService } from '@hedhog/prisma';
3
- import {
4
- BadRequestException,
5
- Inject,
6
- Injectable,
7
- forwardRef,
8
- } from '@nestjs/common';
9
- import { CreateDTO } from './dto/create.dto';
10
- import { DeleteDTO } from './dto/delete.dto';
11
- import { UpdateDTO } from './dto/update.dto';
12
-
13
- @Injectable()
14
- export class LocaleService {
15
- private codes: any = {};
16
-
17
- constructor(
18
- @Inject(forwardRef(() => PrismaService))
19
- private readonly prismaService: PrismaService,
20
- @Inject(forwardRef(() => PaginationService))
21
- private readonly paginationService: PaginationService,
22
- ) {}
23
-
24
- async setEnabled(codes: string[]) {
25
- if (!codes || codes.length < 1) {
26
- throw new BadRequestException('You must select at least one item code.');
27
- }
28
-
29
- await this.prismaService.locale.updateMany({
30
- where: {
31
- enabled: true,
32
- },
33
- data: {
34
- enabled: false,
35
- },
36
- });
37
-
38
- return this.prismaService.locale.updateMany({
39
- where: {
40
- code: {
41
- in: codes,
42
- },
43
- },
44
- data: {
45
- enabled: true,
46
- },
47
- });
48
- }
49
-
50
- async getEnables(currentLocale: string, paginationParams: PaginationDTO) {
51
- const fields = ['code', 'region'];
52
- const OR: any[] = this.prismaService.createInsensitiveSearch(
53
- fields,
54
- paginationParams,
55
- );
56
-
57
- const result = await this.paginationService.paginate(
58
- this.prismaService.locale,
59
- paginationParams,
60
- {
61
- where: {
62
- AND: [
63
- {
64
- enabled: true,
65
- },
66
- {
67
- OR,
68
- },
69
- ],
70
- },
71
- },
72
- );
73
-
74
- const codes = [];
75
-
76
- for (const item of result.data) {
77
- codes.push((item as any).code);
78
- }
79
-
80
- const { code, region, locale } = this.parseLocale(currentLocale);
81
-
82
- const where: any = {
83
- locale: {
84
- code,
85
- },
86
- translation_namespace: {
87
- name: 'translation',
88
- },
89
- };
90
-
91
- if (locale.length > 1) {
92
- where.locale.region = region;
93
- }
94
-
95
- const values = await this.prismaService.translation.findMany({
96
- where,
97
- select: {
98
- name: true,
99
- value: true,
100
- },
101
- });
102
-
103
- for (let i = 0; i < result.data.length; i++) {
104
- for (const value of values) {
105
- if (value.name === (result.data[i] as any).code) {
106
- (result.data[i] as any).name = value.value;
107
- break;
108
- }
109
- }
110
- }
111
-
112
- return result;
113
- }
114
-
115
- parseLocale(locale: string) {
116
- const localeCodes = locale.toLowerCase().split('-');
117
- const code = localeCodes[0];
118
- const region = localeCodes[1];
119
- return {
120
- code,
121
- region,
122
- locale: localeCodes,
123
- };
124
- }
125
-
126
- async getTranslations(localeCode: string, namespace: string) {
127
- if (!localeCode) {
128
- throw new BadRequestException('Locale code is required.');
129
- }
130
-
131
- if (!namespace) {
132
- namespace = 'translation';
133
- }
134
-
135
- const { code, region, locale } = this.parseLocale(localeCode);
136
- const where: any = {
137
- locale: {
138
- code,
139
- },
140
- translation_namespace: {
141
- name: namespace,
142
- },
143
- };
144
-
145
- if (locale.length > 1) {
146
- where.locale.region = region;
147
- }
148
-
149
- const values = await this.prismaService.translation.findMany({
150
- where,
151
- select: {
152
- name: true,
153
- value: true,
154
- },
155
- });
156
-
157
- const translation = {};
158
-
159
- for (const value of values) {
160
- translation[value.name] = value.value;
161
- }
162
-
163
- return translation;
164
- }
165
-
166
- async get(currentLocale: string, paginationParams: PaginationDTO) {
167
- const fields = ['code', 'region'];
168
- const OR: any[] = this.prismaService.createInsensitiveSearch(
169
- fields,
170
- paginationParams,
171
- );
172
-
173
- const result = await this.paginationService.paginate(
174
- this.prismaService.locale,
175
- paginationParams,
176
- {
177
- where: {
178
- OR,
179
- },
180
- },
181
- );
182
-
183
- const codes = [];
184
-
185
- for (const item of result.data) {
186
- codes.push((item as any).code);
187
- }
188
-
189
- const { code, region, locale } = this.parseLocale(currentLocale);
190
-
191
- const where: any = {
192
- locale: {
193
- code,
194
- },
195
- translation_namespace: {
196
- name: 'translation',
197
- },
198
- };
199
-
200
- if (locale.length > 1) {
201
- where.locale.region = region;
202
- }
203
-
204
- const values = await this.prismaService.translation.findMany({
205
- where,
206
- select: {
207
- name: true,
208
- value: true,
209
- },
210
- });
211
-
212
- for (let i = 0; i < result.data.length; i++) {
213
- for (const value of values) {
214
- if (value.name === (result.data[i] as any).code) {
215
- (result.data[i] as any).name = value.value;
216
- break;
217
- }
218
- }
219
- }
220
-
221
- return result;
222
- }
223
-
224
- async getByCode(code: string) {
225
- if (this.codes[code]) {
226
- return this.codes[code];
227
- }
228
- return (this.codes[code] = await this.prismaService.locale.findFirst({
229
- where: { code },
230
- }));
231
- }
232
-
233
- async getById(localeId: number) {
234
- return this.prismaService.locale.findUnique({
235
- where: { id: localeId },
236
- });
237
- }
238
-
239
- async create(data: CreateDTO) {
240
- return this.prismaService.locale.create({
241
- data,
242
- });
243
- }
244
-
245
- async update({ id, data }: { id: number; data: UpdateDTO }) {
246
- return this.prismaService.locale.update({
247
- where: { id },
248
- data,
249
- });
250
- }
251
-
252
- async delete({ ids }: DeleteDTO) {
253
- if (ids == undefined || ids == null) {
254
- throw new BadRequestException(
255
- 'You must select at least one item to delete.',
256
- );
257
- }
258
-
259
- return this.prismaService.locale.deleteMany({
260
- where: {
261
- id: {
262
- in: ids,
263
- },
264
- },
265
- });
266
- }
267
-
268
- private getTableNameTranslations(modelName: string) {
269
- return `${modelName}_locale`;
270
- }
271
-
272
- async createModelWithLocale<T>(
273
- modelName: string,
274
- foreignKeyName: string,
275
- data: T,
276
- locale: Record<string, Record<string, string>>,
277
- ) {
278
- try {
279
- const model = await this.prismaService[modelName].create({ data });
280
-
281
- if (locale) {
282
- await Promise.all(
283
- Object.entries(locale).map(async ([localeCode, localeData]) => {
284
- const localeRecord = await this.getByCode(localeCode);
285
- const localeEntry = {
286
- [foreignKeyName]: model.id,
287
- locale_id: localeRecord.id,
288
- ...localeData,
289
- };
290
-
291
- await this.prismaService[
292
- this.getTableNameTranslations(modelName)
293
- ].create({
294
- data: localeEntry,
295
- });
296
- }),
297
- );
298
- }
299
-
300
- return this.prismaService[modelName].findUnique({
301
- where: { id: model.id },
302
- include: {
303
- [this.getTableNameTranslations(modelName)]: {
304
- where: { locale: { enabled: true } },
305
- include: { locale: { select: { code: true } } },
306
- },
307
- },
308
- });
309
- } catch (error: any) {
310
- if (error.message.includes('Unique constraint failed')) {
311
- throw new BadRequestException('Data already exists.');
312
- } else {
313
- throw new BadRequestException(error);
314
- }
315
- }
316
- }
317
-
318
- async updateModelWithLocale<T>(
319
- modelName: string,
320
- foreignKeyName: string,
321
- id: number,
322
- data: T,
323
- locale: Record<string, Record<string, string>>,
324
- ) {
325
- try {
326
- if (locale) {
327
- await Promise.all(
328
- Object.entries(locale).map(async ([localeCode, localeData]) => {
329
- const localeRecord = await this.getByCode(localeCode);
330
- const localeEntry = {
331
- [foreignKeyName]: id,
332
- locale_id: localeRecord.id,
333
- ...localeData,
334
- };
335
-
336
- await this.prismaService[
337
- this.getTableNameTranslations(modelName)
338
- ].upsert({
339
- where: {
340
- [`${foreignKeyName}_locale_id`]: {
341
- [foreignKeyName]: id,
342
- locale_id: localeRecord.id,
343
- },
344
- },
345
- create: localeEntry,
346
- update: localeData,
347
- });
348
- }),
349
- );
350
- }
351
-
352
- return this.prismaService[modelName].update({
353
- where: { id },
354
- data,
355
- });
356
- } catch (error: any) {
357
- if (error.message.includes('Unique constraint failed')) {
358
- throw new BadRequestException('Data already exists.');
359
- } else {
360
- throw new BadRequestException(error);
361
- }
362
- }
363
- }
364
- }
@@ -1,7 +0,0 @@
1
- import { SetMetadata } from '@nestjs/common';
2
-
3
- export const WITH_ROLE = 'withRole';
4
-
5
- export function Role() {
6
- return SetMetadata(WITH_ROLE, true);
7
- }
@@ -1 +0,0 @@
1
- export class CreateDTO {}
@@ -1,6 +0,0 @@
1
- import { IsString } from 'class-validator';
2
-
3
- export class SettingUserDTO {
4
- @IsString()
5
- value: string;
6
- }
@@ -1,17 +0,0 @@
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
- }
@@ -1,3 +0,0 @@
1
- import { CreateDTO } from './create.dto';
2
-
3
- export class UpdateDTO extends CreateDTO {}
@@ -1,100 +0,0 @@
1
- import { Pagination } from '@hedhog/pagination';
2
- import {
3
- Body,
4
- Controller,
5
- Delete,
6
- Get,
7
- Inject,
8
- Param,
9
- ParseIntPipe,
10
- Patch,
11
- Post,
12
- Put,
13
- forwardRef,
14
- } from '@nestjs/common';
15
- import { User } from '../auth/decorators/user.decorator';
16
- import { DeleteDTO } from '../dto/delete.dto';
17
- import { Locale } from '../locale';
18
- import { Role } from '../role/decorators/role.decorator';
19
- import { CreateDTO } from './dto/create.dto';
20
- import { SettingUserDTO } from './dto/setting-user.dto';
21
- import { SettingDTO } from './dto/setting.dto';
22
- import { UpdateDTO } from './dto/update.dto';
23
- import { SettingService } from './setting.service';
24
-
25
- @Role()
26
- @Controller('setting')
27
- export class SettingsController {
28
- constructor(
29
- @Inject(forwardRef(() => SettingService))
30
- private readonly settingService: SettingService,
31
- ) {}
32
-
33
- @Get('group/:slug')
34
- async getSettingFromGroup(
35
- @Pagination() paginationParams,
36
- @Locale() locale,
37
- @Param('slug') slug: string,
38
- ) {
39
- return this.settingService.getSettingFromGroup(
40
- locale,
41
- paginationParams,
42
- slug,
43
- );
44
- }
45
-
46
- @Get('group')
47
- async listSettingGroups(@Pagination() paginationParams, @Locale() locale) {
48
- return this.settingService.listSettingGroups(locale, paginationParams);
49
- }
50
-
51
- @Get()
52
- async listSettings(@Pagination() paginationParams, @Locale() locale) {
53
- return this.settingService.listSettings(locale, paginationParams);
54
- }
55
-
56
- @Get(':settingId')
57
- async show(@Param('settingId', ParseIntPipe) settingId: number) {
58
- return this.settingService.get(settingId);
59
- }
60
-
61
- @Post()
62
- async create(@Body() data: CreateDTO) {
63
- return this.settingService.create(data);
64
- }
65
-
66
- @Put('user/:slug')
67
- async updateUserFromSlug(
68
- @Param('slug') slug: string,
69
- @Body() { value }: SettingUserDTO,
70
- @User() { id },
71
- ) {
72
- return this.settingService.setSettingUserValue(id, slug, value);
73
- }
74
-
75
- @Put(':slug')
76
- async updateFromSlug(@Param('slug') slug: string, @Body() data: UpdateDTO) {
77
- return this.settingService.updateFromSlug(slug, data);
78
- }
79
-
80
- @Put()
81
- async setManySettings(@Body() data: SettingDTO) {
82
- return this.settingService.setManySettings(data);
83
- }
84
-
85
- @Patch(':settingId')
86
- async update(
87
- @Param('settingId', ParseIntPipe) settingId: number,
88
- @Body() data: UpdateDTO,
89
- ) {
90
- return this.settingService.update({
91
- id: settingId,
92
- data,
93
- });
94
- }
95
-
96
- @Delete()
97
- async delete(@Body() data: DeleteDTO) {
98
- return this.settingService.delete(data);
99
- }
100
- }
@@ -1,18 +0,0 @@
1
- import { PaginationModule } from '@hedhog/pagination';
2
- import { PrismaModule } from '@hedhog/prisma';
3
- import { Module, forwardRef } from '@nestjs/common';
4
- import { AuthModule } from '../auth/auth.module';
5
- import { SettingsController } from './setting.controller';
6
- import { SettingService } from './setting.service';
7
-
8
- @Module({
9
- providers: [SettingService],
10
- exports: [SettingService],
11
- controllers: [SettingsController],
12
- imports: [
13
- forwardRef(() => AuthModule),
14
- forwardRef(() => PrismaModule),
15
- forwardRef(() => PaginationModule),
16
- ],
17
- })
18
- export class SettingModule {}
@@ -1,183 +0,0 @@
1
- import { PaginationService } from '@hedhog/pagination';
2
- import { PrismaService } from '@hedhog/prisma';
3
- import { BadRequestException } from '@nestjs/common';
4
- import { Test, TestingModule } from '@nestjs/testing';
5
- import { DeleteDTO } from '../dto/delete.dto';
6
- import { UpdateDTO } from './dto/update.dto';
7
- import { SettingService } from './setting.service';
8
-
9
- describe('SettingService', () => {
10
- let service: SettingService;
11
- let prismaService: PrismaService;
12
- // let paginationService: PaginationService;
13
-
14
- beforeEach(async () => {
15
- const module: TestingModule = await Test.createTestingModule({
16
- providers: [
17
- SettingService,
18
- {
19
- provide: PrismaService,
20
- useValue: {
21
- setting: {
22
- findUnique: jest.fn(),
23
- create: jest.fn(),
24
- update: jest.fn(),
25
- deleteMany: jest.fn(),
26
- },
27
- createInsensitiveSearch: jest.fn(),
28
- },
29
- },
30
- {
31
- provide: PaginationService,
32
- useValue: {
33
- paginate: jest.fn(),
34
- },
35
- },
36
- ],
37
- }).compile();
38
-
39
- service = module.get<SettingService>(SettingService);
40
- prismaService = module.get<PrismaService>(PrismaService);
41
- // paginationService = module.get<PaginationService>(PaginationService);
42
- });
43
-
44
- it('should be defined', () => {
45
- expect(service).toBeDefined();
46
- });
47
-
48
- /* describe('getSettings', () => {
49
- it('should return paginated setting', async () => {
50
- const paginationParams: PaginationDTO = {
51
- page: 1,
52
- pageSize: 10,
53
- search: '',
54
- sortField: '',
55
- sortOrder: PageOrderDirection.Asc,
56
- fields: '',
57
- };
58
-
59
- const mockPaginatedData = {
60
- data: [],
61
- total: 10,
62
- lastPage: 1,
63
- page: 1,
64
- prev: 0,
65
- next: 2,
66
- pageSize: 10,
67
- };
68
-
69
- jest.spyOn(prismaService, 'createInsensitiveSearch').mockReturnValue([]);
70
- jest
71
- .spyOn(paginationService, 'paginate')
72
- .mockResolvedValue(mockPaginatedData);
73
-
74
- const result = await service.getSettings(paginationParams);
75
-
76
- expect(prismaService.createInsensitiveSearch).toHaveBeenCalledWith(
77
- ['name'],
78
- paginationParams,
79
- );
80
- expect(paginationService.paginate).toHaveBeenCalledWith(
81
- prismaService.setting,
82
- paginationParams,
83
- { where: { OR: [] } },
84
- );
85
- expect(result).toEqual(mockPaginatedData);
86
- });
87
- });*/
88
-
89
- describe('get', () => {
90
- it('should return a specific setting by ID', async () => {
91
- const mockSetting = {
92
- id: 1,
93
- name: 'Test Setting',
94
- created_at: new Date(),
95
- updated_at: new Date(),
96
- };
97
- jest
98
- .spyOn(prismaService.setting, 'findUnique')
99
- .mockResolvedValue(mockSetting);
100
-
101
- const result = await service.get(1);
102
-
103
- expect(prismaService.setting.findUnique).toHaveBeenCalledWith({
104
- where: { id: 1 },
105
- });
106
- expect(result).toEqual(mockSetting);
107
- });
108
- });
109
- /*
110
- describe('create', () => {
111
- it('should create a new setting', async () => {
112
- const createDTO: CreateDTO = { name: 'New Setting' };
113
- const mockSetting = {
114
- id: 1,
115
- name: 'Test Setting',
116
- created_at: new Date(),
117
- updated_at: new Date(),
118
- };
119
-
120
- jest
121
- .spyOn(prismaService.setting, 'create')
122
- .mockResolvedValue(mockSetting);
123
-
124
- const result = await service.create(createDTO);
125
-
126
- expect(prismaService.setting.create).toHaveBeenCalledWith({
127
- data: { name: createDTO.name },
128
- });
129
- expect(result).toEqual(mockSetting);
130
- });
131
- });*/
132
-
133
- describe('update', () => {
134
- it('should update an existing setting', async () => {
135
- const updateDTO: UpdateDTO = { name: 'Updated Setting' };
136
- const updateInput = { id: 1, data: updateDTO };
137
- const mockSetting = {
138
- id: 1,
139
- name: 'Update Setting',
140
- created_at: new Date(),
141
- updated_at: new Date(),
142
- };
143
-
144
- jest
145
- .spyOn(prismaService.setting, 'update')
146
- .mockResolvedValue(mockSetting);
147
-
148
- const result = await service.update(updateInput);
149
-
150
- expect(prismaService.setting.update).toHaveBeenCalledWith({
151
- where: { id: updateInput.id },
152
- data: updateInput.data,
153
- });
154
- expect(result).toEqual(mockSetting);
155
- });
156
- });
157
-
158
- describe('delete', () => {
159
- it('should throw an error if no ids are provided', async () => {
160
- const deleteDTO: DeleteDTO = { ids: null };
161
-
162
- await expect(service.delete(deleteDTO)).rejects.toThrow(
163
- new BadRequestException(
164
- `You must select at least one setting to delete.`,
165
- ),
166
- );
167
- });
168
-
169
- it('should delete setting by ids', async () => {
170
- const deleteDTO: DeleteDTO = { ids: [1, 2] };
171
-
172
- jest
173
- .spyOn(prismaService.setting, 'deleteMany')
174
- .mockResolvedValue({ count: 2 });
175
-
176
- await service.delete(deleteDTO);
177
-
178
- expect(prismaService.setting.deleteMany).toHaveBeenCalledWith({
179
- where: { id: { in: deleteDTO.ids } },
180
- });
181
- });
182
- });
183
- });