Built motion from commit 3c2ce842.|2.6.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 _0xb7fc=['WhatsappMessage,\x20%s','finally','utf8mb4','utf8mb4_unicode_ci','lodash','../../config/logger','api','moment','request-promise','path','rimraf','../../config/environment','./whatsappMessage.attributes','WhatsappMessage','whatsapp_messages','messageId','fti_whatsapp_messages','FULLTEXT','models','get','toLowerCase','direction','out','secret','UserId','WhatsappInteractionId','then','createdAt','catch','error','WhatsappAccount','find','WhatsappAccountId','type','twilio','phone','authToken','body','whatsapp:','remote','/api/whatsapp/messages/','token','sid','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken','sinch','accountSid','baseUrl','/messages/','text','POST','application/json','statuses','message_id','update'];(function(_0x5da7fb,_0x79a363){var _0x5c4d09=function(_0x37a707){while(--_0x37a707){_0x5da7fb['push'](_0x5da7fb['shift']());}};_0x5c4d09(++_0x79a363);}(_0xb7fc,0x193));var _0xcb7f=function(_0x426de2,_0x24acaf){_0x426de2=_0x426de2-0x0;var _0x1dbafc=_0xb7fc[_0x426de2];return _0x1dbafc;};'use strict';var _=require(_0xcb7f('0x0'));var util=require('util');var logger=require(_0xcb7f('0x1'))(_0xcb7f('0x2'));var moment=require(_0xcb7f('0x3'));var BPromise=require('bluebird');var rp=require(_0xcb7f('0x4'));var fetch=require('node-fetch');var fs=require('fs');var path=require(_0xcb7f('0x5'));var rimraf=require(_0xcb7f('0x6'));var config=require(_0xcb7f('0x7'));var attributes=require(_0xcb7f('0x8'));module['exports']=function(_0x2eab51,_0x2fb562){return _0x2eab51['define'](_0xcb7f('0x9'),attributes,{'tableName':_0xcb7f('0xa'),'paranoid':![],'indexes':[{'name':_0xcb7f('0xb'),'fields':[_0xcb7f('0xb')]},{'name':_0xcb7f('0xc'),'fields':['body'],'type':_0xcb7f('0xd')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x2abbae,_0x599aa0,_0x3845e7){var _0x29eba9=_0x2eab51[_0xcb7f('0xe')];var _0x469e57=_0x2abbae[_0xcb7f('0xf')]({'plain':!![]});if(_0x469e57['direction'][_0xcb7f('0x10')]()==='in'||_0x469e57[_0xcb7f('0x11')][_0xcb7f('0x10')]()===_0xcb7f('0x12')&&!_0x469e57[_0xcb7f('0x13')]&&!_['isNil'](_0x469e57[_0xcb7f('0x14')])){_0x29eba9['WhatsappInteraction']['findOne']({'where':{'id':_0x469e57[_0xcb7f('0x15')]}})[_0xcb7f('0x16')](function(_0x1cb0cf){if(_0x1cb0cf){_0x1cb0cf['update']({'lastMsgAt':_0x469e57[_0xcb7f('0x17')],'lastMsgDirection':_0x469e57['direction'][_0xcb7f('0x10')]()});}})[_0xcb7f('0x18')](function(_0x202a3a){console[_0xcb7f('0x19')](_0x202a3a);});}if(_0x469e57['direction']===_0xcb7f('0x12')&&!_0x469e57[_0xcb7f('0x13')]){var _0x5d4b49;_0x29eba9[_0xcb7f('0x1a')][_0xcb7f('0x1b')]({'where':{'id':_0x469e57[_0xcb7f('0x1c')]},'raw':!![]})[_0xcb7f('0x16')](function(_0x54bc13){_0x5d4b49=_0x54bc13;if(_0x5d4b49&&_0x5d4b49[_0xcb7f('0x1d')]){switch(_0x5d4b49['type']){case _0xcb7f('0x1e'):var _0x43c339=require('twilio');if(_0x5d4b49['accountSid']&&_0x5d4b49['authToken']&&_0x5d4b49[_0xcb7f('0x1f')]){var _0x273de2=new _0x43c339(_0x5d4b49['accountSid'],_0x5d4b49[_0xcb7f('0x20')]);_0x273de2['messages']['create']({'body':_0x469e57[_0xcb7f('0x21')],'to':_0xcb7f('0x22')+_0x469e57['phone'],'from':_0xcb7f('0x22')+_0x5d4b49['phone'],'statusCallback':_0x5d4b49[_0xcb7f('0x23')]+_0xcb7f('0x24')+_0x469e57['id']+'/status?token='+_0x5d4b49[_0xcb7f('0x25')]})[_0xcb7f('0x16')](function(_0x801502){if(_0x801502[_0xcb7f('0x26')]){return _0x2abbae['update']({'messageId':_0x801502[_0xcb7f('0x26')]});}})[_0xcb7f('0x18')](function(_0x45c27a){logger['error'](_0xcb7f('0x27'),_0xcb7f('0x28'),_0xcb7f('0x1e'),util[_0xcb7f('0x29')](_0x45c27a,{'showHidden':![],'depth':null}));});}else{logger[_0xcb7f('0x19')](_0xcb7f('0x27'),'afterCreate',_0xcb7f('0x1e'),_0xcb7f('0x2a'));}break;case _0xcb7f('0x2b'):var _0x28b517=_0x5d4b49[_0xcb7f('0x2c')];var _0x5a228d=_0x5d4b49[_0xcb7f('0x20')];var _0x1a920e=_0x5d4b49[_0xcb7f('0x2d')]+'/whatsapp/v1/'+_0x28b517+_0xcb7f('0x2e');var _0x2e162f={'to':[_0x469e57[_0xcb7f('0x1f')]],'message':{'type':_0xcb7f('0x2f'),'text':_0x469e57[_0xcb7f('0x21')]}};var _0x2529d7={'method':_0xcb7f('0x30'),'uri':_0x1a920e,'headers':{'Authorization':'Bearer\x20'+_0x5a228d,'Content-Type':_0xcb7f('0x31')},'body':_0x2e162f,'json':!![]};rp(_0x2529d7)[_0xcb7f('0x16')](function(_0x7c6126){if(_0x7c6126[_0xcb7f('0x32')][0x0][_0xcb7f('0x33')]){return _0x2abbae[_0xcb7f('0x34')]({'messageId':_0x7c6126[_0xcb7f('0x32')][0x0][_0xcb7f('0x33')]});}})[_0xcb7f('0x18')](function(_0x529cc7){logger['err'](_0xcb7f('0x35'),_0x529cc7);});break;}}})[_0xcb7f('0x36')](function(){_0x3845e7();});}else{_0x3845e7();}}},'charset':_0xcb7f('0x37'),'collate':_0xcb7f('0x38')});};