adefecfdf4b97b2a8884935a9ba548b43bad02fc
[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 _0xd618=['find','WhatsappAccountId','type','twilio','authToken','phone','messages','create','whatsapp:','remote','/status?token=','token','sid','error','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken','accountSid','baseUrl','/whatsapp/v1/','/messages/','text','POST','Bearer\x20','application/json','statuses','message_id','update','WhatsappMessage,\x20%s','finally','utf8mb4','utf8mb4_unicode_ci','lodash','util','api','bluebird','request-promise','node-fetch','path','rimraf','../../config/environment','./whatsappMessage.attributes','exports','whatsapp_messages','messageId','fti_whatsapp_messages','body','FULLTEXT','models','get','direction','toLowerCase','out','secret','isNil','WhatsappInteraction','WhatsappInteractionId','then','createdAt','catch'];(function(_0x4737f4,_0x5c9310){var _0x43751d=function(_0x21f009){while(--_0x21f009){_0x4737f4['push'](_0x4737f4['shift']());}};_0x43751d(++_0x5c9310);}(_0xd618,0x9b));var _0x8d61=function(_0x1db803,_0x582d17){_0x1db803=_0x1db803-0x0;var _0x50aae9=_0xd618[_0x1db803];return _0x50aae9;};'use strict';var _=require(_0x8d61('0x0'));var util=require(_0x8d61('0x1'));var logger=require('../../config/logger')(_0x8d61('0x2'));var moment=require('moment');var BPromise=require(_0x8d61('0x3'));var rp=require(_0x8d61('0x4'));var fetch=require(_0x8d61('0x5'));var fs=require('fs');var path=require(_0x8d61('0x6'));var rimraf=require(_0x8d61('0x7'));var config=require(_0x8d61('0x8'));var attributes=require(_0x8d61('0x9'));module[_0x8d61('0xa')]=function(_0x32a95f,_0x231dc9){return _0x32a95f['define']('WhatsappMessage',attributes,{'tableName':_0x8d61('0xb'),'paranoid':![],'indexes':[{'name':'messageId','fields':[_0x8d61('0xc')]},{'name':_0x8d61('0xd'),'fields':[_0x8d61('0xe')],'type':_0x8d61('0xf')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x31c03c,_0x575a1c,_0x331664){var _0x5e330a=_0x32a95f[_0x8d61('0x10')];var _0x18ee5a=_0x31c03c[_0x8d61('0x11')]({'plain':!![]});if(_0x18ee5a[_0x8d61('0x12')]['toLowerCase']()==='in'||_0x18ee5a[_0x8d61('0x12')][_0x8d61('0x13')]()===_0x8d61('0x14')&&!_0x18ee5a[_0x8d61('0x15')]&&!_[_0x8d61('0x16')](_0x18ee5a['UserId'])){_0x5e330a[_0x8d61('0x17')]['findOne']({'where':{'id':_0x18ee5a[_0x8d61('0x18')]}})[_0x8d61('0x19')](function(_0x3bc026){if(_0x3bc026){_0x3bc026['update']({'lastMsgAt':_0x18ee5a[_0x8d61('0x1a')],'lastMsgDirection':_0x18ee5a[_0x8d61('0x12')]['toLowerCase']()});}})[_0x8d61('0x1b')](function(_0x52b0b6){console['error'](_0x52b0b6);});}if(_0x18ee5a[_0x8d61('0x12')]===_0x8d61('0x14')&&!_0x18ee5a[_0x8d61('0x15')]){var _0x5b6791;_0x5e330a['WhatsappAccount'][_0x8d61('0x1c')]({'where':{'id':_0x18ee5a[_0x8d61('0x1d')]},'raw':!![]})[_0x8d61('0x19')](function(_0x250ede){_0x5b6791=_0x250ede;if(_0x5b6791&&_0x5b6791[_0x8d61('0x1e')]){switch(_0x5b6791[_0x8d61('0x1e')]){case'twilio':var _0x309cee=require(_0x8d61('0x1f'));if(_0x5b6791['accountSid']&&_0x5b6791[_0x8d61('0x20')]&&_0x5b6791[_0x8d61('0x21')]){var _0x4a46f5=new _0x309cee(_0x5b6791['accountSid'],_0x5b6791[_0x8d61('0x20')]);_0x4a46f5[_0x8d61('0x22')][_0x8d61('0x23')]({'body':_0x18ee5a[_0x8d61('0xe')],'to':_0x8d61('0x24')+_0x18ee5a[_0x8d61('0x21')],'from':_0x8d61('0x24')+_0x5b6791[_0x8d61('0x21')],'statusCallback':_0x5b6791[_0x8d61('0x25')]+'/api/whatsapp/messages/'+_0x18ee5a['id']+_0x8d61('0x26')+_0x5b6791[_0x8d61('0x27')]})[_0x8d61('0x19')](function(_0x5abafd){if(_0x5abafd[_0x8d61('0x28')]){return _0x31c03c['update']({'messageId':_0x5abafd['sid']});}})[_0x8d61('0x1b')](function(_0x9ddc82){logger[_0x8d61('0x29')](_0x8d61('0x2a'),_0x8d61('0x2b'),_0x8d61('0x1f'),util[_0x8d61('0x2c')](_0x9ddc82,{'showHidden':![],'depth':null}));});}else{logger[_0x8d61('0x29')](_0x8d61('0x2a'),_0x8d61('0x2b'),'twilio',_0x8d61('0x2d'));}break;case'sinch':var _0x4f9137=_0x5b6791[_0x8d61('0x2e')];var _0x31d79e=_0x5b6791[_0x8d61('0x20')];var _0x1948dd=_0x5b6791[_0x8d61('0x2f')]+_0x8d61('0x30')+_0x4f9137+_0x8d61('0x31');var _0x27b8fe={'to':[_0x18ee5a[_0x8d61('0x21')]],'message':{'type':_0x8d61('0x32'),'text':_0x18ee5a[_0x8d61('0xe')]}};var _0x4cefe0={'method':_0x8d61('0x33'),'uri':_0x1948dd,'headers':{'Authorization':_0x8d61('0x34')+_0x31d79e,'Content-Type':_0x8d61('0x35')},'body':_0x27b8fe,'json':!![]};rp(_0x4cefe0)[_0x8d61('0x19')](function(_0x1c7d08){if(_0x1c7d08[_0x8d61('0x36')][0x0][_0x8d61('0x37')]){return _0x31c03c[_0x8d61('0x38')]({'messageId':_0x1c7d08[_0x8d61('0x36')][0x0][_0x8d61('0x37')]});}})['catch'](function(_0x538482){logger['err'](_0x8d61('0x39'),_0x538482);});break;}}})[_0x8d61('0x3a')](function(){_0x331664();});}else{_0x331664();}}},'charset':_0x8d61('0x3b'),'collate':_0x8d61('0x3c')});};