@hed-hog/lms 0.0.364 → 0.0.365

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 (218) hide show
  1. package/dist/bitcode-wallet/bitcode-wallet.service.d.ts +1 -0
  2. package/dist/bitcode-wallet/bitcode-wallet.service.d.ts.map +1 -1
  3. package/dist/bitcode-wallet/bitcode-wallet.service.js +22 -3
  4. package/dist/bitcode-wallet/bitcode-wallet.service.js.map +1 -1
  5. package/dist/course/course-export-scorm12-worker.service.d.ts +21 -0
  6. package/dist/course/course-export-scorm12-worker.service.d.ts.map +1 -0
  7. package/dist/course/course-export-scorm12-worker.service.js +109 -0
  8. package/dist/course/course-export-scorm12-worker.service.js.map +1 -0
  9. package/dist/course/course-export-scorm12.service.d.ts +42 -0
  10. package/dist/course/course-export-scorm12.service.d.ts.map +1 -0
  11. package/dist/course/course-export-scorm12.service.js +628 -0
  12. package/dist/course/course-export-scorm12.service.js.map +1 -0
  13. package/dist/course/course-export.service.d.ts +84 -0
  14. package/dist/course/course-export.service.d.ts.map +1 -0
  15. package/dist/course/course-export.service.js +237 -0
  16. package/dist/course/course-export.service.js.map +1 -0
  17. package/dist/course/course-structure.controller.d.ts +17 -9
  18. package/dist/course/course-structure.controller.d.ts.map +1 -1
  19. package/dist/course/course-structure.controller.js +17 -4
  20. package/dist/course/course-structure.controller.js.map +1 -1
  21. package/dist/course/course-structure.service.d.ts +12 -4
  22. package/dist/course/course-structure.service.d.ts.map +1 -1
  23. package/dist/course/course-structure.service.js +98 -23
  24. package/dist/course/course-structure.service.js.map +1 -1
  25. package/dist/course/course-video-hls.service.d.ts +57 -0
  26. package/dist/course/course-video-hls.service.d.ts.map +1 -0
  27. package/dist/course/course-video-hls.service.js +767 -0
  28. package/dist/course/course-video-hls.service.js.map +1 -0
  29. package/dist/course/course.controller.d.ts +45 -13
  30. package/dist/course/course.controller.d.ts.map +1 -1
  31. package/dist/course/course.controller.js +40 -26
  32. package/dist/course/course.controller.js.map +1 -1
  33. package/dist/course/course.mcp-tools.js +1 -1
  34. package/dist/course/course.mcp-tools.js.map +1 -1
  35. package/dist/course/course.module.d.ts.map +1 -1
  36. package/dist/course/course.module.js +11 -0
  37. package/dist/course/course.module.js.map +1 -1
  38. package/dist/course/course.service.d.ts +6 -9
  39. package/dist/course/course.service.d.ts.map +1 -1
  40. package/dist/course/course.service.js +57 -48
  41. package/dist/course/course.service.js.map +1 -1
  42. package/dist/course/dto/cleanup-course-storage.dto.d.ts +1 -1
  43. package/dist/course/dto/cleanup-course-storage.dto.d.ts.map +1 -1
  44. package/dist/course/dto/cleanup-course-storage.dto.js +1 -0
  45. package/dist/course/dto/cleanup-course-storage.dto.js.map +1 -1
  46. package/dist/course/dto/cleanup-upload-history.dto.d.ts +1 -1
  47. package/dist/course/dto/cleanup-upload-history.dto.d.ts.map +1 -1
  48. package/dist/course/dto/cleanup-upload-history.dto.js +1 -1
  49. package/dist/course/dto/cleanup-upload-history.dto.js.map +1 -1
  50. package/dist/course/dto/create-course-bulk-job.dto.d.ts +2 -1
  51. package/dist/course/dto/create-course-bulk-job.dto.d.ts.map +1 -1
  52. package/dist/course/dto/create-course-bulk-job.dto.js +6 -1
  53. package/dist/course/dto/create-course-bulk-job.dto.js.map +1 -1
  54. package/dist/course/dto/create-course-export.dto.d.ts +14 -0
  55. package/dist/course/dto/create-course-export.dto.d.ts.map +1 -0
  56. package/dist/course/dto/create-course-export.dto.js +71 -0
  57. package/dist/course/dto/create-course-export.dto.js.map +1 -0
  58. package/dist/course/dto/create-course-structure-lesson.dto.d.ts +2 -2
  59. package/dist/course/dto/create-course-structure-lesson.dto.d.ts.map +1 -1
  60. package/dist/course/dto/create-course-structure-lesson.dto.js +3 -2
  61. package/dist/course/dto/create-course-structure-lesson.dto.js.map +1 -1
  62. package/dist/course/lms-bulk-upload-automation.service.d.ts +16 -1
  63. package/dist/course/lms-bulk-upload-automation.service.d.ts.map +1 -1
  64. package/dist/course/lms-bulk-upload-automation.service.js +102 -8
  65. package/dist/course/lms-bulk-upload-automation.service.js.map +1 -1
  66. package/dist/course/lms-bulk-upload-infra.service.d.ts +1 -0
  67. package/dist/course/lms-bulk-upload-infra.service.d.ts.map +1 -1
  68. package/dist/course/lms-bulk-upload-infra.service.js +32 -8
  69. package/dist/course/lms-bulk-upload-infra.service.js.map +1 -1
  70. package/dist/course/lms-bulk-upload.controller.d.ts +30 -3
  71. package/dist/course/lms-bulk-upload.controller.d.ts.map +1 -1
  72. package/dist/course/lms-bulk-upload.controller.js +43 -2
  73. package/dist/course/lms-bulk-upload.controller.js.map +1 -1
  74. package/dist/course/lms-bulk-upload.service.d.ts +11 -0
  75. package/dist/course/lms-bulk-upload.service.d.ts.map +1 -1
  76. package/dist/course/lms-bulk-upload.service.js +59 -6
  77. package/dist/course/lms-bulk-upload.service.js.map +1 -1
  78. package/dist/course/lms-setting.controller.d.ts +2 -1
  79. package/dist/course/lms-setting.controller.d.ts.map +1 -1
  80. package/dist/course/lms-setting.controller.js +4 -2
  81. package/dist/course/lms-setting.controller.js.map +1 -1
  82. package/dist/course/scorm12-schemas.d.ts +4 -0
  83. package/dist/course/scorm12-schemas.d.ts.map +1 -0
  84. package/dist/course/scorm12-schemas.js +9 -0
  85. package/dist/course/scorm12-schemas.js.map +1 -0
  86. package/dist/enterprise/training/training-student.service.d.ts +51 -0
  87. package/dist/enterprise/training/training-student.service.d.ts.map +1 -1
  88. package/dist/enterprise/training/training-student.service.js +217 -4
  89. package/dist/enterprise/training/training-student.service.js.map +1 -1
  90. package/dist/evaluation/evaluation.service.d.ts +18 -0
  91. package/dist/evaluation/evaluation.service.d.ts.map +1 -1
  92. package/dist/evaluation/evaluation.service.js +125 -0
  93. package/dist/evaluation/evaluation.service.js.map +1 -1
  94. package/dist/exam/dto/create-standalone-question.dto.d.ts +12 -0
  95. package/dist/exam/dto/create-standalone-question.dto.d.ts.map +1 -0
  96. package/dist/exam/dto/create-standalone-question.dto.js +70 -0
  97. package/dist/exam/dto/create-standalone-question.dto.js.map +1 -0
  98. package/dist/exam/exam.module.d.ts.map +1 -1
  99. package/dist/exam/exam.module.js +2 -1
  100. package/dist/exam/exam.module.js.map +1 -1
  101. package/dist/exam/exam.service.d.ts +21 -0
  102. package/dist/exam/exam.service.d.ts.map +1 -1
  103. package/dist/exam/exam.service.js +80 -0
  104. package/dist/exam/exam.service.js.map +1 -1
  105. package/dist/exam/question.controller.d.ts +27 -0
  106. package/dist/exam/question.controller.d.ts.map +1 -0
  107. package/dist/exam/question.controller.js +53 -0
  108. package/dist/exam/question.controller.js.map +1 -0
  109. package/dist/lesson-xp-map/lesson-xp-ai-calculation.service.d.ts +4 -0
  110. package/dist/lesson-xp-map/lesson-xp-ai-calculation.service.d.ts.map +1 -1
  111. package/dist/lesson-xp-map/lesson-xp-ai-calculation.service.js +161 -25
  112. package/dist/lesson-xp-map/lesson-xp-ai-calculation.service.js.map +1 -1
  113. package/dist/libraries/lms/tsconfig.tsbuildinfo +1 -1
  114. package/dist/lms-commerce-access.subscriber.d.ts +11 -0
  115. package/dist/lms-commerce-access.subscriber.d.ts.map +1 -0
  116. package/dist/lms-commerce-access.subscriber.js +74 -0
  117. package/dist/lms-commerce-access.subscriber.js.map +1 -0
  118. package/dist/lms.module.d.ts.map +1 -1
  119. package/dist/lms.module.js +6 -5
  120. package/dist/lms.module.js.map +1 -1
  121. package/dist/platforma/platforma-video.service.d.ts +39 -0
  122. package/dist/platforma/platforma-video.service.d.ts.map +1 -0
  123. package/dist/platforma/platforma-video.service.js +301 -0
  124. package/dist/platforma/platforma-video.service.js.map +1 -0
  125. package/dist/platforma/platforma.controller.d.ts +95 -1
  126. package/dist/platforma/platforma.controller.d.ts.map +1 -1
  127. package/dist/platforma/platforma.controller.js +160 -2
  128. package/dist/platforma/platforma.controller.js.map +1 -1
  129. package/dist/student-xp/dto/grant-skill-card-xp.dto.d.ts +5 -0
  130. package/dist/student-xp/dto/grant-skill-card-xp.dto.d.ts.map +1 -0
  131. package/dist/student-xp/dto/grant-skill-card-xp.dto.js +26 -0
  132. package/dist/student-xp/dto/grant-skill-card-xp.dto.js.map +1 -0
  133. package/dist/student-xp/student-xp.controller.d.ts +15 -0
  134. package/dist/student-xp/student-xp.controller.d.ts.map +1 -1
  135. package/dist/student-xp/student-xp.controller.js +24 -0
  136. package/dist/student-xp/student-xp.controller.js.map +1 -1
  137. package/dist/student-xp/student-xp.service.d.ts +16 -0
  138. package/dist/student-xp/student-xp.service.d.ts.map +1 -1
  139. package/dist/student-xp/student-xp.service.js +51 -1
  140. package/dist/student-xp/student-xp.service.js.map +1 -1
  141. package/hedhog/data/evaluation_topic.yaml +17 -0
  142. package/hedhog/data/menu.yaml +0 -17
  143. package/hedhog/data/queue_definition.yaml +48 -0
  144. package/hedhog/data/route.yaml +94 -124
  145. package/hedhog/data/setting_group.yaml +19 -19
  146. package/hedhog/frontend/app/bulk-upload-sessions/page.tsx.ejs +337 -41
  147. package/hedhog/frontend/app/courses/[id]/page.tsx.ejs +69 -4
  148. package/hedhog/frontend/app/courses/[id]/structure/_components/course-export-sheet.tsx.ejs +420 -0
  149. package/hedhog/frontend/app/courses/[id]/structure/_components/course-exports-tab.tsx.ejs +308 -0
  150. package/hedhog/frontend/app/courses/[id]/structure/_components/course-overview-tab.tsx.ejs +17 -15
  151. package/hedhog/frontend/app/courses/[id]/structure/_components/detail-lesson.tsx.ejs +51 -63
  152. package/hedhog/frontend/app/courses/[id]/structure/_components/detail-panel.tsx.ejs +8 -3
  153. package/hedhog/frontend/app/courses/[id]/structure/_components/detail-session.tsx.ejs +31 -8
  154. package/hedhog/frontend/app/courses/[id]/structure/_components/drag-overlay.tsx.ejs +16 -9
  155. package/hedhog/frontend/app/courses/[id]/structure/_components/editor-course.tsx.ejs +201 -401
  156. package/hedhog/frontend/app/courses/[id]/structure/_components/editor-lesson.tsx.ejs +378 -690
  157. package/hedhog/frontend/app/courses/[id]/structure/_components/mock-data.ts.ejs +1 -2
  158. package/hedhog/frontend/app/courses/[id]/structure/_components/tree-row-lesson.tsx.ejs +3 -9
  159. package/hedhog/frontend/app/courses/[id]/structure/_components/types.ts.ejs +1 -1
  160. package/hedhog/frontend/app/courses/[id]/structure/_data/adapters/course-structure.adapter.ts.ejs +6 -10
  161. package/hedhog/frontend/app/courses/[id]/structure/_data/services/course-structure.service.ts.ejs +49 -0
  162. package/hedhog/frontend/app/courses/[id]/structure/_data/types/api-course.types.ts.ejs +4 -3
  163. package/hedhog/frontend/app/courses/[id]/structure/_data/use-course-content-overview.ts.ejs +0 -1
  164. package/hedhog/frontend/app/courses/[id]/structure/_data/use-course-exports.ts.ejs +106 -0
  165. package/hedhog/frontend/app/courses/[id]/structure/_data/use-course-structure-mutations.ts.ejs +28 -1
  166. package/hedhog/frontend/app/courses/[id]/structure/_data/use-lms-settings-query.ts.ejs +0 -2
  167. package/hedhog/frontend/app/courses/page.tsx.ejs +45 -0
  168. package/hedhog/frontend/messages/en.json +26 -28
  169. package/hedhog/frontend/messages/pt.json +26 -28
  170. package/hedhog/table/course_export.yaml +62 -0
  171. package/package.json +13 -9
  172. package/src/bitcode-wallet/bitcode-wallet.service.ts +43 -4
  173. package/src/course/course-export-scorm12-worker.service.ts +124 -0
  174. package/src/course/course-export-scorm12.service.ts +668 -0
  175. package/src/course/course-export.service.ts +280 -0
  176. package/src/course/course-structure.controller.ts +14 -2
  177. package/src/course/course-structure.service.ts +100 -7
  178. package/src/course/course-video-hls.service.ts +946 -0
  179. package/src/course/course.controller.ts +33 -19
  180. package/src/course/course.mcp-tools.ts +1 -1
  181. package/src/course/course.module.ts +11 -0
  182. package/src/course/course.service.ts +73 -60
  183. package/src/course/dto/cleanup-course-storage.dto.ts +1 -0
  184. package/src/course/dto/cleanup-upload-history.dto.ts +1 -1
  185. package/src/course/dto/create-course-bulk-job.dto.ts +7 -3
  186. package/src/course/dto/create-course-export.dto.ts +56 -0
  187. package/src/course/dto/create-course-structure-lesson.dto.ts +4 -3
  188. package/src/course/lms-bulk-upload-automation.service.ts +153 -6
  189. package/src/course/lms-bulk-upload-infra.service.ts +39 -6
  190. package/src/course/lms-bulk-upload.controller.ts +32 -2
  191. package/src/course/lms-bulk-upload.service.ts +70 -7
  192. package/src/course/lms-setting.controller.ts +4 -2
  193. package/src/course/scorm12-schemas.ts +9 -0
  194. package/src/enterprise/training/training-student.service.ts +221 -2
  195. package/src/evaluation/evaluation.service.ts +123 -0
  196. package/src/exam/dto/create-standalone-question.dto.ts +66 -0
  197. package/src/exam/exam.module.ts +2 -1
  198. package/src/exam/exam.service.ts +86 -0
  199. package/src/exam/question.controller.ts +28 -0
  200. package/src/lesson-xp-map/lesson-xp-ai-calculation.service.ts +205 -31
  201. package/src/lms-commerce-access.subscriber.ts +88 -0
  202. package/src/lms.module.ts +6 -5
  203. package/src/platforma/platforma-video.service.ts +346 -0
  204. package/src/platforma/platforma.controller.ts +95 -1
  205. package/src/platforma/platforma.service.ts +268 -268
  206. package/src/student-xp/dto/grant-skill-card-xp.dto.ts +10 -0
  207. package/src/student-xp/student-xp.controller.ts +18 -2
  208. package/src/student-xp/student-xp.service.ts +84 -2
  209. package/hedhog/data/video_resolution_profile.yaml +0 -7
  210. package/hedhog/frontend/app/video-resolution-profiles/page.tsx.ejs +0 -607
  211. package/hedhog/table/course_video_resolution_profile.yaml +0 -22
  212. package/hedhog/table/video_resolution_profile.yaml +0 -18
  213. package/src/video-resolution-profile/dto/create-video-resolution-profile.dto.ts +0 -16
  214. package/src/video-resolution-profile/dto/update-video-resolution-profile.dto.ts +0 -16
  215. package/src/video-resolution-profile/video-resolution-profile.controller.ts +0 -62
  216. package/src/video-resolution-profile/video-resolution-profile.mcp-tools.ts +0 -128
  217. package/src/video-resolution-profile/video-resolution-profile.module.ts +0 -13
  218. package/src/video-resolution-profile/video-resolution-profile.service.ts +0 -117
