@greatapps/common 1.1.97 → 1.1.99

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 (115) hide show
  1. package/dist/components/account/hooks/useTwoFactorAuthForm.mjs +5 -13
  2. package/dist/components/account/hooks/useTwoFactorAuthForm.mjs.map +1 -1
  3. package/dist/components/account/sections/MyProfileSection.mjs +30 -19
  4. package/dist/components/account/sections/MyProfileSection.mjs.map +1 -1
  5. package/dist/components/account/sections/PreferencesSection.mjs +11 -10
  6. package/dist/components/account/sections/PreferencesSection.mjs.map +1 -1
  7. package/dist/index.mjs +3 -1
  8. package/dist/index.mjs.map +1 -1
  9. package/dist/modules/accounts/actions/account-management.action.mjs +36 -168
  10. package/dist/modules/accounts/actions/account-management.action.mjs.map +1 -1
  11. package/dist/modules/accounts/actions/find-current-account.action.mjs +3 -2
  12. package/dist/modules/accounts/actions/find-current-account.action.mjs.map +1 -1
  13. package/dist/modules/accounts/actions/list-currencies.action.mjs +3 -2
  14. package/dist/modules/accounts/actions/list-currencies.action.mjs.map +1 -1
  15. package/dist/modules/accounts/actions/list-timezones.action.mjs +3 -2
  16. package/dist/modules/accounts/actions/list-timezones.action.mjs.map +1 -1
  17. package/dist/modules/accounts/hooks/current-account.hook.mjs +2 -1
  18. package/dist/modules/accounts/hooks/current-account.hook.mjs.map +1 -1
  19. package/dist/modules/accounts/hooks/useAccountManagement.mjs +8 -5
  20. package/dist/modules/accounts/hooks/useAccountManagement.mjs.map +1 -1
  21. package/dist/modules/accounts/hooks/useCurrencies.mjs +2 -1
  22. package/dist/modules/accounts/hooks/useCurrencies.mjs.map +1 -1
  23. package/dist/modules/accounts/hooks/useTimezones.mjs +2 -1
  24. package/dist/modules/accounts/hooks/useTimezones.mjs.map +1 -1
  25. package/dist/modules/auth/actions/logout.action.mjs +6 -3
  26. package/dist/modules/auth/actions/logout.action.mjs.map +1 -1
  27. package/dist/modules/auth/actions/register.action.mjs +7 -2
  28. package/dist/modules/auth/actions/register.action.mjs.map +1 -1
  29. package/dist/modules/auth/actions/verify-two-factor.action.mjs +5 -10
  30. package/dist/modules/auth/actions/verify-two-factor.action.mjs.map +1 -1
  31. package/dist/modules/auth/hooks/logout.hook.mjs +2 -1
  32. package/dist/modules/auth/hooks/logout.hook.mjs.map +1 -1
  33. package/dist/modules/auth/hooks/register.hook.mjs +2 -1
  34. package/dist/modules/auth/hooks/register.hook.mjs.map +1 -1
  35. package/dist/modules/auth/hooks/useUserQuery.mjs +2 -2
  36. package/dist/modules/auth/hooks/useUserQuery.mjs.map +1 -1
  37. package/dist/modules/auth/services/auth.service.mjs +9 -4
  38. package/dist/modules/auth/services/auth.service.mjs.map +1 -1
  39. package/dist/modules/ia-credits/actions/list-ia-credits.action.mjs +3 -2
  40. package/dist/modules/ia-credits/actions/list-ia-credits.action.mjs.map +1 -1
  41. package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs +2 -1
  42. package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs.map +1 -1
  43. package/dist/modules/projects/actions/add-project-user.action.mjs +3 -2
  44. package/dist/modules/projects/actions/add-project-user.action.mjs.map +1 -1
  45. package/dist/modules/projects/actions/list-available-users.action.mjs +3 -2
  46. package/dist/modules/projects/actions/list-available-users.action.mjs.map +1 -1
  47. package/dist/modules/projects/actions/list-project-users.action.mjs +3 -2
  48. package/dist/modules/projects/actions/list-project-users.action.mjs.map +1 -1
  49. package/dist/modules/projects/actions/remove-project-user.action.mjs +3 -2
  50. package/dist/modules/projects/actions/remove-project-user.action.mjs.map +1 -1
  51. package/dist/modules/projects/hooks/list-available-users.hook.mjs +8 -1
  52. package/dist/modules/projects/hooks/list-available-users.hook.mjs.map +1 -1
  53. package/dist/modules/projects/hooks/list-project-users.hook.mjs +8 -1
  54. package/dist/modules/projects/hooks/list-project-users.hook.mjs.map +1 -1
  55. package/dist/modules/subscriptions/actions/list-subscriptions.action.mjs +3 -2
  56. package/dist/modules/subscriptions/actions/list-subscriptions.action.mjs.map +1 -1
  57. package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs +5 -1
  58. package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs.map +1 -1
  59. package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs +2 -1
  60. package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs.map +1 -1
  61. package/dist/modules/users/action/find-user-by-id.action.mjs +3 -14
  62. package/dist/modules/users/action/find-user-by-id.action.mjs.map +1 -1
  63. package/dist/modules/users/action/list-messages.action.mjs +6 -2
  64. package/dist/modules/users/action/list-messages.action.mjs.map +1 -1
  65. package/dist/modules/users/action/mark-all-messages-as-read.action.mjs +3 -2
  66. package/dist/modules/users/action/mark-all-messages-as-read.action.mjs.map +1 -1
  67. package/dist/modules/users/action/mark-message-as-read.action.mjs +3 -2
  68. package/dist/modules/users/action/mark-message-as-read.action.mjs.map +1 -1
  69. package/dist/modules/users/hooks/messages.hook.mjs +11 -9
  70. package/dist/modules/users/hooks/messages.hook.mjs.map +1 -1
  71. package/dist/providers/auth.provider.mjs +9 -15
  72. package/dist/providers/auth.provider.mjs.map +1 -1
  73. package/dist/utils/safeServerAction.mjs +10 -16
  74. package/dist/utils/safeServerAction.mjs.map +1 -1
  75. package/dist/utils/withAction.mjs +13 -0
  76. package/dist/utils/withAction.mjs.map +1 -0
  77. package/package.json +1 -1
  78. package/src/components/account/hooks/useTwoFactorAuthForm.tsx +101 -114
  79. package/src/components/account/sections/MyProfileSection.tsx +29 -26
  80. package/src/components/account/sections/PreferencesSection.tsx +10 -16
  81. package/src/index.ts +1 -0
  82. package/src/modules/accounts/actions/account-management.action.ts +90 -261
  83. package/src/modules/accounts/actions/find-current-account.action.ts +5 -4
  84. package/src/modules/accounts/actions/list-currencies.action.ts +8 -7
  85. package/src/modules/accounts/actions/list-timezones.action.ts +8 -7
  86. package/src/modules/accounts/hooks/current-account.hook.tsx +6 -5
  87. package/src/modules/accounts/hooks/useAccountManagement.ts +85 -83
  88. package/src/modules/accounts/hooks/useCurrencies.ts +15 -14
  89. package/src/modules/accounts/hooks/useTimezones.ts +15 -14
  90. package/src/modules/auth/actions/logout.action.ts +8 -7
  91. package/src/modules/auth/actions/register.action.ts +9 -4
  92. package/src/modules/auth/actions/verify-two-factor.action.ts +8 -21
  93. package/src/modules/auth/hooks/logout.hook.tsx +2 -1
  94. package/src/modules/auth/hooks/register.hook.tsx +2 -1
  95. package/src/modules/auth/hooks/useUserQuery.ts +2 -2
  96. package/src/modules/auth/services/auth.service.ts +11 -6
  97. package/src/modules/ia-credits/actions/list-ia-credits.action.ts +8 -11
  98. package/src/modules/ia-credits/hooks/ia-credits.hook.ts +2 -1
  99. package/src/modules/projects/actions/add-project-user.action.ts +9 -8
  100. package/src/modules/projects/actions/list-available-users.action.ts +9 -8
  101. package/src/modules/projects/actions/list-project-users.action.ts +9 -8
  102. package/src/modules/projects/actions/remove-project-user.action.ts +9 -8
  103. package/src/modules/projects/hooks/list-available-users.hook.ts +33 -26
  104. package/src/modules/projects/hooks/list-project-users.hook.ts +33 -26
  105. package/src/modules/subscriptions/actions/list-subscriptions.action.ts +9 -11
  106. package/src/modules/subscriptions/hooks/find-active-subscription.hook.ts +19 -15
  107. package/src/modules/subscriptions/hooks/list-subscriptions.hook.ts +17 -15
  108. package/src/modules/users/action/find-user-by-id.action.ts +8 -25
  109. package/src/modules/users/action/list-messages.action.ts +12 -10
  110. package/src/modules/users/action/mark-all-messages-as-read.action.ts +8 -8
  111. package/src/modules/users/action/mark-message-as-read.action.ts +8 -8
  112. package/src/modules/users/hooks/messages.hook.ts +60 -58
  113. package/src/providers/auth.provider.tsx +9 -16
  114. package/src/utils/safeServerAction.ts +18 -24
  115. package/src/utils/withAction.ts +13 -0
