Built motion from commit fe909640.|2.6.8
[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 _0x5f79=['text','Bearer\x20','application/json','statuses','WhatsappMessage,\x20%s','finally','utf8mb4','utf8mb4_unicode_ci','lodash','../../config/logger','api','moment','bluebird','request-promise','rimraf','../../config/environment','exports','WhatsappMessage','whatsapp_messages','messageId','FULLTEXT','models','get','toLowerCase','secret','isNil','WhatsappInteraction','WhatsappInteractionId','then','createdAt','direction','error','out','WhatsappAccountId','type','twilio','accountSid','authToken','phone','messages','create','body','whatsapp:','/api/whatsapp/messages/','/status?token=','sid','update','catch','WhatsappMessage,\x20%s,\x20%s,\x20%s','inspect','sinch','/whatsapp/v1/','/messages/'];(function(_0x13b02f,_0x462a53){var _0x12e9a6=function(_0x187d57){while(--_0x187d57){_0x13b02f['push'](_0x13b02f['shift']());}};_0x12e9a6(++_0x462a53);}(_0x5f79,0x146));var _0x95f7=function(_0x243b59,_0x3f9495){_0x243b59=_0x243b59-0x0;var _0x3e1676=_0x5f79[_0x243b59];return _0x3e1676;};'use strict';var _=require(_0x95f7('0x0'));var util=require('util');var logger=require(_0x95f7('0x1'))(_0x95f7('0x2'));var moment=require(_0x95f7('0x3'));var BPromise=require(_0x95f7('0x4'));var rp=require(_0x95f7('0x5'));var fetch=require('node-fetch');var fs=require('fs');var path=require('path');var rimraf=require(_0x95f7('0x6'));var config=require(_0x95f7('0x7'));var attributes=require('./whatsappMessage.attributes');module[_0x95f7('0x8')]=function(_0x1547df,_0x533411){return _0x1547df['define'](_0x95f7('0x9'),attributes,{'tableName':_0x95f7('0xa'),'paranoid':![],'indexes':[{'name':_0x95f7('0xb'),'fields':[_0x95f7('0xb')]},{'name':'fti_whatsapp_messages','fields':['body'],'type':_0x95f7('0xc')}],'timestamps':!![],'hooks':{'afterCreate':function(_0xc46f35,_0x2d4a18,_0x404a2f){var _0x595b28=_0x1547df[_0x95f7('0xd')];var _0x37b180=_0xc46f35[_0x95f7('0xe')]({'plain':!![]});if(_0x37b180['direction']['toLowerCase']()==='in'||_0x37b180['direction'][_0x95f7('0xf')]()==='out'&&!_0x37b180[_0x95f7('0x10')]&&!_[_0x95f7('0x11')](_0x37b180['UserId'])){_0x595b28[_0x95f7('0x12')]['findOne']({'where':{'id':_0x37b180[_0x95f7('0x13')]}})[_0x95f7('0x14')](function(_0x8c046d){if(_0x8c046d){_0x8c046d['update']({'lastMsgAt':_0x37b180[_0x95f7('0x15')],'lastMsgDirection':_0x37b180[_0x95f7('0x16')][_0x95f7('0xf')]()});}})['catch'](function(_0x1ed0a4){console[_0x95f7('0x17')](_0x1ed0a4);});}if(_0x37b180[_0x95f7('0x16')]===_0x95f7('0x18')&&!_0x37b180[_0x95f7('0x10')]){var _0x13e180;_0x595b28['WhatsappAccount']['find']({'where':{'id':_0x37b180[_0x95f7('0x19')]},'raw':!![]})[_0x95f7('0x14')](function(_0x37f487){_0x13e180=_0x37f487;if(_0x13e180&&_0x13e180['type']){switch(_0x13e180[_0x95f7('0x1a')]){case _0x95f7('0x1b'):var _0x8cdfb3=require(_0x95f7('0x1b'));if(_0x13e180[_0x95f7('0x1c')]&&_0x13e180[_0x95f7('0x1d')]&&_0x13e180[_0x95f7('0x1e')]){var _0x111f10=new _0x8cdfb3(_0x13e180[_0x95f7('0x1c')],_0x13e180[_0x95f7('0x1d')]);_0x111f10[_0x95f7('0x1f')][_0x95f7('0x20')]({'body':_0x37b180[_0x95f7('0x21')],'to':_0x95f7('0x22')+_0x37b180[_0x95f7('0x1e')],'from':_0x95f7('0x22')+_0x13e180[_0x95f7('0x1e')],'statusCallback':_0x13e180['remote']+_0x95f7('0x23')+_0x37b180['id']+_0x95f7('0x24')+_0x13e180['token']})['then'](function(_0x3fb698){if(_0x3fb698[_0x95f7('0x25')]){return _0xc46f35[_0x95f7('0x26')]({'messageId':_0x3fb698[_0x95f7('0x25')]});}})[_0x95f7('0x27')](function(_0x485277){logger['error'](_0x95f7('0x28'),'afterCreate',_0x95f7('0x1b'),util[_0x95f7('0x29')](_0x485277,{'showHidden':![],'depth':null}));});}else{logger['error'](_0x95f7('0x28'),'afterCreate',_0x95f7('0x1b'),'Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken');}break;case _0x95f7('0x2a'):var _0x11b901=_0x13e180['accountSid'];var _0x4636a0=_0x13e180[_0x95f7('0x1d')];var _0x39f853=_0x13e180['baseUrl']+_0x95f7('0x2b')+_0x11b901+_0x95f7('0x2c');var _0x58d9ff={'to':[_0x37b180[_0x95f7('0x1e')]],'message':{'type':_0x95f7('0x2d'),'text':_0x37b180[_0x95f7('0x21')]}};var _0x258335={'method':'POST','uri':_0x39f853,'headers':{'Authorization':_0x95f7('0x2e')+_0x4636a0,'Content-Type':_0x95f7('0x2f')},'body':_0x58d9ff,'json':!![]};rp(_0x258335)[_0x95f7('0x14')](function(_0x20060c){if(_0x20060c[_0x95f7('0x30')][0x0]['message_id']){return _0xc46f35['update']({'messageId':_0x20060c['statuses'][0x0]['message_id']});}})[_0x95f7('0x27')](function(_0x4e76fb){logger['err'](_0x95f7('0x31'),_0x4e76fb);});break;}}})[_0x95f7('0x32')](function(){_0x404a2f();});}else{_0x404a2f();}}},'charset':_0x95f7('0x33'),'collate':_0x95f7('0x34')});};