139d7b1919e623ea69f8955c5d7a9715baf747f0
[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 _0x4a70=['bluebird','path','rimraf','./whatsappMessage.attributes','exports','define','WhatsappMessage','whatsapp_messages','messageId','fti_whatsapp_messages','body','FULLTEXT','models','get','direction','toLowerCase','out','secret','isNil','UserId','WhatsappInteraction','update','catch','error','WhatsappAccount','find','then','type','twilio','accountSid','authToken','phone','whatsapp:','remote','/api/whatsapp/messages/','/status?token=','token','sid','WhatsappMessage,\x20%s,\x20%s,\x20%s','inspect','afterCreate','finally','utf8mb4','utf8mb4_unicode_ci','lodash','util','../../config/logger','moment'];(function(_0x2f5b27,_0x423ec8){var _0x535af5=function(_0x1e8c96){while(--_0x1e8c96){_0x2f5b27['push'](_0x2f5b27['shift']());}};_0x535af5(++_0x423ec8);}(_0x4a70,0xbc));var _0x04a7=function(_0x1573ea,_0x434f02){_0x1573ea=_0x1573ea-0x0;var _0x51c0e2=_0x4a70[_0x1573ea];return _0x51c0e2;};'use strict';var _=require(_0x04a7('0x0'));var util=require(_0x04a7('0x1'));var logger=require(_0x04a7('0x2'))('api');var moment=require(_0x04a7('0x3'));var BPromise=require(_0x04a7('0x4'));var rp=require('request-promise');var fs=require('fs');var path=require(_0x04a7('0x5'));var rimraf=require(_0x04a7('0x6'));var config=require('../../config/environment');var attributes=require(_0x04a7('0x7'));module[_0x04a7('0x8')]=function(_0x34d04c,_0x5a43f6){return _0x34d04c[_0x04a7('0x9')](_0x04a7('0xa'),attributes,{'tableName':_0x04a7('0xb'),'paranoid':![],'indexes':[{'name':_0x04a7('0xc'),'fields':[_0x04a7('0xc')]},{'name':_0x04a7('0xd'),'fields':[_0x04a7('0xe')],'type':_0x04a7('0xf')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x4abb93,_0x553521,_0x4f7fd3){var _0x4ac06b=_0x34d04c[_0x04a7('0x10')];var _0x300005=_0x4abb93[_0x04a7('0x11')]({'plain':!![]});if(_0x300005[_0x04a7('0x12')][_0x04a7('0x13')]()==='in'||_0x300005[_0x04a7('0x12')]['toLowerCase']()===_0x04a7('0x14')&&!_0x300005[_0x04a7('0x15')]&&!_[_0x04a7('0x16')](_0x300005[_0x04a7('0x17')])){_0x4ac06b[_0x04a7('0x18')]['findOne']({'where':{'id':_0x300005['WhatsappInteractionId']}})['then'](function(_0x150cd3){if(_0x150cd3){_0x150cd3[_0x04a7('0x19')]({'lastMsgAt':_0x300005['createdAt'],'lastMsgDirection':_0x300005['direction']['toLowerCase']()});}})[_0x04a7('0x1a')](function(_0x141182){console[_0x04a7('0x1b')](_0x141182);});}if(_0x300005[_0x04a7('0x12')]==='out'&&!_0x300005['secret']){var _0x5cc28c;_0x4ac06b[_0x04a7('0x1c')][_0x04a7('0x1d')]({'where':{'id':_0x300005['WhatsappAccountId']},'raw':!![]})[_0x04a7('0x1e')](function(_0x4eb4ee){_0x5cc28c=_0x4eb4ee;if(_0x5cc28c&&_0x5cc28c['type']){switch(_0x5cc28c[_0x04a7('0x1f')]){case _0x04a7('0x20'):var _0x477015=require(_0x04a7('0x20'));if(_0x5cc28c[_0x04a7('0x21')]&&_0x5cc28c[_0x04a7('0x22')]&&_0x5cc28c[_0x04a7('0x23')]){var _0x275edc=new _0x477015(_0x5cc28c['accountSid'],_0x5cc28c[_0x04a7('0x22')]);_0x275edc['messages']['create']({'body':_0x300005[_0x04a7('0xe')],'to':'whatsapp:'+_0x300005[_0x04a7('0x23')],'from':_0x04a7('0x24')+_0x5cc28c[_0x04a7('0x23')],'statusCallback':_0x5cc28c[_0x04a7('0x25')]+_0x04a7('0x26')+_0x300005['id']+_0x04a7('0x27')+_0x5cc28c[_0x04a7('0x28')]})[_0x04a7('0x1e')](function(_0x58e8dc){if(_0x58e8dc[_0x04a7('0x29')]){return _0x4abb93[_0x04a7('0x19')]({'messageId':_0x58e8dc[_0x04a7('0x29')]});}})['catch'](function(_0x414353){logger['error'](_0x04a7('0x2a'),'afterCreate',_0x04a7('0x20'),util[_0x04a7('0x2b')](_0x414353,{'showHidden':![],'depth':null}));});}else{logger[_0x04a7('0x1b')](_0x04a7('0x2a'),_0x04a7('0x2c'),_0x04a7('0x20'),'Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken');}break;}}})[_0x04a7('0x2d')](function(){_0x4f7fd3();});}else{_0x4f7fd3();}}},'charset':_0x04a7('0x2e'),'collate':_0x04a7('0x2f')});};