@@ -1,11 +1,12 @@
1
1
  'use server';
2
2
 
3
- import { getClientInfoFromRequest } from "../../../infra/utils/client-info";
4
- import { LogoutResponse } from "../schema";
5
- import { authService } from "../services/auth.service";
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { getClientInfoFromRequest } from '../../../infra/utils/client-info';
5
+ import { authService } from '../services/auth.service';
6
6
 
7
- export async function logoutAction(): Promise<LogoutResponse> {
8
- const clientInfo = await getClientInfoFromRequest();
9
-
10
- return await authService.logout(clientInfo)
7
+ export function logoutAction() {
8
+ return safeServerAction(async () => {
9
+ const clientInfo = await getClientInfoFromRequest();
10
+ return authService.logout(clientInfo);
11
+ });
11
12
  }
@@ -1,8 +1,13 @@
1
1
  'use server';
2
2
 
3
- import { RegisterRequest, RegisterResponse } from "../schema";
4
- import { authService } from "../services/auth.service";
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { RegisterRequest } from '../schema';
5
+ import { authService } from '../services/auth.service';
6
+ import { getClientInfoFromRequest } from '../../../infra/utils/client-info';
5
7
 
6
- export async function registerAction(data: RegisterRequest): Promise<RegisterResponse> {
7
- return await authService.register(data)
8
+ export function registerAction(data: RegisterRequest) {
9
+ return safeServerAction(async () => {
10
+ const clientInfo = await getClientInfoFromRequest();
11
+ return authService.register(data, clientInfo);
12
+ });
8
13
  }
@@ -1,28 +1,15 @@
1
- "use server";
1
+ 'use server';
2
2
 
3
- import { authService } from "../services/auth.service";
4
- import { ApiError } from "../../../infra/api/types";
5
- import { getClientInfoFromRequest } from "../../../infra/utils/client-info";
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { authService } from '../services/auth.service';
5
+ import { getClientInfoFromRequest } from '../../../infra/utils/client-info';
6
6
 
7
- export async function verifyTwoFactorAction(
8
- cookie: string,
9
- code: string
10
- ): Promise<{ status: 0 | 1; message?: string }> {
11
- try {
7
+ export function verifyTwoFactorAction(cookie: string, code: string) {
8
+ return safeServerAction(async () => {
12
9
  if (!/^\d{6}$/.test(code)) {
13
- return { status: 0, message: "Formato inválido" };
10
+ throw new Error('Formato inválido');
14
11
  }
15
-
16
12
  const clientInfo = await getClientInfoFromRequest();
17
13
  await authService.verifyTwoFactor(cookie, code, clientInfo);
18
-
19
- return { status: 1 };
20
- } catch (error) {
21
- return {
22
- status: 0,
23
- message:
24
- (error as ApiError).message ||
25
- "Ocorreu um erro durante a verificação de dois fatores",
26
- };
27
- }
14
+ });
28
15
  }
@@ -2,9 +2,10 @@
2
2
 
3
3
  import { useMutation } from "@tanstack/react-query";
4
4
  import { logoutAction } from "../actions/logout.action";
5
+ import { withAction } from "../../../utils/withAction";
5
6
 
6
7
  export function useLogout() {
7
8
  return useMutation({
8
- mutationFn: logoutAction,
9
+ mutationFn: withAction(logoutAction),
9
10
  });
10
11
  }
@@ -2,9 +2,10 @@
2
2
 
3
3
  import { useMutation } from "@tanstack/react-query";
4
4
  import { registerAction } from "../actions/register.action";
5
+ import { withAction } from "../../../utils/withAction";
5
6
 
6
7
  export function useRegister() {
7
8
  return useMutation({
8
- mutationFn: registerAction,
9
+ mutationFn: withAction(registerAction),
9
10
  });
10
11
  }
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { useMutation, useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query';
4
4
  import { User } from '../../users/schema';
5
- import { safeServerAction } from '../../../utils/safeServerAction';
5
+ import { withAction } from '../../../utils/withAction';
6
6
 
7
7
  export const USER_QUERY_KEY = ['user'];
8
8
 
@@ -45,7 +45,7 @@ export function useTwoFactorVerify() {
45
45
  return useMutation({
46
46
  mutationFn: async ({ cookie, code }: { cookie: string; code: string }) => {
47
47
  const { verifyTwoFactorAction } = await import('../actions/verify-two-factor.action');
48
- return safeServerAction(verifyTwoFactorAction, cookie, code);
48
+ return withAction(() => verifyTwoFactorAction(cookie, code))();
49
49
  },
50
50
  onSuccess: () => {
51
51
  queryClient.invalidateQueries({ queryKey: USER_QUERY_KEY });
@@ -127,7 +127,7 @@ class AuthService {
127
127
 
128
128
  return { status: 1 };
129
129
  }
130
- async register(data: RegisterRequest): Promise<RegisterResponse> {
130
+ async register(data: RegisterRequest, clientInfo: ClientInfo): Promise<RegisterResponse> {
131
131
  const today = new Date();
132
132
  const trialEndDate = new Date(today);
133
133
  trialEndDate.setDate(today.getDate() + 7); // 7 dias de trial
@@ -183,12 +183,17 @@ class AuthService {
183
183
 
184
184
  const accountData = response.data[0];
185
185
 
186
- // Retornar sem token pois o registro não retorna cookie diretamente
187
- // O usuário precisará fazer login após o registro
188
- // O usuário completo será carregado após o login
186
+ const loginResult = await this.login(
187
+ { email: data.email, password: data.password },
188
+ clientInfo
189
+ );
190
+
191
+ const user = loginResult.result === 'success' ? loginResult.user : {} as User;
192
+ const accessToken = loginResult.result === 'success' ? loginResult.accessToken : "";
193
+
189
194
  return {
190
- user: {} as User, // Placeholder, será preenchido após login
191
- accessToken: "",
195
+ user,
196
+ accessToken,
192
197
  refreshToken: "",
193
198
  expiresAt: new Date(Date.now() + COOKIE_MAX_AGE * 1000).toISOString(),
194
199
  requiresEmailVerification: !accountData.verified,
@@ -1,11 +1,8 @@
1
- 'use server';
2
-
3
- import { PaginatedSuccessResult } from '../../../infra/api/types';
4
- import type { IaCreditOperation } from '../types';
5
- import { iaCreditsService } from '../services/ia-credits.service';
6
-
7
- export async function listIaCreditsAction(
8
- subscriptionId: number | string
9
- ): Promise<PaginatedSuccessResult<IaCreditOperation>> {
10
- return iaCreditsService.listOperations(subscriptionId);
11
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { iaCreditsService } from '../services/ia-credits.service';
5
+
6
+ export function listIaCreditsAction(subscriptionId: number | string) {
7
+ return safeServerAction(() => iaCreditsService.listOperations(subscriptionId));
8
+ }
@@ -5,6 +5,7 @@ import { useQuery } from '@tanstack/react-query';
5
5
  import { listIaCreditsAction } from '../actions/list-ia-credits.action';
6
6
  import type { IaCreditOperation, IaCreditsSummary } from '../types';
7
7
  import { useActiveSubscription } from '../../subscriptions/hooks/find-active-subscription.hook';
8
+ import { withAction } from '../../../utils/withAction';
8
9
 
9
10
  function calculateSummary(operations: IaCreditOperation[]): IaCreditsSummary {
10
11
  const today = new Date();
@@ -74,7 +75,7 @@ export function useIaCredits() {
74
75
 
75
76
  const query = useQuery({
76
77
  queryKey: ['ia-credits', subscriptionId],
77
- queryFn: () => listIaCreditsAction(subscriptionId!),
78
+ queryFn: withAction(() => listIaCreditsAction(subscriptionId!)),
78
79
  enabled: subscriptionId != null,
79
80
  });
80
81
 
@@ -1,8 +1,9 @@
1
- 'use server';
2
-
3
- import { projectUsersService } from '../services/project-users.service';
4
- import type { AddRemoveProjectUsersParams } from '../types';
5
-
6
- export async function addProjectUserAction(params: AddRemoveProjectUsersParams): Promise<void> {
7
- return projectUsersService.addToProject(params);
8
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { projectUsersService } from '../services/project-users.service';
5
+ import type { AddRemoveProjectUsersParams } from '../types';
6
+
7
+ export function addProjectUserAction(params: AddRemoveProjectUsersParams) {
8
+ return safeServerAction(() => projectUsersService.addToProject(params));
9
+ }
@@ -1,8 +1,9 @@
1
- 'use server';
2
-
3
- import { projectUsersService } from '../services/project-users.service';
4
- import type { AccountUser, ListUsersParams, UsersPage } from '../types';
5
-
6
- export async function listAvailableUsersAction(projectId: number, params: ListUsersParams = {}): Promise<UsersPage<AccountUser>> {
7
- return projectUsersService.listNotInProject(projectId, params);
8
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { projectUsersService } from '../services/project-users.service';
5
+ import type { ListUsersParams } from '../types';
6
+
7
+ export function listAvailableUsersAction(projectId: number, params: ListUsersParams = {}) {
8
+ return safeServerAction(() => projectUsersService.listNotInProject(projectId, params));
9
+ }
@@ -1,8 +1,9 @@
1
- 'use server';
2
-
3
- import { projectUsersService } from '../services/project-users.service';
4
- import type { ListUsersParams, ProjectUser, UsersPage } from '../types';
5
-
6
- export async function listProjectUsersAction(projectId: number, params: ListUsersParams = {}): Promise<UsersPage<ProjectUser>> {
7
- return projectUsersService.listInProject(projectId, params);
8
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { projectUsersService } from '../services/project-users.service';
5
+ import type { ListUsersParams } from '../types';
6
+
7
+ export function listProjectUsersAction(projectId: number, params: ListUsersParams = {}) {
8
+ return safeServerAction(() => projectUsersService.listInProject(projectId, params));
9
+ }
@@ -1,8 +1,9 @@
1
- 'use server';
2
-
3
- import { projectUsersService } from '../services/project-users.service';
4
- import type { AddRemoveProjectUsersParams } from '../types';
5
-
6
- export async function removeProjectUserAction(params: AddRemoveProjectUsersParams): Promise<void> {
7
- return projectUsersService.removeFromProject(params);
8
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { projectUsersService } from '../services/project-users.service';
5
+ import type { AddRemoveProjectUsersParams } from '../types';
6
+
7
+ export function removeProjectUserAction(params: AddRemoveProjectUsersParams) {
8
+ return safeServerAction(() => projectUsersService.removeFromProject(params));
9
+ }
@@ -1,26 +1,33 @@
1
- 'use client';
2
-
3
- import { useInfiniteQuery } from '@tanstack/react-query';
4
- import { listAvailableUsersAction } from '../actions/list-available-users.action';
5
-
6
- const PAGE_SIZE = 20;
7
-
8
- export const LIST_AVAILABLE_USERS_BASE_KEY = (projectId: number) => ['project-users', projectId, 'out'];
9
- export const LIST_AVAILABLE_USERS_QUERY_KEY = (projectId: number, search: string = '') => [
10
- ...LIST_AVAILABLE_USERS_BASE_KEY(projectId),
11
- search,
12
- ];
13
-
14
- export function useListAvailableUsers(projectId: number, search: string = '') {
15
- return useInfiniteQuery({
16
- queryKey: LIST_AVAILABLE_USERS_QUERY_KEY(projectId, search),
17
- queryFn: ({ pageParam }) =>
18
- listAvailableUsersAction(projectId, { search: search || undefined, page: pageParam as number, limit: PAGE_SIZE }),
19
- getNextPageParam: (lastPage, allPages) => {
20
- const loaded = allPages.reduce((sum, p) => sum + p.data.length, 0);
21
- return loaded < lastPage.total ? allPages.length + 1 : undefined;
22
- },
23
- initialPageParam: 1,
24
- enabled: !!projectId,
25
- });
26
- }
1
+ 'use client';
2
+
3
+ import { useInfiniteQuery } from '@tanstack/react-query';
4
+ import { listAvailableUsersAction } from '../actions/list-available-users.action';
5
+ import { withAction } from '../../../utils/withAction';
6
+
7
+ const PAGE_SIZE = 20;
8
+
9
+ export const LIST_AVAILABLE_USERS_BASE_KEY = (projectId: number) => ['project-users', projectId, 'out'];
10
+ export const LIST_AVAILABLE_USERS_QUERY_KEY = (projectId: number, search: string = '') => [
11
+ ...LIST_AVAILABLE_USERS_BASE_KEY(projectId),
12
+ search,
13
+ ];
14
+
15
+ export function useListAvailableUsers(projectId: number, search: string = '') {
16
+ return useInfiniteQuery({
17
+ queryKey: LIST_AVAILABLE_USERS_QUERY_KEY(projectId, search),
18
+ queryFn: ({ pageParam }) =>
19
+ withAction(() =>
20
+ listAvailableUsersAction(projectId, {
21
+ search: search || undefined,
22
+ page: pageParam as number,
23
+ limit: PAGE_SIZE,
24
+ })
25
+ )(),
26
+ getNextPageParam: (lastPage, allPages) => {
27
+ const loaded = allPages.reduce((sum, p) => sum + p.data.length, 0);
28
+ return loaded < lastPage.total ? allPages.length + 1 : undefined;
29
+ },
30
+ initialPageParam: 1,
31
+ enabled: !!projectId,
32
+ });
33
+ }
@@ -1,26 +1,33 @@
1
- 'use client';
2
-
3
- import { useInfiniteQuery } from '@tanstack/react-query';
4
- import { listProjectUsersAction } from '../actions/list-project-users.action';
5
-
6
- const PAGE_SIZE = 20;
7
-
8
- export const LIST_PROJECT_USERS_BASE_KEY = (projectId: number) => ['project-users', projectId, 'in'];
9
- export const LIST_PROJECT_USERS_QUERY_KEY = (projectId: number, search: string = '') => [
10
- ...LIST_PROJECT_USERS_BASE_KEY(projectId),
11
- search,
12
- ];
13
-
14
- export function useListProjectUsers(projectId: number, search: string = '') {
15
- return useInfiniteQuery({
16
- queryKey: LIST_PROJECT_USERS_QUERY_KEY(projectId, search),
17
- queryFn: ({ pageParam }) =>
18
- listProjectUsersAction(projectId, { search: search || undefined, page: pageParam as number, limit: PAGE_SIZE }),
19
- getNextPageParam: (lastPage, allPages) => {
20
- const loaded = allPages.reduce((sum, p) => sum + p.data.length, 0);
21
- return loaded < lastPage.total ? allPages.length + 1 : undefined;
22
- },
23
- initialPageParam: 1,
24
- enabled: !!projectId,
25
- });
26
- }
1
+ 'use client';
2
+
3
+ import { useInfiniteQuery } from '@tanstack/react-query';
4
+ import { listProjectUsersAction } from '../actions/list-project-users.action';
5
+ import { withAction } from '../../../utils/withAction';
6
+
7
+ const PAGE_SIZE = 20;
8
+
9
+ export const LIST_PROJECT_USERS_BASE_KEY = (projectId: number) => ['project-users', projectId, 'in'];
10
+ export const LIST_PROJECT_USERS_QUERY_KEY = (projectId: number, search: string = '') => [
11
+ ...LIST_PROJECT_USERS_BASE_KEY(projectId),
12
+ search,
13
+ ];
14
+
15
+ export function useListProjectUsers(projectId: number, search: string = '') {
16
+ return useInfiniteQuery({
17
+ queryKey: LIST_PROJECT_USERS_QUERY_KEY(projectId, search),
18
+ queryFn: ({ pageParam }) =>
19
+ withAction(() =>
20
+ listProjectUsersAction(projectId, {
21
+ search: search || undefined,
22
+ page: pageParam as number,
23
+ limit: PAGE_SIZE,
24
+ })
25
+ )(),
26
+ getNextPageParam: (lastPage, allPages) => {
27
+ const loaded = allPages.reduce((sum, p) => sum + p.data.length, 0);
28
+ return loaded < lastPage.total ? allPages.length + 1 : undefined;
29
+ },
30
+ initialPageParam: 1,
31
+ enabled: !!projectId,
32
+ });
33
+ }
@@ -1,11 +1,9 @@
1
- 'use server';
2
-
3
- import { PaginatedSuccessResult } from '../../../infra/api/types';
4
- import type { Subscription, FindSubscriptionsParams } from '../types';
5
- import { subscriptionsService } from '../services/subscriptions.service';
6
-
7
- export async function listSubscriptionsAction(
8
- params?: Partial<FindSubscriptionsParams>
9
- ): Promise<PaginatedSuccessResult<Subscription>> {
10
- return subscriptionsService.listSubscriptions(params);
11
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import type { FindSubscriptionsParams } from '../types';
5
+ import { subscriptionsService } from '../services/subscriptions.service';
6
+
7
+ export function listSubscriptionsAction(params?: Partial<FindSubscriptionsParams>) {
8
+ return safeServerAction(() => subscriptionsService.listSubscriptions(params));
9
+ }
@@ -1,15 +1,19 @@
1
- 'use client';
2
-
3
- import { useQuery } from '@tanstack/react-query';
4
- import { listSubscriptionsAction } from '../actions/list-subscriptions.action';
5
- import { parseResult } from '../../../infra/utils/parser';
6
- import { SubscriptionSchema } from '../types';
7
- import { SUBSCRIPTIONS_QUERY_KEY } from './list-subscriptions.hook';
8
-
9
- export function useActiveSubscription() {
10
- return useQuery({
11
- queryKey: [...SUBSCRIPTIONS_QUERY_KEY, 'active'],
12
- queryFn: async () =>
13
- parseResult(SubscriptionSchema, await listSubscriptionsAction({ active: true })),
14
- });
15
- }
1
+ 'use client';
2
+
3
+ import { useQuery } from '@tanstack/react-query';
4
+ import { listSubscriptionsAction } from '../actions/list-subscriptions.action';
5
+ import { parseResult } from '../../../infra/utils/parser';
6
+ import { SubscriptionSchema } from '../types';
7
+ import { SUBSCRIPTIONS_QUERY_KEY } from './list-subscriptions.hook';
8
+ import { withAction } from '../../../utils/withAction';
9
+
10
+ export function useActiveSubscription() {
11
+ return useQuery({
12
+ queryKey: [...SUBSCRIPTIONS_QUERY_KEY, 'active'],
13
+ queryFn: async () =>
14
+ parseResult(
15
+ SubscriptionSchema,
16
+ await withAction(() => listSubscriptionsAction({ active: true }))()
17
+ ),
18
+ });
19
+ }
@@ -1,15 +1,17 @@
1
- 'use client';
2
-
3
- import { useQuery } from '@tanstack/react-query';
4
- import { listSubscriptionsAction } from '../actions/list-subscriptions.action';
5
- import { parseResult } from '../../../infra/utils/parser';
6
- import { SubscriptionSchema, type FindSubscriptionsParams } from '../types';
7
-
8
- export const SUBSCRIPTIONS_QUERY_KEY = ['subscriptions'];
9
-
10
- export function useSubscriptions(params?: Partial<FindSubscriptionsParams>) {
11
- return useQuery({
12
- queryKey: [...SUBSCRIPTIONS_QUERY_KEY, params],
13
- queryFn: async () => parseResult(SubscriptionSchema, await listSubscriptionsAction(params)),
14
- });
15
- }
1
+ 'use client';
2
+
3
+ import { useQuery } from '@tanstack/react-query';
4
+ import { listSubscriptionsAction } from '../actions/list-subscriptions.action';
5
+ import { parseResult } from '../../../infra/utils/parser';
6
+ import { SubscriptionSchema, type FindSubscriptionsParams } from '../types';
7
+ import { withAction } from '../../../utils/withAction';
8
+
9
+ export const SUBSCRIPTIONS_QUERY_KEY = ['subscriptions'];
10
+
11
+ export function useSubscriptions(params?: Partial<FindSubscriptionsParams>) {
12
+ return useQuery({
13
+ queryKey: [...SUBSCRIPTIONS_QUERY_KEY, params],
14
+ queryFn: async () =>
15
+ parseResult(SubscriptionSchema, await withAction(() => listSubscriptionsAction(params))()),
16
+ });
17
+ }
@@ -1,25 +1,8 @@
1
- 'use server';
2
-
3
- import { userService } from '../services/user.service';
4
- import { User } from '../schema';
5
-
6
- export async function findUserById(): Promise<{
7
- success: boolean;
8
- data?: User;
9
- error?: string;
10
- }> {
11
- try {
12
- const user = await userService.findById();
13
-
14
- return {
15
- success: true,
16
- data: user,
17
- };
18
- } catch (error) {
19
- console.error('[findUserById] Error:', error);
20
- return {
21
- success: false,
22
- error: error instanceof Error ? error.message : 'Erro desconhecido',
23
- };
24
- }
25
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { userService } from '../services/user.service';
5
+
6
+ export function findUserById() {
7
+ return safeServerAction(() => userService.findById());
8
+ }
@@ -1,10 +1,12 @@
1
- 'use server';
2
-
3
- import { messagesService } from '../services/messages.service';
4
- import type { FindMessagesParams, ListMessagesApiResponse } from '../schema/messages.schema';
5
-
6
- export async function listMessagesAction(
7
- params?: Partial<FindMessagesParams>
8
- ): Promise<ListMessagesApiResponse> {
9
- return messagesService.listMessages(params);
10
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { messagesService } from '../services/messages.service';
5
+ import type { FindMessagesParams } from '../schema/messages.schema';
6
+
7
+ export function listMessagesAction(params?: Partial<FindMessagesParams>) {
8
+ return safeServerAction(async () => {
9
+ const result = await messagesService.listMessages(params);
10
+ return { data: result.data, total: result.total, totalUnread: result.totalUnread };
11
+ });
12
+ }
@@ -1,8 +1,8 @@
1
- 'use server';
2
-
3
- import { messagesService } from '../services/messages.service';
4
- import type { MarkAllAsReadApiResponse } from '../schema/messages.schema';
5
-
6
- export async function markAllMessagesAsReadAction(): Promise<MarkAllAsReadApiResponse> {
7
- return messagesService.markAllAsRead();
8
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { messagesService } from '../services/messages.service';
5
+
6
+ export function markAllMessagesAsReadAction() {
7
+ return safeServerAction(() => messagesService.markAllAsRead());
8
+ }
@@ -1,8 +1,8 @@
1
- 'use server';
2
-
3
- import { messagesService } from '../services/messages.service';
4
- import type { MarkAsReadApiResponse } from '../schema/messages.schema';
5
-
6
- export async function markMessageAsReadAction(messageId: string): Promise<MarkAsReadApiResponse> {
7
- return messagesService.markAsRead(messageId);
8
- }
1
+ 'use server';
2
+
3
+ import { safeServerAction } from '../../../utils/safeServerAction';
4
+ import { messagesService } from '../services/messages.service';
5
+
6
+ export function markMessageAsReadAction(messageId: string) {
7
+ return safeServerAction(() => messagesService.markAsRead(messageId));
8
+ }