Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ff500b515 |
25 changed files with 261 additions and 75 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { LoginForm } from '@/packages/administrativo/components/GUsuario/GUsuarioLoginForm';
|
||||
import { LoginForm } from '@/packages/administrativo/components/User/UserLoginForm';
|
||||
|
||||
export default function LoginPage() {
|
||||
return (
|
||||
|
|
|
|||
|
|
@ -6,24 +6,24 @@ import { Card, CardContent } from '@/components/ui/card';
|
|||
import { Input } from '@/components/ui/input';
|
||||
import z from 'zod';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import GUsuarioLoginService from '@/packages/administrativo/services/GUsuario/GUsuarioLoginService';
|
||||
import { UserLoginService } from '@/packages/administrativo/services/User/UserLoginService';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { useState } from 'react';
|
||||
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '../../../../components/ui/form';
|
||||
import LoadingButton from '@/shared/components/loadingButton/LoadingButton';
|
||||
import { Button } from '../../../../components/ui/button';
|
||||
import { GUsuarioLoginSchema } from '@/packages/administrativo/schemas/GUsuario/GUsuarioLoginSchema';
|
||||
import { UserLoginSchema } from '@/packages/administrativo/schemas/User/UserLoginSchema';
|
||||
|
||||
type FormValues = z.infer<typeof GUsuarioLoginSchema>;
|
||||
type FormValues = z.infer<typeof UserLoginSchema>;
|
||||
|
||||
export function LoginForm({ className, ...props }: React.ComponentProps<'div'>) {
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(GUsuarioLoginSchema),
|
||||
resolver: zodResolver(UserLoginSchema),
|
||||
defaultValues: {
|
||||
login: '',
|
||||
senha_api: '',
|
||||
email: '',
|
||||
password: '',
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ export function LoginForm({ className, ...props }: React.ComponentProps<'div'>)
|
|||
setLoading(true);
|
||||
|
||||
// Realiza o login
|
||||
await GUsuarioLoginService(values);
|
||||
await UserLoginService(values);
|
||||
|
||||
// Removo o estado de loading do botão
|
||||
setLoading(false);
|
||||
|
|
@ -53,7 +53,7 @@ export function LoginForm({ className, ...props }: React.ComponentProps<'div'>)
|
|||
</div>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="login"
|
||||
name="email"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Login</FormLabel>
|
||||
|
|
@ -66,7 +66,7 @@ export function LoginForm({ className, ...props }: React.ComponentProps<'div'>)
|
|||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="senha_api"
|
||||
name="password"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Senha</FormLabel>
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
'use server';
|
||||
|
||||
import { Methods } from '@/shared/services/api/enums/ApiMethodEnum';
|
||||
import API from '@/shared/services/api/Api';
|
||||
|
||||
export default async function GUsuarioLoginData(form: any) {
|
||||
const api = new API();
|
||||
// Realiza o envio dos dados
|
||||
const response = await api.send({
|
||||
method: Methods.POST,
|
||||
endpoint: `administrativo/g_usuario/authenticate`,
|
||||
body: form,
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
20
src/packages/administrativo/data/User/UserDeleteData.ts
Normal file
20
src/packages/administrativo/data/User/UserDeleteData.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
'use server'
|
||||
|
||||
import { Methods } from '@/shared/services/api/enums/ApiMethodEnum';
|
||||
import API from '@/shared/services/api/Api';
|
||||
import { withClientErrorHandler } from '@/withClientErrorHandler/withClientErrorHandler';
|
||||
|
||||
async function executeUserDeleteData(usuarioId: number) {
|
||||
|
||||
const api = new API();
|
||||
|
||||
const response = await api.send({
|
||||
'method': Methods.DELETE,
|
||||
'endpoint': `administrativo/user/${usuarioId}`
|
||||
});
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
export const UserDeleteData = withClientErrorHandler(executeUserDeleteData)
|
||||
19
src/packages/administrativo/data/User/UserIndexData.ts
Normal file
19
src/packages/administrativo/data/User/UserIndexData.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
'use server'
|
||||
import { Methods } from '@/shared/services/api/enums/ApiMethodEnum';
|
||||
import API from '@/shared/services/api/Api';
|
||||
import { withClientErrorHandler } from '@/withClientErrorHandler/withClientErrorHandler';
|
||||
|
||||
async function executeUserIndexData() {
|
||||
|
||||
const api = new API();
|
||||
|
||||
const response = await api.send({
|
||||
'method': Methods.GET,
|
||||
'endpoint': `administrativo/user`
|
||||
});
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
export const UserIndexData = withClientErrorHandler(executeUserIndexData)
|
||||
21
src/packages/administrativo/data/User/UserLoginData.ts
Normal file
21
src/packages/administrativo/data/User/UserLoginData.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
'use server';
|
||||
|
||||
import { Methods } from '@/shared/services/api/enums/ApiMethodEnum';
|
||||
import API from '@/shared/services/api/Api';
|
||||
import { AuthenticateUserInterface } from '@/shared/interfaces/AuthenticateUserInterface';
|
||||
import { withClientErrorHandler } from '@/withClientErrorHandler/withClientErrorHandler';
|
||||
|
||||
async function executeUserLoginData(form: AuthenticateUserInterface) {
|
||||
const api = new API();
|
||||
|
||||
const response = await api.send({
|
||||
method: Methods.POST,
|
||||
endpoint: `administrativo/user/authenticate`,
|
||||
body: form,
|
||||
});
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
export const UserLoginData = withClientErrorHandler(executeUserLoginData)
|
||||
20
src/packages/administrativo/data/User/UserReadData.ts
Normal file
20
src/packages/administrativo/data/User/UserReadData.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
'use server'
|
||||
|
||||
import { Methods } from '@/shared/services/api/enums/ApiMethodEnum';
|
||||
import API from '@/shared/services/api/Api';
|
||||
import { withClientErrorHandler } from '@/withClientErrorHandler/withClientErrorHandler';
|
||||
|
||||
async function executeUserReadData(usuarioId: number) {
|
||||
|
||||
const api = new API();
|
||||
|
||||
const response = await api.send({
|
||||
'method': Methods.GET,
|
||||
'endpoint': `administrativo/user/${usuarioId}`
|
||||
});
|
||||
|
||||
return response
|
||||
|
||||
}
|
||||
|
||||
export const UserReadData = withClientErrorHandler(executeUserReadData)
|
||||
21
src/packages/administrativo/data/User/UserSaveData.ts
Normal file
21
src/packages/administrativo/data/User/UserSaveData.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
'use server'
|
||||
|
||||
import { Methods } from '@/shared/services/api/enums/ApiMethodEnum';
|
||||
import API from '@/shared/services/api/Api';
|
||||
import { withClientErrorHandler } from '@/withClientErrorHandler/withClientErrorHandler';
|
||||
|
||||
async function executeUserSaveData(form: any) {
|
||||
|
||||
const api = new API();
|
||||
|
||||
const response = await api.send({
|
||||
'method': Methods.POST,
|
||||
'endpoint': `administrativo/user/`,
|
||||
'body': form
|
||||
});
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
export const UserSaveData = withClientErrorHandler(executeUserSaveData)
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
'use client';
|
||||
|
||||
import GUsuarioLogoutService from '../../services/GUsuario/GUsuarioLogoutService';
|
||||
import UserLogoutService from '../../services/User/UserLogoutService';
|
||||
|
||||
export const useGUsuarioLogoutHook = () => {
|
||||
const logoutUsuario = async () => {
|
||||
await GUsuarioLogoutService('access_token');
|
||||
await UserLogoutService('access_token');
|
||||
};
|
||||
|
||||
return { logoutUsuario };
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
export default interface GUsuario {
|
||||
usuario_id: number;
|
||||
trocarsenha: string;
|
||||
login: string;
|
||||
senha: string;
|
||||
situacao: string;
|
||||
nome_completo: string;
|
||||
funcao: string;
|
||||
assina: string;
|
||||
sigla: string;
|
||||
usuario_tab: string;
|
||||
ultimo_login: string;
|
||||
ultimo_login_regs: string;
|
||||
data_expiracao: string;
|
||||
senha_anterior: string;
|
||||
andamento_padrao: string;
|
||||
lembrete_pergunta: string;
|
||||
lembrete_resposta: string;
|
||||
andamento_padrao2: string;
|
||||
receber_mensagem_arrolamento: string;
|
||||
email: string;
|
||||
assina_certidao: string;
|
||||
receber_email_penhora: string;
|
||||
foto: string;
|
||||
nao_receber_chat_todos: string;
|
||||
pode_alterar_caixa: string;
|
||||
receber_chat_certidao_online: string;
|
||||
receber_chat_cancelamento: string;
|
||||
cpf: string;
|
||||
somente_leitura: string;
|
||||
receber_chat_envio_onr: string;
|
||||
tipo_usuario: string;
|
||||
senha_api: string;
|
||||
}
|
||||
17
src/packages/administrativo/interfaces/User/UserInterface.ts
Normal file
17
src/packages/administrativo/interfaces/User/UserInterface.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { SituacoesEnum } from "@/shared/enums/SituacoesEnum";
|
||||
|
||||
export interface UserInterface {
|
||||
user_id: number;
|
||||
name: string;
|
||||
email: string;
|
||||
password: string; // Presumo que seja uma string com hash
|
||||
password_temp: string | null; // Pode ser "N" ou outro valor, ou null
|
||||
password_temp_confirm: string; // Pode ser "N"
|
||||
position: string | null; // Pode ser uma string ou null
|
||||
team: string; // Equipe onde o usuário pertence
|
||||
status: SituacoesEnum; // A ou I
|
||||
date_register: string; // Data de registro no formato ISO 8601
|
||||
date_update: string | null; // Data de atualização ou null
|
||||
user_id_create: number | null; // ID do usuário que criou (se aplicável)
|
||||
user_id_update: number | null; // ID do usuário que fez a última atualização (se aplicável)
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
import { z } from 'zod';
|
||||
|
||||
export const GUsuarioLoginSchema = z.object({
|
||||
login: z.string().min(1, 'O campo deve ser preenchido'),
|
||||
senha_api: z.string().min(1, 'O campo deve ser preenchido'),
|
||||
});
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import { z } from 'zod';
|
||||
|
||||
export const UserLoginSchema = z.object({
|
||||
email: z.string().min(1, 'O campo deve ser preenchido'),
|
||||
password: z.string().min(1, 'O campo deve ser preenchido'),
|
||||
});
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
'use server'
|
||||
|
||||
import { withClientErrorHandler } from "@/withClientErrorHandler/withClientErrorHandler"
|
||||
import { UserDeleteData } from "../../data/User/UserDeleteData"
|
||||
|
||||
async function executeUserDeleteService(usuarioId: number) {
|
||||
|
||||
if (usuarioId <= 0) {
|
||||
return {
|
||||
'code': 400,
|
||||
'message': 'Usuário informado inválido',
|
||||
}
|
||||
}
|
||||
|
||||
const response = await UserDeleteData(usuarioId)
|
||||
return response
|
||||
|
||||
}
|
||||
|
||||
export const UserDeleteService = withClientErrorHandler(executeUserDeleteService)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
'use server'
|
||||
|
||||
import { withClientErrorHandler } from "@/withClientErrorHandler/withClientErrorHandler";
|
||||
import { UserIndexData } from "../../data/User/UserIndexData";
|
||||
|
||||
async function executeUserIndexService() {
|
||||
|
||||
const response = await UserIndexData();
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
export const UserIndexService = withClientErrorHandler(executeUserIndexService)
|
||||
|
|
@ -2,14 +2,15 @@
|
|||
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
import GUsuarioLoginData from '../../data/GUsuario/GUsuarioLoginData';
|
||||
import { UserLoginData } from '../../data/User/UserLoginData';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { withClientErrorHandler } from '@/withClientErrorHandler/withClientErrorHandler';
|
||||
|
||||
export default async function GUsuarioLoginService(form: any) {
|
||||
async function executeUserLoginService(form: any) {
|
||||
// Obtem a resposta da requisição
|
||||
const response = await GUsuarioLoginData(form);
|
||||
const response = await UserLoginData(form);
|
||||
// Verifica se localizou o usuário
|
||||
if (response.data.usuario_id <= 0) {
|
||||
if (response.data.user_id <= 0) {
|
||||
return {
|
||||
code: 404,
|
||||
message: 'Não foi localizado o usuário',
|
||||
|
|
@ -29,5 +30,7 @@ export default async function GUsuarioLoginService(form: any) {
|
|||
});
|
||||
|
||||
// Redireciona para a págian desejada
|
||||
redirect('/servicos');
|
||||
redirect('/user');
|
||||
}
|
||||
|
||||
export const UserLoginService = withClientErrorHandler(executeUserLoginService)
|
||||
|
|
@ -1,10 +1,11 @@
|
|||
'use server';
|
||||
|
||||
import { withClientErrorHandler } from '@/withClientErrorHandler/withClientErrorHandler';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export default async function GUsuarioLogoutService(token: string) {
|
||||
async function executeUserLogoutService(token: string) {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.set(token, '', {
|
||||
expires: new Date(0),
|
||||
|
|
@ -13,3 +14,5 @@ export default async function GUsuarioLogoutService(token: string) {
|
|||
|
||||
redirect('/login');
|
||||
}
|
||||
|
||||
export const UserLogoutService = withClientErrorHandler(executeUserLogoutService)
|
||||
22
src/packages/administrativo/services/User/UserReadService.ts
Normal file
22
src/packages/administrativo/services/User/UserReadService.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
'use server'
|
||||
|
||||
import { withClientErrorHandler } from "@/withClientErrorHandler/withClientErrorHandler";
|
||||
import { UserReadData } from "../../data/User/UserReadData";
|
||||
|
||||
async function executeUserReadService(usuarioId: number) {
|
||||
|
||||
// Verifica se o id informado é válido
|
||||
if (usuarioId <= 0) {
|
||||
return {
|
||||
'code': 400,
|
||||
'message': 'Usuário informado inválido',
|
||||
}
|
||||
}
|
||||
|
||||
const response = await UserReadData(usuarioId);
|
||||
return response
|
||||
|
||||
|
||||
}
|
||||
|
||||
export const UserReadService = withClientErrorHandler(executeUserReadService)
|
||||
12
src/packages/administrativo/services/User/UserSaveService.ts
Normal file
12
src/packages/administrativo/services/User/UserSaveService.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
'use server'
|
||||
|
||||
import { withClientErrorHandler } from "@/withClientErrorHandler/withClientErrorHandler";
|
||||
import { UserSaveData } from "../../data/User/UserSaveData";
|
||||
|
||||
async function executeUserSave(form: any) {
|
||||
|
||||
return await UserSaveData(form);
|
||||
|
||||
}
|
||||
|
||||
export const UserSave = withClientErrorHandler(executeUserSave)
|
||||
4
src/shared/interfaces/AuthenticateUserInterface.ts
Normal file
4
src/shared/interfaces/AuthenticateUserInterface.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export interface AuthenticateUserInterface {
|
||||
email: string;
|
||||
password: string
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
export default interface GUsuarioAuthenticatedInterface {
|
||||
usuario_id?: number;
|
||||
user_id?: number;
|
||||
login?: string;
|
||||
nome?: string;
|
||||
email?: string;
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ export default class API {
|
|||
? Object.fromEntries(Object.entries(_data.body).filter(([_, v]) => v != null && v !== ''))
|
||||
: null;
|
||||
|
||||
console.log(`${this.ApiSchema.url}${this.ApiSchema.prefix}${this.ApiSchema.endpoint}`)
|
||||
// Realiza a requisição
|
||||
const response = await fetch(
|
||||
`${this.ApiSchema.url}${this.ApiSchema.prefix}${this.ApiSchema.endpoint}`,
|
||||
|
|
|
|||
32
src/withClientErrorHandler/withClientErrorHandler.ts
Normal file
32
src/withClientErrorHandler/withClientErrorHandler.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import withClientErrorHandlerInterface from "./withClientErrorHandlerInterface";
|
||||
|
||||
/**
|
||||
* Códigos de erro que começam com 6, são do front entd, na ordem do alfabeto o F de frontend é a sexta letra
|
||||
*/
|
||||
export function withClientErrorHandler<T extends (...args: any[]) => Promise<any>>(
|
||||
action: T
|
||||
) {
|
||||
return async (...args: Parameters<T>): Promise<withClientErrorHandlerInterface> => {
|
||||
|
||||
try {
|
||||
|
||||
// Executa a função definida
|
||||
const data = await action(...args);
|
||||
|
||||
// Retorna exatamente a mesma resposta retornada pela função
|
||||
return data;
|
||||
|
||||
} catch (error: any) {
|
||||
|
||||
// Retorna o erro de execuçãformatado
|
||||
return {
|
||||
status: 600,
|
||||
message: error?.message || "Erro interno do servidor",
|
||||
data: error
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
export default interface withClientErrorHandlerInterface<T = any> {
|
||||
|
||||
status: number;
|
||||
data?: T;
|
||||
message?: string;
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue