@greatapps/common 1.1.234 → 1.1.236

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 (226) hide show
  1. package/dist/components/account/ConfigurationsMyAccountModal.mjs +1 -1
  2. package/dist/components/account/ConfigurationsMyAccountModal.mjs.map +1 -1
  3. package/dist/components/account/ConfirmDeleteAccountModal.mjs.map +1 -1
  4. package/dist/components/account/ConfirmGlobalPreferencesModal.mjs.map +1 -1
  5. package/dist/components/account/DeleteAccountModal.mjs.map +1 -1
  6. package/dist/components/account/DisableTwoFactorAuthModal.mjs.map +1 -1
  7. package/dist/components/account/IsntPossibleDeleteAccountModal.mjs.map +1 -1
  8. package/dist/components/account/TwoFactorAuthModal.mjs.map +1 -1
  9. package/dist/components/account/hooks/useChangePhoneForm.mjs.map +1 -1
  10. package/dist/components/account/hooks/useTwoFactorAuthForm.mjs.map +1 -1
  11. package/dist/components/account/sections/ChangeEmailModal.mjs.map +1 -1
  12. package/dist/components/account/sections/ChangePasswordSection.mjs.map +1 -1
  13. package/dist/components/account/sections/ChangePhoneModal.mjs.map +1 -1
  14. package/dist/components/account/sections/MyProfileSection.mjs.map +1 -1
  15. package/dist/components/account/sections/PreferencesSection.mjs.map +1 -1
  16. package/dist/components/account/sections/SecuritySection.mjs.map +1 -1
  17. package/dist/components/embeds/CrispEmbed.mjs.map +1 -1
  18. package/dist/components/embeds/FrillEmbed.mjs.map +1 -1
  19. package/dist/components/layouts/AppMobileNavBar.mjs.map +1 -1
  20. package/dist/components/layouts/AppNavBar.mjs.map +1 -1
  21. package/dist/components/layouts/MdSideBarNavigation.mjs.map +1 -1
  22. package/dist/components/layouts/NavBar.mjs.map +1 -1
  23. package/dist/components/layouts/NotificationsPopover.mjs.map +1 -1
  24. package/dist/components/layouts/ProfilePopover.mjs.map +1 -1
  25. package/dist/components/layouts/UsersSelectorPopover.mjs.map +1 -1
  26. package/dist/components/navigation/CancelledSubscriptionBanner.mjs.map +1 -1
  27. package/dist/components/navigation/NavItemsContext.mjs.map +1 -1
  28. package/dist/components/navigation/ProjectSelectorPopover.mjs.map +1 -1
  29. package/dist/components/navigation/shells/TabsMobileShell.mjs.map +1 -1
  30. package/dist/components/navigation/subcomponents/ProjectSelector.mjs.map +1 -1
  31. package/dist/components/pages/notifications/Notifications.mjs.map +1 -1
  32. package/dist/components/ui/buttons/Button.mjs.map +1 -1
  33. package/dist/components/ui/buttons/CancelButton.mjs.map +1 -1
  34. package/dist/components/ui/buttons/CopyButton.mjs.map +1 -1
  35. package/dist/components/ui/data-display/Accordion.mjs.map +1 -1
  36. package/dist/components/ui/data-display/Badge.mjs.map +1 -1
  37. package/dist/components/ui/data-display/Card.mjs.map +1 -1
  38. package/dist/components/ui/data-display/Pagination.mjs.map +1 -1
  39. package/dist/components/ui/data-display/ScrollArea.mjs.map +1 -1
  40. package/dist/components/ui/data-display/Separator.mjs.map +1 -1
  41. package/dist/components/ui/data-display/Table.mjs.map +1 -1
  42. package/dist/components/ui/data-display/Tabs.mjs.map +1 -1
  43. package/dist/components/ui/feedback/CircularProgress.mjs.map +1 -1
  44. package/dist/components/ui/feedback/DefaultCircularProgress.mjs.map +1 -1
  45. package/dist/components/ui/feedback/LoadingOverlay.mjs.map +1 -1
  46. package/dist/components/ui/feedback/Progress.mjs.map +1 -1
  47. package/dist/components/ui/feedback/Toast.mjs.map +1 -1
  48. package/dist/components/ui/form/Calendar.mjs.map +1 -1
  49. package/dist/components/ui/form/Checkbox.mjs.map +1 -1
  50. package/dist/components/ui/form/ComboboxField.mjs.map +1 -1
  51. package/dist/components/ui/form/DatePicker.mjs.map +1 -1
  52. package/dist/components/ui/form/DateRangePicker.mjs.map +1 -1
  53. package/dist/components/ui/form/FormField.mjs.map +1 -1
  54. package/dist/components/ui/form/Input.mjs.map +1 -1
  55. package/dist/components/ui/form/InputOtp.mjs.map +1 -1
  56. package/dist/components/ui/form/PhoneInput.mjs.map +1 -1
  57. package/dist/components/ui/form/RadioGroup.mjs.map +1 -1
  58. package/dist/components/ui/form/Select.mjs +1 -1
  59. package/dist/components/ui/form/Select.mjs.map +1 -1
  60. package/dist/components/ui/form/Switch.mjs.map +1 -1
  61. package/dist/components/ui/form/Textarea.mjs.map +1 -1
  62. package/dist/components/ui/overlay/Command.mjs.map +1 -1
  63. package/dist/components/ui/overlay/Dialog.mjs.map +1 -1
  64. package/dist/components/ui/overlay/Popover.mjs.map +1 -1
  65. package/dist/components/ui/overlay/Sheet.mjs.map +1 -1
  66. package/dist/components/ui/overlay/Tooltip.mjs.map +1 -1
  67. package/dist/hooks/copy-to-clipboard.hook.mjs.map +1 -1
  68. package/dist/index.mjs.map +1 -1
  69. package/dist/middleware.mjs.map +1 -1
  70. package/dist/modules/accounts/actions/account-management.action.mjs +1 -0
  71. package/dist/modules/accounts/actions/account-management.action.mjs.map +1 -1
  72. package/dist/modules/accounts/actions/find-current-account.action.mjs.map +1 -1
  73. package/dist/modules/accounts/actions/list-currencies.action.mjs.map +1 -1
  74. package/dist/modules/accounts/actions/list-timezones.action.mjs.map +1 -1
  75. package/dist/modules/accounts/hooks/current-account.hook.mjs.map +1 -1
  76. package/dist/modules/accounts/hooks/useAccountManagement.mjs.map +1 -1
  77. package/dist/modules/accounts/hooks/useCurrencies.mjs.map +1 -1
  78. package/dist/modules/accounts/hooks/useTimezones.mjs.map +1 -1
  79. package/dist/modules/accounts/services/account.service.mjs.map +1 -1
  80. package/dist/modules/auth/hooks/login.hook.mjs.map +1 -1
  81. package/dist/modules/auth/hooks/logout.hook.mjs.map +1 -1
  82. package/dist/modules/auth/hooks/register.hook.mjs.map +1 -1
  83. package/dist/modules/ia-credits/actions/list-ia-credits.action.mjs.map +1 -1
  84. package/dist/modules/projects/actions/add-project-user.action.mjs.map +1 -1
  85. package/dist/modules/projects/actions/list-all-account-users.action.mjs.map +1 -1
  86. package/dist/modules/projects/actions/list-available-users.action.mjs.map +1 -1
  87. package/dist/modules/projects/actions/list-project-users.action.mjs.map +1 -1
  88. package/dist/modules/projects/actions/remove-project-user.action.mjs.map +1 -1
  89. package/dist/modules/projects/hooks/add-project-user.hook.mjs.map +1 -1
  90. package/dist/modules/projects/hooks/list-all-account-users.hook.mjs.map +1 -1
  91. package/dist/modules/projects/hooks/list-available-users.hook.mjs.map +1 -1
  92. package/dist/modules/projects/hooks/list-project-users.hook.mjs.map +1 -1
  93. package/dist/modules/projects/hooks/remove-project-user.hook.mjs.map +1 -1
  94. package/dist/modules/subscriptions/actions/find-plan-by-id.action.mjs.map +1 -1
  95. package/dist/modules/subscriptions/actions/list-subscriptions.action.mjs.map +1 -1
  96. package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs.map +1 -1
  97. package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs.map +1 -1
  98. package/dist/modules/subscriptions/hooks/use-has-plan-addon.hook.mjs.map +1 -1
  99. package/dist/modules/subscriptions/hooks/use-plan-by-id.hook.mjs.map +1 -1
  100. package/dist/modules/subscriptions/services/plans.service.mjs.map +1 -1
  101. package/dist/modules/subscriptions/types/plan.type.mjs.map +1 -1
  102. package/dist/modules/users/action/find-user-by-id.action.mjs.map +1 -1
  103. package/dist/modules/users/action/list-messages.action.mjs.map +1 -1
  104. package/dist/modules/users/action/mark-all-messages-as-read.action.mjs.map +1 -1
  105. package/dist/modules/users/action/mark-message-as-read.action.mjs.map +1 -1
  106. package/dist/modules/users/hooks/messages.hook.mjs.map +1 -1
  107. package/dist/modules/whitelabel/actions/find-whitelabel-styles.action.mjs.map +1 -1
  108. package/dist/modules/whitelabel/actions/find-whitelabel.action.mjs.map +1 -1
  109. package/dist/modules/whitelabel/services/whitelabel-styles.service.mjs.map +1 -1
  110. package/dist/providers/whitelabel.provider.mjs.map +1 -1
  111. package/dist/utils/countries.mjs.map +1 -1
  112. package/dist/utils/safeServerAction.mjs.map +1 -1
  113. package/dist/utils/withAction.mjs.map +1 -1
  114. package/package.json +89 -89
  115. package/src/components/account/ConfigurationsMyAccountModal.tsx +150 -150
  116. package/src/components/account/ConfirmDeleteAccountModal.tsx +55 -55
  117. package/src/components/account/ConfirmGlobalPreferencesModal.tsx +66 -66
  118. package/src/components/account/DeleteAccountModal.tsx +130 -130
  119. package/src/components/account/DisableTwoFactorAuthModal.tsx +86 -86
  120. package/src/components/account/IsntPossibleDeleteAccountModal.tsx +49 -49
  121. package/src/components/account/TwoFactorAuthModal.tsx +223 -223
  122. package/src/components/account/hooks/useChangePhoneForm.tsx +81 -81
  123. package/src/components/account/hooks/useTwoFactorAuthForm.tsx +101 -101
  124. package/src/components/account/sections/ChangeEmailModal.tsx +225 -225
  125. package/src/components/account/sections/ChangePasswordSection.tsx +64 -64
  126. package/src/components/account/sections/ChangePhoneModal.tsx +231 -231
  127. package/src/components/account/sections/MyProfileSection.tsx +327 -327
  128. package/src/components/account/sections/PreferencesSection.tsx +273 -273
  129. package/src/components/account/sections/SecuritySection.tsx +160 -160
  130. package/src/components/embeds/CrispEmbed.tsx +101 -101
  131. package/src/components/embeds/FrillEmbed.tsx +119 -119
  132. package/src/components/layouts/AppMobileNavBar.tsx +110 -110
  133. package/src/components/layouts/AppNavBar.tsx +57 -57
  134. package/src/components/layouts/MdSideBarNavigation.tsx +56 -56
  135. package/src/components/layouts/NavBar.tsx +120 -120
  136. package/src/components/layouts/NotificationsPopover.tsx +115 -115
  137. package/src/components/layouts/ProfilePopover.tsx +167 -167
  138. package/src/components/layouts/UsersSelectorPopover.tsx +238 -238
  139. package/src/components/navigation/CancelledSubscriptionBanner.tsx +69 -69
  140. package/src/components/navigation/NavItemsContext.ts +20 -20
  141. package/src/components/navigation/ProjectSelectorPopover.tsx +157 -157
  142. package/src/components/navigation/shells/TabsMobileShell.tsx +72 -72
  143. package/src/components/navigation/subcomponents/ProjectSelector.tsx +154 -154
  144. package/src/components/pages/notifications/Notifications.tsx +160 -160
  145. package/src/components/ui/buttons/Button.tsx +57 -57
  146. package/src/components/ui/buttons/CancelButton.tsx +14 -14
  147. package/src/components/ui/buttons/CopyButton.tsx +54 -54
  148. package/src/components/ui/data-display/Accordion.tsx +81 -81
  149. package/src/components/ui/data-display/Badge.tsx +47 -47
  150. package/src/components/ui/data-display/Card.tsx +93 -93
  151. package/src/components/ui/data-display/Pagination.tsx +126 -126
  152. package/src/components/ui/data-display/ScrollArea.tsx +59 -59
  153. package/src/components/ui/data-display/Separator.tsx +29 -29
  154. package/src/components/ui/data-display/Table.tsx +117 -117
  155. package/src/components/ui/data-display/Tabs.tsx +67 -67
  156. package/src/components/ui/feedback/CircularProgress.tsx +75 -75
  157. package/src/components/ui/feedback/DefaultCircularProgress.tsx +61 -61
  158. package/src/components/ui/feedback/LoadingOverlay.tsx +31 -31
  159. package/src/components/ui/feedback/Progress.tsx +40 -40
  160. package/src/components/ui/feedback/Toast.tsx +101 -101
  161. package/src/components/ui/form/Calendar.tsx +228 -228
  162. package/src/components/ui/form/Checkbox.tsx +31 -31
  163. package/src/components/ui/form/ComboboxField.tsx +154 -154
  164. package/src/components/ui/form/DatePicker.tsx +97 -97
  165. package/src/components/ui/form/DateRangePicker.tsx +108 -108
  166. package/src/components/ui/form/FormField.tsx +131 -131
  167. package/src/components/ui/form/Input.tsx +20 -20
  168. package/src/components/ui/form/InputOtp.tsx +80 -80
  169. package/src/components/ui/form/PhoneInput.tsx +205 -205
  170. package/src/components/ui/form/RadioGroup.tsx +45 -45
  171. package/src/components/ui/form/Select.tsx +200 -200
  172. package/src/components/ui/form/Switch.tsx +32 -32
  173. package/src/components/ui/form/Textarea.tsx +19 -19
  174. package/src/components/ui/overlay/Command.tsx +185 -185
  175. package/src/components/ui/overlay/Dialog.tsx +151 -151
  176. package/src/components/ui/overlay/Popover.tsx +49 -49
  177. package/src/components/ui/overlay/Sheet.tsx +166 -166
  178. package/src/components/ui/overlay/Tooltip.tsx +62 -62
  179. package/src/hooks/copy-to-clipboard.hook.tsx +67 -67
  180. package/src/index.ts +359 -359
  181. package/src/middleware.ts +4 -4
  182. package/src/modules/accounts/actions/account-management.action.ts +91 -90
  183. package/src/modules/accounts/actions/find-current-account.action.ts +9 -9
  184. package/src/modules/accounts/actions/list-currencies.action.ts +8 -8
  185. package/src/modules/accounts/actions/list-timezones.action.ts +8 -8
  186. package/src/modules/accounts/hooks/current-account.hook.tsx +22 -22
  187. package/src/modules/accounts/hooks/useAccountManagement.ts +85 -85
  188. package/src/modules/accounts/hooks/useCurrencies.ts +15 -15
  189. package/src/modules/accounts/hooks/useTimezones.ts +15 -15
  190. package/src/modules/accounts/services/account.service.ts +236 -236
  191. package/src/modules/accounts/types.ts +214 -214
  192. package/src/modules/auth/hooks/login.hook.tsx +13 -13
  193. package/src/modules/auth/hooks/logout.hook.tsx +11 -11
  194. package/src/modules/auth/hooks/register.hook.tsx +11 -11
  195. package/src/modules/ia-credits/actions/list-ia-credits.action.ts +8 -8
  196. package/src/modules/projects/actions/add-project-user.action.ts +9 -9
  197. package/src/modules/projects/actions/list-all-account-users.action.ts +9 -9
  198. package/src/modules/projects/actions/list-available-users.action.ts +9 -9
  199. package/src/modules/projects/actions/list-project-users.action.ts +9 -9
  200. package/src/modules/projects/actions/remove-project-user.action.ts +9 -9
  201. package/src/modules/projects/hooks/add-project-user.hook.ts +69 -69
  202. package/src/modules/projects/hooks/list-all-account-users.hook.ts +34 -34
  203. package/src/modules/projects/hooks/list-available-users.hook.ts +33 -33
  204. package/src/modules/projects/hooks/list-project-users.hook.ts +33 -33
  205. package/src/modules/projects/hooks/remove-project-user.hook.ts +69 -69
  206. package/src/modules/subscriptions/actions/find-plan-by-id.action.ts +8 -8
  207. package/src/modules/subscriptions/actions/list-subscriptions.action.ts +9 -9
  208. package/src/modules/subscriptions/hooks/find-active-subscription.hook.ts +19 -19
  209. package/src/modules/subscriptions/hooks/list-subscriptions.hook.ts +17 -17
  210. package/src/modules/subscriptions/hooks/use-has-plan-addon.hook.ts +17 -17
  211. package/src/modules/subscriptions/hooks/use-plan-by-id.hook.ts +13 -13
  212. package/src/modules/subscriptions/services/plans.service.ts +36 -36
  213. package/src/modules/subscriptions/types/plan.type.ts +21 -21
  214. package/src/modules/users/action/find-user-by-id.action.ts +8 -8
  215. package/src/modules/users/action/list-messages.action.ts +12 -12
  216. package/src/modules/users/action/mark-all-messages-as-read.action.ts +8 -8
  217. package/src/modules/users/action/mark-message-as-read.action.ts +8 -8
  218. package/src/modules/users/hooks/messages.hook.ts +60 -60
  219. package/src/modules/whitelabel/actions/find-whitelabel-styles.action.ts +14 -14
  220. package/src/modules/whitelabel/actions/find-whitelabel.action.ts +23 -23
  221. package/src/modules/whitelabel/services/whitelabel-styles.service.ts +98 -98
  222. package/src/modules/whitelabel/styles-schema.ts +37 -37
  223. package/src/providers/whitelabel.provider.tsx +46 -46
  224. package/src/utils/countries.ts +224 -224
  225. package/src/utils/safeServerAction.ts +21 -21
  226. package/src/utils/withAction.ts +13 -13
