-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
2,474 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"config": "src/config/database.ts", | ||
"migrations-path": "src/database/migrations", | ||
"models-path": "src/models", | ||
"seeders-path": "src/database/seeders" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Request, Response } from "express"; | ||
import { setChannelWebhook } from "../helpers/setChannelHubWebhook"; | ||
import CreateChannelsService from "../services/HubServices/CreateHubChannelsService"; | ||
import ListChannels from "../services/HubServices/ListHubChannels"; | ||
|
||
export interface IChannel { | ||
name: string; | ||
status?: string; | ||
isDefault?: boolean; | ||
qrcode?: string; | ||
type?: string; | ||
channel?: string; | ||
id?: string; | ||
} | ||
|
||
export const store = async (req: Request, res: Response): Promise<Response> => { | ||
const { whatsapps } = await CreateChannelsService(req.body); | ||
|
||
whatsapps.forEach(whatsapp => { | ||
setTimeout(() => { | ||
setChannelWebhook(whatsapp, whatsapp.id.toString()); | ||
}, 2000); | ||
}); | ||
|
||
return res.status(200).json(whatsapps); | ||
}; | ||
|
||
export const index = async (req: Request, res: Response): Promise<Response> => { | ||
try { | ||
const channels = await ListChannels(); | ||
return res.status(200).json(channels); | ||
} catch (error) { | ||
return res.status(500).json({ error }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Request, Response } from "express"; | ||
import { getIO } from "../libs/socket"; | ||
import Contact from "../models/Contact"; | ||
import Ticket from "../models/Ticket"; | ||
import Whatsapp from "../models/Whatsapp"; | ||
import CreateHubTicketService from "../services/HubServices/CreateHubTicketService"; | ||
import { SendMediaMessageService } from "../services/HubServices/SendMediaMessageHubService"; | ||
import { SendTextMessageService } from "../services/HubServices/SendTextMessageHubService"; | ||
|
||
interface TicketData { | ||
contactId: number; | ||
status: string; | ||
queueId: number; | ||
userId: number; | ||
channel: string; | ||
} | ||
|
||
export const send = async (req: Request, res: Response): Promise<Response> => { | ||
const { body: message } = req.body; | ||
const { ticketId } = req.params; | ||
const medias = req.files as Express.Multer.File[]; | ||
|
||
const ticket = await Ticket.findByPk(ticketId, { | ||
include: [ | ||
{ | ||
model: Contact, | ||
as: "contact", | ||
attributes: [ | ||
"number", | ||
"email", | ||
"messengerId", | ||
"instagramId", | ||
"telegramId", | ||
"webchatId" | ||
] | ||
}, | ||
{ | ||
model: Whatsapp, | ||
as: "whatsapp", | ||
attributes: ["qrcode", "type"] | ||
} | ||
] | ||
}); | ||
|
||
if (!ticket) { | ||
return res.status(404).json({ message: "Ticket not found" }); | ||
} | ||
|
||
try { | ||
if (medias) { | ||
await Promise.all( | ||
medias.map(async (media: Express.Multer.File) => { | ||
await SendMediaMessageService( | ||
media, | ||
message, | ||
ticket.id, | ||
ticket.contact, | ||
ticket.whatsapp | ||
); | ||
}) | ||
); | ||
} else { | ||
await SendTextMessageService( | ||
message, | ||
ticket.id, | ||
ticket.contact, | ||
ticket.whatsapp | ||
); | ||
} | ||
|
||
return res.status(200).json({ message: "Message sent" }); | ||
} catch (error) { | ||
console.log(error); | ||
|
||
return res.status(400).json({ message: error }); | ||
} | ||
}; | ||
|
||
export const store = async (req: Request, res: Response): Promise<Response> => { | ||
const { contactId, status, userId, channel }: TicketData = req.body; | ||
|
||
const ticket = await CreateHubTicketService({ | ||
contactId, | ||
status, | ||
userId, | ||
channel | ||
}); | ||
|
||
const io = getIO(); | ||
io.to(ticket.status).emit("ticket", { | ||
action: "update", | ||
ticket | ||
}); | ||
|
||
return res.status(200).json(ticket); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Request, Response } from "express"; | ||
import Whatsapp from "../models/Whatsapp"; | ||
import HubMessageListener from "../services/HubServices/HubMessageListener"; | ||
|
||
export const listen = async ( | ||
req: Request, | ||
res: Response | ||
): Promise<Response> => { | ||
const medias = req.files as Express.Multer.File[]; | ||
const { channelId } = req.params; | ||
|
||
const connection = await Whatsapp.findOne({ | ||
where: { qrcode: channelId } | ||
}); | ||
|
||
if (!connection) { | ||
return res.status(404).json({ message: "Whatsapp channel not found" }); | ||
} | ||
|
||
try { | ||
await HubMessageListener(req.body, connection, medias); | ||
|
||
return res.status(200).json({ message: "Webhook received" }); | ||
} catch (error) { | ||
return res.status(400).json({ message: error }); | ||
} | ||
}; |
Oops, something went wrong.