Built motion from commit b0a9a629.|2.6.33
[motion2.git] / server / api / whatsappMessage / whatsappMessage.controller.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 _0x8497=['null','RejectMessage','rejectMessage','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','ContactId','sequelize','QueryTypes','contact','User','interface','SIP/%s','channel','motionChannel','event','reject','EventManager','WhatsappAccount','WhatsappAccountId','WhatsppMessage,\x20%s,\x20%s,\x20%s','received\x20delivery\x20report\x20from','inspect','MessageSid','messageId','MessageStatus','read','util','ioredis','../../components/interaction/service','../../components/parsers/qs','../../config/logger','api','../../mysqldb','redis','./whatsappMessage.socket','jayson/promise','client','http','whatsapp-interactions','request','then','WhatsappMessage,\x20%s,\x20%s','request\x20sent','debug','WhatsappMessage,\x20%s,\x20%s,\x20%s','stringify','error','message','info','result','catch','status','json','limit','undefined','offset','Content-Range','update','destroy','end','sendStatus','name','index','map','WhatsappMessage','type','key','query','keys','filters','intersection','model','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','filter','merge','options','count','includeAll','include','rows','params','find','create','body','describe','accept','whatsapp','UserId','user','agent','get','WhatsappInteractionId','interaction','[WHATSAPPMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s'];(function(_0x1387cb,_0x137c44){var _0x43bc67=function(_0x478a6e){while(--_0x478a6e){_0x1387cb['push'](_0x1387cb['shift']());}};_0x43bc67(++_0x137c44);}(_0x8497,0x1a9));var _0x7849=function(_0x491602,_0x932ba0){_0x491602=_0x491602-0x0;var _0x422f2f=_0x8497[_0x491602];return _0x422f2f;};'use strict';var util=require(_0x7849('0x0'));var _=require('lodash');var Redis=require(_0x7849('0x1'));var interactionService=require(_0x7849('0x2'));var qs=require(_0x7849('0x3'));var logger=require(_0x7849('0x4'))(_0x7849('0x5'));var config=require('../../config/environment');var db=require(_0x7849('0x6'))['db'];config[_0x7849('0x7')]=_['defaults'](config[_0x7849('0x7')],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x7849('0x7')]));require(_0x7849('0x8'))['register'](socket);var jayson=require(_0x7849('0x9'));var client=jayson[_0x7849('0xa')]['http']({'port':0x232c});var client9002=jayson['client'][_0x7849('0xb')]({'port':0x232a});var interaction_log=require(_0x7849('0x4'))(_0x7849('0xc'));function respondWithRpcPromise(_0xa2e564,_0x26f13a,_0x393ccd,_0x349c0b){return new Promise(function(_0x16a5f6,_0x41bc08){var _0xe6d712=_0x349c0b||client;return _0xe6d712[_0x7849('0xd')](_0xa2e564,_0x393ccd)[_0x7849('0xe')](function(_0x48f369){logger['info'](_0x7849('0xf'),_0x26f13a,_0x7849('0x10'));logger[_0x7849('0x11')](_0x7849('0x12'),_0x26f13a,_0x7849('0x10'),JSON[_0x7849('0x13')](_0x48f369));if(_0x48f369[_0x7849('0x14')]){if(_0x48f369[_0x7849('0x14')]['code']===0x1f4){logger['error'](_0x7849('0xf'),_0x26f13a,_0x48f369['error'][_0x7849('0x15')]);return _0x41bc08(_0x48f369[_0x7849('0x14')][_0x7849('0x15')]);}logger[_0x7849('0x14')](_0x7849('0xf'),_0x26f13a,_0x48f369[_0x7849('0x14')][_0x7849('0x15')]);return _0x16a5f6(_0x48f369[_0x7849('0x14')][_0x7849('0x15')]);}else{logger[_0x7849('0x16')](_0x7849('0xf'),_0x26f13a,_0x7849('0x10'));_0x16a5f6(_0x48f369[_0x7849('0x17')]['message']);}})[_0x7849('0x18')](function(_0x3abccc){logger['error'](_0x7849('0xf'),_0x26f13a,_0x3abccc);_0x41bc08(_0x3abccc);});});}function respondWithResult(_0x17e5cb,_0x582813){_0x582813=_0x582813||0xc8;return function(_0x5ee868){if(_0x5ee868){return _0x17e5cb[_0x7849('0x19')](_0x582813)[_0x7849('0x1a')](_0x5ee868);}};}function respondWithFilteredResult(_0x12ec55,_0x48d58c){return function(_0x321df2){if(_0x321df2){var _0x271e04=typeof _0x48d58c['offset']==='undefined'&&typeof _0x48d58c[_0x7849('0x1b')]===_0x7849('0x1c');var _0x2ace89=_0x321df2['count'];var _0x243c0b=_0x271e04?0x0:_0x48d58c[_0x7849('0x1d')];var _0x5f1a25=_0x271e04?_0x321df2['count']:_0x48d58c[_0x7849('0x1d')]+_0x48d58c[_0x7849('0x1b')];var _0x523b6a;if(_0x5f1a25>=_0x2ace89){_0x5f1a25=_0x2ace89;_0x523b6a=0xc8;}else{_0x523b6a=0xce;}_0x12ec55['status'](_0x523b6a);return _0x12ec55['set'](_0x7849('0x1e'),_0x243c0b+'-'+_0x5f1a25+'/'+_0x2ace89)[_0x7849('0x1a')](_0x321df2);}return null;};}function saveUpdates(_0x4d16b9){return function(_0x2c9d8e){if(_0x2c9d8e){return _0x2c9d8e[_0x7849('0x1f')](_0x4d16b9)[_0x7849('0xe')](function(_0x45c6bb){return _0x45c6bb;});}return null;};}function removeEntity(_0x49e3cc){return function(_0x26deb1){if(_0x26deb1){return _0x26deb1[_0x7849('0x20')]()['then'](function(){_0x49e3cc['status'](0xcc)[_0x7849('0x21')]();});}};}function handleEntityNotFound(_0xf3c6b4){return function(_0x285f9b){if(!_0x285f9b){_0xf3c6b4[_0x7849('0x22')](0x194);}return _0x285f9b;};}function handleError(_0x4f5b3a,_0x41a7a8){_0x41a7a8=_0x41a7a8||0x1f4;return function(_0x17b4ad){logger['error'](_0x17b4ad['stack']);if(_0x17b4ad[_0x7849('0x23')]){delete _0x17b4ad[_0x7849('0x23')];}_0x4f5b3a['status'](_0x41a7a8)['send'](_0x17b4ad);};}exports[_0x7849('0x24')]=function(_0x458bd9,_0x584154){var _0x460d85={},_0x231cc1={},_0x53a41a={'count':0x0,'rows':[]};var _0x4f721e=_[_0x7849('0x25')](db[_0x7849('0x26')]['rawAttributes'],function(_0x50933c){return{'name':_0x50933c['fieldName'],'type':_0x50933c[_0x7849('0x27')][_0x7849('0x28')]};});_0x231cc1['model']=_[_0x7849('0x25')](_0x4f721e,'name');_0x231cc1[_0x7849('0x29')]=_[_0x7849('0x2a')](_0x458bd9[_0x7849('0x29')]);_0x231cc1[_0x7849('0x2b')]=_[_0x7849('0x2c')](_0x231cc1[_0x7849('0x2d')],_0x231cc1[_0x7849('0x29')]);_0x460d85[_0x7849('0x2e')]=_[_0x7849('0x2c')](_0x231cc1['model'],qs[_0x7849('0x2f')](_0x458bd9[_0x7849('0x29')]['fields']));_0x460d85['attributes']=_0x460d85[_0x7849('0x2e')][_0x7849('0x30')]?_0x460d85[_0x7849('0x2e')]:_0x231cc1['model'];if(!_0x458bd9[_0x7849('0x29')][_0x7849('0x31')](_0x7849('0x32'))){_0x460d85['limit']=qs[_0x7849('0x1b')](_0x458bd9['query']['limit']);_0x460d85[_0x7849('0x1d')]=qs[_0x7849('0x1d')](_0x458bd9[_0x7849('0x29')][_0x7849('0x1d')]);}_0x460d85[_0x7849('0x33')]=qs[_0x7849('0x34')](_0x458bd9['query'][_0x7849('0x34')]);_0x460d85[_0x7849('0x35')]=qs[_0x7849('0x2b')](_['pick'](_0x458bd9[_0x7849('0x29')],_0x231cc1[_0x7849('0x2b')]),_0x4f721e);if(_0x458bd9[_0x7849('0x29')][_0x7849('0x36')]){_0x460d85[_0x7849('0x35')]=_['merge'](_0x460d85[_0x7849('0x35')],{'$or':_['map'](_0x4f721e,function(_0x5aeb06){if(_0x5aeb06[_0x7849('0x27')]!=='VIRTUAL'){var _0x1abf15={};_0x1abf15[_0x5aeb06['name']]={'$like':'%'+_0x458bd9[_0x7849('0x29')]['filter']+'%'};return _0x1abf15;}})});}_0x460d85=_[_0x7849('0x37')]({},_0x460d85,_0x458bd9[_0x7849('0x38')]);var _0x1f74f1={'where':_0x460d85[_0x7849('0x35')]};return db['WhatsappMessage'][_0x7849('0x39')](_0x1f74f1)[_0x7849('0xe')](function(_0x23f7a2){_0x53a41a[_0x7849('0x39')]=_0x23f7a2;if(_0x458bd9['query'][_0x7849('0x3a')]){_0x460d85[_0x7849('0x3b')]=[{'all':!![]}];}return db[_0x7849('0x26')]['findAll'](_0x460d85);})[_0x7849('0xe')](function(_0x1caf1a){_0x53a41a[_0x7849('0x3c')]=_0x1caf1a;return _0x53a41a;})[_0x7849('0xe')](respondWithFilteredResult(_0x584154,_0x460d85))[_0x7849('0x18')](handleError(_0x584154,null));};exports['show']=function(_0x56a9d4,_0x565baf){var _0x10ff9d={'raw':!![],'where':{'id':_0x56a9d4[_0x7849('0x3d')]['id']}},_0x1b2838={};_0x1b2838[_0x7849('0x2d')]=_[_0x7849('0x2a')](db[_0x7849('0x26')]['rawAttributes']);_0x1b2838['query']=_[_0x7849('0x2a')](_0x56a9d4[_0x7849('0x29')]);_0x1b2838[_0x7849('0x2b')]=_['intersection'](_0x1b2838[_0x7849('0x2d')],_0x1b2838[_0x7849('0x29')]);_0x10ff9d['attributes']=_['intersection'](_0x1b2838[_0x7849('0x2d')],qs[_0x7849('0x2f')](_0x56a9d4[_0x7849('0x29')][_0x7849('0x2f')]));_0x10ff9d[_0x7849('0x2e')]=_0x10ff9d['attributes'][_0x7849('0x30')]?_0x10ff9d[_0x7849('0x2e')]:_0x1b2838[_0x7849('0x2d')];if(_0x56a9d4[_0x7849('0x29')][_0x7849('0x3a')]){_0x10ff9d[_0x7849('0x3b')]=[{'all':!![]}];}_0x10ff9d=_[_0x7849('0x37')]({},_0x10ff9d,_0x56a9d4[_0x7849('0x38')]);return db['WhatsappMessage'][_0x7849('0x3e')](_0x10ff9d)[_0x7849('0xe')](handleEntityNotFound(_0x565baf,null))[_0x7849('0xe')](respondWithResult(_0x565baf,null))['catch'](handleError(_0x565baf,null));};exports[_0x7849('0x3f')]=function(_0x50616a,_0x33ffd0){return db['WhatsappMessage'][_0x7849('0x3f')](_0x50616a['body'],{})[_0x7849('0xe')](respondWithResult(_0x33ffd0,0xc9))[_0x7849('0x18')](handleError(_0x33ffd0,null));};exports[_0x7849('0x1f')]=function(_0x3903d8,_0x133326){if(_0x3903d8[_0x7849('0x40')]['id']){delete _0x3903d8['body']['id'];}return db[_0x7849('0x26')]['find']({'where':{'id':_0x3903d8[_0x7849('0x3d')]['id']}})[_0x7849('0xe')](handleEntityNotFound(_0x133326,null))[_0x7849('0xe')](saveUpdates(_0x3903d8[_0x7849('0x40')],null))[_0x7849('0xe')](respondWithResult(_0x133326,null))[_0x7849('0x18')](handleError(_0x133326,null));};exports['destroy']=function(_0x1badaf,_0x1b1928){return db[_0x7849('0x26')][_0x7849('0x3e')]({'where':{'id':_0x1badaf[_0x7849('0x3d')]['id']}})[_0x7849('0xe')](handleEntityNotFound(_0x1b1928,null))[_0x7849('0xe')](removeEntity(_0x1b1928,null))[_0x7849('0x18')](handleError(_0x1b1928,null));};exports[_0x7849('0x41')]=function(_0xfd3469,_0x38aac5){return db['WhatsappMessage'][_0x7849('0x41')]()['then'](respondWithResult(_0x38aac5,null))[_0x7849('0x18')](handleError(_0x38aac5,null));};exports[_0x7849('0x42')]=function(_0x324d94,_0x1ca588){var _0x96bcc2=_0x7849('0x43');return interactionService['acceptMessage'](_0x324d94,_0x96bcc2)['then'](respondWithResult(_0x1ca588,null))[_0x7849('0x18')](handleError(_0x1ca588,null));};exports['reject']=function(_0x28a3e9,_0xbb4acc){var _0x36f466={'agent':{},'channel':_0x7849('0x43')};if(_0x28a3e9['body']['id']){delete _0x28a3e9['body']['id'];}_0x28a3e9[_0x7849('0x40')][_0x7849('0x44')]=_0x28a3e9['body']['UserId']||_0x28a3e9[_0x7849('0x45')]['id'];_0x36f466[_0x7849('0x46')]['id']=_0x28a3e9['body']['UserId'];return db[_0x7849('0x26')][_0x7849('0x3e')]({'where':{'id':_0x28a3e9[_0x7849('0x3d')]['id'],'UserId':null}})['then'](handleEntityNotFound(_0xbb4acc,null))['then'](function(_0x44af50){if(_0x44af50){_0x36f466[_0x7849('0x15')]=_0x44af50[_0x7849('0x47')]({'plain':!![]});return db['WhatsappInteraction']['find']({'where':{'id':_0x44af50[_0x7849('0x48')]}});}return null;})[_0x7849('0xe')](handleEntityNotFound(_0xbb4acc,null))[_0x7849('0xe')](function(_0xf584e){if(_0xf584e){_0x36f466[_0x7849('0x49')]=_0xf584e[_0x7849('0x47')]({'plain':!![]});interaction_log[_0x7849('0x16')](_0x7849('0x4a'),_0x28a3e9[_0x7849('0x45')]['id'],_0x28a3e9[_0x7849('0x45')][_0x7849('0x23')],_0x28a3e9['user']['role'],_0x36f466[_0x7849('0x49')]['id'],_0x28a3e9['body']?JSON[_0x7849('0x13')](_0x28a3e9[_0x7849('0x40')]):_0x7849('0x4b'));return respondWithRpcPromise(_0x7849('0x4c'),_0x7849('0x4d'),_0x36f466);}return null;})[_0x7849('0xe')](function(_0x34ba84){var _0x46cf37=_0x7849('0x4e')+_0x34ba84[_0x7849('0x15')][_0x7849('0x4f')];return db[_0x7849('0x50')]['query'](_0x46cf37,{'type':db['Sequelize'][_0x7849('0x51')]['SELECT'],'raw':!![]})[_0x7849('0xe')](function(_0x41010c){return _0x41010c;});})[_0x7849('0xe')](function(_0x4f6741){if(_0x4f6741){_0x36f466['message'][_0x7849('0x52')]=_0x4f6741[0x0];if(!_0x28a3e9['body']['UserId'])return;return db[_0x7849('0x53')]['find']({'where':{'id':_0x28a3e9[_0x7849('0x40')][_0x7849('0x44')],'role':'agent'},'attributes':['id',_0x7849('0x23'),'fullname','email','internal'],'raw':!![]});}return null;})[_0x7849('0xe')](function(_0x352ec8){if(!_0x352ec8)return null;_0x36f466['message'][_0x7849('0x46')]=_0x352ec8;return _0x36f466;})[_0x7849('0xe')](function(_0x29586b){if(_0x29586b){_0x29586b[_0x7849('0x15')][_0x7849('0x54')]=util['format'](_0x7849('0x55'),_0x28a3e9[_0x7849('0x45')][_0x7849('0x23')]);_0x29586b[_0x7849('0x15')][_0x7849('0x56')]=_0x36f466[_0x7849('0x56')];_0x29586b[_0x7849('0x15')][_0x7849('0x57')]=_0x36f466['channel'];_0x29586b[_0x7849('0x15')][_0x7849('0x58')]=_0x7849('0x59');respondWithRpcPromise(_0x7849('0x5a'),'EventManager',{'event':'rejectmessage','message':_0x29586b['message']},client9002);return _0x36f466;}return null;})[_0x7849('0xe')](respondWithResult(_0xbb4acc,null))[_0x7849('0x18')](handleError(_0xbb4acc,null));};exports[_0x7849('0x19')]=function(_0x516f44,_0x3f4392){if(_0x516f44[_0x7849('0x40')]['id']){delete _0x516f44['body']['id'];}var _0x4f2663;return db[_0x7849('0x26')][_0x7849('0x3e')]({'where':{'id':_0x516f44[_0x7849('0x3d')]['id']}})['then'](handleEntityNotFound(_0x3f4392,null))[_0x7849('0xe')](function(_0x229249){if(_0x229249){_0x4f2663=_0x229249;return db[_0x7849('0x5b')]['find']({'where':{'id':_0x229249[_0x7849('0x5c')]}});}})[_0x7849('0xe')](handleEntityNotFound(_0x3f4392,null))[_0x7849('0xe')](function(_0x30d25f){if(_0x30d25f){logger[_0x7849('0x11')](_0x7849('0x5d'),_0x7849('0x19'),_0x7849('0x5e'),_0x30d25f[_0x7849('0x27')],util[_0x7849('0x5f')](_0x516f44['body'],{'showHidden':![],'depth':null}));switch(_0x30d25f[_0x7849('0x27')]){case'twilio':if(_0x516f44['body'][_0x7849('0x60')]&&_0x516f44['body'][_0x7849('0x60')]===_0x4f2663[_0x7849('0x61')]&&_0x516f44[_0x7849('0x40')][_0x7849('0x62')]&&_0x516f44[_0x7849('0x40')][_0x7849('0x62')]===_0x7849('0x63')){return _0x4f2663['update']({'read':!![]});}else{logger[_0x7849('0x14')]('WhatsappAccount,\x20%s,\x20%s,\x20%s','status','twilio',util[_0x7849('0x5f')](_0x516f44[_0x7849('0x40')],{'showHidden':![],'depth':null}));}break;}return _0x4f2663;}})['then'](respondWithResult(_0x3f4392,null))['catch'](handleError(_0x3f4392,null));};