@@ -1,236 +1,236 @@
1
- import 'server-only';
2
-
3
- import { api } from '../../../infra/api/client';
4
- import { ApiError } from '../../../infra/api/types';
5
- import { buildQueryParams } from '../../../infra/utils/params';
6
- import { getUserContext } from '../../auth/utils/get-user-context';
7
- import { fileToBase64 } from '../../../utils/file/index';
8
- import type {
9
- Account,
10
- AccountUsersPaginationParams,
11
- ChangePasswordRequest,
12
- ChangePasswordResponse,
13
- ChangeUserPhotoResponse,
14
- CreateAccountUserRequest,
15
- CreateAccountUserResponse,
16
- DeleteAccountResponse,
17
- DeleteAccountUserResponse,
18
- GetAccountDataResponse,
19
- ListAccountUsersResponse,
20
- UpdateAccountRequest,
21
- UpdateAccountResponse,
22
- UpdateAccountUserRequest,
23
- UpdateAccountUserResponse,
24
- } from '../types';
25
-
26
- class AccountService {
27
- async findCurrentAccount(): Promise<Account> {
28
- const { id_account } = await getUserContext();
29
- const response = await api.apps.get<GetAccountDataResponse>(`/accounts/${id_account}`);
30
-
31
- if (response.status === 0 || !response.data?.[0]) {
32
- throw new ApiError(
33
- response.message || 'Erro ao buscar dados da conta',
34
- 'GET_ACCOUNT_FAILED',
35
- 400
36
- );
37
- }
38
-
39
- return response.data[0];
40
- }
41
-
42
- async listAccountUsers(params?: Partial<AccountUsersPaginationParams>) {
43
- const { id_account } = await getUserContext();
44
- const queryParams = buildQueryParams(params);
45
- const endpoint = `/accounts/${id_account}/users?${queryParams}`;
46
- const response = await api.apps.get<ListAccountUsersResponse>(endpoint);
47
-
48
- if (response.status === 0) {
49
- throw new ApiError(
50
- response.message || 'Erro ao listar usuários',
51
- 'LIST_ACCOUNT_USERS_FAILED',
52
- 400
53
- );
54
- }
55
-
56
- return {
57
- users: response.data || [],
58
- total: response.total || 0,
59
- };
60
- }
61
-
62
- async changeUserPhoto(userId: number, photo: File): Promise<ChangeUserPhotoResponse> {
63
- const { id_account } = await getUserContext();
64
- const base64 = await fileToBase64(photo);
65
- const response = await api.apps.put<ChangeUserPhotoResponse>(
66
- `/accounts/${id_account}/users/${userId}/file/photo`,
67
- { file: base64 }
68
- );
69
-
70
- if (response.status === 0) {
71
- throw new ApiError(
72
- response.message || 'Erro ao mudar foto do usuário',
73
- 'CHANGE_USER_PHOTO_FAILED',
74
- 400
75
- );
76
- }
77
-
78
- return response;
79
- }
80
-
81
- async deleteAccountUser(userId: number): Promise<DeleteAccountUserResponse> {
82
- const { id_account } = await getUserContext();
83
- const response = await api.apps.delete<DeleteAccountUserResponse>(
84
- `/accounts/${id_account}/users/${userId}`
85
- );
86
-
87
- if (response.status === 0) {
88
- throw new ApiError(response.message || 'Erro ao deletar usuário', 'DELETE_USER_FAILED', 400);
89
- }
90
-
91
- return response;
92
- }
93
-
94
- async deleteAccount(): Promise<DeleteAccountResponse> {
95
- const { id_account } = await getUserContext();
96
- const response = await api.apps.delete<DeleteAccountResponse>(`/accounts/${id_account}`);
97
-
98
- if (response.status === 0) {
99
- throw new ApiError(response.message || 'Erro ao deletar conta', 'DELETE_ACCOUNT_FAILED', 400);
100
- }
101
-
102
- return response;
103
- }
104
-
105
- async createAccountUser(user: CreateAccountUserRequest): Promise<CreateAccountUserResponse> {
106
- const { id_account } = await getUserContext();
107
- const response = await api.apps.post<CreateAccountUserResponse>(
108
- `/accounts/${id_account}/users`,
109
- {
110
- ...user,
111
- photo: user.photo ? await fileToBase64(user.photo) : null,
112
- }
113
- );
114
-
115
- if (response.status === 0) {
116
- throw new ApiError(response.message || 'Erro ao criar usuário', 'CREATE_USER_FAILED', 400);
117
- }
118
-
119
- return response;
120
- }
121
-
122
- private async resolvePhotoField(user: UpdateAccountUserRequest): Promise<{ photo?: string | null }> {
123
- if (user.photo === undefined) return {};
124
- const photo =
125
- user.photo != null && typeof user.photo !== 'string'
126
- ? await fileToBase64(user.photo)
127
- : user.photo;
128
- return { photo };
129
- }
130
-
131
- async updateAccountUser(user: UpdateAccountUserRequest): Promise<UpdateAccountUserResponse> {
132
- const { id_account, id_user } = await getUserContext();
133
- const response = await api.apps.put<UpdateAccountUserResponse>(
134
- `/accounts/${id_account}/users/${id_user}`,
135
- { ...user, ...(await this.resolvePhotoField(user)) }
136
- );
137
-
138
- if (response.status === 0) {
139
- throw new ApiError(
140
- response.message || 'Erro ao atualizar usuário',
141
- 'UPDATE_USER_FAILED',
142
- 400
143
- );
144
- }
145
-
146
- return response;
147
- }
148
-
149
- async updateAccountUserById(
150
- userId: number,
151
- user: UpdateAccountUserRequest
152
- ): Promise<UpdateAccountUserResponse> {
153
- const { id_account } = await getUserContext();
154
- const response = await api.apps.put<UpdateAccountUserResponse>(
155
- `/accounts/${id_account}/users/${userId}`,
156
- { ...user, ...(await this.resolvePhotoField(user)) }
157
- );
158
-
159
- if (response.status === 0) {
160
- throw new ApiError(
161
- response.message || 'Erro ao atualizar usuário',
162
- 'UPDATE_USER_FAILED',
163
- 400
164
- );
165
- }
166
-
167
- return response;
168
- }
169
-
170
- async updateAccount(data: UpdateAccountRequest): Promise<UpdateAccountResponse> {
171
- const { id_account } = await getUserContext();
172
- const response = await api.apps.put<UpdateAccountResponse>(`/accounts/${id_account}`, data);
173
-
174
- if (response.status === 0) {
175
- throw new ApiError(
176
- response.message || 'Erro ao atualizar conta',
177
- 'UPDATE_ACCOUNT_FAILED',
178
- 400
179
- );
180
- }
181
-
182
- return response;
183
- }
184
-
185
- async requestContactReset(type: 'email' | 'phone', newContact: string, ddi?: string): Promise<void> {
186
- const { id_account, id_user } = await getUserContext();
187
- const response = await api.apps.post<{ status: number; message: string }>(
188
- `/accounts/${id_account}/users/${id_user}/resetContact/request`,
189
- { type, newContact, ...(ddi ? { ddi } : {}) }
190
- );
191
-
192
- if (response.status === 0) {
193
- throw new ApiError(
194
- response.message || 'Erro ao solicitar alteração',
195
- 'REQUEST_CONTACT_RESET_FAILED',
196
- 400
197
- );
198
- }
199
- }
200
-
201
- async confirmContactReset(type: 'email' | 'phone', token: string, newContact: string, ddi?: string): Promise<void> {
202
- const { id_account, id_user } = await getUserContext();
203
- const response = await api.apps.post<{ status: number; message: string }>(
204
- `/accounts/${id_account}/users/${id_user}/resetContact/confirm`,
205
- { type, token, newContact, ...(ddi ? { ddi } : {}) }
206
- );
207
-
208
- if (response.status === 0) {
209
- throw new ApiError(
210
- response.message || 'Código inválido ou expirado',
211
- 'CONFIRM_CONTACT_RESET_FAILED',
212
- 400
213
- );
214
- }
215
- }
216
-
217
- async changePassword(data: ChangePasswordRequest): Promise<ChangePasswordResponse> {
218
- const { id_account, id_user } = await getUserContext();
219
- const response = await api.apps.put<ChangePasswordResponse>(
220
- `/accounts/${id_account}/users/${id_user}/action/changePassword`,
221
- data
222
- );
223
-
224
- if (response.status === 0) {
225
- throw new ApiError(
226
- response.message || 'Erro ao alterar senha',
227
- 'CHANGE_PASSWORD_FAILED',
228
- 400
229
- );
230
- }
231
-
232
- return response;
233
- }
234
- }
235
-
236
- export const accountService = new AccountService();
1
+ import 'server-only';
2
+
3
+ import { api } from '../../../infra/api/client';
4
+ import { ApiError } from '../../../infra/api/types';
5
+ import { buildQueryParams } from '../../../infra/utils/params';
6
+ import { getUserContext } from '../../auth/utils/get-user-context';
7
+ import { fileToBase64 } from '../../../utils/file/index';
8
+ import type {
9
+ Account,
10
+ AccountUsersPaginationParams,
11
+ ChangePasswordRequest,
12
+ ChangePasswordResponse,
13
+ ChangeUserPhotoResponse,
14
+ CreateAccountUserRequest,
15
+ CreateAccountUserResponse,
16
+ DeleteAccountResponse,
17
+ DeleteAccountUserResponse,
18
+ GetAccountDataResponse,
19
+ ListAccountUsersResponse,
20
+ UpdateAccountRequest,
21
+ UpdateAccountResponse,
22
+ UpdateAccountUserRequest,
23
+ UpdateAccountUserResponse,
24
+ } from '../types';
25
+
26
+ class AccountService {
27
+ async findCurrentAccount(): Promise<Account> {
28
+ const { id_account } = await getUserContext();
29
+ const response = await api.apps.get<GetAccountDataResponse>(`/accounts/${id_account}`);
30
+
31
+ if (response.status === 0 || !response.data?.[0]) {
32
+ throw new ApiError(
33
+ response.message || 'Erro ao buscar dados da conta',
34
+ 'GET_ACCOUNT_FAILED',
35
+ 400
36
+ );
37
+ }
38
+
39
+ return response.data[0];
40
+ }
41
+
42
+ async listAccountUsers(params?: Partial<AccountUsersPaginationParams>) {
43
+ const { id_account } = await getUserContext();
44
+ const queryParams = buildQueryParams(params);
45
+ const endpoint = `/accounts/${id_account}/users?${queryParams}`;
46
+ const response = await api.apps.get<ListAccountUsersResponse>(endpoint);
47
+
48
+ if (response.status === 0) {
49
+ throw new ApiError(
50
+ response.message || 'Erro ao listar usuários',
51
+ 'LIST_ACCOUNT_USERS_FAILED',
52
+ 400
53
+ );
54
+ }
55
+
56
+ return {
57
+ users: response.data || [],
58
+ total: response.total || 0,
59
+ };
60
+ }
61
+
62
+ async changeUserPhoto(userId: number, photo: File): Promise<ChangeUserPhotoResponse> {
63
+ const { id_account } = await getUserContext();
64
+ const base64 = await fileToBase64(photo);
65
+ const response = await api.apps.put<ChangeUserPhotoResponse>(
66
+ `/accounts/${id_account}/users/${userId}/file/photo`,
67
+ { file: base64 }
68
+ );
69
+
70
+ if (response.status === 0) {
71
+ throw new ApiError(
72
+ response.message || 'Erro ao mudar foto do usuário',
73
+ 'CHANGE_USER_PHOTO_FAILED',
74
+ 400
75
+ );
76
+ }
77
+
78
+ return response;
79
+ }
80
+
81
+ async deleteAccountUser(userId: number): Promise<DeleteAccountUserResponse> {
82
+ const { id_account } = await getUserContext();
83
+ const response = await api.apps.delete<DeleteAccountUserResponse>(
84
+ `/accounts/${id_account}/users/${userId}`
85
+ );
86
+
87
+ if (response.status === 0) {
88
+ throw new ApiError(response.message || 'Erro ao deletar usuário', 'DELETE_USER_FAILED', 400);
89
+ }
90
+
91
+ return response;
92
+ }
93
+
94
+ async deleteAccount(): Promise<DeleteAccountResponse> {
95
+ const { id_account } = await getUserContext();
96
+ const response = await api.apps.delete<DeleteAccountResponse>(`/accounts/${id_account}`);
97
+
98
+ if (response.status === 0) {
99
+ throw new ApiError(response.message || 'Erro ao deletar conta', 'DELETE_ACCOUNT_FAILED', 400);
100
+ }
101
+
102
+ return response;
103
+ }
104
+
105
+ async createAccountUser(user: CreateAccountUserRequest): Promise<CreateAccountUserResponse> {
106
+ const { id_account } = await getUserContext();
107
+ const response = await api.apps.post<CreateAccountUserResponse>(
108
+ `/accounts/${id_account}/users`,
109
+ {
110
+ ...user,
111
+ photo: user.photo ? await fileToBase64(user.photo) : null,
112
+ }
113
+ );
114
+
115
+ if (response.status === 0) {
116
+ throw new ApiError(response.message || 'Erro ao criar usuário', 'CREATE_USER_FAILED', 400);
117
+ }
118
+
119
+ return response;
120
+ }
121
+
122
+ private async resolvePhotoField(user: UpdateAccountUserRequest): Promise<{ photo?: string | null }> {
123
+ if (user.photo === undefined) return {};
124
+ const photo =
125
+ user.photo != null && typeof user.photo !== 'string'
126
+ ? await fileToBase64(user.photo)
127
+ : user.photo;
128
+ return { photo };
129
+ }
130
+
131
+ async updateAccountUser(user: UpdateAccountUserRequest): Promise<UpdateAccountUserResponse> {
132
+ const { id_account, id_user } = await getUserContext();
133
+ const response = await api.apps.put<UpdateAccountUserResponse>(
134
+ `/accounts/${id_account}/users/${id_user}`,
135
+ { ...user, ...(await this.resolvePhotoField(user)) }
136
+ );
137
+
138
+ if (response.status === 0) {
139
+ throw new ApiError(
140
+ response.message || 'Erro ao atualizar usuário',
141
+ 'UPDATE_USER_FAILED',
142
+ 400
143
+ );
144
+ }
145
+
146
+ return response;
147
+ }
148
+
149
+ async updateAccountUserById(
150
+ userId: number,
151
+ user: UpdateAccountUserRequest
152
+ ): Promise<UpdateAccountUserResponse> {
153
+ const { id_account } = await getUserContext();
154
+ const response = await api.apps.put<UpdateAccountUserResponse>(
155
+ `/accounts/${id_account}/users/${userId}`,
156
+ { ...user, ...(await this.resolvePhotoField(user)) }
157
+ );
158
+
159
+ if (response.status === 0) {
160
+ throw new ApiError(
161
+ response.message || 'Erro ao atualizar usuário',
162
+ 'UPDATE_USER_FAILED',
163
+ 400
164
+ );
165
+ }
166
+
167
+ return response;
168
+ }
169
+
170
+ async updateAccount(data: UpdateAccountRequest): Promise<UpdateAccountResponse> {
171
+ const { id_account } = await getUserContext();
172
+ const response = await api.apps.put<UpdateAccountResponse>(`/accounts/${id_account}`, data);
173
+
174
+ if (response.status === 0) {
175
+ throw new ApiError(
176
+ response.message || 'Erro ao atualizar conta',
177
+ 'UPDATE_ACCOUNT_FAILED',
178
+ 400
179
+ );
180
+ }
181
+
182
+ return response;
183
+ }
184
+
185
+ async requestContactReset(type: 'email' | 'phone', newContact: string, ddi?: string): Promise<void> {
186
+ const { id_account, id_user } = await getUserContext();
187
+ const response = await api.apps.post<{ status: number; message: string }>(
188
+ `/accounts/${id_account}/users/${id_user}/resetContact/request`,
189
+ { type, newContact, ...(ddi ? { ddi } : {}) }
190
+ );
191
+
192
+ if (response.status === 0) {
193
+ throw new ApiError(
194
+ response.message || 'Erro ao solicitar alteração',
195
+ 'REQUEST_CONTACT_RESET_FAILED',
196
+ 400
197
+ );
198
+ }
199
+ }
200
+
201
+ async confirmContactReset(type: 'email' | 'phone', token: string, newContact: string, ddi?: string): Promise<void> {
202
+ const { id_account, id_user } = await getUserContext();
203
+ const response = await api.apps.post<{ status: number; message: string }>(
204
+ `/accounts/${id_account}/users/${id_user}/resetContact/confirm`,
205
+ { type, token, newContact, ...(ddi ? { ddi } : {}) }
206
+ );
207
+
208
+ if (response.status === 0) {
209
+ throw new ApiError(
210
+ response.message || 'Código inválido ou expirado',
211
+ 'CONFIRM_CONTACT_RESET_FAILED',
212
+ 400
213
+ );
214
+ }
215
+ }
216
+
217
+ async changePassword(data: ChangePasswordRequest): Promise<ChangePasswordResponse> {
218
+ const { id_account, id_user } = await getUserContext();
219
+ const response = await api.apps.put<ChangePasswordResponse>(
220
+ `/accounts/${id_account}/users/${id_user}/action/changePassword`,
221
+ data
222
+ );
223
+
224
+ if (response.status === 0) {
225
+ throw new ApiError(
226
+ response.message || 'Erro ao alterar senha',
227
+ 'CHANGE_PASSWORD_FAILED',
228
+ 400
229
+ );
230
+ }
231
+
232
+ return response;
233
+ }
234
+ }
235
+
236
+ export const accountService = new AccountService();