@@ -1,268 +1,268 @@
1
- import { PrismaService } from '@hed-hog/api-prisma';
2
- import { Injectable } from '@nestjs/common';
3
- import { UpdatePlatformaProfileDto } from './dto/update-profile.dto';
4
-
5
- function mapGenderToDb(
6
- gender?: string | null,
7
- ): 'male' | 'female' | 'other' | null {
8
- if (!gender) return null;
9
- if (gender === 'Masculino') return 'male';
10
- if (gender === 'Feminino') return 'female';
11
- return 'other';
12
- }
13
-
14
- function mapGenderToDisplay(gender?: string | null): string | null {
15
- if (!gender) return null;
16
- if (gender === 'male') return 'Masculino';
17
- if (gender === 'female') return 'Feminino';
18
- return 'Prefiro nao dizer';
19
- }
20
-
21
- function buildLine1(street?: string | null, number?: string | null): string {
22
- if (!street && !number) return '';
23
- if (!number) return street ?? '';
24
- if (!street) return number;
25
- return `${street}, ${number}`;
26
- }
27
-
28
- function buildLine2(
29
- neighborhood?: string | null,
30
- complement?: string | null,
31
- ): string {
32
- const parts = [neighborhood, complement].filter(Boolean);
33
- return parts.join('; ');
34
- }
35
-
36
- function parseAddressLine1(line1?: string | null): [string, string] {
37
- if (!line1) return ['', ''];
38
- const idx = line1.lastIndexOf(', ');
39
- if (idx === -1) return [line1, ''];
40
- return [line1.slice(0, idx), line1.slice(idx + 2)];
41
- }
42
-
43
- function parseAddressLine2(line2?: string | null): [string, string] {
44
- if (!line2) return ['', ''];
45
- const idx = line2.indexOf('; ');
46
- if (idx === -1) return [line2, ''];
47
- return [line2.slice(0, idx), line2.slice(idx + 2)];
48
- }
49
-
50
- @Injectable()
51
- export class PlatformaService {
52
- constructor(private readonly prisma: PrismaService) {}
53
-
54
- async getProfile(userId: number) {
55
- const [user, personUser] = await Promise.all([
56
- this.prisma.user.findUnique({ where: { id: userId } }),
57
- this.prisma.person_user.findFirst({
58
- where: { user_id: userId },
59
- select: { person_id: true },
60
- }),
61
- ]);
62
-
63
- const personId = personUser?.person_id;
64
-
65
- const [individual, contacts, documents, personAddresses] =
66
- await Promise.all([
67
- personId
68
- ? this.prisma.person_individual.findUnique({
69
- where: { id: personId },
70
- })
71
- : Promise.resolve(null),
72
- personId
73
- ? this.prisma.contact.findMany({
74
- where: { person_id: personId },
75
- include: { contact_type: true },
76
- })
77
- : Promise.resolve([]),
78
- personId
79
- ? this.prisma.document.findMany({
80
- where: { person_id: personId },
81
- include: { document_type: true },
82
- })
83
- : Promise.resolve([]),
84
- personId
85
- ? this.prisma.person_address.findMany({
86
- where: { person_id: personId },
87
- include: { address: true },
88
- orderBy: { address: { is_primary: 'desc' } },
89
- })
90
- : Promise.resolve([]),
91
- ]);
92
-
93
- const phoneContact = contacts.find((c) => c.contact_type.code === 'PHONE');
94
- const cpfDoc = documents.find((d) => d.document_type.code === 'CPF');
95
- const primaryAddress =
96
- personAddresses.find((pa) => pa.address.is_primary)?.address ??
97
- personAddresses[0]?.address;
98
-
99
- const [street, number] = parseAddressLine1(primaryAddress?.line1);
100
- const [neighborhood, complement] = parseAddressLine2(primaryAddress?.line2);
101
-
102
- return {
103
- id: user?.id,
104
- name: user?.name ?? null,
105
- cpf: cpfDoc?.value ?? null,
106
- dob: individual?.birth_date
107
- ? individual.birth_date.toISOString().slice(0, 10)
108
- : null,
109
- gender: mapGenderToDisplay(individual?.gender as string | null),
110
- country: primaryAddress?.country_code ?? null,
111
- city: primaryAddress?.city ?? null,
112
- state: primaryAddress?.state ?? null,
113
- neighborhood,
114
- street,
115
- number,
116
- complement,
117
- zip: primaryAddress?.postal_code ?? null,
118
- phone: phoneContact?.value ?? null,
119
- profession: individual?.job_title ?? null,
120
- };
121
- }
122
-
123
- async updateProfile(userId: number, dto: UpdatePlatformaProfileDto) {
124
- await this.prisma.$transaction(async (tx) => {
125
- if (dto.name) {
126
- await tx.user.update({
127
- where: { id: userId },
128
- data: { name: dto.name },
129
- });
130
- }
131
-
132
- let personUser = await tx.person_user.findFirst({
133
- where: { user_id: userId },
134
- });
135
- let personId: number;
136
-
137
- if (!personUser) {
138
- const user = await tx.user.findUnique({ where: { id: userId } });
139
- const newPerson = await tx.person.create({
140
- data: {
141
- name: dto.name ?? user?.name ?? '',
142
- type: 'individual',
143
- status: 'active',
144
- } as any,
145
- });
146
- await tx.person_user.create({
147
- data: { person_id: newPerson.id, user_id: userId },
148
- });
149
- personId = newPerson.id;
150
- } else {
151
- personId = personUser.person_id;
152
- if (dto.name) {
153
- await tx.person.update({
154
- where: { id: personId },
155
- data: { name: dto.name },
156
- });
157
- }
158
- }
159
-
160
- const individualData: Record<string, unknown> = {};
161
- if (dto.dob !== undefined) {
162
- individualData.birth_date = dto.dob ? new Date(dto.dob) : null;
163
- }
164
- if (dto.gender !== undefined) {
165
- individualData.gender = mapGenderToDb(dto.gender);
166
- }
167
- if (dto.profession !== undefined) {
168
- individualData.job_title = dto.profession || null;
169
- }
170
-
171
- await (tx as any).person_individual.upsert({
172
- where: { id: personId },
173
- create: { id: personId, ...individualData },
174
- update: individualData,
175
- });
176
-
177
- const phoneType = await tx.contact_type.findFirst({
178
- where: { code: 'PHONE' },
179
- });
180
- if (phoneType && dto.phone !== undefined) {
181
- const existingPhone = await tx.contact.findFirst({
182
- where: { person_id: personId, contact_type_id: phoneType.id },
183
- });
184
- if (dto.phone) {
185
- if (existingPhone) {
186
- await tx.contact.update({
187
- where: { id: existingPhone.id },
188
- data: { value: dto.phone },
189
- });
190
- } else {
191
- await tx.contact.create({
192
- data: {
193
- person_id: personId,
194
- contact_type_id: phoneType.id,
195
- value: dto.phone,
196
- is_primary: true,
197
- },
198
- });
199
- }
200
- } else if (existingPhone) {
201
- await tx.contact.delete({ where: { id: existingPhone.id } });
202
- }
203
- }
204
-
205
- const cpfType = await tx.document_type.findFirst({
206
- where: { code: 'CPF' },
207
- });
208
- if (cpfType && dto.cpf !== undefined) {
209
- const existingCpf = await tx.document.findFirst({
210
- where: { person_id: personId, document_type_id: cpfType.id },
211
- });
212
- if (dto.cpf) {
213
- if (existingCpf) {
214
- await tx.document.update({
215
- where: { id: existingCpf.id },
216
- data: { value: dto.cpf },
217
- });
218
- } else {
219
- await tx.document.create({
220
- data: {
221
- person_id: personId,
222
- document_type_id: cpfType.id,
223
- value: dto.cpf,
224
- },
225
- });
226
- }
227
- } else if (existingCpf) {
228
- await tx.document.delete({ where: { id: existingCpf.id } });
229
- }
230
- }
231
-
232
- const hasAddressData =
233
- dto.street || dto.city || dto.state || dto.zip || dto.country;
234
- if (hasAddressData) {
235
- const existingPersonAddress = await tx.person_address.findFirst({
236
- where: { person_id: personId },
237
- include: { address: true },
238
- orderBy: { address: { is_primary: 'desc' } },
239
- });
240
-
241
- const addressData = {
242
- country_code: dto.country ?? '',
243
- state: dto.state ?? '',
244
- city: dto.city ?? '',
245
- postal_code: (dto.zip ?? '').replace(/\D/g, ''),
246
- line1: buildLine1(dto.street, dto.number),
247
- line2: buildLine2(dto.neighborhood, dto.complement),
248
- is_primary: true,
249
- address_type: 'residential' as const,
250
- };
251
-
252
- if (existingPersonAddress) {
253
- await tx.address.update({
254
- where: { id: existingPersonAddress.address_id },
255
- data: addressData,
256
- });
257
- } else {
258
- const newAddress = await tx.address.create({ data: addressData });
259
- await tx.person_address.create({
260
- data: { person_id: personId, address_id: newAddress.id },
261
- });
262
- }
263
- }
264
- });
265
-
266
- return { success: true };
267
- }
268
- }
1
+ import { PrismaService } from '@hed-hog/api-prisma';
2
+ import { Injectable } from '@nestjs/common';
3
+ import { UpdatePlatformaProfileDto } from './dto/update-profile.dto';
4
+
5
+ function mapGenderToDb(
6
+ gender?: string | null,
7
+ ): 'male' | 'female' | 'other' | null {
8
+ if (!gender) return null;
9
+ if (gender === 'Masculino') return 'male';
10
+ if (gender === 'Feminino') return 'female';
11
+ return 'other';
12
+ }
13
+
14
+ function mapGenderToDisplay(gender?: string | null): string | null {
15
+ if (!gender) return null;
16
+ if (gender === 'male') return 'Masculino';
17
+ if (gender === 'female') return 'Feminino';
18
+ return 'Prefiro nao dizer';
19
+ }
20
+
21
+ function buildLine1(street?: string | null, number?: string | null): string {
22
+ if (!street && !number) return '';
23
+ if (!number) return street ?? '';
24
+ if (!street) return number;
25
+ return `${street}, ${number}`;
26
+ }
27
+
28
+ function buildLine2(
29
+ neighborhood?: string | null,
30
+ complement?: string | null,
31
+ ): string {
32
+ const parts = [neighborhood, complement].filter(Boolean);
33
+ return parts.join('; ');
34
+ }
35
+
36
+ function parseAddressLine1(line1?: string | null): [string, string] {
37
+ if (!line1) return ['', ''];
38
+ const idx = line1.lastIndexOf(', ');
39
+ if (idx === -1) return [line1, ''];
40
+ return [line1.slice(0, idx), line1.slice(idx + 2)];
41
+ }
42
+
43
+ function parseAddressLine2(line2?: string | null): [string, string] {
44
+ if (!line2) return ['', ''];
45
+ const idx = line2.indexOf('; ');
46
+ if (idx === -1) return [line2, ''];
47
+ return [line2.slice(0, idx), line2.slice(idx + 2)];
48
+ }
49
+
50
+ @Injectable()
51
+ export class PlatformaService {
52
+ constructor(private readonly prisma: PrismaService) {}
53
+
54
+ async getProfile(userId: number) {
55
+ const [user, personUser] = await Promise.all([
56
+ this.prisma.user.findUnique({ where: { id: userId } }),
57
+ this.prisma.person_user.findFirst({
58
+ where: { user_id: userId },
59
+ select: { person_id: true },
60
+ }),
61
+ ]);
62
+
63
+ const personId = personUser?.person_id;
64
+
65
+ const [individual, contacts, documents, personAddresses] =
66
+ await Promise.all([
67
+ personId
68
+ ? this.prisma.person_individual.findUnique({
69
+ where: { id: personId },
70
+ })
71
+ : Promise.resolve(null),
72
+ personId
73
+ ? this.prisma.contact.findMany({
74
+ where: { person_id: personId },
75
+ include: { contact_type: true },
76
+ })
77
+ : Promise.resolve([]),
78
+ personId
79
+ ? this.prisma.document.findMany({
80
+ where: { person_id: personId },
81
+ include: { document_type: true },
82
+ })
83
+ : Promise.resolve([]),
84
+ personId
85
+ ? this.prisma.person_address.findMany({
86
+ where: { person_id: personId },
87
+ include: { address: true },
88
+ orderBy: { address: { is_primary: 'desc' } },
89
+ })
90
+ : Promise.resolve([]),
91
+ ]);
92
+
93
+ const phoneContact = contacts.find((c) => c.contact_type.code === 'PHONE');
94
+ const cpfDoc = documents.find((d) => d.document_type.code === 'CPF');
95
+ const primaryAddress =
96
+ personAddresses.find((pa) => pa.address.is_primary)?.address ??
97
+ personAddresses[0]?.address;
98
+
99
+ const [street, number] = parseAddressLine1(primaryAddress?.line1);
100
+ const [neighborhood, complement] = parseAddressLine2(primaryAddress?.line2);
101
+
102
+ return {
103
+ id: user?.id,
104
+ name: user?.name ?? null,
105
+ cpf: cpfDoc?.value ?? null,
106
+ dob: individual?.birth_date
107
+ ? individual.birth_date.toISOString().slice(0, 10)
108
+ : null,
109
+ gender: mapGenderToDisplay(individual?.gender as string | null),
110
+ country: primaryAddress?.country_code ?? null,
111
+ city: primaryAddress?.city ?? null,
112
+ state: primaryAddress?.state ?? null,
113
+ neighborhood,
114
+ street,
115
+ number,
116
+ complement,
117
+ zip: primaryAddress?.postal_code ?? null,
118
+ phone: phoneContact?.value ?? null,
119
+ profession: individual?.job_title ?? null,
120
+ };
121
+ }
122
+
123
+ async updateProfile(userId: number, dto: UpdatePlatformaProfileDto) {
124
+ await this.prisma.$transaction(async (tx) => {
125
+ if (dto.name) {
126
+ await tx.user.update({
127
+ where: { id: userId },
128
+ data: { name: dto.name },
129
+ });
130
+ }
131
+
132
+ let personUser = await tx.person_user.findFirst({
133
+ where: { user_id: userId },
134
+ });
135
+ let personId: number;
136
+
137
+ if (!personUser) {
138
+ const user = await tx.user.findUnique({ where: { id: userId } });
139
+ const newPerson = await tx.person.create({
140
+ data: {
141
+ name: dto.name ?? user?.name ?? '',
142
+ type: 'individual',
143
+ status: 'active',
144
+ } as any,
145
+ });
146
+ await tx.person_user.create({
147
+ data: { person_id: newPerson.id, user_id: userId },
148
+ });
149
+ personId = newPerson.id;
150
+ } else {
151
+ personId = personUser.person_id;
152
+ if (dto.name) {
153
+ await tx.person.update({
154
+ where: { id: personId },
155
+ data: { name: dto.name },
156
+ });
157
+ }
158
+ }
159
+
160
+ const individualData: Record<string, unknown> = {};
161
+ if (dto.dob !== undefined) {
162
+ individualData.birth_date = dto.dob ? new Date(dto.dob) : null;
163
+ }
164
+ if (dto.gender !== undefined) {
165
+ individualData.gender = mapGenderToDb(dto.gender);
166
+ }
167
+ if (dto.profession !== undefined) {
168
+ individualData.job_title = dto.profession || null;
169
+ }
170
+
171
+ await (tx as any).person_individual.upsert({
172
+ where: { id: personId },
173
+ create: { id: personId, ...individualData },
174
+ update: individualData,
175
+ });
176
+
177
+ const phoneType = await tx.contact_type.findFirst({
178
+ where: { code: 'PHONE' },
179
+ });
180
+ if (phoneType && dto.phone !== undefined) {
181
+ const existingPhone = await tx.contact.findFirst({
182
+ where: { person_id: personId, contact_type_id: phoneType.id },
183
+ });
184
+ if (dto.phone) {
185
+ if (existingPhone) {
186
+ await tx.contact.update({
187
+ where: { id: existingPhone.id },
188
+ data: { value: dto.phone },
189
+ });
190
+ } else {
191
+ await tx.contact.create({
192
+ data: {
193
+ person_id: personId,
194
+ contact_type_id: phoneType.id,
195
+ value: dto.phone,
196
+ is_primary: true,
197
+ },
198
+ });
199
+ }
200
+ } else if (existingPhone) {
201
+ await tx.contact.delete({ where: { id: existingPhone.id } });
202
+ }
203
+ }
204
+
205
+ const cpfType = await tx.document_type.findFirst({
206
+ where: { code: 'CPF' },
207
+ });
208
+ if (cpfType && dto.cpf !== undefined) {
209
+ const existingCpf = await tx.document.findFirst({
210
+ where: { person_id: personId, document_type_id: cpfType.id },
211
+ });
212
+ if (dto.cpf) {
213
+ if (existingCpf) {
214
+ await tx.document.update({
215
+ where: { id: existingCpf.id },
216
+ data: { value: dto.cpf },
217
+ });
218
+ } else {
219
+ await tx.document.create({
220
+ data: {
221
+ person_id: personId,
222
+ document_type_id: cpfType.id,
223
+ value: dto.cpf,
224
+ },
225
+ });
226
+ }
227
+ } else if (existingCpf) {
228
+ await tx.document.delete({ where: { id: existingCpf.id } });
229
+ }
230
+ }
231
+
232
+ const hasAddressData =
233
+ dto.street || dto.city || dto.state || dto.zip || dto.country;
234
+ if (hasAddressData) {
235
+ const existingPersonAddress = await tx.person_address.findFirst({
236
+ where: { person_id: personId },
237
+ include: { address: true },
238
+ orderBy: { address: { is_primary: 'desc' } },
239
+ });
240
+
241
+ const addressData = {
242
+ country_code: dto.country ?? '',
243
+ state: dto.state ?? '',
244
+ city: dto.city ?? '',
245
+ postal_code: (dto.zip ?? '').replace(/\D/g, ''),
246
+ line1: buildLine1(dto.street, dto.number),
247
+ line2: buildLine2(dto.neighborhood, dto.complement),
248
+ is_primary: true,
249
+ address_type: 'residential' as const,
250
+ };
251
+
252
+ if (existingPersonAddress) {
253
+ await tx.address.update({
254
+ where: { id: existingPersonAddress.address_id },
255
+ data: addressData,
256
+ });
257
+ } else {
258
+ const newAddress = await tx.address.create({ data: addressData });
259
+ await tx.person_address.create({
260
+ data: { person_id: personId, address_id: newAddress.id },
261
+ });
262
+ }
263
+ }
264
+ });
265
+
266
+ return { success: true };
267
+ }
268
+ }
@@ -0,0 +1,10 @@
1
+ import { IsInt, IsString, Min } from 'class-validator';
2
+
3
+ export class GrantSkillCardXpDto {
4
+ @IsString()
5
+ skillCardSlug: string;
6
+
7
+ @IsInt()
8
+ @Min(1)
9
+ xpEarned: number;
10
+ }
@@ -1,5 +1,6 @@
1
- import { Role } from '@hed-hog/api';
2
- import { Controller, Get, Param, ParseIntPipe, Query } from '@nestjs/common';
1
+ import { NoRole, Role, User } from '@hed-hog/api';
2
+ import { Body, Controller, Get, Param, ParseIntPipe, Post, Query } from '@nestjs/common';
3
+ import { GrantSkillCardXpDto } from './dto/grant-skill-card-xp.dto';
3
4
  import { StudentXpService } from './student-xp.service';
