started messages migration to ts

This commit is contained in:
canove
2020-09-19 22:49:27 -03:00
parent 70c391e8ef
commit 9368ef2512
10 changed files with 295 additions and 230 deletions

View File

@@ -16,16 +16,6 @@ const ListContactsService = async ({
searchParam = "",
pageNumber = "1"
}: Request): Promise<Response> => {
let includeCondition = [
{
model: Contact,
as: "contact",
attributes: ["name", "number", "profilePicUrl"]
}
];
// let whereCondition = {};
const whereCondition = {
[Op.or]: [
{
@@ -43,7 +33,6 @@ const ListContactsService = async ({
const { count, rows: contacts } = await Contact.findAndCountAll({
where: whereCondition,
include: includeCondition,
limit,
offset,
order: [["createdAt", "DESC"]]

View File

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

View File

@@ -0,0 +1,60 @@
import { where, fn, col } from "sequelize";
import Message from "../../models/Message";
import Ticket from "../../models/Ticket";
import FindTicketService from "../TicketServices/FindTicketService";
interface Request {
ticketId: string;
searchParam?: string;
pageNumber?: string;
}
interface Response {
messages: Message[];
ticket: Ticket;
count: number;
hasMore: boolean;
}
const ListMessagesService = async ({
searchParam = "",
pageNumber = "1",
ticketId
}: Request): Promise<Response> => {
const whereCondition = {
body: where(
fn("LOWER", col("body")),
"LIKE",
`%${searchParam.toLowerCase()}%`
),
ticketId
};
const ticket = await FindTicketService({ where: { id: +ticketId } });
if (!ticket) {
throw new Error("No ticket found with this ID");
}
// await setMessagesAsRead(ticket);
const limit = 20;
const offset = limit * (+pageNumber - 1);
const { count, rows: messages } = await Message.findAndCountAll({
where: whereCondition,
limit,
offset,
order: [["createdAt", "DESC"]]
});
const hasMore = count > offset + messages.length;
return {
messages: messages.reverse(),
ticket,
count,
hasMore
};
};
export default ListMessagesService;

View File

@@ -1,28 +1,45 @@
import Whatsapp from "../../models/Whatsapp";
import Ticket from "../../models/Ticket";
import AppError from "../../errors/AppError";
import Contact from "../../models/Contact";
import User from "../../models/User";
interface WhereParams {
id?: number;
name?: string;
isDefault?: boolean;
status?: string;
userId?: number;
contactId?: number;
whatsappId?: number;
}
interface Request {
where?: WhereParams;
}
const FindTicketService = async ({ where }: Request): Promise<Whatsapp> => {
const FindTicketService = async ({ where }: Request): Promise<Ticket> => {
const whereCondition = { ...where };
const whatsapp = await Whatsapp.findOne({
where: whereCondition
const ticket = await Ticket.findOne({
where: whereCondition,
include: [
{
model: Contact,
as: "contact",
attributes: ["id", "name", "number", "profilePicUrl"],
include: ["extraInfo"]
},
{
model: User,
as: "user",
attributes: ["id", "name"]
}
]
});
if (!whatsapp) {
throw new AppError("No whatsapp found with this conditions.", 404);
if (!ticket) {
throw new AppError("No ticket found with this conditions.", 404);
}
return whatsapp;
return ticket;
};
export default FindTicketService;

View File

@@ -35,7 +35,8 @@ const ListTicketsService = async ({
{
model: Contact,
as: "contact",
attributes: ["name", "number", "profilePicUrl"]
attributes: ["id", "name", "number", "profilePicUrl"],
include: ["extraInfo"]
}
];