11import { MessengerChannel } from "@/channels/messenger.channel" ;
22import { LOCALE_KEY } from "@/constants" ;
3- import { db } from "@/database/db" ;
4- import { channelTypes , channels } from "@/database/schema" ;
53import { HttpException } from "@/exceptions/http-exception" ;
64import { LocaleService } from "@/i18n/ctx" ;
7- import { and , eq } from "drizzle-orm" ;
85import { Request , Response } from "express" ;
96import { StatusCodes } from "http-status-codes" ;
107import { Inject , Service } from "typedi" ;
8+ import { ChannelService } from "./channels.service" ;
119
1210@Service ( )
1311export class WebhookService {
1412 constructor (
15- @Inject ( LOCALE_KEY ) private readonly localeService : LocaleService
13+ @Inject ( LOCALE_KEY ) private readonly localeService : LocaleService ,
14+ private readonly chanelService : ChannelService
1615 ) { }
1716
1817 public async verifyWebhook ( contactId : string , req : Request , res : Response ) {
19- const expectedChannel = await db . select ( {
20- id : channels . id ,
21- contactId : channels . contactId ,
22- contactName : channels . contactName ,
23- channelType : channelTypes . name ,
24- credentials : channels . credentials ,
25- } )
26- . from ( channels )
27- . where (
28- and ( eq ( channels . contactId , contactId ) ,
29- eq ( channels . deleted , false )
30- )
31- )
32- . innerJoin ( channelTypes , eq ( channels . channelTypeId , channelTypes . id ) ) ;
18+ const expectedChannel = await this . chanelService . findOneByContactId ( contactId ) ;
3319
3420 if ( ! expectedChannel ) {
3521 throw new HttpException (
@@ -40,9 +26,9 @@ export class WebhookService {
4026
4127 let verifyResult = null ;
4228
43- if ( expectedChannel [ 0 ] ) {
44- const { id, contactId, contactName, channelType, credentials } = expectedChannel [ 0 ] ;
45- switch ( expectedChannel [ 0 ] . channelType ) {
29+ if ( expectedChannel ) {
30+ const { id, contactId, contactName, channelType, credentials } = expectedChannel ;
31+ switch ( channelType ) {
4632 case 'MSG' :
4733 const messengerChannel = new MessengerChannel ( id , contactId , contactName , channelType , credentials ) ;
4834 verifyResult = messengerChannel . verifyWebhook ( req , res ) ;
@@ -58,4 +44,29 @@ export class WebhookService {
5844
5945 return verifyResult ;
6046 }
47+
48+ public async handleIncomingMessage ( contactId : string , req : Request , res : Response ) {
49+ const expectedChannel = await this . chanelService . findOneByContactId ( contactId ) ;
50+
51+ if ( ! expectedChannel ) {
52+ console . log ( 'Incoming message: Can not find channel with id ' , req . params . id ) ;
53+ return ;
54+ }
55+
56+ const { id, contactName, channelType, credentials } = expectedChannel ;
57+
58+ let prepareMessage = null ;
59+
60+ switch ( channelType ) {
61+ case 'MSG' :
62+ const messengerChannel = new MessengerChannel ( id , contactId , contactName , channelType , credentials ) ;
63+ prepareMessage = await messengerChannel . prepareMessage ( req , res ) ;
64+ break ;
65+ default :
66+ console . log ( `Incoming message: Does not support channel type ${ channelType } ` ) ;
67+ break ;
68+ }
69+
70+ return prepareMessage ;
71+ }
6172}
0 commit comments