ÿØÿà JFIF ` ` ÿþ
|
Server : Apache System : Linux cloud.heroica.com.br 4.18.0-553.36.1.el8_10.x86_64 #1 SMP Wed Jan 22 03:07:54 EST 2025 x86_64 User : farolpborg ( 1053) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /var/www/node_services/sebraevisitas/src/controllers/ |
Upload File : |
import { NextFunction, Request, Response } from "express";
import UserServices from "../services/UserServices";
const UserControllers = class {
static async create(req: Request, res: Response, next: NextFunction) {
try {
// Normalize payload coming from frontend UserContext
const body = req.body || {};
const normalized = {
name: body.name,
login: body.login || body.cpf, // fallback
password: body.password,
confirmPassword: body.confirmPassword,
email: body.email,
cpf: body.cpf,
empresa: body.empresa,
profileImage: body.profileImage || "",
profileId: body.profileId || body.profile, // allow either
active: body.active,
escolaridade: body.escolaridade,
datanasc:body.datanasc,
nomesocial:body.nomesocial || '',
possuinomesocial:body.possuinomesocial,
profile:body.profile ,
sexo:body.sexo,
createdBy:body.createdBy || '',
} as any;
console.log( 'normalized -> ' ,normalized);
const user = await UserServices.create(normalized);
return res.status(201).json(user);
} catch (err) {
return next(err);
}
}
static async list(_req: Request, res: Response, next: NextFunction) {
try {
const users = await UserServices.list();
return res.json(users);
} catch (err) {
return next(err);
}
}
static async getById(req: Request, res: Response, next: NextFunction) {
try {
const { id } = req.params;
const user = await UserServices.getById(id);
return res.json(user);
} catch (err) {
return next(err);
}
}
static async update(req: Request, res: Response, next: NextFunction) {
try {
const { id } = req.params;
const body = req.body || {};
const normalized = {
...body,
profileId: body.profileId || body.profile,
} as any;
const updated = await UserServices.update(id, normalized);
return res.json(updated);
} catch (err) {
return next(err);
}
}
static async remove(req: Request, res: Response, next: NextFunction) {
try {
const { id } = req.params;
const result = await UserServices.remove(id);
return res.json(result);
} catch (err) {
return next(err);
}
}
static async login(req: Request, res: Response, next: NextFunction) {
try {
console.log(req.body);
const { cpf, password } = req.body || {};
const user = await UserServices.login(cpf, password);
return res.json(user);
} catch (err) {
return next(err);
}
}
static async getEdit(req: Request, res: Response, next: NextFunction) {
try {
const { id } = req.params;
const user = await UserServices.getById(id);
return res.json(user);
} catch (err) {
return next(err);
}
}
static async updateByBody(req: Request, res: Response, next: NextFunction) {
try {
const body = req.body || {};
const { id } = body;
if (!id) throw new Error("ID é obrigatório");
const normalized = {
...body,
profileId: body.profileId || body.profile,
} as any;
const updated = await UserServices.update(id, normalized);
return res.json(updated);
} catch (err) {
return next(err);
}
}
static async setUserProfile(req: Request, res: Response, next: NextFunction) {
try {
const {id} = req.params;
const {profileId} = req.body;
const user = await UserServices.setUserProfile(id, profileId);
return res.json(user);
}catch (err){
return next(err);
}
}
};
export default UserControllers;