4
5
 
5
6
  @Role()
@@ -73,4 +74,19 @@ export class StudentXpController {
73
74
  getPersonBalanceSummary(@Param('personId', ParseIntPipe) personId: number) {
74
75
  return this.studentXpService.getPersonBalanceSummary(personId);
75
76
  }
77
+
78
+ @Get('my-balance')
79
+ @NoRole()
80
+ getMyBalance(@User('id') userId: number) {
81
+ return this.studentXpService.getMyBalance(userId);
82
+ }
83
+
84
+ @Post('skill-card')
85
+ @NoRole()
86
+ grantSkillCardXp(
87
+ @User('id') userId: number,
88
+ @Body() dto: GrantSkillCardXpDto,
89
+ ) {
90
+ return this.studentXpService.grantSkillCardXp(userId, dto);
91
+ }
76
92
  }
@@ -1,5 +1,10 @@
1
1
  import { PrismaService } from '@hed-hog/api-prisma';
2
- import { Injectable } from '@nestjs/common';
2
+ import {
3
+ BadRequestException,
4
+ Injectable,
5
+ NotFoundException,
6
+ } from '@nestjs/common';
7
+ import { GrantSkillCardXpDto } from './dto/grant-skill-card-xp.dto';
3
8
 
4
9
  @Injectable()
