d2009cdc9fed7c17e93bb50a24268c51f836ac12
[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 _0x35cf=['err','WhatsappMessage,\x20%s','lodash','util','../../config/logger','bluebird','request-promise','node-fetch','path','rimraf','../../config/environment','./whatsappMessage.attributes','exports','define','WhatsappMessage','messageId','fti_whatsapp_messages','body','FULLTEXT','models','get','toLowerCase','direction','out','secret','isNil','UserId','WhatsappInteraction','findOne','WhatsappInteractionId','update','createdAt','error','find','WhatsappAccountId','type','twilio','phone','accountSid','authToken','messages','create','whatsapp:','/api/whatsapp/messages/','/status?token=','token','then','sid','catch','WhatsappMessage,\x20%s,\x20%s,\x20%s','inspect','afterCreate','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken','sinch','baseUrl','/whatsapp/v1/','/messages/','text','POST','Bearer\x20','application/json','statuses','message_id'];(function(_0x22f2c6,_0x5673bc){var _0x534421=function(_0x44ba17){while(--_0x44ba17){_0x22f2c6['push'](_0x22f2c6['shift']());}};_0x534421(++_0x5673bc);}(_0x35cf,0xbf));var _0xf35c=function(_0x12d5e9,_0x1decd6){_0x12d5e9=_0x12d5e9-0x0;var _0x343939=_0x35cf[_0x12d5e9];return _0x343939;};'use strict';var _=require(_0xf35c('0x0'));var util=require(_0xf35c('0x1'));var logger=require(_0xf35c('0x2'))('api');var moment=require('moment');var BPromise=require(_0xf35c('0x3'));var rp=require(_0xf35c('0x4'));var fetch=require(_0xf35c('0x5'));var fs=require('fs');var path=require(_0xf35c('0x6'));var rimraf=require(_0xf35c('0x7'));var config=require(_0xf35c('0x8'));var attributes=require(_0xf35c('0x9'));module[_0xf35c('0xa')]=function(_0x5795e3,_0x20d12a){return _0x5795e3[_0xf35c('0xb')](_0xf35c('0xc'),attributes,{'tableName':'whatsapp_messages','paranoid':![],'indexes':[{'name':_0xf35c('0xd'),'fields':[_0xf35c('0xd')]},{'name':_0xf35c('0xe'),'fields':[_0xf35c('0xf')],'type':_0xf35c('0x10')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x210126,_0x2f8f3c,_0x5e17ab){var _0x1a6560=_0x5795e3[_0xf35c('0x11')];var _0x17378c=_0x210126[_0xf35c('0x12')]({'plain':!![]});if(_0x17378c['direction'][_0xf35c('0x13')]()==='in'||_0x17378c[_0xf35c('0x14')]['toLowerCase']()===_0xf35c('0x15')&&!_0x17378c[_0xf35c('0x16')]&&!_[_0xf35c('0x17')](_0x17378c[_0xf35c('0x18')])){_0x1a6560[_0xf35c('0x19')][_0xf35c('0x1a')]({'where':{'id':_0x17378c[_0xf35c('0x1b')]}})['then'](function(_0x51ce52){if(_0x51ce52){_0x51ce52[_0xf35c('0x1c')]({'lastMsgAt':_0x17378c[_0xf35c('0x1d')],'lastMsgDirection':_0x17378c['direction']['toLowerCase']()});}})['catch'](function(_0x4f5daf){console[_0xf35c('0x1e')](_0x4f5daf);});}if(_0x17378c['direction']==='out'&&!_0x17378c[_0xf35c('0x16')]){var _0x352de8;_0x1a6560['WhatsappAccount'][_0xf35c('0x1f')]({'where':{'id':_0x17378c[_0xf35c('0x20')]},'raw':!![]})['then'](function(_0x4e35c9){_0x352de8=_0x4e35c9;if(_0x352de8&&_0x352de8['type']){switch(_0x352de8[_0xf35c('0x21')]){case _0xf35c('0x22'):var _0x5534d5=require('twilio');if(_0x352de8['accountSid']&&_0x352de8['authToken']&&_0x352de8[_0xf35c('0x23')]){var _0x53d173=new _0x5534d5(_0x352de8[_0xf35c('0x24')],_0x352de8[_0xf35c('0x25')]);_0x53d173[_0xf35c('0x26')][_0xf35c('0x27')]({'body':_0x17378c['body'],'to':_0xf35c('0x28')+_0x17378c[_0xf35c('0x23')],'from':_0xf35c('0x28')+_0x352de8[_0xf35c('0x23')],'statusCallback':_0x352de8['remote']+_0xf35c('0x29')+_0x17378c['id']+_0xf35c('0x2a')+_0x352de8[_0xf35c('0x2b')]})[_0xf35c('0x2c')](function(_0x31e162){if(_0x31e162[_0xf35c('0x2d')]){return _0x210126[_0xf35c('0x1c')]({'messageId':_0x31e162[_0xf35c('0x2d')]});}})[_0xf35c('0x2e')](function(_0x4b2f81){logger[_0xf35c('0x1e')](_0xf35c('0x2f'),'afterCreate',_0xf35c('0x22'),util[_0xf35c('0x30')](_0x4b2f81,{'showHidden':![],'depth':null}));});}else{logger[_0xf35c('0x1e')](_0xf35c('0x2f'),_0xf35c('0x31'),_0xf35c('0x22'),_0xf35c('0x32'));}break;case _0xf35c('0x33'):var _0x43ab0e=_0x352de8[_0xf35c('0x24')];var _0x2dd6a2=_0x352de8[_0xf35c('0x25')];var _0x465a3c=_0x352de8[_0xf35c('0x34')]+_0xf35c('0x35')+_0x43ab0e+_0xf35c('0x36');var _0x53f4d1={'to':[_0x17378c['phone']],'message':{'type':_0xf35c('0x37'),'text':_0x17378c[_0xf35c('0xf')]}};var _0x5ecaad={'method':_0xf35c('0x38'),'uri':_0x465a3c,'headers':{'Authorization':_0xf35c('0x39')+_0x2dd6a2,'Content-Type':_0xf35c('0x3a')},'body':_0x53f4d1,'json':!![]};rp(_0x5ecaad)[_0xf35c('0x2c')](function(_0x2446ac){if(_0x2446ac[_0xf35c('0x3b')][0x0][_0xf35c('0x3c')]){return _0x210126['update']({'messageId':_0x2446ac['statuses'][0x0]['message_id']});}})[_0xf35c('0x2e')](function(_0x157d35){logger[_0xf35c('0x3d')](_0xf35c('0x3e'),_0x157d35);});break;}}})['finally'](function(){_0x5e17ab();});}else{_0x5e17ab();}}},'charset':'utf8mb4','collate':'utf8mb4_unicode_ci'});};