@greatapps/common 1.1.71 → 1.1.73
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.
- package/dist/components/layouts/AppNavBar.mjs +0 -2
- package/dist/components/layouts/AppNavBar.mjs.map +1 -1
- package/dist/components/layouts/NotificationItem.mjs +4 -7
- package/dist/components/layouts/NotificationItem.mjs.map +1 -1
- package/dist/components/layouts/NotificationsPopover.mjs +22 -12
- package/dist/components/layouts/NotificationsPopover.mjs.map +1 -1
- package/dist/index.mjs +151 -21
- package/dist/index.mjs.map +1 -1
- package/dist/modules/auth/actions/login.action.mjs +1 -1
- package/dist/modules/auth/actions/login.action.mjs.map +1 -1
- package/dist/modules/auth/hooks/login.hook.mjs +5 -1
- package/dist/modules/auth/hooks/login.hook.mjs.map +1 -1
- package/dist/modules/auth/hooks/useUserQuery.mjs +2 -1
- package/dist/modules/auth/hooks/useUserQuery.mjs.map +1 -1
- package/dist/modules/auth/services/auth.service.mjs +2 -2
- package/dist/modules/auth/services/auth.service.mjs.map +1 -1
- package/dist/modules/users/action/list-messages.action.mjs +9 -0
- package/dist/modules/users/action/list-messages.action.mjs.map +1 -0
- package/dist/modules/users/action/mark-all-messages-as-read.action.mjs +9 -0
- package/dist/modules/users/action/mark-all-messages-as-read.action.mjs.map +1 -0
- package/dist/modules/users/action/mark-message-as-read.action.mjs +9 -0
- package/dist/modules/users/action/mark-message-as-read.action.mjs.map +1 -0
- package/dist/modules/users/hooks/messages.hook.mjs +38 -0
- package/dist/modules/users/hooks/messages.hook.mjs.map +1 -0
- package/dist/modules/users/schema/messages.schema.mjs +21 -0
- package/dist/modules/users/schema/messages.schema.mjs.map +1 -0
- package/dist/modules/users/services/messages.service.mjs +35 -0
- package/dist/modules/users/services/messages.service.mjs.map +1 -0
- package/dist/providers/auth.provider.mjs +3 -5
- package/dist/providers/auth.provider.mjs.map +1 -1
- package/dist/utils/safeServerAction.mjs +22 -0
- package/dist/utils/safeServerAction.mjs.map +1 -0
- package/package.json +1 -1
- package/src/components/layouts/AppNavBar.tsx +0 -4
- package/src/components/layouts/NotificationItem.tsx +2 -2
- package/src/components/layouts/NotificationsPopover.tsx +25 -22
- package/src/index.ts +267 -117
- package/src/modules/auth/actions/login.action.ts +1 -1
- package/src/modules/auth/hooks/login.hook.tsx +6 -1
- package/src/modules/auth/hooks/useUserQuery.ts +2 -1
- package/src/modules/auth/schema.ts +6 -4
- package/src/modules/auth/services/auth.service.ts +2 -2
- package/src/modules/users/action/list-messages.action.ts +10 -0
- package/src/modules/users/action/mark-all-messages-as-read.action.ts +8 -0
- package/src/modules/users/action/mark-message-as-read.action.ts +8 -0
- package/src/modules/users/hooks/messages.hook.ts +39 -0
- package/src/modules/users/schema/messages.schema.ts +46 -0
- package/src/modules/users/services/messages.service.ts +48 -0
- package/src/providers/auth.provider.tsx +6 -9
- package/src/utils/safeServerAction.ts +27 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export const MessageSchema = z.object({
|
|
4
|
+
id: z.string(),
|
|
5
|
+
deleted: z.number(),
|
|
6
|
+
datetime_alt: z.string().nullable(),
|
|
7
|
+
datetime_del: z.string().nullable(),
|
|
8
|
+
datetime_add: z.string(),
|
|
9
|
+
id_wl: z.number(),
|
|
10
|
+
id_account: z.number(),
|
|
11
|
+
id_user: z.number(),
|
|
12
|
+
status: z.number().nullable(),
|
|
13
|
+
type: z.string(),
|
|
14
|
+
subject: z.string(),
|
|
15
|
+
message: z.string(),
|
|
16
|
+
to: z.string(),
|
|
17
|
+
url: z.string(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export type Message = z.infer<typeof MessageSchema>;
|
|
21
|
+
|
|
22
|
+
export interface FindMessagesParams {
|
|
23
|
+
id_user: number;
|
|
24
|
+
type: string;
|
|
25
|
+
limit: number;
|
|
26
|
+
page: number;
|
|
27
|
+
order: 'asc' | 'desc';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ListMessagesApiResponse {
|
|
31
|
+
status: 0 | 1;
|
|
32
|
+
total: number;
|
|
33
|
+
data: Message[];
|
|
34
|
+
totalUnread: number;
|
|
35
|
+
message?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface MarkAsReadApiResponse {
|
|
39
|
+
status: 0 | 1;
|
|
40
|
+
message?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface MarkAllAsReadApiResponse {
|
|
44
|
+
status: 0 | 1;
|
|
45
|
+
message?: string;
|
|
46
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import 'server-only';
|
|
2
|
+
|
|
3
|
+
import { api } from '../../../infra/api/client';
|
|
4
|
+
import { buildQueryParams } from '../../../infra/utils/params';
|
|
5
|
+
import { getUserContext } from '../../auth/utils/get-user-context';
|
|
6
|
+
import {
|
|
7
|
+
FindMessagesParams,
|
|
8
|
+
ListMessagesApiResponse,
|
|
9
|
+
MarkAsReadApiResponse,
|
|
10
|
+
MarkAllAsReadApiResponse,
|
|
11
|
+
} from '../schema/messages.schema';
|
|
12
|
+
|
|
13
|
+
class MessagesService {
|
|
14
|
+
async listMessages(
|
|
15
|
+
params?: Partial<FindMessagesParams>
|
|
16
|
+
): Promise<ListMessagesApiResponse> {
|
|
17
|
+
const { id_account, id_user } = await getUserContext();
|
|
18
|
+
|
|
19
|
+
const query = buildQueryParams({
|
|
20
|
+
id_account,
|
|
21
|
+
id_user,
|
|
22
|
+
type: 'notification',
|
|
23
|
+
limit: 20,
|
|
24
|
+
page: 1,
|
|
25
|
+
order: 'desc',
|
|
26
|
+
...params,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const url = `/accounts/${id_account}/messages${query ? `?${query}` : ''}`;
|
|
30
|
+
return api.apps.get<ListMessagesApiResponse>(url);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async markAsRead(messageId: string): Promise<MarkAsReadApiResponse> {
|
|
34
|
+
const { id_account } = await getUserContext();
|
|
35
|
+
|
|
36
|
+
const url = `/accounts/${id_account}/messages/${messageId}/action/markAsRead`;
|
|
37
|
+
return api.apps.put<MarkAsReadApiResponse>(url);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async markAllAsRead(): Promise<MarkAllAsReadApiResponse> {
|
|
41
|
+
const { id_account, id_user } = await getUserContext();
|
|
42
|
+
|
|
43
|
+
const url = `/accounts/${id_account}/messages/action/markAllAsRead`;
|
|
44
|
+
return api.apps.put<MarkAllAsReadApiResponse>(url, { id_user });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const messagesService = new MessagesService();
|
|
@@ -23,9 +23,9 @@ import {
|
|
|
23
23
|
} from "../modules/auth/schema";
|
|
24
24
|
|
|
25
25
|
export type LoginResult =
|
|
26
|
-
| {
|
|
27
|
-
| {
|
|
28
|
-
| {
|
|
26
|
+
| { result: "success" }
|
|
27
|
+
| { result: "two_factor_required"; cookie: string }
|
|
28
|
+
| { result: "error"; message: string };
|
|
29
29
|
import {
|
|
30
30
|
useCurrentAccount,
|
|
31
31
|
useInvalidateAccount,
|
|
@@ -75,11 +75,8 @@ export function AuthProvider({ children }: AuthProviderProps) {
|
|
|
75
75
|
async (credentials: LoginRequest): Promise<LoginResult> => {
|
|
76
76
|
const data = await loginMutate(credentials);
|
|
77
77
|
|
|
78
|
-
if (data.
|
|
79
|
-
return {
|
|
80
|
-
|
|
81
|
-
if (data.twoFactorRequired) {
|
|
82
|
-
return { status: "two_factor_required", cookie: data.cookie };
|
|
78
|
+
if (data.result === "two_factor_required") {
|
|
79
|
+
return { result: "two_factor_required", cookie: data.cookie };
|
|
83
80
|
}
|
|
84
81
|
|
|
85
82
|
invalidateUser();
|
|
@@ -87,7 +84,7 @@ export function AuthProvider({ children }: AuthProviderProps) {
|
|
|
87
84
|
|
|
88
85
|
setUserData(data.user);
|
|
89
86
|
|
|
90
|
-
return {
|
|
87
|
+
return { result: "success" };
|
|
91
88
|
},
|
|
92
89
|
[setUserData, invalidateUser, invalidateAccount]
|
|
93
90
|
);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export class ServerActionError extends Error {
|
|
2
|
+
status: number;
|
|
3
|
+
|
|
4
|
+
constructor(message: string, status: number) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'ServerActionError';
|
|
7
|
+
this.status = status;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function safeServerAction<
|
|
12
|
+
TArgs extends any[],
|
|
13
|
+
TResult extends Record<string, any>,
|
|
14
|
+
>(
|
|
15
|
+
action: (...args: TArgs) => Promise<TResult>,
|
|
16
|
+
...args: TArgs
|
|
17
|
+
): Promise<Omit<TResult, 'error'>> {
|
|
18
|
+
const result: TResult = await action(...args);
|
|
19
|
+
|
|
20
|
+
if (result.status === 0 || result.result === 'error' || result.error) {
|
|
21
|
+
const msg = result.message || result.error || 'Erro na ação';
|
|
22
|
+
throw new ServerActionError(msg, result.status || 0);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const { error, ...successData } = result;
|
|
26
|
+
return successData as Omit<TResult, 'error'>;
|
|
27
|
+
}
|