71071908fd391dc1a90799f2a58493ba32db6078
[motion2.git] / server / api / whatsappMessage / whatsappMessage.model.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0xf8ca=['define','WhatsappMessage','messageId','fti_whatsapp_messages','body','models','get','direction','toLowerCase','out','secret','isNil','UserId','WhatsappInteraction','findOne','WhatsappInteractionId','then','update','createdAt','catch','WhatsappAccount','type','twilio','accountSid','authToken','messages','create','whatsapp:','phone','/status?token=','token','error','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken','sinch','baseUrl','/whatsapp/v1/','/messages/','text','POST','Bearer\x20','statuses','message_id','WhatsappMessage,\x20%s','utf8mb4','lodash','util','api','moment','bluebird','request-promise','node-fetch','../../config/environment','./whatsappMessage.attributes','exports'];(function(_0x414ea8,_0x524afc){var _0x3975e7=function(_0x1ec591){while(--_0x1ec591){_0x414ea8['push'](_0x414ea8['shift']());}};_0x3975e7(++_0x524afc);}(_0xf8ca,0x1be));var _0xaf8c=function(_0x474bc0,_0x26989c){_0x474bc0=_0x474bc0-0x0;var _0x4b24cf=_0xf8ca[_0x474bc0];return _0x4b24cf;};'use strict';var _=require(_0xaf8c('0x0'));var util=require(_0xaf8c('0x1'));var logger=require('../../config/logger')(_0xaf8c('0x2'));var moment=require(_0xaf8c('0x3'));var BPromise=require(_0xaf8c('0x4'));var rp=require(_0xaf8c('0x5'));var fetch=require(_0xaf8c('0x6'));var fs=require('fs');var path=require('path');var rimraf=require('rimraf');var config=require(_0xaf8c('0x7'));var attributes=require(_0xaf8c('0x8'));module[_0xaf8c('0x9')]=function(_0x184499,_0x238cbb){return _0x184499[_0xaf8c('0xa')](_0xaf8c('0xb'),attributes,{'tableName':'whatsapp_messages','paranoid':![],'indexes':[{'name':_0xaf8c('0xc'),'fields':['messageId']},{'name':_0xaf8c('0xd'),'fields':[_0xaf8c('0xe')],'type':'FULLTEXT'}],'timestamps':!![],'hooks':{'afterCreate':function(_0x4585b1,_0x25a9c5,_0x2e7986){var _0x47cd17=_0x184499[_0xaf8c('0xf')];var _0x1e5ed0=_0x4585b1[_0xaf8c('0x10')]({'plain':!![]});if(_0x1e5ed0[_0xaf8c('0x11')][_0xaf8c('0x12')]()==='in'||_0x1e5ed0['direction'][_0xaf8c('0x12')]()===_0xaf8c('0x13')&&!_0x1e5ed0[_0xaf8c('0x14')]&&!_[_0xaf8c('0x15')](_0x1e5ed0[_0xaf8c('0x16')])){_0x47cd17[_0xaf8c('0x17')][_0xaf8c('0x18')]({'where':{'id':_0x1e5ed0[_0xaf8c('0x19')]}})[_0xaf8c('0x1a')](function(_0x2e1504){if(_0x2e1504){_0x2e1504[_0xaf8c('0x1b')]({'lastMsgAt':_0x1e5ed0[_0xaf8c('0x1c')],'lastMsgDirection':_0x1e5ed0[_0xaf8c('0x11')]['toLowerCase']()});}})[_0xaf8c('0x1d')](function(_0x20d7b1){console['error'](_0x20d7b1);});}if(_0x1e5ed0[_0xaf8c('0x11')]==='out'&&!_0x1e5ed0[_0xaf8c('0x14')]){var _0x28ca08;_0x47cd17[_0xaf8c('0x1e')]['find']({'where':{'id':_0x1e5ed0['WhatsappAccountId']},'raw':!![]})[_0xaf8c('0x1a')](function(_0x422f69){_0x28ca08=_0x422f69;if(_0x28ca08&&_0x28ca08[_0xaf8c('0x1f')]){switch(_0x28ca08['type']){case _0xaf8c('0x20'):var _0x47cba4=require(_0xaf8c('0x20'));if(_0x28ca08[_0xaf8c('0x21')]&&_0x28ca08[_0xaf8c('0x22')]&&_0x28ca08['phone']){var _0x406da4=new _0x47cba4(_0x28ca08[_0xaf8c('0x21')],_0x28ca08['authToken']);_0x406da4[_0xaf8c('0x23')][_0xaf8c('0x24')]({'body':_0x1e5ed0[_0xaf8c('0xe')],'to':_0xaf8c('0x25')+_0x1e5ed0[_0xaf8c('0x26')],'from':_0xaf8c('0x25')+_0x28ca08[_0xaf8c('0x26')],'statusCallback':_0x28ca08['remote']+'/api/whatsapp/messages/'+_0x1e5ed0['id']+_0xaf8c('0x27')+_0x28ca08[_0xaf8c('0x28')]})['then'](function(_0x2e3581){if(_0x2e3581['sid']){return _0x4585b1[_0xaf8c('0x1b')]({'messageId':_0x2e3581['sid']});}})['catch'](function(_0x3abf22){logger[_0xaf8c('0x29')](_0xaf8c('0x2a'),_0xaf8c('0x2b'),_0xaf8c('0x20'),util[_0xaf8c('0x2c')](_0x3abf22,{'showHidden':![],'depth':null}));});}else{logger['error'](_0xaf8c('0x2a'),_0xaf8c('0x2b'),'twilio',_0xaf8c('0x2d'));}break;case _0xaf8c('0x2e'):var _0x55e9ea=_0x28ca08['accountSid'];var _0x4e3903=_0x28ca08['authToken'];var _0x4c0389=_0x28ca08[_0xaf8c('0x2f')]+_0xaf8c('0x30')+_0x55e9ea+_0xaf8c('0x31');var _0x71dee6={'to':[_0x1e5ed0[_0xaf8c('0x26')]],'message':{'type':_0xaf8c('0x32'),'text':_0x1e5ed0['body']}};var _0x5ad7d5={'method':_0xaf8c('0x33'),'uri':_0x4c0389,'headers':{'Authorization':_0xaf8c('0x34')+_0x4e3903,'Content-Type':'application/json'},'body':_0x71dee6,'json':!![]};rp(_0x5ad7d5)[_0xaf8c('0x1a')](function(_0x4869c0){if(_0x4869c0[_0xaf8c('0x35')][0x0]['message_id']){return _0x4585b1[_0xaf8c('0x1b')]({'messageId':_0x4869c0['statuses'][0x0][_0xaf8c('0x36')]});}})[_0xaf8c('0x1d')](function(_0x4bf47c){logger['err'](_0xaf8c('0x37'),_0x4bf47c);});break;}}})['finally'](function(){_0x2e7986();});}else{_0x2e7986();}}},'charset':_0xaf8c('0x38'),'collate':'utf8mb4_unicode_ci'});};