changed find to show services

This commit is contained in:
canove
2020-09-20 11:27:58 -03:00
parent 4fda0b18c6
commit 02d90af657
13 changed files with 40 additions and 59 deletions

View File

@@ -1,17 +0,0 @@
import Contact from "../../models/Contact";
import AppError from "../../errors/AppError";
const FindContactService = async (id: string): Promise<Contact> => {
const user = await Contact.findOne({
where: { id },
attributes: ["id", "name", "number", "email"]
});
if (!user) {
throw new AppError("No contact found with this ID.", 404);
}
return user;
};
export default FindContactService;

View File

@@ -0,0 +1,14 @@
import Contact from "../../models/Contact";
import AppError from "../../errors/AppError";
const ShowContactService = async (id: string | number): Promise<Contact> => {
const user = await Contact.findByPk(id);
if (!user) {
throw new AppError("No contact found with this ID.", 404);
}
return user;
};
export default ShowContactService;

View File

@@ -1,5 +1,5 @@
import Message from "../../models/Message";
import FindTicketService from "../TicketServices/FindTicketService";
import ShowTicketService from "../TicketServices/ShowTicketService";
interface Request {
ticketId: string;
@@ -10,7 +10,7 @@ const CreateMessageService = async ({
messageData,
ticketId
}: Request): Promise<Message> => {
const ticket = await FindTicketService({ where: { id: +ticketId } });
const ticket = await ShowTicketService(ticketId);
if (!ticket) {
throw new Error("No ticket found with this ID");

View File

@@ -1,7 +1,7 @@
import { where, fn, col } from "sequelize";
import Message from "../../models/Message";
import Ticket from "../../models/Ticket";
import FindTicketService from "../TicketServices/FindTicketService";
import ShowTicketService from "../TicketServices/ShowTicketService";
interface Request {
ticketId: string;
@@ -30,7 +30,7 @@ const ListMessagesService = async ({
ticketId
};
const ticket = await FindTicketService({ where: { id: +ticketId } });
const ticket = await ShowTicketService(ticketId);
if (!ticket) {
throw new Error("No ticket found with this ID");

View File

@@ -1,5 +1,5 @@
import AppError from "../../errors/AppError";
import GetDefaultWhatsapp from "../../helpers/GetDefaultWhatsapp";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import Ticket from "../../models/Ticket";
interface Request {
@@ -11,7 +11,7 @@ const CreateTicketService = async ({
contactId,
status
}: Request): Promise<Ticket> => {
const defaultWhatsapp = await GetDefaultWhatsapp();
const defaultWhatsapp = await GetDefaultWhatsApp();
if (!defaultWhatsapp) {
throw new AppError("No default WhatsApp found. Check Connection page.");

View File

@@ -3,23 +3,8 @@ import AppError from "../../errors/AppError";
import Contact from "../../models/Contact";
import User from "../../models/User";
interface WhereParams {
id?: number;
status?: string;
userId?: number;
contactId?: number;
whatsappId?: number;
}
interface Request {
where?: WhereParams;
}
const FindTicketService = async ({ where }: Request): Promise<Ticket> => {
const whereCondition = { ...where };
const ticket = await Ticket.findOne({
where: whereCondition,
const ShowTicketService = async (id: string | number): Promise<Ticket> => {
const ticket = await Ticket.findByPk(id, {
include: [
{
model: Contact,
@@ -42,4 +27,4 @@ const FindTicketService = async ({ where }: Request): Promise<Ticket> => {
return ticket;
};
export default FindTicketService;
export default ShowTicketService;

View File

@@ -1,9 +1,10 @@
import User from "../../models/User";
import AppError from "../../errors/AppError";
const FindUserService = async (id: string): Promise<User | undefined> => {
const user = await User.findOne({
where: { id },
const ShowUserService = async (
id: string | number
): Promise<User | undefined> => {
const user = await User.findByPk(id, {
attributes: ["name", "id", "email", "profile"]
});
@@ -14,4 +15,4 @@ const FindUserService = async (id: string): Promise<User | undefined> => {
return user;
};
export default FindUserService;
export default ShowUserService;

View File

@@ -1,7 +1,7 @@
import Whatsapp from "../../models/Whatsapp";
import AppError from "../../errors/AppError";
const FindWhatsAppService = async (
const ShowWhatsAppService = async (
id: string | number
): Promise<Whatsapp | undefined> => {
const whatsapp = await Whatsapp.findByPk(id);
@@ -13,4 +13,4 @@ const FindWhatsAppService = async (
return whatsapp;
};
export default FindWhatsAppService;
export default ShowWhatsAppService;