5b5fdf39d7c86e77dc055470949ab05f8efdfd74
[motion2.git] / server / api / openchannelMessage / openchannelMessage.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 _0x0cea=['Contact','POST','GET','Message\x20%d\x20forwarded\x20to\x20%s','threadId','Interaction','error','inspect','utf8mb4','utf8mb4_unicode_ci','lodash','util','api','bluebird','request-promise','path','rimraf','../../config/environment','./openchannelMessage.attributes','exports','define','OpenchannelMessage','openchannel_messages','fti_openchannel_messages','body','FULLTEXT','models','get','direction','toLowerCase','out','isNil','OpenchannelInteraction','OpenchannelInteractionId','then','update','createdAt','catch','secret','describe','OpenchannelAccount','find','Interactions','CmContact','keys','replyUri','info','OpenchannelMessage,\x20%s,\x20%s','afterCreate','format','length'];(function(_0xc4001c,_0x1bf8f0){var _0x49f7bc=function(_0x1059b8){while(--_0x1059b8){_0xc4001c['push'](_0xc4001c['shift']());}};_0x49f7bc(++_0x1bf8f0);}(_0x0cea,0x70));var _0xa0ce=function(_0x39418d,_0x27ee49){_0x39418d=_0x39418d-0x0;var _0x221357=_0x0cea[_0x39418d];return _0x221357;};'use strict';var _=require(_0xa0ce('0x0'));var util=require(_0xa0ce('0x1'));var logger=require('../../config/logger')(_0xa0ce('0x2'));var moment=require('moment');var BPromise=require(_0xa0ce('0x3'));var rp=require(_0xa0ce('0x4'));var fs=require('fs');var path=require(_0xa0ce('0x5'));var rimraf=require(_0xa0ce('0x6'));var config=require(_0xa0ce('0x7'));var attributes=require(_0xa0ce('0x8'));module[_0xa0ce('0x9')]=function(_0x5562e6,_0x2899a3){return _0x5562e6[_0xa0ce('0xa')](_0xa0ce('0xb'),attributes,{'tableName':_0xa0ce('0xc'),'paranoid':![],'indexes':[{'name':_0xa0ce('0xd'),'fields':[_0xa0ce('0xe')],'type':_0xa0ce('0xf')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x2c8c0c,_0x24bbd5,_0xc1a30f){var _0xa4b6d8=_0x5562e6[_0xa0ce('0x10')];var _0x20011b=_0x2c8c0c[_0xa0ce('0x11')]({'plain':!![]});if(_0x20011b[_0xa0ce('0x12')][_0xa0ce('0x13')]()==='in'||_0x20011b[_0xa0ce('0x12')]['toLowerCase']()===_0xa0ce('0x14')&&!_0x20011b['secret']&&!_[_0xa0ce('0x15')](_0x20011b['UserId'])){_0xa4b6d8[_0xa0ce('0x16')]['findOne']({'where':{'id':_0x20011b[_0xa0ce('0x17')]}})[_0xa0ce('0x18')](function(_0x4e7611){if(_0x4e7611){_0x4e7611[_0xa0ce('0x19')]({'lastMsgAt':_0x20011b[_0xa0ce('0x1a')],'lastMsgDirection':_0x20011b[_0xa0ce('0x12')]['toLowerCase']()});}})[_0xa0ce('0x1b')](function(_0x1c4ef2){console['error'](_0x1c4ef2);});}if(_0x20011b[_0xa0ce('0x12')]==='out'&&!_0x20011b[_0xa0ce('0x1c')]){var _0x294ee9;_0xa4b6d8['CmContact'][_0xa0ce('0x1d')]()[_0xa0ce('0x18')](function(_0x10589f){return _0xa4b6d8[_0xa0ce('0x1e')][_0xa0ce('0x1f')]({'where':{'id':_0x20011b['OpenchannelAccountId']},'include':[{'model':_0xa4b6d8[_0xa0ce('0x16')],'as':_0xa0ce('0x20'),'where':{'id':_0x20011b['OpenchannelInteractionId']},'limit':0x1,'include':[{'model':_0xa4b6d8[_0xa0ce('0x21')],'as':'Contact','attributes':_[_0xa0ce('0x22')](_0x10589f)}]}]});})['then'](function(_0x5de12b){_0x294ee9=_0x5de12b[_0xa0ce('0x11')]({'plain':!![]});if(_0x294ee9&&_0x294ee9[_0xa0ce('0x23')]){logger[_0xa0ce('0x24')](_0xa0ce('0x25'),_0xa0ce('0x26'),util[_0xa0ce('0x27')]('Forwarding\x20message\x20%d\x20to\x20%s',_0x20011b['id'],_0x294ee9[_0xa0ce('0x23')]));if(_0x294ee9[_0xa0ce('0x20')]&&_0x294ee9['Interactions'][_0xa0ce('0x28')]){_0x20011b['Interaction']=_['omit'](_0x294ee9['Interactions'][0x0],_0xa0ce('0x29'));_0x20011b[_0xa0ce('0x29')]=_0x294ee9['Interactions'][0x0][_0xa0ce('0x29')];}var _0x24bbd5={'method':_0xa0ce('0x2a'),'uri':encodeURI(_0x294ee9[_0xa0ce('0x23')]),'json':!![],'resolveWithFullResponse':!![]};switch(_0x24bbd5['method']){case _0xa0ce('0x2b'):_0x24bbd5['qs']=_0x20011b;break;case _0xa0ce('0x2a'):_0x24bbd5['body']=_0x20011b;break;}return rp(_0x24bbd5)['then'](function(_0x2a7fa8){logger[_0xa0ce('0x24')]('OpenchannelMessage,\x20%s,\x20%s',_0xa0ce('0x26'),util['format'](_0xa0ce('0x2c'),_0x20011b['id'],_0x294ee9[_0xa0ce('0x23')]));if(_0x2a7fa8[_0xa0ce('0xe')][_0xa0ce('0x2d')]&&_0x2a7fa8[_0xa0ce('0xe')][_0xa0ce('0x2d')]!=_0x20011b['Interaction'][_0xa0ce('0x2d')]){return _0xa4b6d8['OpenchannelInteraction'][_0xa0ce('0x19')]({'threadId':_0x2a7fa8[_0xa0ce('0xe')]['threadId']},{'where':{'id':_0x20011b[_0xa0ce('0x2e')]['id']}});}else{return;}})[_0xa0ce('0x18')](function(){return _0x2c8c0c[_0xa0ce('0x19')]({'read':!![]});})['catch'](function(_0x32e880){logger[_0xa0ce('0x2f')]('OpenchannelMessage,\x20%s,\x20%s','afterCreate',util[_0xa0ce('0x30')](_0x32e880,{'showHidden':![],'depth':null}));_0xc1a30f();});}else{_0xc1a30f();}})['finally'](function(){_0xc1a30f();});}else{_0xc1a30f();}}},'charset':_0xa0ce('0x31'),'collate':_0xa0ce('0x32')});};