diff --git a/backend/src/app.ts b/backend/src/app.ts index a369052..5aa2a5c 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -6,8 +6,6 @@ import cors from "cors"; import cookieParser from "cookie-parser"; import * as Sentry from "@sentry/node"; -console.log('dddddddddddddddddddddddddddd') - import "./database"; import uploadConfig from "./config/upload"; import AppError from "./errors/AppError"; @@ -41,5 +39,6 @@ app.use(async (err: Error, req: Request, res: Response, _: NextFunction) => { logger.error(err); return res.status(500).json({ error: "Internal server error" }); }); + export default app; diff --git a/backend/src/controllers/ReportController.ts b/backend/src/controllers/ReportController.ts new file mode 100644 index 0000000..56f5c80 --- /dev/null +++ b/backend/src/controllers/ReportController.ts @@ -0,0 +1,30 @@ +//relatorio + +import { Request, Response } from "express"; +import AppError from "../errors/AppError"; +import ShowTicketReport from "../services/TicketServices/ShowTicketReport"; + + +export const reportUser = async (req: Request, res: Response): Promise => { + + const { userId } = req.params; + + console.log('******* Estrou no report: ', userId) + + const test = await ShowTicketReport(userId); + + return res.status(200).json(test); + +}; + +/*export const reportAll = async (req: Request, res: Response): Promise => { + + const test = await ShowTicketReport(); + + return res.status(200).json(test); + +};*/ + + + + \ No newline at end of file diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index 7ed8c95..1dddfa6 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -3,7 +3,7 @@ import { getIO } from "../libs/socket"; import CreateTicketService from "../services/TicketServices/CreateTicketService"; import DeleteTicketService from "../services/TicketServices/DeleteTicketService"; -import ListTicketsService from "../services/TicketServices/ListTicketsService"; +import ListTicketsService from "../services/TicketServices/ListTicketsService"; import ShowTicketService from "../services/TicketServices/ShowTicketService"; import UpdateTicketService from "../services/TicketServices/UpdateTicketService"; import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage"; @@ -76,12 +76,15 @@ export const store = async (req: Request, res: Response): Promise => { return res.status(200).json(ticket); }; + export const show = async (req: Request, res: Response): Promise => { const { ticketId } = req.params; const contact = await ShowTicketService(ticketId); + //console.log('------- contact: ',JSON.stringify(contact)) + return res.status(200).json(contact); }; diff --git a/backend/src/controllers/UserController.ts b/backend/src/controllers/UserController.ts index 988ff95..a04de26 100644 --- a/backend/src/controllers/UserController.ts +++ b/backend/src/controllers/UserController.ts @@ -31,8 +31,8 @@ export const index = async (req: Request, res: Response): Promise => { if(user.profile!=='master'){ auxUsers.push(user) } - } - + } + return res.json({ users: auxUsers, count, hasMore }); } diff --git a/backend/src/routes/index.ts b/backend/src/routes/index.ts index ee09ebe..ab2d36e 100644 --- a/backend/src/routes/index.ts +++ b/backend/src/routes/index.ts @@ -4,26 +4,29 @@ import userRoutes from "./userRoutes"; import authRoutes from "./authRoutes"; import settingRoutes from "./settingRoutes"; import contactRoutes from "./contactRoutes"; -import ticketRoutes from "./ticketRoutes"; +import ticketRoutes from "./ticketRoutes"; import whatsappRoutes from "./whatsappRoutes"; import messageRoutes from "./messageRoutes"; import whatsappSessionRoutes from "./whatsappSessionRoutes"; import queueRoutes from "./queueRoutes"; import quickAnswerRoutes from "./quickAnswerRoutes"; +import reportRoutes from "./reportRoutes"; -const routes = Router(); - +const routes = Router(); routes.use(userRoutes); routes.use("/auth", authRoutes); routes.use(settingRoutes); -routes.use(contactRoutes); -routes.use(ticketRoutes); +routes.use(contactRoutes); +routes.use(ticketRoutes); routes.use(whatsappRoutes); routes.use(messageRoutes); routes.use(whatsappSessionRoutes); routes.use(queueRoutes); routes.use(quickAnswerRoutes); +routes.use(reportRoutes); + + export default routes; diff --git a/backend/src/routes/reportRoutes.ts b/backend/src/routes/reportRoutes.ts new file mode 100644 index 0000000..c05f6f4 --- /dev/null +++ b/backend/src/routes/reportRoutes.ts @@ -0,0 +1,14 @@ +//relatorio +import express from "express"; + +import isAuth from "../middleware/isAuth"; + +import * as ReportController from "../controllers/ReportController"; + +const reportRoutes = express.Router(); + +reportRoutes.get("/reports/:userId", /*isAuth,*/ ReportController.reportUser); + +//reportRoutes.get("/reports", /*isAuth,*/ ReportController.reportAll); + +export default reportRoutes; diff --git a/backend/src/routes/ticketRoutes.ts b/backend/src/routes/ticketRoutes.ts index eef72ee..742d375 100644 --- a/backend/src/routes/ticketRoutes.ts +++ b/backend/src/routes/ticketRoutes.ts @@ -3,11 +3,10 @@ import isAuth from "../middleware/isAuth"; import * as TicketController from "../controllers/TicketController"; -const ticketRoutes = express.Router(); - +const ticketRoutes = express.Router(); -ticketRoutes.get("/tickets", isAuth, TicketController.index); +ticketRoutes.get("/tickets", isAuth, TicketController.index); ticketRoutes.get("/tickets/:ticketId", isAuth, TicketController.show); diff --git a/backend/src/services/TicketServices/ShowTicketReport.ts b/backend/src/services/TicketServices/ShowTicketReport.ts new file mode 100644 index 0000000..68505b9 --- /dev/null +++ b/backend/src/services/TicketServices/ShowTicketReport.ts @@ -0,0 +1,133 @@ +import Ticket from "../../models/Ticket"; +import AppError from "../../errors/AppError"; +import Contact from "../../models/Contact"; +import User from "../../models/User"; +import Queue from "../../models/Queue"; + +import Message from "../../models/Message"; +import { userInfo } from "os"; + +//Report by user +const ShowTicketReport = async (id: string | number): Promise => { + + + + const ticket = await Ticket.findAll({ + where: {userid: id} , + attributes: ['id', 'status', 'createdAt', 'updatedAt'], + include: [ + { + model: Message, + required:true, + separate: true, + attributes: ['body', 'read', 'mediaType', 'mediaUrl','createdAt'], + order: [ + ['createdAt', 'ASC'] + ] + }, + { + model: Contact, + required:true, + attributes: ['name', 'number'] + }, + { + model: User, + required:true, + attributes: ['name', 'email'] + }, + { + model: Queue, + required:true, + attributes: ['name'] + }, + + ], + + + + }); + + + + + + /*const ticket = await Message.findAll({ + //raw: true, + where:{''}, + attributes: ['body', 'createdAt', 'ticketId'], + include: [ + { + model: Ticket, + attributes: ['contactid', 'userid', 'queueid', 'status'], + include:[ + { + model: Contact, + attributes: ['name', 'number'] + }, + { + model: User, + attributes: ['name', 'email'] + }, + { + model: Queue, + attributes: ['name'] + } + ], + }, + + ], + order: + ['ticketId', 'createdAt'] + + }); */ + + + if (!ticket) { + throw new AppError("ERR_NO_TICKET_FOUND", 404); + } + + return ticket; +}; + + +// Report all +/*const ShowTicketReport = async (): Promise => { + + const ticket = await Message.findAll({ + //raw: true, + attributes: ['body', 'createdAt', 'ticketId'], + include: [ + { + model: Ticket, + attributes: ['contactid', 'userid', 'queueid', 'status'], + include:[ + { + model: Contact, + attributes: ['name', 'number'] + }, + { + model: User, + attributes: ['name', 'email'] + }, + { + model: Queue, + attributes: ['name'] + } + ], + }, + + ], + order: + ['ticketId', 'createdAt'] + + }); + + + if (!ticket) { + throw new AppError("ERR_NO_TICKET_FOUND", 404); + } + + return ticket; +};*/ + +export default ShowTicketReport;