72e8be17497f13e00c834bb809b90f78ca42b1a9
[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 _0x3aae=['WhatsappMessage,\x20%s,\x20%s,\x20%s','finally','utf8mb4','lodash','util','../../config/logger','api','moment','bluebird','request-promise','path','rimraf','../../config/environment','./whatsappMessage.attributes','exports','define','WhatsappMessage','whatsapp_messages','messageId','fti_whatsapp_messages','body','FULLTEXT','toLowerCase','out','secret','isNil','UserId','WhatsappInteraction','findOne','WhatsappInteractionId','then','update','createdAt','direction','error','WhatsappAccount','find','type','twilio','accountSid','phone','authToken','messages','create','whatsapp:','remote','/api/whatsapp/messages/','token','sid','catch','inspect'];(function(_0x4c1991,_0x11a24b){var _0x4fbd4a=function(_0x4f478f){while(--_0x4f478f){_0x4c1991['push'](_0x4c1991['shift']());}};_0x4fbd4a(++_0x11a24b);}(_0x3aae,0x102));var _0xe3aa=function(_0xa464c9,_0x7c86d8){_0xa464c9=_0xa464c9-0x0;var _0x3d73c4=_0x3aae[_0xa464c9];return _0x3d73c4;};'use strict';var _=require(_0xe3aa('0x0'));var util=require(_0xe3aa('0x1'));var logger=require(_0xe3aa('0x2'))(_0xe3aa('0x3'));var moment=require(_0xe3aa('0x4'));var BPromise=require(_0xe3aa('0x5'));var rp=require(_0xe3aa('0x6'));var fs=require('fs');var path=require(_0xe3aa('0x7'));var rimraf=require(_0xe3aa('0x8'));var config=require(_0xe3aa('0x9'));var attributes=require(_0xe3aa('0xa'));module[_0xe3aa('0xb')]=function(_0x5426a5,_0xfda58){return _0x5426a5[_0xe3aa('0xc')](_0xe3aa('0xd'),attributes,{'tableName':_0xe3aa('0xe'),'paranoid':![],'indexes':[{'name':_0xe3aa('0xf'),'fields':[_0xe3aa('0xf')]},{'name':_0xe3aa('0x10'),'fields':[_0xe3aa('0x11')],'type':_0xe3aa('0x12')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x421922,_0x46afb4,_0x20d13f){var _0x329352=_0x5426a5['models'];var _0x582ae6=_0x421922['get']({'plain':!![]});if(_0x582ae6['direction']['toLowerCase']()==='in'||_0x582ae6['direction'][_0xe3aa('0x13')]()===_0xe3aa('0x14')&&!_0x582ae6[_0xe3aa('0x15')]&&!_[_0xe3aa('0x16')](_0x582ae6[_0xe3aa('0x17')])){_0x329352[_0xe3aa('0x18')][_0xe3aa('0x19')]({'where':{'id':_0x582ae6[_0xe3aa('0x1a')]}})[_0xe3aa('0x1b')](function(_0x2e09f5){if(_0x2e09f5){_0x2e09f5[_0xe3aa('0x1c')]({'lastMsgAt':_0x582ae6[_0xe3aa('0x1d')],'lastMsgDirection':_0x582ae6[_0xe3aa('0x1e')][_0xe3aa('0x13')]()});}})['catch'](function(_0x22368e){console[_0xe3aa('0x1f')](_0x22368e);});}if(_0x582ae6['direction']==='out'&&!_0x582ae6[_0xe3aa('0x15')]){var _0x14f175;_0x329352[_0xe3aa('0x20')][_0xe3aa('0x21')]({'where':{'id':_0x582ae6['WhatsappAccountId']},'raw':!![]})[_0xe3aa('0x1b')](function(_0x2dd2da){_0x14f175=_0x2dd2da;if(_0x14f175&&_0x14f175[_0xe3aa('0x22')]){switch(_0x14f175['type']){case _0xe3aa('0x23'):var _0x4ea55e=require('twilio');if(_0x14f175[_0xe3aa('0x24')]&&_0x14f175['authToken']&&_0x14f175[_0xe3aa('0x25')]){var _0xf29ddb=new _0x4ea55e(_0x14f175['accountSid'],_0x14f175[_0xe3aa('0x26')]);_0xf29ddb[_0xe3aa('0x27')][_0xe3aa('0x28')]({'body':_0x582ae6[_0xe3aa('0x11')],'to':_0xe3aa('0x29')+_0x582ae6[_0xe3aa('0x25')],'from':_0xe3aa('0x29')+_0x14f175[_0xe3aa('0x25')],'statusCallback':_0x14f175[_0xe3aa('0x2a')]+_0xe3aa('0x2b')+_0x582ae6['id']+'/status?token='+_0x14f175[_0xe3aa('0x2c')]})[_0xe3aa('0x1b')](function(_0x53e4ff){if(_0x53e4ff[_0xe3aa('0x2d')]){return _0x421922['update']({'messageId':_0x53e4ff[_0xe3aa('0x2d')]});}})[_0xe3aa('0x2e')](function(_0xcc9a62){logger[_0xe3aa('0x1f')]('WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','twilio',util[_0xe3aa('0x2f')](_0xcc9a62,{'showHidden':![],'depth':null}));});}else{logger[_0xe3aa('0x1f')](_0xe3aa('0x30'),'afterCreate',_0xe3aa('0x23'),'Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken');}break;}}})[_0xe3aa('0x31')](function(){_0x20d13f();});}else{_0x20d13f();}}},'charset':_0xe3aa('0x32'),'collate':'utf8mb4_unicode_ci'});};