dbController.js 15.1 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"use strict";var _dbconn=require("../config/dbconn");function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.dbController=void 0;function _regeneratorRuntime(){"use strict";/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */function a(a,b,c){return Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}),a[b]}function b(a,b,e,f){var g=b&&b.prototype instanceof d?b:d,h=Object.create(g.prototype),j=new l(f||[]);return h._invoke=function(a,b,d){var e="suspendedStart";return function(f,g){if("executing"===e)throw new Error("Generator is already running");if("completed"===e){if("throw"===f)throw g;return n()}for(d.method=f,d.arg=g;;){var h=d.delegate;if(h){var j=i(h,d);if(j){if(j===v)continue;return j}}if("next"===d.method)d.sent=d._sent=d.arg;else if("throw"===d.method){if("suspendedStart"===e)throw e="completed",d.arg;d.dispatchException(d.arg)}else"return"===d.method&&d.abrupt("return",d.arg);e="executing";var k=c(a,b,d);if("normal"===k.type){if(e=d.done?"completed":"suspendedYield",k.arg===v)continue;return{value:k.arg,done:d.done}}"throw"===k.type&&(e="completed",d.method="throw",d.arg=k.arg)}}}(a,e,j),h}function c(a,b,c){try{return{type:"normal",arg:a.call(b,c)}}catch(a){return{type:"throw",arg:a}}}function d(){}function e(){}function f(){}function g(b){["next","throw","return"].forEach(function(c){a(b,c,function(a){return this._invoke(c,a)})})}function h(a,b){function d(e,f,g,h){var i=c(a[e],a,f);if("throw"!==i.type){var j=i.arg,k=j.value;return k&&"object"==_typeof(k)&&q.call(k,"__await")?b.resolve(k.__await).then(function(a){d("next",a,g,h)},function(a){d("throw",a,g,h)}):b.resolve(k).then(function(a){j.value=a,g(j)},function(a){return d("throw",a,g,h)})}h(i.arg)}var e;this._invoke=function(a,c){function f(){return new b(function(b,e){d(a,c,b,e)})}return e=e?e.then(f,f):f()}}function i(a,b){var d=a.iterator[b.method];if(void 0===d){if(b.delegate=null,"throw"===b.method){if(a.iterator["return"]&&(b.method="return",b.arg=void 0,i(a,b),"throw"===b.method))return v;b.method="throw",b.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var e=c(d,a.iterator,b.arg);if("throw"===e.type)return b.method="throw",b.arg=e.arg,b.delegate=null,v;var f=e.arg;return f?f.done?(b[a.resultName]=f.value,b.next=a.nextLoc,"return"!==b.method&&(b.method="next",b.arg=void 0),b.delegate=null,v):f:(b.method="throw",b.arg=new TypeError("iterator result is not an object"),b.delegate=null,v)}function j(a){var b={tryLoc:a[0]};1 in a&&(b.catchLoc=a[1]),2 in a&&(b.finallyLoc=a[2],b.afterLoc=a[3]),this.tryEntries.push(b)}function k(a){var b=a.completion||{};b.type="normal",delete b.arg,a.completion=b}function l(a){this.tryEntries=[{tryLoc:"root"}],a.forEach(j,this),this.reset(!0)}function m(a){if(a){var b=a[s];if(b)return b.call(a);if("function"==typeof a.next)return a;if(!isNaN(a.length)){var c=-1,d=function b(){for(;++c<a.length;)if(q.call(a,c))return b.value=a[c],b.done=!1,b;return b.value=void 0,b.done=!0,b};return d.next=d}}return{next:n}}function n(){return{value:void 0,done:!0}}_regeneratorRuntime=function(){return o};var o={},p=Object.prototype,q=p.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},s=r.iterator||"@@iterator",t=r.asyncIterator||"@@asyncIterator",u=r.toStringTag||"@@toStringTag";try{a({},"")}catch(b){a=function(a,b,c){return a[b]=c}}o.wrap=b;var v={},w={};a(w,s,function(){return this});var x=Object.getPrototypeOf,y=x&&x(x(m([])));y&&y!==p&&q.call(y,s)&&(w=y);var z=f.prototype=d.prototype=Object.create(w);return e.prototype=f,a(z,"constructor",f),a(f,"constructor",e),e.displayName=a(f,u,"GeneratorFunction"),o.isGeneratorFunction=function(a){var b="function"==typeof a&&a.constructor;return!!b&&(b===e||"GeneratorFunction"===(b.displayName||b.name))},o.mark=function(b){return Object.setPrototypeOf?Object.setPrototypeOf(b,f):(b.__proto__=f,a(b,u,"GeneratorFunction")),b.prototype=Object.create(z),b},o.awrap=function(a){return{__await:a}},g(h.prototype),a(h.prototype,t,function(){return this}),o.AsyncIterator=h,o.async=function(a,c,d,e,f){void 0===f&&(f=Promise);var g=new h(b(a,c,d,e),f);return o.isGeneratorFunction(c)?g:g.next().then(function(a){return a.done?a.value:g.next()})},g(z),a(z,u,"Generator"),a(z,s,function(){return this}),a(z,"toString",function(){return"[object Generator]"}),o.keys=function(a){var b=[];for(var c in a)b.push(c);return b.reverse(),function c(){for(;b.length;){var d=b.pop();if(d in a)return c.value=d,c.done=!1,c}return c.done=!0,c}},o.values=m,l.prototype={constructor:l,reset:function reset(a){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!a)for(var b in this)"t"===b.charAt(0)&&q.call(this,b)&&!isNaN(+b.slice(1))&&(this[b]=void 0)},stop:function stop(){this.done=!0;var a=this.tryEntries[0].completion;if("throw"===a.type)throw a.arg;return this.rval},dispatchException:function dispatchException(a){function b(b,d){return f.type="throw",f.arg=a,c.next=b,d&&(c.method="next",c.arg=void 0),!!d}if(this.done)throw a;for(var c=this,d=this.tryEntries.length-1;0<=d;--d){var e=this.tryEntries[d],f=e.completion;if("root"===e.tryLoc)return b("end");if(e.tryLoc<=this.prev){var g=q.call(e,"catchLoc"),h=q.call(e,"finallyLoc");if(g&&h){if(this.prev<e.catchLoc)return b(e.catchLoc,!0);if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(!g){if(!h)throw new Error("try statement without catch or finally");if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(this.prev<e.catchLoc)return b(e.catchLoc,!0)}}},abrupt:function abrupt(a,b){for(var c,d=this.tryEntries.length-1;0<=d;--d)if(c=this.tryEntries[d],c.tryLoc<=this.prev&&q.call(c,"finallyLoc")&&this.prev<c.finallyLoc){var e=c;break}e&&("break"===a||"continue"===a)&&e.tryLoc<=b&&b<=e.finallyLoc&&(e=null);var f=e?e.completion:{};return f.type=a,f.arg=b,e?(this.method="next",this.next=e.finallyLoc,v):this.complete(f)},complete:function complete(a,b){if("throw"===a.type)throw a.arg;return"break"===a.type||"continue"===a.type?this.next=a.arg:"return"===a.type?(this.rval=this.arg=a.arg,this.method="return",this.next="end"):"normal"===a.type&&b&&(this.next=b),v},finish:function finish(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.finallyLoc===a)return this.complete(b.completion,b.afterLoc),k(b),v},catch:function _catch(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.tryLoc===a){var d=b.completion;if("throw"===d.type){var e=d.arg;k(b)}return e}throw new Error("illegal catch attempt")},delegateYield:function delegateYield(a,b,c){return this.delegate={iterator:m(a),resultName:b,nextLoc:c},"next"===this.method&&(this.arg=void 0),v}},o}function asyncGeneratorStep(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){return void c(a)}h.done?b(i):Promise.resolve(i).then(d,e)}function _asyncToGenerator(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){function f(a){asyncGeneratorStep(h,d,e,f,g,"next",a)}function g(a){asyncGeneratorStep(h,d,e,f,g,"throw",a)}var h=a.apply(b,c);f(void 0)})}}var dbController={// ===================== user db =====================
registerNewUser:function registerNewUser(a,b){_dbconn.dbConnection.user.getConnection(function(c,d){d.beginTransaction(function(b){// START TRANSACTION
if(b)throw b;// insert profile
d.query("INSERT INTO user SET ?",a.profile,function(b,c){if(b)return d.rollback(function(){throw b});var e=c.insertId,f={user_id:e,password:a.password};// set password
d.query("INSERT INTO credential SET ?",f,function(b){if(b)return d.rollback(function(){throw b});// set default user-project-role
d.query("INSERT INTO user_project_role SET ?",{project_id:1,// M4_LAB
role_id:2,// USER
user_id:e},function(b){if(b)return d.rollback(function(){throw b});// MLAB-129: INSERT verification token
var c={user_id:e,token:a.verificationToken};d.query("INSERT INTO verification SET ?",c,function(a){return a?d.rollback(function(){throw a}):void// COMMIT
d.commit(function(a){if(a)return d.rollback(function(){throw a})})})})})})}),b(c)})},getUserByEmail:function(){function a(){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c;return _regeneratorRuntime().wrap(function d(b){for(;1;)switch(b.prev=b.next){case 0:return b.prev=0,b.next=3,_dbconn.dbConnection.user.promise().query("SELECT id, verificationStatus, salutation, title, firstname, lastname, industry, organisation, speciality, m4lab_idp FROM user WHERE email = \""+a+"\"");case 3:if(c=b.sent,!c[0][0]){b.next=8;break}return b.abrupt("return",c[0][0]);case 8:return b.abrupt("return",null);case 9:b.next=14;break;case 11:b.prev=11,b.t0=b["catch"](0),console.error(b.t0);case 14:return b.abrupt("return",null);case 15:case"end":return b.stop();}},b,null,[[0,11]])}));return a}(),getUserEmailById:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c;return _regeneratorRuntime().wrap(function d(b){for(;1;)switch(b.prev=b.next){case 0:return b.prev=0,b.next=3,_dbconn.dbConnection.user.promise().query("SELECT email FROM user WHERE id = "+a);case 3:if(c=b.sent,!c[0][0]){b.next=8;break}return b.abrupt("return",c[0][0].email);case 8:return b.abrupt("return",null);case 9:b.next=14;break;case 11:b.prev=11,b.t0=b["catch"](0),console.error(b.t0);case 14:return b.abrupt("return",null);case 15:case"end":return b.stop();}},b,null,[[0,11]])}));return a}(),checkUserEmail:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c;return _regeneratorRuntime().wrap(function d(b){for(;1;)switch(b.prev=b.next){case 0:return b.prev=0,b.next=3,_dbconn.dbConnection.user.promise().query("SELECT id, email FROM user WHERE email = \""+a+"\"");case 3:if(c=b.sent,!c[0][0]){b.next=8;break}return b.abrupt("return",c[0][0]);case 8:return b.abrupt("return",null);case 9:b.next=14;break;case 11:b.prev=11,b.t0=b["catch"](0),console.error(b.t0);case 14:return b.abrupt("return",null);case 15:case"end":return b.stop();}},b,null,[[0,11]])}));return a}(),getUserByToken:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c;return _regeneratorRuntime().wrap(function d(b){for(;1;)switch(b.prev=b.next){case 0:return b.prev=0,b.next=3,_dbconn.dbConnection.user.promise().query("SELECT t1.user_id, t2.email FROM userdb.credential AS t1 INNER JOIN userdb.user AS t2 ON t1.user_id = t2.id AND t1.resetPasswordToken = \""+a+"\" and resetPasswordExpires > "+Date.now());case 3:if(c=b.sent,!c[0][0]){b.next=8;break}return b.abrupt("return",c[0][0]);case 8:return b.abrupt("return",null);case 9:b.next=14;break;case 11:b.prev=11,b.t0=b["catch"](0),console.error(b.t0);case 14:return b.abrupt("return",null);case 15:case"end":return b.stop();}},b,null,[[0,11]])}));return a}(),updateUserById:function(){function a(a,c){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function c(a,b){var d;return _regeneratorRuntime().wrap(function e(c){for(;1;)switch(c.prev=c.next){case 0:return c.prev=0,c.next=3,_dbconn.dbConnection.user.promise().query("UPDATE user SET ? WHERE id = "+a,b);case 3:return d=c.sent,c.abrupt("return",d);case 7:c.prev=7,c.t0=c["catch"](0),console.error(c.t0);case 10:return c.abrupt("return",null);case 11:case"end":return c.stop();}},c,null,[[0,7]])}));return a}(),updateCredential:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c;return _regeneratorRuntime().wrap(function d(b){for(;1;)switch(b.prev=b.next){case 0:return b.prev=0,b.next=3,_dbconn.dbConnection.user.promise().query("UPDATE credential SET ? WHERE user_id = "+a.user_id,a);case 3:return c=b.sent,b.abrupt("return",c);case 7:b.prev=7,b.t0=b["catch"](0),console.error(b.t0);case 10:return b.abrupt("return",null);case 11:case"end":return b.stop();}},b,null,[[0,7]])}));return a}(),addUserProjectRole_OBSOLETE:function addUserProjectRole_OBSOLETE(a,b){_dbconn.dbConnection.user.query("INSERT INTO user_project_role SET ?",a,function(a){if(a)throw a;b(a)})},getVerificationTokenByUserId:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c;return _regeneratorRuntime().wrap(function d(b){for(;1;)switch(b.prev=b.next){case 0:return b.prev=0,b.next=3,_dbconn.dbConnection.user.promise().query("SELECT token FROM verification WHERE user_id = \""+a+"\"");case 3:if(c=b.sent,!c[0][0]){b.next=8;break}return b.abrupt("return",c[0][0].token);case 8:return b.abrupt("return",null);case 9:b.next=14;break;case 11:b.prev=11,b.t0=b["catch"](0),console.error(b.t0);case 14:return b.abrupt("return",null);case 15:case"end":return b.stop();}},b,null,[[0,11]])}));return a}(),getUserIdByVerificationToken:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c;return _regeneratorRuntime().wrap(function d(b){for(;1;)switch(b.prev=b.next){case 0:return b.prev=0,b.next=3,_dbconn.dbConnection.user.promise().query("SELECT user_id FROM verification WHERE token = \""+a+"\"");case 3:if(c=b.sent,!c[0][0]){b.next=8;break}return b.abrupt("return",c[0][0].user_id);case 8:return b.abrupt("return",null);case 9:b.next=14;break;case 11:b.prev=11,b.t0=b["catch"](0),console.error(b.t0);case 14:return b.abrupt("return",null);case 15:case"end":return b.stop();}},b,null,[[0,11]])}));return a}(),verifyUserAccount:function verifyUserAccount(a,b){_dbconn.dbConnection.user.getConnection(function(c,d){d.beginTransaction(function(b){// START TRANSACTION
if(b)throw b;// update user status
d.query("UPDATE user SET ? WHERE id ="+a.id,a,function(b,c,e){return b?d.rollback(function(){throw b}):void// delete verification token
d.query("DELETE FROM verification WHERE user_id = "+a.id,function(a,b,c){return a?d.rollback(function(){throw a}):void// COMMIT
d.commit(function(a){if(a)return d.rollback(function(){throw a})})})})}),b(c)})},/* ===== GitLab ===== */getGitlabId:function(){function a(a){return b.apply(this,arguments)}var b=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function b(a){var c;return _regeneratorRuntime().wrap(function d(b){for(;1;)switch(b.prev=b.next){case 0:return b.prev=0,b.next=3,_dbconn.dbConnection.user.promise().query("SELECT gu.gitlab_userId FROM user_gitlab gu, user u WHERE u.id = \""+a+"\" and gu.user_id = u.id");case 3:if(c=b.sent,!c[0][0]){b.next=8;break}return b.abrupt("return",c[0][0].gitlab_userId);case 8:return b.abrupt("return",null);case 9:b.next=15;break;case 11:return b.prev=11,b.t0=b["catch"](0),console.error(b.t0),b.abrupt("return",b.t0);case 15:case"end":return b.stop();}},b,null,[[0,11]])}));return a}(),addGitlabUser:function addGitlabUser(a,b){_dbconn.dbConnection.user.query("INSERT INTO user_gitlab SET ?",a,function(a){if(a)throw a;b(a)})}};exports.dbController=dbController;