Built motion from commit (unavailable).|2.5.31
[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 _0x45e2=['body','whatsapp:','/api/whatsapp/messages/','/status?token=','sid','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken','finally','utf8mb4','utf8mb4_unicode_ci','util','../../config/logger','moment','bluebird','request-promise','path','../../config/environment','exports','define','WhatsappMessage','whatsapp_messages','messageId','models','get','toLowerCase','direction','out','secret','isNil','UserId','findOne','WhatsappInteractionId','then','update','createdAt','catch','error','WhatsappAccount','find','type','twilio','authToken','phone','accountSid'];(function(_0x557517,_0x161c97){var _0x59a869=function(_0x1f4467){while(--_0x1f4467){_0x557517['push'](_0x557517['shift']());}};_0x59a869(++_0x161c97);}(_0x45e2,0x1a0));var _0x245e=function(_0xe2a39a,_0x4e58a0){_0xe2a39a=_0xe2a39a-0x0;var _0x53699f=_0x45e2[_0xe2a39a];return _0x53699f;};'use strict';var _=require('lodash');var util=require(_0x245e('0x0'));var logger=require(_0x245e('0x1'))('api');var moment=require(_0x245e('0x2'));var BPromise=require(_0x245e('0x3'));var rp=require(_0x245e('0x4'));var fs=require('fs');var path=require(_0x245e('0x5'));var rimraf=require('rimraf');var config=require(_0x245e('0x6'));var attributes=require('./whatsappMessage.attributes');module[_0x245e('0x7')]=function(_0x4f0359,_0x1d1602){return _0x4f0359[_0x245e('0x8')](_0x245e('0x9'),attributes,{'tableName':_0x245e('0xa'),'paranoid':![],'indexes':[{'name':_0x245e('0xb'),'fields':[_0x245e('0xb')]},{'name':'fti_whatsapp_messages','fields':['body'],'type':'FULLTEXT'}],'timestamps':!![],'hooks':{'afterCreate':function(_0x231a67,_0x28f0fc,_0xf7dc6b){var _0x66db1b=_0x4f0359[_0x245e('0xc')];var _0x504d3a=_0x231a67[_0x245e('0xd')]({'plain':!![]});if(_0x504d3a['direction'][_0x245e('0xe')]()==='in'||_0x504d3a[_0x245e('0xf')][_0x245e('0xe')]()===_0x245e('0x10')&&!_0x504d3a[_0x245e('0x11')]&&!_[_0x245e('0x12')](_0x504d3a[_0x245e('0x13')])){_0x66db1b['WhatsappInteraction'][_0x245e('0x14')]({'where':{'id':_0x504d3a[_0x245e('0x15')]}})[_0x245e('0x16')](function(_0x56a05b){if(_0x56a05b){_0x56a05b[_0x245e('0x17')]({'lastMsgAt':_0x504d3a[_0x245e('0x18')],'lastMsgDirection':_0x504d3a[_0x245e('0xf')]['toLowerCase']()});}})[_0x245e('0x19')](function(_0x37af4e){console[_0x245e('0x1a')](_0x37af4e);});}if(_0x504d3a['direction']===_0x245e('0x10')&&!_0x504d3a[_0x245e('0x11')]){var _0x5fde3a;_0x66db1b[_0x245e('0x1b')][_0x245e('0x1c')]({'where':{'id':_0x504d3a['WhatsappAccountId']},'raw':!![]})[_0x245e('0x16')](function(_0x423c5a){_0x5fde3a=_0x423c5a;if(_0x5fde3a&&_0x5fde3a[_0x245e('0x1d')]){switch(_0x5fde3a[_0x245e('0x1d')]){case _0x245e('0x1e'):var _0xfadd4a=require(_0x245e('0x1e'));if(_0x5fde3a['accountSid']&&_0x5fde3a[_0x245e('0x1f')]&&_0x5fde3a[_0x245e('0x20')]){var _0x235b63=new _0xfadd4a(_0x5fde3a[_0x245e('0x21')],_0x5fde3a[_0x245e('0x1f')]);_0x235b63['messages']['create']({'body':_0x504d3a[_0x245e('0x22')],'to':_0x245e('0x23')+_0x504d3a[_0x245e('0x20')],'from':_0x245e('0x23')+_0x5fde3a[_0x245e('0x20')],'statusCallback':_0x5fde3a['remote']+_0x245e('0x24')+_0x504d3a['id']+_0x245e('0x25')+_0x5fde3a['token']})[_0x245e('0x16')](function(_0x503cd2){if(_0x503cd2[_0x245e('0x26')]){return _0x231a67['update']({'messageId':_0x503cd2['sid']});}})['catch'](function(_0x262564){logger[_0x245e('0x1a')](_0x245e('0x27'),_0x245e('0x28'),_0x245e('0x1e'),util['inspect'](_0x262564,{'showHidden':![],'depth':null}));});}else{logger[_0x245e('0x1a')](_0x245e('0x27'),_0x245e('0x28'),_0x245e('0x1e'),_0x245e('0x29'));}break;}}})[_0x245e('0x2a')](function(){_0xf7dc6b();});}else{_0xf7dc6b();}}},'charset':_0x245e('0x2b'),'collate':_0x245e('0x2c')});};