@hedhog/admin 0.0.104 → 0.0.106

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. package/README.md +141 -141
  2. package/dist/admin.module.d.ts.map +1 -1
  3. package/dist/admin.module.js +8 -7
  4. package/dist/admin.module.js.map +1 -1
  5. package/dist/auth/auth.service.d.ts +1 -1
  6. package/dist/auth/auth.service.d.ts.map +1 -1
  7. package/dist/auth/auth.service.js +8 -8
  8. package/dist/auth/auth.service.js.map +1 -1
  9. package/dist/auth/auth.service.spec.js +16 -16
  10. package/dist/auth/auth.service.spec.js.map +1 -1
  11. package/dist/dto/with-locale.dto.d.ts +4 -0
  12. package/dist/dto/with-locale.dto.d.ts.map +1 -0
  13. package/dist/dto/{with-locales.dto.js → with-locale.dto.js} +5 -5
  14. package/dist/dto/with-locale.dto.js.map +1 -0
  15. package/dist/index.d.ts +2 -2
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +2 -2
  18. package/dist/index.js.map +1 -1
  19. package/dist/locale/locale.controller.d.ts +1 -1
  20. package/dist/locale/locale.controller.d.ts.map +1 -1
  21. package/dist/locale/locale.controller.js +4 -4
  22. package/dist/locale/locale.controller.js.map +1 -1
  23. package/dist/locale/locale.middleware.d.ts +1 -1
  24. package/dist/locale/locale.middleware.d.ts.map +1 -1
  25. package/dist/locale/locale.middleware.js +2 -2
  26. package/dist/locale/locale.middleware.js.map +1 -1
  27. package/dist/locale/locale.service.d.ts +6 -6
  28. package/dist/locale/locale.service.d.ts.map +1 -1
  29. package/dist/locale/locale.service.js +43 -43
  30. package/dist/locale/locale.service.js.map +1 -1
  31. package/dist/locale/locale.service.spec.js +14 -14
  32. package/dist/locale/locale.service.spec.js.map +1 -1
  33. package/dist/menu/menu.controller.js +3 -3
  34. package/dist/menu/menu.controller.js.map +1 -1
  35. package/dist/menu/menu.service.d.ts +3 -3
  36. package/dist/menu/menu.service.d.ts.map +1 -1
  37. package/dist/menu/menu.service.js +35 -35
  38. package/dist/menu/menu.service.js.map +1 -1
  39. package/dist/menu/menu.service.spec.js +33 -37
  40. package/dist/menu/menu.service.spec.js.map +1 -1
  41. package/dist/role/dto/create.dto.d.ts +2 -2
  42. package/dist/role/dto/create.dto.d.ts.map +1 -1
  43. package/dist/role/dto/create.dto.js +2 -2
  44. package/dist/role/dto/create.dto.js.map +1 -1
  45. package/dist/role/guards/role.guard.js +4 -4
  46. package/dist/role/guards/role.guard.js.map +1 -1
  47. package/dist/role/role.controller.d.ts.map +1 -1
  48. package/dist/role/role.controller.js +9 -9
  49. package/dist/role/role.controller.js.map +1 -1
  50. package/dist/role/role.service.d.ts +2 -2
  51. package/dist/role/role.service.d.ts.map +1 -1
  52. package/dist/role/role.service.js +35 -35
  53. package/dist/role/role.service.js.map +1 -1
  54. package/dist/role/role.service.spec.d.ts +0 -1
  55. package/dist/role/role.service.spec.js +400 -340
  56. package/dist/role/role.service.spec.js.map +1 -1
  57. package/dist/route/route.controller.js +3 -3
  58. package/dist/route/route.controller.js.map +1 -1
  59. package/dist/route/route.service.d.ts +2 -2
  60. package/dist/route/route.service.d.ts.map +1 -1
  61. package/dist/route/route.service.js +19 -19
  62. package/dist/route/route.service.js.map +1 -1
  63. package/dist/route/route.service.spec.js +27 -27
  64. package/dist/route/route.service.spec.js.map +1 -1
  65. package/dist/screen/screen.controller.d.ts +1 -1
  66. package/dist/screen/screen.controller.d.ts.map +1 -1
  67. package/dist/screen/screen.controller.js +8 -8
  68. package/dist/screen/screen.controller.js.map +1 -1
  69. package/dist/screen/screen.service.d.ts +3 -3
  70. package/dist/screen/screen.service.d.ts.map +1 -1
  71. package/dist/screen/screen.service.js +18 -18
  72. package/dist/screen/screen.service.js.map +1 -1
  73. package/dist/screen/screen.service.spec.js +16 -16
  74. package/dist/screen/screen.service.spec.js.map +1 -1
  75. package/dist/setting/dto/setting.dto.d.ts +9 -0
  76. package/dist/setting/dto/setting.dto.d.ts.map +1 -0
  77. package/dist/setting/dto/{settings.dto.js → setting.dto.js} +6 -6
  78. package/dist/setting/dto/setting.dto.js.map +1 -0
  79. package/dist/setting/{settings.controller.d.ts → setting.controller.d.ts} +8 -8
  80. package/dist/setting/setting.controller.d.ts.map +1 -0
  81. package/dist/setting/{settings.controller.js → setting.controller.js} +26 -26
  82. package/dist/setting/setting.controller.js.map +1 -0
  83. package/dist/setting/setting.module.d.ts +3 -0
  84. package/dist/setting/setting.module.d.ts.map +1 -0
  85. package/dist/setting/{settings.module.js → setting.module.js} +6 -6
  86. package/dist/setting/setting.module.js.map +1 -0
  87. package/dist/setting/{settings.service.d.ts → setting.service.d.ts} +6 -6
  88. package/dist/setting/setting.service.d.ts.map +1 -0
  89. package/dist/setting/{settings.service.js → setting.service.js} +44 -44
  90. package/dist/setting/setting.service.js.map +1 -0
  91. package/dist/setting/setting.service.spec.d.ts +2 -0
  92. package/dist/setting/setting.service.spec.d.ts.map +1 -0
  93. package/dist/setting/{settings.service.spec.js → setting.service.spec.js} +17 -17
  94. package/dist/setting/setting.service.spec.js.map +1 -0
  95. package/dist/user/user.controller.d.ts +2 -2
  96. package/dist/user/user.controller.d.ts.map +1 -1
  97. package/dist/user/user.controller.js +8 -8
  98. package/dist/user/user.controller.js.map +1 -1
  99. package/dist/user/user.service.d.ts +3 -3
  100. package/dist/user/user.service.d.ts.map +1 -1
  101. package/dist/user/user.service.js +10 -10
  102. package/dist/user/user.service.js.map +1 -1
  103. package/dist/user/user.service.spec.js +62 -49
  104. package/dist/user/user.service.spec.js.map +1 -1
  105. package/package.json +2 -2
  106. package/src/admin.module.ts +37 -0
  107. package/src/auth/auth.controller.ts +48 -0
  108. package/src/auth/auth.module.ts +39 -0
  109. package/src/auth/auth.service.spec.ts +196 -0
  110. package/src/auth/auth.service.ts +175 -0
  111. package/src/auth/decorators/public.decorator.ts +4 -0
  112. package/src/auth/decorators/user.decorator.ts +17 -0
  113. package/src/auth/dto/forget.dto.ts +6 -0
  114. package/src/auth/dto/login.dto.ts +15 -0
  115. package/src/auth/dto/otp.dto.ts +11 -0
  116. package/src/auth/enums/multifactor-type.enum.ts +4 -0
  117. package/src/auth/guards/auth.guard.ts +50 -0
  118. package/src/auth/types/user.type.ts +8 -0
  119. package/src/dto/delete.dto.ts +8 -0
  120. package/src/dto/update-ids.dto.ts +9 -0
  121. package/src/dto/with-locale.dto.ts +8 -0
  122. package/src/index.ts +34 -0
  123. package/src/locale/dto/create.dto.ts +12 -0
  124. package/src/locale/dto/delete.dto.ts +8 -0
  125. package/src/locale/dto/set-enabled.dto.ts +9 -0
  126. package/src/locale/dto/update.dto.ts +15 -0
  127. package/src/locale/index.ts +4 -0
  128. package/src/locale/locale.controller.ts +79 -0
  129. package/src/locale/locale.decorator.ts +8 -0
  130. package/src/locale/locale.middleware.ts +34 -0
  131. package/src/locale/locale.module.ts +23 -0
  132. package/src/locale/locale.service.spec.ts +193 -0
  133. package/src/locale/locale.service.ts +366 -0
  134. package/src/menu/dto/create.dto.ts +25 -0
  135. package/src/menu/dto/order.dto.ts +8 -0
  136. package/src/menu/dto/update.dto.ts +19 -0
  137. package/src/menu/menu.controller.ts +106 -0
  138. package/src/menu/menu.module.ts +18 -0
  139. package/src/menu/menu.service.spec.ts +247 -0
  140. package/src/menu/menu.service.ts +263 -0
  141. package/src/role/decorators/role.decorator.ts +4 -0
  142. package/src/role/dto/create.dto.ts +7 -0
  143. package/src/role/dto/update.dto.ts +4 -0
  144. package/src/role/guards/role.guard.ts +122 -0
  145. package/src/role/role.controller.ts +126 -0
  146. package/src/role/role.module.ts +28 -0
  147. package/src/role/role.service.spec.ts +417 -0
  148. package/src/role/role.service.ts +302 -0
  149. package/src/route/dto/create.dto.ts +13 -0
  150. package/src/route/dto/update.dto.ts +15 -0
  151. package/src/route/route.controller.ts +91 -0
  152. package/src/route/route.module.ts +18 -0
  153. package/src/route/route.service.spec.ts +299 -0
  154. package/src/route/route.service.ts +164 -0
  155. package/src/screen/dto/create.dto.ts +19 -0
  156. package/src/screen/dto/update.dto.ts +19 -0
  157. package/src/screen/screen.controller.ts +93 -0
  158. package/src/screen/screen.module.ts +18 -0
  159. package/src/screen/screen.service.spec.ts +298 -0
  160. package/src/screen/screen.service.ts +181 -0
  161. package/src/setting/dto/create.dto.ts +1 -0
  162. package/src/setting/dto/setting-user.dto.ts +6 -0
  163. package/src/setting/dto/setting.dto.ts +17 -0
  164. package/src/setting/dto/update.dto.ts +3 -0
  165. package/src/setting/setting.controller.ts +100 -0
  166. package/src/setting/setting.module.ts +18 -0
  167. package/src/setting/setting.service.spec.ts +183 -0
  168. package/src/setting/setting.service.ts +346 -0
  169. package/src/types/http-method.ts +8 -0
  170. package/src/user/constants/user.constants.ts +1 -0
  171. package/src/user/dto/create.dto.ts +24 -0
  172. package/src/user/dto/update.dto.ts +41 -0
  173. package/src/user/user.controller.ts +75 -0
  174. package/src/user/user.module.ts +18 -0
  175. package/src/user/user.service.spec.ts +294 -0
  176. package/src/user/user.service.ts +129 -0
  177. package/tsconfig.lib.json +9 -0
  178. package/tsconfig.production.json +20 -0
  179. package/dist/dto/with-locales.dto.d.ts +0 -4
  180. package/dist/dto/with-locales.dto.d.ts.map +0 -1
  181. package/dist/dto/with-locales.dto.js.map +0 -1
  182. package/dist/setting/dto/settings.dto.d.ts +0 -9
  183. package/dist/setting/dto/settings.dto.d.ts.map +0 -1
  184. package/dist/setting/dto/settings.dto.js.map +0 -1
  185. package/dist/setting/settings.controller.d.ts.map +0 -1
  186. package/dist/setting/settings.controller.js.map +0 -1
  187. package/dist/setting/settings.module.d.ts +0 -3
  188. package/dist/setting/settings.module.d.ts.map +0 -1
  189. package/dist/setting/settings.module.js.map +0 -1
  190. package/dist/setting/settings.service.d.ts.map +0 -1
  191. package/dist/setting/settings.service.js.map +0 -1
  192. package/dist/setting/settings.service.spec.d.ts +0 -2
  193. package/dist/setting/settings.service.spec.d.ts.map +0 -1
  194. package/dist/setting/settings.service.spec.js.map +0 -1
