ff9dc3a0960322311f5db4a918c60f680bf2df0f
[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 _0x672d=['models','get','direction','toLowerCase','secret','isNil','UserId','WhatsappInteraction','findOne','then','createdAt','catch','error','out','WhatsappAccount','find','WhatsappAccountId','type','twilio','accountSid','authToken','phone','messages','whatsapp:','/api/whatsapp/messages/','token','sid','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','finally','utf8mb4','utf8mb4_unicode_ci','lodash','util','../../config/logger','api','moment','request-promise','path','../../config/environment','exports','WhatsappMessage','messageId','fti_whatsapp_messages','body'];(function(_0x392e79,_0x4dfb14){var _0x1e4af0=function(_0x1ebc10){while(--_0x1ebc10){_0x392e79['push'](_0x392e79['shift']());}};_0x1e4af0(++_0x4dfb14);}(_0x672d,0x191));var _0xd672=function(_0x13ae6e,_0x325acf){_0x13ae6e=_0x13ae6e-0x0;var _0x15450d=_0x672d[_0x13ae6e];return _0x15450d;};'use strict';var _=require(_0xd672('0x0'));var util=require(_0xd672('0x1'));var logger=require(_0xd672('0x2'))(_0xd672('0x3'));var moment=require(_0xd672('0x4'));var BPromise=require('bluebird');var rp=require(_0xd672('0x5'));var fs=require('fs');var path=require(_0xd672('0x6'));var rimraf=require('rimraf');var config=require(_0xd672('0x7'));var attributes=require('./whatsappMessage.attributes');module[_0xd672('0x8')]=function(_0x5a22ef,_0xe1de7b){return _0x5a22ef['define'](_0xd672('0x9'),attributes,{'tableName':'whatsapp_messages','paranoid':![],'indexes':[{'name':'messageId','fields':[_0xd672('0xa')]},{'name':_0xd672('0xb'),'fields':[_0xd672('0xc')],'type':'FULLTEXT'}],'timestamps':!![],'hooks':{'afterCreate':function(_0x5153d9,_0x2e8753,_0x1b1554){var _0x164020=_0x5a22ef[_0xd672('0xd')];var _0x26de94=_0x5153d9[_0xd672('0xe')]({'plain':!![]});if(_0x26de94[_0xd672('0xf')]['toLowerCase']()==='in'||_0x26de94[_0xd672('0xf')][_0xd672('0x10')]()==='out'&&!_0x26de94[_0xd672('0x11')]&&!_[_0xd672('0x12')](_0x26de94[_0xd672('0x13')])){_0x164020[_0xd672('0x14')][_0xd672('0x15')]({'where':{'id':_0x26de94['WhatsappInteractionId']}})[_0xd672('0x16')](function(_0x49e677){if(_0x49e677){_0x49e677['update']({'lastMsgAt':_0x26de94[_0xd672('0x17')],'lastMsgDirection':_0x26de94[_0xd672('0xf')][_0xd672('0x10')]()});}})[_0xd672('0x18')](function(_0x4e3f4c){console[_0xd672('0x19')](_0x4e3f4c);});}if(_0x26de94['direction']===_0xd672('0x1a')&&!_0x26de94[_0xd672('0x11')]){var _0x2c6aeb;_0x164020[_0xd672('0x1b')][_0xd672('0x1c')]({'where':{'id':_0x26de94[_0xd672('0x1d')]},'raw':!![]})[_0xd672('0x16')](function(_0x4936db){_0x2c6aeb=_0x4936db;if(_0x2c6aeb&&_0x2c6aeb[_0xd672('0x1e')]){switch(_0x2c6aeb[_0xd672('0x1e')]){case _0xd672('0x1f'):var _0x167613=require('twilio');if(_0x2c6aeb[_0xd672('0x20')]&&_0x2c6aeb[_0xd672('0x21')]&&_0x2c6aeb[_0xd672('0x22')]){var _0xf938d3=new _0x167613(_0x2c6aeb['accountSid'],_0x2c6aeb['authToken']);_0xf938d3[_0xd672('0x23')]['create']({'body':_0x26de94[_0xd672('0xc')],'to':'whatsapp:'+_0x26de94[_0xd672('0x22')],'from':_0xd672('0x24')+_0x2c6aeb[_0xd672('0x22')],'statusCallback':_0x2c6aeb['remote']+_0xd672('0x25')+_0x26de94['id']+'/status?token='+_0x2c6aeb[_0xd672('0x26')]})[_0xd672('0x16')](function(_0x419099){if(_0x419099['sid']){return _0x5153d9['update']({'messageId':_0x419099[_0xd672('0x27')]});}})[_0xd672('0x18')](function(_0x116f37){logger[_0xd672('0x19')](_0xd672('0x28'),_0xd672('0x29'),_0xd672('0x1f'),util[_0xd672('0x2a')](_0x116f37,{'showHidden':![],'depth':null}));});}else{logger[_0xd672('0x19')]('WhatsappMessage,\x20%s,\x20%s,\x20%s',_0xd672('0x29'),_0xd672('0x1f'),'Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken');}break;}}})[_0xd672('0x2b')](function(){_0x1b1554();});}else{_0x1b1554();}}},'charset':_0xd672('0x2c'),'collate':_0xd672('0x2d')});};