5
10
  export class StudentXpService {
@@ -206,7 +211,11 @@ export class StudentXpService {
206
211
  [personId],
207
212
  ),
208
213
  this.querySingle<{ total: string }>(
209
- `SELECT COALESCE(SUM(total_xp), 0)::text AS total FROM student_area_xp WHERE person_id = $1`,
214
+ `SELECT GREATEST(
215
+ (SELECT COALESCE(SUM(total_xp), 0) FROM student_area_xp WHERE person_id = $1),
216
+ (SELECT COALESCE(SUM(total_xp), 0) FROM student_skill_xp WHERE person_id = $1),
217
+ (SELECT COALESCE(SUM(total_xp), 0) FROM student_learning_type_xp WHERE person_id = $1)
218
+ )::text AS total`,
210
219
  [personId],
211
220
  ),
212
221
  ]);
@@ -233,4 +242,77 @@ export class StudentXpService {
233
242
  const rows = await this.prisma.$queryRawUnsafe<T[]>(query, ...values);
234
243
  return rows[0] ?? null;
235
244
  }
245
+
246
+ private async resolvePersonId(userId: number): Promise<number> {
247
+ const row = await this.querySingle<{ personId: number }>(
248
+ `SELECT person_id AS "personId" FROM person_user WHERE user_id = $1 LIMIT 1`,
249
+ [userId],
250
+ );
251
+
252
+ if (!row) {
253
+ throw new NotFoundException('No person profile found for this user');
254
+ }
255
+
256
+ return Number(row.personId);
257
+ }
258
+
259
+ async getMyBalance(userId: number) {
260
+ const personId = await this.resolvePersonId(userId);
261
+ return this.getPersonBalanceSummary(personId);
262
+ }
263
+
264
+ async grantSkillCardXp(userId: number, dto: GrantSkillCardXpDto) {
265
+ const skillCardSlug = String(dto.skillCardSlug ?? '').trim();
266
+
267
+ if (!skillCardSlug) {
268
+ throw new BadRequestException('Skill card slug is required');
269
+ }
270
+
271
+ const parsedXpEarned = Math.trunc(Number(dto.xpEarned));
272
+ const xpEarned = Number.isFinite(parsedXpEarned)
273
+ ? Math.max(1, parsedXpEarned)
274
+ : 1;
275
+ const personId = await this.resolvePersonId(userId);
276
+
277
+ const skill = await this.querySingle<{ id: number; xpAreaId: number | null }>(
278
+ `SELECT id, xp_area_id AS "xpAreaId" FROM xp_skill WHERE slug = $1 LIMIT 1`,
279
+ [skillCardSlug],
280
+ );
281
+
282
+ if (!skill) {
283
+ throw new NotFoundException('XP skill not found');
284
+ }
285
+
286
+ await this.prisma.$transaction(async (tx) => {
287
+ await tx.$executeRawUnsafe(
288
+ `INSERT INTO student_skill_xp (person_id, xp_skill_id, total_xp, event_count, last_updated_at, created_at, updated_at)
289
+ VALUES ($1, $2, $3, 1, now(), now(), now())
290
+ ON CONFLICT (person_id, xp_skill_id) DO UPDATE
291
+ SET total_xp = student_skill_xp.total_xp + EXCLUDED.total_xp,
292
+ event_count = student_skill_xp.event_count + 1,
293
+ last_updated_at = now(),
294
+ updated_at = now()`,
295
+ personId,
296
+ skill.id,
297
+ xpEarned,
298
+ );
299
+
300
+ if (skill.xpAreaId) {
301
+ await tx.$executeRawUnsafe(
302
+ `INSERT INTO student_area_xp (person_id, xp_area_id, total_xp, event_count, last_updated_at, created_at, updated_at)
303
+ VALUES ($1, $2, $3, 1, now(), now(), now())
304
+ ON CONFLICT (person_id, xp_area_id) DO UPDATE
305
+ SET total_xp = student_area_xp.total_xp + EXCLUDED.total_xp,
306
+ event_count = student_area_xp.event_count + 1,
307
+ last_updated_at = now(),
308
+ updated_at = now()`,
309
+ personId,
310
+ skill.xpAreaId,
311
+ xpEarned,
312
+ );
313
+ }
314
+ });
315
+
316
+ return this.getPersonBalanceSummary(personId);
317
+ }
236
318
  }