@@ -0,0 +1,183 @@
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
+ });
@@ -0,0 +1,346 @@
1
+ import { PaginationDTO, PaginationService } from '@hedhog/pagination';
2
+ import { PrismaService } from '@hedhog/prisma';
3
+ import { itemTranslations } from '@hedhog/utils';
4
+ import {
5
+ BadRequestException,
6
+ Inject,
7
+ Injectable,
8
+ forwardRef,
9
+ } from '@nestjs/common';
10
+ import { DeleteDTO } from '../dto/delete.dto';
11
+ import { CreateDTO } from './dto/create.dto';
12
+ import { SettingDTO } from './dto/setting.dto';
13
+ import { UpdateDTO } from './dto/update.dto';
14
+
15
+ @Injectable()
16
+ export class SettingService {
17
+ constructor(
18
+ @Inject(forwardRef(() => PrismaService))
19
+ private readonly prismaService: PrismaService,
20
+ @Inject(forwardRef(() => PaginationService))
21
+ private readonly paginationService: PaginationService,
22
+ ) {}
23
+
24
+ async setManySettings(data: SettingDTO) {
25
+ for (const { slug, value } of data.setting) {
26
+ await this.prismaService.setting.updateMany({
27
+ where: {
28
+ slug,
29
+ },
30
+ data: {
31
+ value,
32
+ },
33
+ });
34
+ }
35
+
36
+ return { success: true };
37
+ }
38
+
39
+ async getSettingFromGroup(locale: any, paginationParams: any, slug: string) {
40
+ const fields = ['slug', 'value'];
41
+
42
+ paginationParams.pageSize = 100;
43
+
44
+ const OR: any[] = this.prismaService.createInsensitiveSearch(
45
+ fields,
46
+ paginationParams,
47
+ );
48
+
49
+ const result = await this.paginationService.paginate(
50
+ this.prismaService.setting,
51
+ paginationParams,
52
+ {
53
+ where: {
54
+ AND: {
55
+ setting_group: {
56
+ slug,
57
+ },
58
+ OR,
59
+ },
60
+ },
61
+ include: {
62
+ setting_group: {
63
+ include: {
64
+ setting_group_locale: {
65
+ where: {
66
+ locale: {
67
+ code: locale,
68
+ },
69
+ },
70
+ select: {
71
+ name: true,
72
+ description: true,
73
+ },
74
+ },
75
+ },
76
+ },
77
+ setting_locale: {
78
+ where: {
79
+ locale: {
80
+ code: locale,
81
+ },
82
+ },
83
+ select: {
84
+ name: true,
85
+ description: true,
86
+ },
87
+ },
88
+ },
89
+ },
90
+ 'setting_locale',
91
+ );
92
+
93
+ result.data = result.data.map((setting: any) => {
94
+ setting.setting_group = itemTranslations(
95
+ 'setting_group_locale',
96
+ setting.setting_group,
97
+ );
98
+ return setting;
99
+ });
100
+
101
+ return result;
102
+ }
103
+
104
+ async listSettingGroups(locale: string, paginationParams: PaginationDTO) {
105
+ const fields = ['slug', 'icon'];
106
+
107
+ paginationParams.pageSize = 100;
108
+
109
+ const OR: any[] = this.prismaService.createInsensitiveSearch(
110
+ fields,
111
+ paginationParams,
112
+ );
113
+
114
+ const result = await this.paginationService.paginate(
115
+ this.prismaService.setting_group,
116
+ paginationParams,
117
+ {
118
+ where: {
119
+ OR,
120
+ },
121
+ include: {
122
+ setting_group_locale: {
123
+ where: {
124
+ locale: {
125
+ code: locale,
126
+ },
127
+ },
128
+ select: {
129
+ name: true,
130
+ description: true,
131
+ },
132
+ },
133
+ },
134
+ },
135
+ 'setting_group_locale',
136
+ );
137
+
138
+ return result;
139
+ }
140
+
141
+ async listSettings(locale: string, paginationParams: PaginationDTO) {
142
+ const fields = ['slug', 'value'];
143
+
144
+ const OR: any[] = this.prismaService.createInsensitiveSearch(
145
+ fields,
146
+ paginationParams,
147
+ );
148
+
149
+ const result = await this.paginationService.paginate(
150
+ this.prismaService.setting,
151
+ paginationParams,
152
+ {
153
+ where: {
154
+ OR,
155
+ },
156
+ include: {
157
+ setting_group: {
158
+ include: {
159
+ setting_group_locale: {
160
+ where: {
161
+ locale: {
162
+ code: locale,
163
+ },
164
+ },
165
+ select: {
166
+ name: true,
167
+ description: true,
168
+ },
169
+ },
170
+ },
171
+ },
172
+ setting_locale: {
173
+ where: {
174
+ locale: {
175
+ code: locale,
176
+ },
177
+ },
178
+ select: {
179
+ name: true,
180
+ description: true,
181
+ },
182
+ },
183
+ },
184
+ },
185
+ 'setting_locale',
186
+ );
187
+
188
+ result.data = result.data.map((setting: any) => {
189
+ setting.setting_group = itemTranslations(
190
+ 'setting_group_locale',
191
+ setting.setting_group,
192
+ );
193
+ return setting;
194
+ });
195
+
196
+ return result;
197
+ }
198
+
199
+ async get(settingId: number) {
200
+ return this.prismaService.setting.findUnique({
201
+ where: { id: settingId },
202
+ });
203
+ }
204
+
205
+ async create({}: CreateDTO) {
206
+ return this.prismaService.setting.create({
207
+ data: {},
208
+ });
209
+ }
210
+
211
+ async update({ id, data }: { id: number; data: UpdateDTO }) {
212
+ return this.prismaService.setting.update({
213
+ where: { id },
214
+ data,
215
+ });
216
+ }
217
+
218
+ async updateFromSlug(slug: string, data: UpdateDTO) {
219
+ const { id } = await this.prismaService.setting.findFirst({
220
+ where: {
221
+ slug,
222
+ },
223
+ });
224
+
225
+ if (!id) {
226
+ throw new BadRequestException(`Setting with slug ${slug} not found.`);
227
+ }
228
+
229
+ return this.update({
230
+ id,
231
+ data,
232
+ });
233
+ }
234
+
235
+ async delete({ ids }: DeleteDTO) {
236
+ if (ids == undefined || ids == null) {
237
+ throw new BadRequestException(
238
+ `You must select at least one setting to delete.`,
239
+ );
240
+ }
241
+
242
+ return this.prismaService.setting.deleteMany({
243
+ where: {
244
+ id: {
245
+ in: ids,
246
+ },
247
+ },
248
+ });
249
+ }
250
+
251
+ async setSettingUserValue(user_id: number, slug: string, value: string) {
252
+ const user = await this.prismaService.user.findUnique({
253
+ where: {
254
+ id: user_id,
255
+ },
256
+ select: {
257
+ id: true,
258
+ },
259
+ });
260
+
261
+ if (!user) {
262
+ throw new BadRequestException(`User with id ${user_id} not found.`);
263
+ }
264
+
265
+ const setting = await this.prismaService.setting.findFirst({
266
+ where: {
267
+ slug,
268
+ user_override: true,
269
+ },
270
+ });
271
+
272
+ if (!setting) {
273
+ throw new BadRequestException(
274
+ `Setting with slug ${slug} not found or user can not override.`,
275
+ );
276
+ }
277
+
278
+ return await this.prismaService.setting_user.upsert({
279
+ where: {
280
+ user_id_setting_id: {
281
+ setting_id: setting.id,
282
+ user_id: user.id,
283
+ },
284
+ },
285
+ create: {
286
+ setting_id: setting.id,
287
+ value,
288
+ user_id: user.id,
289
+ },
290
+ update: {
291
+ value,
292
+ },
293
+ select: {
294
+ setting_id: true,
295
+ user_id: true,
296
+ value: true,
297
+ },
298
+ });
299
+ }
300
+
301
+ async getSettingValues(slug: string | string[]) {
302
+ slug = Array.isArray(slug) ? slug : [slug];
303
+
304
+ let setting = await this.prismaService.setting.findMany({
305
+ where: {
306
+ slug: {
307
+ in: slug,
308
+ },
309
+ },
310
+ select: {
311
+ id: true,
312
+ value: true,
313
+ slug: true,
314
+ type: true,
315
+ user_override: true,
316
+ },
317
+ });
318
+
319
+ const slugUserOverride = setting.filter((setting) => setting.userOverride);
320
+
321
+ const settingUser = await this.prismaService.setting_user.findMany({
322
+ where: {
323
+ setting_id: {
324
+ in: slugUserOverride.map((setting) => setting.id),
325
+ },
326
+ },
327
+ select: {
328
+ value: true,
329
+ setting_id: true,
330
+ },
331
+ });
332
+
333
+ const data: Record<string, any> = {};
334
+
335
+ setting.forEach((setting) => {
336
+ data[setting.slug] = setting.value;
337
+ });
338
+
339
+ settingUser.forEach((setting) => {
340
+ data[slugUserOverride.find((s) => s.id === setting.setting_id).slug] =
341
+ setting.value;
342
+ });
343
+
344
+ return data;
345
+ }
346
+ }
@@ -0,0 +1,8 @@
1
+ export type HttpMethod =
2
+ | 'GET'
3
+ | 'POST'
4
+ | 'PUT'
5
+ | 'DELETE'
6
+ | 'PATCH'
7
+ | 'OPTIONS'
8
+ | 'HEAD';
@@ -0,0 +1 @@
1
+ export const SALT_ROUNDS = 10;
@@ -0,0 +1,24 @@
1
+ import { IsEmail, IsString, IsStrongPassword } from 'class-validator';
2
+
3
+ export class CreateDTO {
4
+ @IsString({ message: 'O nome deve ser uma string' })
5
+ name: string;
6
+
7
+ @IsEmail({}, { message: 'O email deve ser um email válido' })
8
+ email: string;
9
+
10
+ @IsStrongPassword(
11
+ {
12
+ minLength: 6,
13
+ minLowercase: 1,
14
+ minUppercase: 0,
15
+ minNumbers: 0,
16
+ minSymbols: 0,
17
+ },
18
+ {
19
+ message:
20
+ 'A senha deve ter pelo menos 6 caracteres, contendo pelo menos uma letra minúscula',
21
+ },
22
+ )
23
+ password: string;
24
+ }
@@ -0,0 +1,41 @@
1
+ import {
2
+ IsEmail,
3
+ IsInt,
4
+ IsOptional,
5
+ IsString,
6
+ IsStrongPassword,
7
+ } from 'class-validator';
8
+
9
+ export class UpdateDTO {
10
+ @IsString({ message: 'The name must be a string' })
11
+ @IsOptional()
12
+ name?: string;
13
+
14
+ @IsEmail({}, { message: 'The email must be a valid email' })
15
+ @IsOptional()
16
+ email?: string;
17
+
18
+ @IsOptional()
19
+ @IsStrongPassword(
20
+ {
21
+ minLength: 6,
22
+ minLowercase: 1,
23
+ minUppercase: 0,
24
+ minNumbers: 0,
25
+ minSymbols: 0,
26
+ },
27
+ {
28
+ message:
29
+ 'The password must be at least 6 characters long and contain at least one lowercase',
30
+ },
31
+ )
32
+ password?: string;
33
+
34
+ @IsOptional()
35
+ @IsInt({ message: 'The multifactor_id must be an integer' })
36
+ multifactor_id?: number;
37
+
38
+ @IsOptional()
39
+ @IsString({ message: 'The code must be a string' })
40
+ code?: string;
41
+ }
@@ -0,0 +1,75 @@
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
+ forwardRef,
13
+ } from '@nestjs/common';
14
+ import { DeleteDTO } from '../dto/delete.dto';
15
+ import { UpdateIdsDTO } from '../dto/update-ids.dto';
16
+ import { Role } from '../role/decorators/role.decorator';
17
+ import { CreateDTO } from './dto/create.dto';
18
+ import { UpdateDTO } from './dto/update.dto';
19
+ import { UserService } from './user.service';
20
+
21
+ @Role()
22
+ @Controller('user')
23
+ export class UserController {
24
+ constructor(
25
+ @Inject(forwardRef(() => UserService))
26
+ private readonly userService: UserService,
27
+ ) {}
28
+
29
+ @Get()
30
+ async list(@Pagination() paginationParams) {
31
+ return this.userService.list(paginationParams);
32
+ }
33
+
34
+ @Get(':userId/role')
35
+ async listRoles(
36
+ @Param('userId', ParseIntPipe) userId: number,
37
+ @Pagination() paginationParams,
38
+ ) {
39
+ return this.userService.listRoles(userId, paginationParams);
40
+ }
41
+
42
+ @Patch(':userId/role')
43
+ async updateRoles(
44
+ @Param('userId', ParseIntPipe) userId: number,
45
+ @Body() data: UpdateIdsDTO,
46
+ ) {
47
+ return this.userService.updateRoles(userId, data);
48
+ }
49
+
50
+ @Get(':userId')
51
+ async get(@Param('userId', ParseIntPipe) userId: number) {
52
+ return this.userService.get(userId);
53
+ }
54
+
55
+ @Post()
56
+ create(@Body() data: CreateDTO) {
57
+ return this.userService.create(data);
58
+ }
59
+
60
+ @Patch(':userId')
61
+ async update(
62
+ @Param('userId', ParseIntPipe) userId: number,
63
+ @Body() data: UpdateDTO,
64
+ ) {
65
+ return this.userService.update({
66
+ id: userId,
67
+ data,
68
+ });
69
+ }
70
+
71
+ @Delete()
72
+ async delete(@Body() data: DeleteDTO) {
73
+ return this.userService.delete(data);
74
+ }
75
+ }
@@ -0,0 +1,18 @@
1
+ import { PaginationModule } from '@hedhog/pagination';
2
+ import { PrismaModule } from '@hedhog/prisma';
3
+ import { Module, forwardRef } from '@nestjs/common';
4
+ import { UserController } from './user.controller';
5
+ import { UserService } from './user.service';
6
+ import { AuthModule } from '../auth/auth.module';
7
+
8
+ @Module({
9
+ providers: [UserService],
10
+ exports: [UserService],
11
+ controllers: [UserController],
12
+ imports: [
13
+ forwardRef(() => AuthModule),
14
+ forwardRef(() => PrismaModule),
15
+ forwardRef(() => PaginationModule),
16
+ ],
17
+ })
18
+ export class UserModule {}