How to use mongoose.disconnect method in ava

Best JavaScript code snippet using ava

cs.js

Source:cs.js Github

copy

Full Screen

...110 employees.find({},{'username':1,'password':1}).exec(function (err, result) {111 if(err)112 {113 res.send({success:false,message:'Somthing went wrong, Please try again.'});114 mongoose.disconnect();115 }116 else117 {118 119 res.json(result);120 121 122 mongoose.disconnect();123 }124 });125 });126 };127 exports.authUser= function(userdetails, res) {128 mongoose.connect(process.env.MONGOLAB_URI,opts).then(129 ()=>{130 employees.find({'username':userdetails.username,'password':userdetails.password},{'username':1,'profilepic':1,'role':1,'branch':1}).exec(function (err, result) {131 if(err)132 {133 res.send({success:false,message:'Somthing went wrong, Please try again.'});134 mongoose.disconnect();135 }136 else137 {138 if(result.length > 1 || result.length <= 0)139 {140 res.json({success:false,message:'Login cridentials does not matched.'});141 }142 if(result.length == 1)143 {144 var payload = {145 userid: result[0]._id 146 }147 var token = jwt.sign(payload, app.get('superSecret'), {148 expiresIn: 28800 // expires in 24 hours = 86400149 });150 151 var d = new Date();152 d.setTime(d.getTime() + (0.7*24*60*60*1000));153 var expires = d.toUTCString();154 155 res.cookie('token', token, { expires: new Date(expires), httpOnly: true });156 res.cookie('username', result[0].username, { expires: new Date(expires), httpOnly: true });157 res.cookie('id', result[0].id, { expires: new Date(expires), httpOnly: true });158 res.cookie('role', result[0].role, { expires: new Date(expires), httpOnly: true });159 res.cookie('branchid', String(result[0].branch[0]), { expires: new Date(expires), httpOnly: true });160 res.cookie('profilepic', result[0].profilepic, { expires: new Date(expires), httpOnly: true });161 162 res.send({success:true,message:'logged in successfully.'});163 }164 165 mongoose.disconnect();166 }167 });168 });169 }; 170 171 exports.ForgotPassword = function(userdetails, res) {172 173 mongoose.connect(process.env.MONGOLAB_URI,opts).then(174 ()=>{175 employees.find({'username':userdetails.username,'contactdetails.email':userdetails.email},{'username':1,'contactdetails.mobile1':1,'contactdetails.email':1}).exec(function (err, result) {176 177 if(err)178 {179 res.send({success:false,message:'Somthing went wrong, Please try again.'});180 mongoose.disconnect();181 }182 else183 {184 if(result.length > 1 || result.length <= 0)185 {186 res.json({success:false,message:'Details does not matched.'});187 }188 if(result.length == 1)189 {190 var d = new Date();191 d.setTime(d.getTime() + (0.1*24*60*60*1000));192 var expires = d.toUTCString();193 194 var otp = Math.floor(100000 + Math.random() * 900000);195 var sentotp = encrypt(String(otp));196 var userid = String(result[0]._id);197 res.cookie('otp',sentotp, { expires: new Date(expires), httpOnly: true });198 res.cookie('_id',userid, { expires: new Date(expires), httpOnly: true });199 200 /* let text = {201 // To Number is the number you will be sending the text to.202 toNumber: '+91'+result[0].contactdetails.mobile1,203 // From number is the number you will buy from your admin dashboard204 fromNumber: '+14157992515',205 // Text Content206 smsBody: 'Your OTP is '+otp+'. \n Please enter it for reset your password for CS portal.\n This OTP is valid for 10 minuts. \n Thanks, \n Admin \n (CS Pvt. Ltd.)',207 //Sign up for an account to get an API Token208 //apiToken: '6ampbh' registered token209 apiToken: '6ampbh'210 };211 212 puretext.send(text, function (err, response) {213 if(err) console.log(err);214 else {215 res.send({success:true,message:'OTP sent to your registered mobile number.'});216 }217 }); */218 219 const mailOptions = {220 from: 'mhatremayur2520@gmail.com', // sender address221 to: result[0].contactdetails.email, // list of receivers222 subject: 'Forgot Password', // Subject line223 html: '<h1 style="font-weight:bold;text-align:center;">'+otp+'</h1> <br> <p>Please enter it for reset your password for CS portal.<br> This OTP is valid for 10 minuts. <br><br><br> <div style="float:left;text-align:left;">Thanks, <br> Admin <br> (CS Pvt. Ltd.)</div></p>'// plain text body224 };225 226 227 228 transporter.sendMail(mailOptions, function (err, info) {229 if(err)230 console.log(err)231 else232 console.log(info);233 res.send({success:true,message:'OTP sent to your registered mobile number.'});234 });235 236 237 }238 }239 });240 });241 242 }; 243 244 exports.verifyOTP= function(req, res) {245 var cookies = req.headers.cookie.split(';', 5);246 cookies.map(function(value){ getvaluesinObject(value)});247 248 var recievedotp = encrypt(String(req.params.otp))249 if(recievedotp === verificationObject[0].otp)250 {251 res.clearCookie('otp', { path: '/' });252 res.send({status:0});253 }254 else255 {256 res.send({status:1});257 }258 }; 259 260 261 exports.ResetPassword= function(req, res) {262 if(req.headers.cookie)263 {264 var cookies = req.headers.cookie.split(';', 5);265 cookies.map(function(value){ getvaluesinObject(value)});266 if(verificationObject[0]._id)267 {268 mongoose.connect(process.env.MONGOLAB_URI,opts).then(269 ()=>{270 employees.updateOne({_id:verificationObject[0]._id},{$set: { 'password': req.body.password}}).exec(function (err, result) {271 console.log(err);272 if(err)273 {274 res.send({status:1,message:'Somthing went wrong, Please try again!'});275 mongoose.disconnect();276 }277 else278 {279 res.send({status:0,message:'Password updated successfully, Thank you!'});280 mongoose.disconnect();281 }282 });283 });284 }285 else286 {287 res.send({status:1,message:'Somthing went wrong, Please generate OTP again'});288 }289 }290 else291 {292 res.send({status:1,message:'Somthing went wrong, Please generate OTP again'});293 }294 }; 295 296 297 /* TERMS AND CONDITIONS */298 299 300 301 302 303 exports.ListTermsNCondtions= function(req, res) {304 if(req.decoded.success == true)305 {306 mongoose.connect(process.env.MONGOLAB_URI,opts).then(307 ()=>{308 termsnconditions.find({}).populate('createdby','username').exec(function (err, result) {309 if(err)310 {311 res.send({status:1,message:'Somthing went wrong, Please try again!'});312 mongoose.disconnect();313 }314 else315 {316 res.send(result);317 mongoose.disconnect();318 }319 });320 });321 }322 }; 323 324 exports.DeleteTermCondition= function(req, res) {325 if(req.decoded.success == true)326 {327 mongoose.connect(process.env.MONGOLAB_URI,opts).then(328 ()=>{329 var termid = req.params.termid;330 termsnconditions.deleteOne({_id:termid}).exec(function (err, result) {331 if(err)332 {333 res.send({status:1,message:'Somthing went wrong, Please try again!'});334 mongoose.disconnect();335 }336 else337 {338 res.send({status:0,message:'Record deleted successfully!'});339 mongoose.disconnect();340 }341 });342 });343 }344 }; 345 346 exports.getTermsDetails= function(req, res) {347 if(req.decoded.success == true)348 {349 mongoose.connect(process.env.MONGOLAB_URI,opts).then(350 ()=>{351 var termid = req.params.termid;352 termsnconditions.find({_id:termid}).exec(function (err, result) {353 if(err)354 {355 res.send({status:1,message:'Somthing went wrong, Please try again!'});356 mongoose.disconnect();357 }358 else359 {360 res.send(result);361 mongoose.disconnect();362 }363 });364 });365 }366 }; 367 368 exports.SaveTermsDetails= function(req, res) {369 if(req.decoded.success == true)370 {371 var tearmsDetails = req.body;372 var description = tearmsDetails.description ;373 tearmsDetails.createdby = req.Loggedinuser.id;374 mongoose.connect(process.env.MONGOLAB_URI,opts).then(375 ()=>{376 var tearmsdetails = new termsnconditions(tearmsDetails);377 if(tearmsDetails._id)378 {379 termsnconditions.update({ _id: tearmsDetails._id }, tearmsDetails,{ multi: true }, function(err) {380 if(err)381 {382 res.send({status:1,message:'Somthing went wrong, Please try again!'});383 mongoose.disconnect();384 }385 else386 {387 res.send({status:0,message:'Record updated successfully!'});388 mongoose.disconnect();389 }390 });391 }392 else393 {394 tearmsdetails.save(function(err,result) {395 if(err)396 {397 res.send({status:1,message:'Somthing went wrong, Please try again!'});398 mongoose.disconnect();399 }400 else401 {402 res.send({status:0,message:'Record inserted successfully!'});403 mongoose.disconnect();404 }405 });406 }407 });408 }409 }; 410 411 412 exports.RemoveTermDescriptionPoint= function(req, res) {413 if(req.decoded.success == true)414 {415 var tearmsDetailsid = req.params.termdetailsid;416 var termmasterid = req.params.termmasterid;417 mongoose.connect(process.env.MONGOLAB_URI,opts).then(418 ()=>{419 termsnconditions.update( 420 { _id: termmasterid },421 { $pull: { description : { _id : tearmsDetailsid } } },422 { safe: true },423 function removeConnectionsCB(err, result) {424 if(err)425 {426 res.send({status:1,message:'Somthing went wrong, Please try again!'});427 mongoose.disconnect();428 }429 else430 {431 res.send({status:0,message:'Record deleted successfully!'});432 mongoose.disconnect();433 }434 });435 });436 }437 }; 438 439 /* TERMS AND CONDITIONS */440 441 /* BRANCH DETAILS */442 443 exports.ListBranchs= function(req, res) {444 if(req.decoded.success == true)445 {446 mongoose.connect(process.env.MONGOLAB_URI,opts).then(447 ()=>{448 branches.find({}).populate('createdby','username').exec(function (err, result) {449 if(err)450 {451 res.send({status:1,message:'Somthing went wrong, Please try again!'});452 mongoose.disconnect();453 }454 else455 {456 res.send(result);457 mongoose.disconnect();458 }459 });460 });461 }462 }; 463 464 465 exports.getBranchDetails= function(req, res) {466 if(req.decoded.success == true)467 {468 mongoose.connect(process.env.MONGOLAB_URI,opts).then(469 ()=>{470 var branchid = req.params.branchid471 branches.find({_id:branchid}).exec(function (err, result) {472 if(err)473 {474 res.send({status:1,message:'Somthing went wrong, Please try again!'});475 mongoose.disconnect();476 }477 else478 {479 res.send(result);480 mongoose.disconnect();481 }482 });483 });484 }485 }; 486 487 exports.DeleteBranch= function(req, res) {488 if(req.decoded.success == true)489 {490 mongoose.connect(process.env.MONGOLAB_URI,opts).then(491 ()=>{492 var branchid = req.params.branchid493 branches.deleteOne({_id:branchid}).exec(function (err, result) {494 if(err)495 {496 res.send({status:1,message:'Somthing went wrong, Please try again!'});497 mongoose.disconnect();498 }499 else500 {501 res.send({status:0,message:'Record deleted successfully!'});502 mongoose.disconnect();503 }504 });505 });506 }507 }; 508 509 510 511 exports.SaveBranchDetails= function(req, res) {512 if(req.decoded.success == true)513 {514 mongoose.connect(process.env.MONGOLAB_URI,opts).then(515 ()=>{516 var branchDetails = req.body;517 branchDetails.createdby = req.Loggedinuser.id;518 519 var branchdetails = new branches(branchDetails);520 if(branchDetails._id)521 {522 branches.update({ _id: branchDetails._id }, branchDetails,{ multi: true }, function(err) {523 if(err)524 {525 res.send({status:1,message:'Somthing went wrong, Please try again!'});526 mongoose.disconnect();527 }528 else529 {530 res.send({status:0,message:'Record updated successfully!'});531 mongoose.disconnect();532 }533 });534 }535 else536 {537 branchdetails.save(function(err,result) {538 if(err)539 {540 res.send({status:1,message:'Somthing went wrong, Please try again!'});541 mongoose.disconnect();542 }543 else544 {545 res.send({status:0,message:'Record inserted successfully!'});546 mongoose.disconnect();547 }548 });549 }550 });551 }552 }; 553 554 555 /* BRANCH DETAILS */556 557 558 /* AREA DETAILS */559 560 exports.ListAreas= function(req, res) {561 if(req.decoded.success == true)562 {563 mongoose.connect(process.env.MONGOLAB_URI,opts).then(564 ()=>{565 areas.find({}).populate('branchid','branchname').exec(function (err, result) { 566 console.log(err);567 if(err)568 {569 res.send({status:1,message:'Somthing went wrong, Please try again!'});570 mongoose.disconnect();571 }572 else573 {574 res.send(result);575 mongoose.disconnect();576 }577 }); 578 579 });580 }581 }; 582 583 584 exports.getAreaDetails= function(req, res) {585 if(req.decoded.success == true)586 {587 mongoose.connect(process.env.MONGOLAB_URI,opts).then(588 ()=>{589 var areaid = req.params.areaid;590 areas.find({_id:areaid}).populate('branchid','branchname').exec(function (err, result) {591 if(err)592 {593 res.send({status:1,message:'Somthing went wrong, Please try again!'});594 mongoose.disconnect();595 }596 else597 {598 res.send(result);599 mongoose.disconnect();600 }601 });602 });603 }604 }; 605 606 exports.DeleteArea= function(req, res) {607 if(req.decoded.success == true)608 {609 mongoose.connect(process.env.MONGOLAB_URI,opts).then(610 ()=>{611 var areaid = req.params.areaid;612 areas.deleteOne({_id:areaid}).exec(function (err, result) {613 if(err)614 {615 res.send({status:1,message:'Somthing went wrong, Please try again!'});616 mongoose.disconnect();617 }618 else619 {620 res.send({status:0,message:'Record deleted successfully!'});621 mongoose.disconnect();622 }623 });624 });625 }626 }; 627 628 629 630 exports.SaveAreaDetails= function(req, res) {631 if(req.decoded.success == true)632 {633 mongoose.connect(process.env.MONGOLAB_URI,opts).then(634 ()=>{635 636 var AreaDetails = req.body637 638 var areasdetails = new areas(AreaDetails);639 if(AreaDetails._id)640 {641 areas.update({ _id: AreaDetails._id }, AreaDetails,{ multi: true }, function(err) {642 if(err)643 {644 res.send({status:1,message:'Somthing went wrong, Please try again!'});645 mongoose.disconnect();646 }647 else648 {649 res.send({status:0,message:'Record updated successfully!'});650 mongoose.disconnect();651 }652 });653 }654 else655 {656 areasdetails.save(function(err,result) {657 if(err)658 {659 res.send({status:1,message:'Somthing went wrong, Please try again!'});660 mongoose.disconnect();661 }662 else663 {664 res.send({status:0,message:'Record inserted successfully!'});665 mongoose.disconnect();666 }667 });668 }669 });670 }671 }; 672 673 674 /* AREA DETAILS */675 676 677 /* GROUP DETAILS */678 679 exports.ListGroups= function(req, res) {680 if(req.decoded.success == true)681 {682 mongoose.connect(process.env.MONGOLAB_URI,opts).then(683 ()=>{684 groups.find({}).populate('areaid','areaname').populate('branchid','branchname').exec(function (err, result) { 685 if(err)686 {687 res.send({status:1,message:'Somthing went wrong, Please try again!'});688 mongoose.disconnect();689 }690 else691 {692 res.send(result);693 mongoose.disconnect();694 }695 });696 });697 }698 }; 699 700 701 exports.getGroupDetails= function(req, res) {702 if(req.decoded.success == true)703 {704 mongoose.connect(process.env.MONGOLAB_URI,opts).then(705 ()=>{706 var groupid = req.params.groupid707 groups.find({_id:groupid}).exec(function (err, result) {708 if(err)709 {710 res.send({status:1,message:'Somthing went wrong, Please try again!'});711 mongoose.disconnect();712 }713 else714 {715 res.send(result);716 mongoose.disconnect();717 }718 });719 });720 }721 }; 722 723 exports.DeleteGroup= function(req, res) {724 if(req.decoded.success == true)725 {726 mongoose.connect(process.env.MONGOLAB_URI,opts).then(727 ()=>{728 729 var groupid = req.params.groupid730 731 groups.deleteOne({_id:groupid}).exec(function (err, result) {732 if(err)733 {734 res.send({status:1,message:'Somthing went wrong, Please try again!'});735 mongoose.disconnect();736 }737 else738 {739 res.send({status:0,message:'Record deleted successfully!'});740 mongoose.disconnect();741 }742 });743 });744 }745 }; 746 747 748 749 exports.SavegroupDetails = function(req, res) {750 if(req.decoded.success == true)751 {752 mongoose.connect(process.env.MONGOLAB_URI,opts).then(753 ()=>{754 var groupDetails = req.body;755 var groupsdetails = new groups(groupDetails);756 if(groupDetails._id)757 {758 groups.update({ _id: groupDetails._id }, groupDetails,{ multi: true }, function(err) {759 if(err)760 {761 res.send({status:1,message:'Somthing went wrong, Please try again!'});762 mongoose.disconnect();763 }764 else765 {766 res.send({status:0,message:'Record updated successfully!'});767 mongoose.disconnect();768 }769 });770 }771 else772 {773 groupsdetails.save(function(err,result) {774 if(err)775 {776 res.send({status:1,message:'Somthing went wrong, Please try again!'});777 mongoose.disconnect();778 }779 else780 {781 res.send({status:0,message:'Record inserted successfully!'});782 mongoose.disconnect();783 }784 });785 }786 });787 }788 }; 789 790 791 /* GROUP DETAILS */792 793 794 /* EVENTS DETAILS */795 796 exports.ListEvents= function(req, res) {797 if(req.decoded.success == true)798 {799 mongoose.connect(process.env.MONGOLAB_URI,opts).then(800 ()=>{801 events.find({}).populate('branch','branchname').populate('group','groupname').populate('areaid','areaname').exec(function (err, result) { 802 if(err)803 {804 res.send({status:1,message:'Somthing went wrong, Please try again!'});805 mongoose.disconnect();806 }807 else808 {809 res.send(result);810 mongoose.disconnect();811 }812 });813 });814 }815 }; 816 817 818 exports.getEventDetails= function(req, res) {819 if(req.decoded.success == true)820 {821 mongoose.connect(process.env.MONGOLAB_URI,opts).then(822 ()=>{823 824 var eventid = req.params.eventid;825 826 events.find({_id:eventid}).exec(function (err, result) {827 if(err)828 {829 res.send({status:1,message:'Somthing went wrong, Please try again!'});830 mongoose.disconnect();831 }832 else833 {834 res.send(result);835 mongoose.disconnect();836 }837 });838 });839 }840 }; 841 842 exports.DeleteEvent= function(req, res) {843 if(req.decoded.success == true)844 {845 mongoose.connect(process.env.MONGOLAB_URI,opts).then(846 ()=>{847 848 var eventid = req.params.eventid;849 850 events.deleteOne({_id:eventid}).exec(function (err, result) {851 if(err)852 {853 res.send({status:1,message:'Somthing went wrong, Please try again!'});854 mongoose.disconnect();855 }856 else857 {858 res.send({status:0,message:'Record deleted successfully!'});859 mongoose.disconnect();860 }861 });862 });863 }864 }; 865 866 867 868 exports.SaveEventsDetails= function(req, res) {869 if(req.decoded.success == true)870 {871 mongoose.connect(process.env.MONGOLAB_URI,opts).then(872 ()=>{873 var eventDetails = req.body;874 var eventsdetails = new events(eventDetails);875 if(eventDetails._id)876 {877 events.update({ _id: eventDetails._id }, eventDetails,{ multi: true }, function(err) {878 if(err)879 {880 res.send({status:1,message:'Somthing went wrong, Please try again!'});881 mongoose.disconnect();882 }883 else884 {885 res.send({status:0,message:'Record updated successfully!'});886 mongoose.disconnect();887 }888 });889 }890 else891 {892 eventsdetails.save(function(err,result) {893 if(err)894 {895 res.send({status:1,message:'Somthing went wrong, Please try again!'});896 mongoose.disconnect();897 }898 else899 {900 res.send({status:0,message:'Record inserted successfully!'});901 mongoose.disconnect();902 }903 });904 }905 });906 }907 }; 908 909 910 /* EVENTS DETAILS */911 912 913 /* ACCOUNT TYPES */914 915 exports.ListAccounttypes= function(req, res) {916 if(req.decoded.success == true)917 {918 mongoose.connect(process.env.MONGOLAB_URI,opts).then(919 ()=>{920 actypes.find({}).exec(function (err, result) { 921 if(err)922 {923 res.send({status:1,message:'Somthing went wrong, Please try again!'});924 mongoose.disconnect();925 }926 else927 {928 res.send(result);929 mongoose.disconnect();930 }931 });932 });933 }934 }; 935 936 937 exports.getAccounttypesDetails= function(req, res) {938 if(req.decoded.success == true)939 {940 mongoose.connect(process.env.MONGOLAB_URI,opts).then(941 ()=>{942 var acTypeid = req.params.acTypesid;943 944 actypes.find({_id:acTypeid}).exec(function (err, result) {945 if(err)946 {947 res.send({status:1,message:'Somthing went wrong, Please try again!'});948 mongoose.disconnect();949 }950 else951 {952 res.send(result);953 mongoose.disconnect();954 }955 });956 });957 }958 }; 959 960 exports.DeleteACTypeDetails= function(req, res) {961 if(req.decoded.success == true)962 {963 mongoose.connect(process.env.MONGOLAB_URI,opts).then(964 ()=>{965 var acTypeid = req.params.acTypesid;966 967 actypes.deleteOne({_id:acTypeid}).exec(function (err, result) {968 if(err)969 {970 res.send({status:1,message:'Somthing went wrong, Please try again!'});971 mongoose.disconnect();972 }973 else974 {975 res.send({status:0,message:'Record deleted successfully!'});976 mongoose.disconnect();977 }978 });979 });980 }981 }; 982 983 984 985 exports.SaveaccountTypes= function(req, res) {986 if(req.decoded.success == true)987 {988 mongoose.connect(process.env.MONGOLAB_URI,opts).then(989 ()=>{990 var ACTypesDetails = req.body;991 var acTypesdetails = new actypes(ACTypesDetails);992 if(ACTypesDetails._id)993 {994 actypes.update({ _id: ACTypesDetails._id }, ACTypesDetails,{ multi: true }, function(err) {995 if(err)996 {997 res.send({status:1,message:'Somthing went wrong, Please try again!'});998 mongoose.disconnect();999 }1000 else1001 {1002 res.send({status:0,message:'Record updated successfully!'});1003 mongoose.disconnect();1004 }1005 });1006 }1007 else1008 {1009 acTypesdetails.save(function(err,result) {1010 if(err)1011 {1012 res.send({status:1,message:'Somthing went wrong, Please try again!'});1013 mongoose.disconnect();1014 }1015 else1016 {1017 res.send({status:0,message:'Record inserted successfully!'});1018 mongoose.disconnect();1019 }1020 });1021 }1022 });1023 }1024 }; 1025 1026 1027 /* ACCOUNT TYPES */1028 1029 1030 /* ACCOUNT PLANS */1031 1032 exports.ListaccountPlans= function(req, res) {1033 if(req.decoded.success == true)1034 {1035 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1036 ()=>{1037 acplans.find({}).populate('actypeid','accounttype').exec(function (err, result) { 1038 if(err)1039 {1040 res.send({status:1,message:'Somthing went wrong, Please try again!'});1041 mongoose.disconnect();1042 }1043 else1044 {1045 res.send(result);1046 mongoose.disconnect();1047 }1048 }); 1049 }); 1050 1051 }1052 }; 1053 1054 1055 exports.getAccountPlansDetails= function(req, res) {1056 if(req.decoded.success == true)1057 {1058 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1059 ()=>{1060 var acPlanid = req.params.acPlanid;1061 acplans.find({_id:acPlanid}).exec(function (err, result) {1062 if(err)1063 {1064 res.send({status:1,message:'Somthing went wrong, Please try again!'});1065 mongoose.disconnect();1066 }1067 else1068 {1069 res.send(result);1070 mongoose.disconnect();1071 }1072 });1073 });1074 }1075 }; 1076 1077 exports.DeleteACPlanDetails= function(req, res) {1078 if(req.decoded.success == true)1079 {1080 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1081 ()=>{1082 var acPlanid = req.params.acPlanid;1083 acplans.deleteOne({_id:acPlanid}).exec(function (err, result) {1084 if(err)1085 {1086 res.send({status:1,message:'Somthing went wrong, Please try again!'});1087 mongoose.disconnect();1088 }1089 else1090 {1091 res.send({status:0,message:'Record deleted successfully!'});1092 mongoose.disconnect();1093 }1094 });1095 });1096 }1097 }; 1098 1099 1100 1101 exports.SaveAccountPlan= function(req, res) {1102 if(req.decoded.success == true)1103 {1104 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1105 ()=>{1106 var ACPlansDetails = req.body;1107 var acPlansdetails = new acplans(ACPlansDetails);1108 if(ACPlansDetails._id)1109 {1110 acplans.update({ _id: ACPlansDetails._id }, req.body,{ multi: true }, function(err) {1111 if(err)1112 {1113 res.send({status:1,message:'Somthing went wrong, Please try again!'});1114 mongoose.disconnect();1115 }1116 else1117 {1118 res.send({status:0,message:'Record updated successfully!'});1119 mongoose.disconnect();1120 }1121 });1122 }1123 else1124 {1125 acPlansdetails.save(function(err,result) {1126 console.log(err)1127 if(err)1128 {1129 res.send({status:1,message:'Somthing went wrong, Please try again!'});1130 mongoose.disconnect();1131 }1132 else1133 {1134 res.send({status:0,message:'Record inserted successfully!'});1135 mongoose.disconnect();1136 }1137 });1138 }1139 });1140 }1141 }; 1142 1143 /* ACCOUNT PLANS */1144 1145 1146 /* EMPLOYEE DETAILS */1147 1148 exports.ListEmployees= function(req, res) {1149 if(req.decoded.success == true)1150 {1151 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1152 ()=>{1153 1154 if(req.Loggedinuser.role == 'Superadmin')1155 var filterqry ={}1156 if(req.Loggedinuser.role == 'Admin' || req.Loggedinuser.role == 'User')1157 var filterqry ={'branch':req.Loggedinuser.branchid}1158 employees.find(filterqry,{'firstname':1,'lastname':1,'midname':1,'gander':1,'doj':1,'role':1,'branch':1,'contactdetails':1}).populate('branch','branchname').exec(function (err, result) {1159 if(err)1160 {1161 res.send({status:1,message:'Somthing went wrong, Please try again!'});1162 mongoose.disconnect();1163 }1164 else1165 {1166 res.send(result);1167 mongoose.disconnect();1168 }1169 });1170 });1171 }1172 }; 1173 1174 1175 exports.getEmployeesDetails= function(req, res) {1176 if(req.decoded.success == true)1177 {1178 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1179 ()=>{1180 var empid = req.params.empid;1181 1182 employees.find({_id:empid}).exec(function (err, result) {1183 if(err)1184 {1185 res.send({status:1,message:'Somthing went wrong, Please try again!'});1186 mongoose.disconnect();1187 }1188 else1189 {1190 res.send(result);1191 mongoose.disconnect();1192 }1193 });1194 });1195 }1196 }; 1197 1198 exports.DeleteEmployeesDetails= function(req, res) {1199 if(req.decoded.success == true)1200 {1201 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1202 ()=>{1203 var empid = req.params.empid;1204 1205 employees.deleteOne({_id:empid}).exec(function (err, result) {1206 if(err)1207 {1208 res.send({status:1,message:'Somthing went wrong, Please try again!'});1209 mongoose.disconnect();1210 }1211 else1212 {1213 res.send({status:0,message:'Record deleted successfully.'});1214 mongoose.disconnect();1215 }1216 });1217 });1218 }1219 }; 1220 1221 exports.VerifyrequiredFields= function(req, res) {1222 if(req.decoded.success == true)1223 {1224 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1225 ()=>{1226 var data = req.body;1227 var verificationfield = data.verification;1228 1229 if(data.field == 'Mobile')1230 { 1231 if(data.userid)1232 var filterqry = employees.countDocuments({'contactdetails.mobile1':verificationfield,_id: { $ne: data.userid }}) 1233 else1234 var filterqry = employees.countDocuments({'contactdetails.mobile1':verificationfield}) 1235 }1236 if(data.field == 'Email')1237 { 1238 if(data.userid)1239 var filterqry = employees.countDocuments({'contactdetails.email':verificationfield,_id: { $ne: data.userid }})1240 else1241 var filterqry = employees.countDocuments({'contactdetails.email':verificationfield})1242 }1243 if(data.field == 'Username')1244 {1245 if(data.userid)1246 var filterqry = employees.countDocuments({'username':verificationfield,_id: { $ne: data.userid }})1247 else1248 var filterqry = employees.countDocuments({'username':verificationfield})1249 }1250 filterqry.exec(function (err, result) {1251 if(err)1252 {1253 res.send({status:1,message:'Somthing went wrong, Please try again!'});1254 mongoose.disconnect();1255 }1256 else1257 {1258 if(result > 0)1259 {1260 res.send({status:1,message:data.field+' already exist in the system'});1261 mongoose.disconnect();1262 }1263 else1264 {1265 res.send({status:0,message:''});1266 mongoose.disconnect();1267 }1268 }1269 }); 1270 });1271 }1272 }; 1273 1274 1275 exports.SaveEmployeesDetails= function(req, res) {1276 if(req.decoded.success == true)1277 {1278 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1279 ()=>{1280 var employeepic = req.file;1281 var empdet = req.body;1282 empdet.employeedetails.createdby = req.Loggedinuser.id1283 if(employeepic)1284 {1285 empdet.employeedetails.documentsdetails.pic = employeepic.filename;1286 }1287 var empDetails = new employees(empdet.employeedetails);1288 if(empdet.employeedetails._id)1289 {1290 employees.updateOne({ _id: empdet.employeedetails._id }, empdet.employeedetails,{ multi: true }, function(err) {1291 if(err)1292 {1293 res.send({status:1,message:'Somthing went wrong, Please try again!'});1294 mongoose.disconnect();1295 }1296 else1297 {1298 res.send({status:0,message:'Record updated successfully!'});1299 mongoose.disconnect();1300 }1301 });1302 }1303 else1304 {1305 empDetails.save(function(err,result) {1306 if(err)1307 {1308 res.send({status:1,message:'Somthing went wrong, Please try again!'});1309 mongoose.disconnect();1310 }1311 else1312 {1313 res.send({status:0,message:'Record inserted successfully!'});1314 mongoose.disconnect();1315 }1316 });1317 } 1318 });1319 }1320 }; 1321 1322 1323 /* EMPLOYEE DETAILS */1324 /* MEMBERS DETAILS */1325 1326 1327 exports.VerifyrequiredFieldsInMembers= function(req, res) {1328 if(req.decoded.success == true)1329 {1330 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1331 ()=>{1332 var data = req.body;1333 var verificationfield = data.verification;1334 1335 if(data.field == 'Mobile')1336 { 1337 if(data.memberid)1338 var filterqry = members.countDocuments({'contactdetails.mobile1':verificationfield,_id: { $ne: data.memberid }}) 1339 else1340 var filterqry = members.countDocuments({'contactdetails.mobile1':verificationfield}) 1341 }1342 if(data.field == 'Email')1343 { 1344 if(data.memberid)1345 var filterqry = members.countDocuments({'contactdetails.email':verificationfield,_id: { $ne: data.memberid }})1346 else1347 var filterqry = members.countDocuments({'contactdetails.email':verificationfield})1348 }1349 if(data.field == 'Aadhaar card No')1350 {1351 if(data.memberid)1352 var filterqry = members.countDocuments({'documentsdetails.aadhaar':verificationfield,_id: { $ne: data.memberid }})1353 else1354 var filterqry = members.countDocuments({'documentsdetails.aadhaar':verificationfield})1355 }1356 1357 if(data.field == 'Pan No')1358 {1359 if(data.memberid)1360 var filterqry = members.countDocuments({'documentsdetails.pan':verificationfield,_id: { $ne: data.memberid }})1361 else1362 var filterqry = members.countDocuments({'documentsdetails.pan':verificationfield})1363 }1364 filterqry.exec(function (err, result) {1365 if(err)1366 {1367 res.send({status:1,message:'Somthing went wrong, Please try again!'});1368 mongoose.disconnect();1369 }1370 else1371 {1372 if(result > 0)1373 {1374 res.send({status:1,message:data.field+' already exist in the system'});1375 mongoose.disconnect();1376 }1377 else1378 {1379 res.send({status:0,message:''});1380 mongoose.disconnect();1381 }1382 }1383 }); 1384 });1385 }1386 }; 1387 1388 1389 exports.SaveMembersDetails= function(req, res) {1390 if(req.decoded.success == true)1391 {1392 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1393 ()=>{1394 var memberpic = req.file;1395 var memdet = req.body;1396 memdet.membersDetails.createdby = req.Loggedinuser.id1397 if(memberpic)1398 {1399 memdet.membersDetails.documentsdetails.pic = memberpic.filename;1400 }1401 var memberDetails = new members(memdet.membersDetails);1402 if(memdet.membersDetails._id)1403 {1404 members.updateOne({ _id: memdet.membersDetails._id }, memdet.membersDetails,{ multi: true }, function(err) {1405 if(err)1406 {1407 res.send({status:1,message:'Somthing went wrong, Please try again!'});1408 mongoose.disconnect();1409 }1410 else1411 {1412 res.send({status:0,message:'Record updated successfully!'});1413 mongoose.disconnect();1414 }1415 });1416 }1417 if(!memdet.membersDetails._id)1418 {1419 memberDetails.save(function(err,result) {1420 if(err)1421 {1422 res.send({status:1,message:'Somthing went wrong, Please try again!'});1423 mongoose.disconnect();1424 }1425 else1426 {1427 res.send({status:0,message:'Record inserted successfully!'});1428 mongoose.disconnect();1429 }1430 });1431 } 1432 });1433 }1434 }; 1435 1436 exports.ListMembers= function(req, res) {1437 if(req.decoded.success == true)1438 {1439 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1440 ()=>{1441 1442 if(req.Loggedinuser.role == 'Superadmin')1443 var filterqry ={}1444 if(req.Loggedinuser.role == 'Admin' || req.Loggedinuser.role == 'User')1445 var filterqry ={'branch':req.Loggedinuser.branchid}1446 members.find(filterqry,{'firstname':1,'lastname':1,'midname':1,'depositeamount':1,'documentsdetails.aadhaar':1,'contactdetails.mobile1':1,'isagent':1}).populate({ path: 'branch', select: 'branchname' }).exec(function (err, result) {1447 if(err)1448 {1449 res.send({status:1,message:'Somthing went wrong, Please try again!'});1450 mongoose.disconnect();1451 }1452 else1453 {1454 res.send(result);1455 mongoose.disconnect();1456 }1457 });1458 });1459 }1460 }; 1461 1462 1463 exports.ListGuarantors= function(req, res) {1464 if(req.decoded.success == true)1465 {1466 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1467 ()=>{1468 var selectedmemberdetails = req.body;1469 1470 var filterqry = {'branch.id':selectedmemberdetails.branch._id,'_id': { $ne: selectedmemberdetails._id },'depositeamount': { $eq: selectedmemberdetails.depositeamount }};1471 1472 members.find(filterqry,{'firstname':1,'lastname':1,'midname':1,'depositeamount':1,'group.groupname':1,'areaid.areaname':1,'branch.branchname':1}).exec(function (err, result) {1473 console.log(err)1474 if(err)1475 {1476 res.send({status:1,message:'Somthing went wrong, Please try again!'});1477 mongoose.disconnect();1478 }1479 else1480 {1481 res.send(result);1482 mongoose.disconnect();1483 }1484 });1485 });1486 }1487 }; 1488 1489 1490 exports.ListAnotherGuarantor= function(req, res) {1491 if(req.decoded.success == true)1492 {1493 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1494 ()=>{1495 var guadetails = req.body.guarantors;1496 var membersdetails = req.body.memberdetails;1497 1498 var selectedmembersid = [membersdetails._id];1499 1500 guadetails.map(function(value)1501 {1502 if(value.gaumembername && value.gaumembername._id)1503 selectedmembersid.push(value.gaumembername._id)1504 })1505 1506 var filterqry = {'branch.id':membersdetails.branch._id,'_id': { $nin: selectedmembersid },'depositeamount': { $eq: membersdetails.depositeamount }};1507 1508 members.find(filterqry,{'firstname':1,'lastname':1,'midname':1,'depositeamount':1,'group.groupname':1,'areaid.areaname':1,'branch.branchname':1}).exec(function (err, result) {1509 if(err)1510 {1511 res.send({status:1,message:'Somthing went wrong, Please try again!'});1512 mongoose.disconnect();1513 }1514 else1515 {1516 res.send(result);1517 mongoose.disconnect();1518 }1519 }); 1520 });1521 }1522 }; 1523 1524 1525 exports.GetMembersDetails= function(req, res) {1526 if(req.decoded.success == true)1527 {1528 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1529 ()=>{1530 var memberid = req.params.memid;1531 1532 members.find({_id:memberid}).exec(function (err, result) {1533 if(err)1534 {1535 res.send({status:1,message:'Somthing went wrong, Please try again!'});1536 mongoose.disconnect();1537 }1538 else1539 {1540 res.send(result);1541 mongoose.disconnect();1542 }1543 });1544 });1545 }1546 }; 1547 1548 1549 exports.DeleteMember= function(req, res) {1550 if(req.decoded.success == true)1551 {1552 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1553 ()=>{1554 var memberid = req.params.memid;1555 1556 members.deleteOne({_id:memberid}).exec(function (err, result) {1557 console.log(err)1558 if(err)1559 {1560 res.send({status:1,message:'Somthing went wrong, Please try again!'});1561 mongoose.disconnect();1562 }1563 else1564 {1565 res.send({status:0,message:'Record deleted successfully'});1566 mongoose.disconnect();1567 }1568 });1569 });1570 }1571 }; 1572 1573 /* MEMBERS DETAILS */1574 1575 1576 /* MEMBER`S ACCOUNTS DETAILS */1577 1578 1579 1580 exports.getMembersAccounts= function(req, res) {1581 if(req.decoded.success == true)1582 {1583 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1584 ()=>{1585 var memberid = req.params.memid;1586 1587 membersacc.find({'memberid':memberid},{'interestrate':1,'loanamount':1,'netamount':1}).populate('accounttypeid','accounttype').exec(function (err, result) {1588 if(err)1589 {1590 res.send({status:1,message:'Somthing went wrong, Please try again!'});1591 mongoose.disconnect();1592 }1593 else1594 {1595 res.send(result);1596 mongoose.disconnect();1597 }1598 });1599 });1600 }1601 }; 1602 1603 1604 exports.ListMembersAccounts= function(req, res) {1605 if(req.decoded.success == true)1606 {1607 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1608 ()=>{1609 1610 1611 if(req.Loggedinuser.role == 'Superadmin')1612 var filterqry ={}1613 if(req.Loggedinuser.role == 'Admin' || req.Loggedinuser.role == 'User')1614 var filterqry ={'branchid':req.Loggedinuser.branchid}1615 1616 membersacc.find(filterqry,{'interestrate':1,'loanamount':1,'netamount':1}).populate('accounttypeid','accounttype').populate('memberid').populate('branchid','branchname').populate('planid','name').exec(function (err, result) {1617 if(err)1618 {1619 res.send({status:1,message:'Somthing went wrong, Please try again!'});1620 mongoose.disconnect();1621 }1622 else1623 {1624 res.send(result);1625 mongoose.disconnect();1626 }1627 });1628 });1629 }1630 }; 1631 1632 1633 1634 1635 exports.SaveMembersACDetails= function(req, res) {1636 if(req.decoded.success == true)1637 {1638 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1639 ()=>{1640 var membersaccountdDetails = req.body;1641 1642 membersaccountdDetails.memberid = req.body._id;1643 membersaccountdDetails.branchid = req.body.branch[0];1644 1645 1646 if(membersaccountdDetails.gourentorsdetails)1647 {1648 membersaccountdDetails.gourentors = [];1649 1650 membersaccountdDetails.gourentorsdetails.map(function(value){1651 1652 membersaccountdDetails.gourentors.push(value.gaumembername._id)1653 1654 })1655 }1656 1657 membersaccountdDetails.createdby = req.Loggedinuser.id1658 1659 delete membersaccountdDetails._id;1660 delete membersaccountdDetails.id;1661 1662 var membersACDetails = new membersacc(membersaccountdDetails);1663 /* if(ACPlansDetails._id)1664 {1665 membersacc.update({ _id: ACPlansDetails._id }, req.body,{ multi: true }, function(err) {1666 if(err)1667 {1668 res.send({status:1,message:'Somthing went wrong, Please try again!'});1669 mongoose.disconnect();1670 }1671 else1672 {1673 res.send({status:0,message:'Record updated successfully!'});1674 mongoose.disconnect();1675 }1676 });1677 }1678 else1679 { */1680 membersACDetails.save(function(err,result) {1681 console.log(err)1682 if(err)1683 {1684 res.send({status:1,message:'Somthing went wrong, Please try again!'});1685 mongoose.disconnect();1686 }1687 else1688 {1689 res.send({status:0,message:'Record inserted successfully!'});1690 mongoose.disconnect();1691 }1692 });1693 /* } */1694 }); 1695 }1696 }; 1697 1698 1699 1700 1701 1702 /* MEMBER`S ACCOUNTS DETAILS */1703 1704 /* AGENT`S DETAILS */1705 1706 1707 1708 exports.ListAgents= function(req, res) {1709 if(req.decoded.success == true)1710 {1711 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1712 ()=>{1713 1714 1715 if(req.Loggedinuser.role == 'Superadmin')1716 var filterqry ={}1717 if(req.Loggedinuser.role == 'Admin')1718 var filterqry ={'branch':req.Loggedinuser.branchid}1719 1720 agents.find(filterqry,{'createddate':1,'activestatus':1}).populate('memberid').populate('branch','branchname').populate('areaid','areaname').populate('group','groupname').exec(function (err, result) {1721 if(err)1722 {1723 res.send({status:1,message:'Somthing went wrong, Please try again!'});1724 mongoose.disconnect();1725 }1726 else1727 {1728 res.send(result);1729 mongoose.disconnect();1730 }1731 });1732 });1733 }1734 }; 1735 1736 exports.GetAgentsDetails= function(req, res) {1737 if(req.decoded.success == true)1738 {1739 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1740 ()=>{1741 1742 1743 1744 var filterqry ={'_id':req.params.agentid}1745 1746 agents.find(filterqry).populate('memberid').exec(function (err, result) {1747 if(err)1748 {1749 res.send({status:1,message:'Somthing went wrong, Please try again!'});1750 mongoose.disconnect();1751 }1752 else1753 {1754 res.send(result);1755 mongoose.disconnect();1756 }1757 });1758 });1759 }1760 }; 1761 1762 1763 1764 1765 1766 exports.ActiveDeactiveFeatures= function(req, res) {1767 if(req.decoded.success == true)1768 {1769 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1770 ()=>{1771 if(req.body.isagent == 0)1772 var actvsts = 1;1773 if(req.body.isagent == 1)1774 var actvsts = 0;1775 members.updateOne({ _id: req.body._id },{'isagent':actvsts},{ multi: true }, function(err) {1776 if(err)1777 {1778 res.send({status:1,message:'Somthing went wrong, Please try again!'});1779 mongoose.disconnect();1780 }1781 else1782 {1783 agents.find({memberid:req.body._id}).exec(function (err, result) {1784 if(err)1785 {1786 res.send({status:1,message:'Somthing went wrong, Please try again!'});1787 mongoose.disconnect();1788 }1789 else1790 {1791 if(result != null && result.length == 1)1792 {1793 1794 agents.updateOne({ _id:result[0]._id },{'activestatus':actvsts},{ multi: true }, function(err) {1795 if(err)1796 {1797 res.send({status:1,message:'Somthing went wrong, Please try again!'});1798 mongoose.disconnect();1799 }1800 else1801 {1802 res.send({status:0,message:'Status Changed'});1803 mongoose.disconnect();1804 }1805 });1806 }1807 else1808 {1809 res.send({status:0,message:'Status Changed'});1810 mongoose.disconnect();1811 }1812 }1813 });1814 }1815 });1816 });1817 }1818 }; 1819 1820 1821 exports.SaveMemberAsAgent= function(req, res) {1822 if(req.decoded.success == true)1823 {1824 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1825 ()=>{1826 1827 if(req.body.memberid)1828 {1829 var memberid = req.body.memberid._id1830 }1831 else1832 {1833 var memberid = req.body._id1834 }1835 1836 agents.find({memberid:memberid}).exec(function (err, result) {1837 if(err)1838 {1839 res.send({status:1,message:'Somthing went wrong, Please try again!'});1840 mongoose.disconnect();1841 }1842 else1843 {1844 if(result != null && result.length == 1)1845 {1846 1847 agents.updateOne({ _id: result[0]._id },{group:req.body.group[0],areaid:req.body.areaid[0],branch:req.body.branch[0]},{ multi: true }, function(err) {1848 if(err)1849 {1850 res.send({status:1,message:'Somthing went wrong, Please try again!'});1851 mongoose.disconnect();1852 }1853 else1854 {1855 res.send({status:0,message:'Record Updated Successfully'});1856 mongoose.disconnect();1857 }1858 });1859 1860 }1861 else1862 {1863 1864 var agentdetails = new agents({group:req.body.group[0],areaid:req.body.areaid[0],branch:req.body.branch[0],memberid:req.body._id,activestatus:1});1865 1866 agentdetails.save(function(err,result) {1867 if(err)1868 {1869 res.send({status:1,message:'Somthing went wrong, Please try again!'});1870 mongoose.disconnect();1871 }1872 else1873 {1874 members.updateOne({ _id: req.body._id },{'isagent':1},{ multi: true }, function(err) {1875 if(err)1876 {1877 res.send({status:1,message:'Somthing went wrong, Please try again!'});1878 mongoose.disconnect();1879 }1880 else1881 {1882 res.send({status:0,message:'Record Inserted Successfully'});1883 mongoose.disconnect();1884 }1885 });1886 }1887 });1888 }1889 1890 }1891 });1892 });1893 }1894 }; 1895 1896 1897 /* AGENT`S DETAILS */1898 1899 1900 /* REFERANCES */1901 exports.ListBranchsforReferance= function(req, res) {1902 if(req.decoded.success == true)1903 {1904 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1905 ()=>{1906 branches.find({},{'branchname':1}).exec(function (err, result) {1907 if(err)1908 {1909 res.send({status:1,message:'Somthing went wrong, Please try again!'});1910 mongoose.disconnect();1911 }1912 else1913 {1914 res.send(result);1915 mongoose.disconnect();1916 }1917 });1918 });1919 }1920 }; 1921 1922 exports.ListAreasOnBranch= function(req, res) {1923 if(req.decoded.success == true)1924 {1925 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1926 ()=>{1927 var branchid = req.params.branchid;1928 1929 areas.find({'branchid':branchid},{'areaname':1}).exec(function (err, result) {1930 if(err)1931 {1932 res.send({status:1,message:'Somthing went wrong, Please try again!'});1933 mongoose.disconnect();1934 }1935 else1936 {1937 res.send(result);1938 mongoose.disconnect();1939 }1940 });1941 });1942 }1943 }; 1944 1945 exports.ListGroupsOnArea= function(req, res) {1946 if(req.decoded.success == true)1947 {1948 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1949 ()=>{1950 var areaid = req.params.areaid;1951 groups.find({'areaid':areaid},{'groupname':1}).exec(function (err, result) {1952 if(err)1953 {1954 res.send({status:1,message:'Somthing went wrong, Please try again!'});1955 mongoose.disconnect();1956 }1957 else1958 {1959 res.send(result);1960 mongoose.disconnect();1961 }1962 });1963 });1964 }1965 }; 1966 1967 exports.ListAcTypesRef= function(req, res) {1968 if(req.decoded.success == true)1969 {1970 mongoose.connect(process.env.MONGOLAB_URI,opts).then(1971 ()=>{1972 actypes.find({},{'accounttype':1,'installments':1,'guarantor':1,'interestrate':1,'noguarantors':1}).exec(function (err, result) { 1973 if(err)1974 {1975 res.send({status:1,message:'Somthing went wrong, Please try again!'});1976 mongoose.disconnect();1977 }1978 else1979 {1980 res.send(result);1981 mongoose.disconnect();1982 }1983 });1984 });1985 }1986 }; 1987 ...

Full Screen

Full Screen

auth.model.js

Source:auth.model.js Github

copy

Full Screen

...14 return Student.findOne({email: email});15 })16 .then(student => {17 if(student) {18 mongoose.disconnect();19 reject('email is already existed');20 }21 else {22 return bcrypt.hashSync(password, 10);23 }24 }).then(hashedPassword => {25 let student = new Student({26 name: name,27 email: email,28 password: hashedPassword29 })30 return student.save();31 }).then(student => {32 mongoose.disconnect();33 resolve(student);34 }).catch(err => {35 mongoose.disconnect();36 reject(err)37 })38 })39};40exports.studentLogin = (email, password) => {41 return new Promise((resolve, reject) => {42 mongoose43 .connect(DB_URL)44 .then(() => {45 return Student.findOne({email: email});46 })47 .then(student => {48 if(!student) {49 mongoose.disconnect();50 reject('there is no student matches this email');51 }52 else {53 bcrypt54 .compare(password, student.password)55 .then(same => {56 if(!same){57 mongoose.disconnect();58 reject('password is incorrect')59 } else {60 mongoose.disconnect();61 resolve(student);62 }63 })64 }65 }).catch(err => {66 mongoose.disconnect();67 reject(err);68 })69 })70};71exports.createNewTeacher = (name, email, password) => {72 return new Promise((resolve, reject) => {73 mongoose74 .connect(DB_URL, {useNewUrlParser: true, useUnifiedTopology: true})75 .then(() => {76 return Teacher.findOne({email: email});77 })78 .then(teacher => {79 if(teacher){80 mongoose.disconnect();81 reject('email is already existed');82 }83 else {84 return bcrypt.hash(password, 10);85 }86 }).then(hashedPassword => {87 let teacher = new Teacher({88 name: name,89 email: email,90 password: hashedPassword91 })92 return teacher.save();93 }).then(async(teacher) => {94 await fileManager.createDir('./teachers/'+teacher._id)95 .then(() => {})96 .catch(() => reject());97 return teacher;98 })99 .then(teacher => {100 mongoose.disconnect();101 resolve(teacher);102 }).catch(err => {103 mongoose.disconnect();104 reject(err)105 })106 })107};108exports.teacherLogin = (email, password) => {109 return new Promise((resolve, reject) => {110 mongoose111 .connect(DB_URL)112 .then(() => {113 return Teacher.findOne({email: email});114 })115 .then(teacher => {116 if(!teacher) {117 mongoose.disconnect();118 reject('there is no teacher matches this email');119 }120 else {121 bcrypt122 .compare(password, teacher.password)123 .then(same => {124 if(!same){125 mongoose.disconnect();126 reject('password is incorrect')127 } else {128 mongoose.disconnect();129 resolve(teacher);130 }131 })132 }133 }).catch(err => {134 mongoose.disconnect();135 reject(err);136 })137 })138};139exports.adminLogin = (adminName, password) => {140 return new Promise((resolve, reject) => {141 mongoose142 .connect(DB_URL)143 .then(() => {144 return Admin.findOne({adminName: adminName});145 })146 .then(admin => {147 if(!admin) {148 mongoose.disconnect();149 reject('you are not admin');150 }151 else {152 bcrypt153 .compare(password, admin.password)154 .then(same => {155 if(!same){156 mongoose.disconnect();157 reject('you are not admin')158 } else {159 mongoose.disconnect();160 resolve(admin);161 }162 })163 }164 }).catch(err => {165 mongoose.disconnect();166 reject(err);167 })168 })169}170exports.adminSignup = (adminName, password) => {171 return new Promise((resolve, reject) => {172 mongoose173 .connect(DB_URL)174 .then(() => {175 return bcrypt.hash(password, 10);176 })177 .then((hashedPassword) => {178 let admin = new Admin({179 adminName, password: hashedPassword180 })181 return admin.save();182 })183 .then(admin => {184 resolve(admin);185 })186 .catch(err => {187 reject(err);188 })189 })190}191exports.checkIfEamilExisted = async (character, email) => {192 await mongoose.connect(DB_URL);193 if(character === 'student'){194 let student = await Student.findOne({email: email});195 mongoose.disconnect();196 if(student) return true;197 else return false;198 }199 if(character === 'teacher'){200 let teacher = await Teacher.findOne({email: email});201 mongoose.disconnect();202 if(teacher) return true;203 else return false;204 }...

Full Screen

Full Screen

quiz.model.js

Source:quiz.model.js Github

copy

Full Screen

...36 let q = await quiz.save();37 // setTimeout(() => {38 // mongoose.connect(DB_URL, {useNewUrlParser: true});39 // Quiz.findByIdAndUpdate(q._id, {status: "Running"});40 // mongoose.disconnect();41 // }, q.quizDate - Date.now());42 // setTimeout(() => {43 // mongoose.connect(DB_URL, {useNewUrlParser: true});44 // Quiz.findByIdAndUpdate(q._id, {status: "Terminating"});45 // mongoose.disconnect();46 // }, q.quizEnd - Date.now());47 await Course.findByIdAndUpdate(q.courseId, {48 $push: {49 quizes: {id: q._id, name: q.quizName}50 }51 })52 mongoose.disconnect();53 return q;54 } catch (error) {55 mongoose.disconnect();56 throw new Error(error);57 }58}59exports.deleteQuiz = async (courseId, quizId) => {60 try {61 await mongoose.connect(DB_URL);62 await Quiz.findByIdAndDelete(quizId);63 await Course.findByIdAndUpdate(courseId, {64 $pull: {65 quizes: {id: quizId}66 }67 });68 await gradeModel.Grade.deleteMany({quizId: quizId});69 mongoose.disconnect();70 return;71 } catch (error) {72 mongoose.disconnect();73 throw new Error(error);74 }75}76exports.getQuizById = async (quizId) => {77 try {78 await mongoose.connect(DB_URL);79 let quiz = await Quiz.findById(quizId);80 mongoose.disconnect();81 let q = {82 _id: quiz._id,83 quizName: quiz.quizName,84 maxGrade: quiz.maxGrade,85 quizDate: quiz.quizDate,86 quizEnd: quiz.quizEnd87 }88 return q;89 } catch (error) {90 mongoose.disconnect();91 throw new Error(error);92 }93}94exports.getStudentQuizQuestions = async (quizId, attendantStudent) => {95 try {96 let questions;97 await mongoose.connect(DB_URL, {useNewUrlParser: true});98 let quiz = await Quiz.findById(quizId);99 if(quiz.attendants.filter(student => student.studentId == attendantStudent.id)[0]){100 mongoose.disconnect();101 throw 'you can not complete the quiz';102 } 103 else {104 await Quiz.findByIdAndUpdate(quizId, {105 $push: {106 attendants: {studentId: attendantStudent.id, studentName: attendantStudent.name}107 }108 });109 questions = quiz.questions;110 mongoose.disconnect();111 return questions;112 }113 } catch (error) {114 mongoose.disconnect();115 throw error;116 }117}118exports.getTeacherQuizQuestions = async (quizId) => {119 try {120 await mongoose.connect(DB_URL, {useNewUrlParser: true});121 let quiz = await Quiz.findById(quizId);122 mongoose.disconnect();123 return quiz.questions;124 } catch (error) {125 mongoose.disconnect();126 throw error;127 }128}129exports.finishQuiz = async (quiz, student, answers) => {130 try {131 await mongoose.connect(DB_URL, {useNewUrlParser: true});132 let q = await Quiz.findById(quiz.id);133 let questions = q.questions;134 let quizGrade = 0;135 for(let i=0; i<questions.length; i++){136 if(questions[i].trueAnswer === answers[i]){137 quizGrade += questions[i].grades;138 }139 }140 let studentGrade = {141 studentId: student.id,142 studentName: student.name, 143 quizId: quiz.id,144 quizName: quiz.name,145 courseName: quiz.courseName,146 grade: quizGrade147 };148 let g = gradeModel.Grade(studentGrade);149 await g.save();150 mongoose.disconnect();151 return g;152 } catch (error) {153 mongoose.disconnect();154 return;155 }156}157exports.addQuestion = async (quizId, status, question) => {158 try {159 await mongoose.connect(DB_URL);160 if(status === "Waiting"){161 await Quiz.findByIdAndUpdate(quizId, {162 $push: {163 questions: question164 }165 })166 }167 mongoose.disconnect();168 return;169 } catch (error) {170 mongoose.disconnect();171 throw new Error(error);172 }173}174exports.removeQuestion = async (quizId, status, question) => {175 try {176 await mongoose.connect(DB_URL);177 if(status === "Waiting") {178 await Quiz.findByIdAndUpdate(quizId, {179 $pull: {180 questions: question181 }182 })183 }184 mongoose.disconnect();185 return;186 } catch (error) {187 mongoose.disconnect();188 throw new Error(error);189 }190}191exports.runQuiz = async (quizId, status) => {192 try {193 await mongoose.connect(DB_URL);194 if(status === "Running") throw "Quiz is Running";195 if(status === "Terminated") throw "Quiz is Terminated";196 if(status === "Waiting") {197 await Quiz.findByIdAndUpdate(quizId, {status: "Running"});198 }199 mongoose.disconnect();200 return;201 } catch (error) {202 mongoose.disconnect();203 throw new Error(error);204 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1mongoose.disconnect();2mongoose.connection.close();3mongoose.connection.close();4mongoose.disconnect();5mongoose.connection.close();6mongoose.disconnect();7mongoose.disconnect();8mongoose.connection.close();9mongoose.disconnect();10mongoose.connection.close();11mongoose.connection.close();12mongoose.disconnect();13mongoose.connection.close();14mongoose.disconnect();15mongoose.disconnect();16mongoose.connection.close();17mongoose.disconnect();18mongoose.connection.close();19mongoose.connection.close();20mongoose.disconnect();21mongoose.connection.close();22mongoose.disconnect();23mongoose.disconnect();24mongoose.connection.close();25mongoose.disconnect();26mongoose.connection.close();27mongoose.connection.close();28mongoose.disconnect();

Full Screen

Using AI Code Generation

copy

Full Screen

1var mongoose = require('mongoose');2var db = mongoose.connection;3db.on('error', console.error.bind(console, 'connection error:'));4db.once('open', function() {5});6var kittySchema = mongoose.Schema({7});8kittySchema.methods.speak = function () {9: "I don't have a name";10console.log(greeting);11}12var Kitten = mongoose.model('Kitten', kittySchema);13var silence = new Kitten({ name: 'Silence' });14var fluffy = new Kitten({ name: 'fluffy' });15fluffy.save(function (err, fluffy) {16if (err) return console.error(err);17fluffy.speak();18});19Kitten.find(function (err, kittens) {20if (err) return console.error(err);21console.log(kittens);22})23Kitten.find({ name: /^fluff/ }, callback);24mongoose.disconnect();25{26"scripts": {27},28"dependencies": {29}30}31{32"dependencies": {33"mongoose": {34"requires": {

Full Screen

Using AI Code Generation

copy

Full Screen

1mongoose.disconnect(function(err) {2 if (err) {3 console.log(err);4 } else {5 console.log("Disconnected from mongodb");6 }7});

Full Screen

Using AI Code Generation

copy

Full Screen

1mongoose.disconnect();2mongoose.connection.close();3process.exit();4mongoose.disconnect();5mongoose.connection.close();6process.exit();7mongoose.disconnect();8mongoose.connection.close();9process.exit();10mongoose.disconnect();11mongoose.connection.close();12process.exit();13mongoose.disconnect();14mongoose.connection.close();15process.exit();16mongoose.disconnect();17mongoose.connection.close();18process.exit();19mongoose.disconnect();20mongoose.connection.close();21process.exit();22mongoose.disconnect();23mongoose.connection.close();24process.exit();25mongoose.disconnect();26mongoose.connection.close();27process.exit();28mongoose.disconnect();

Full Screen

Using AI Code Generation

copy

Full Screen

1const mongoose = require('mongoose');2mongoose.connect(url, function (err) {3 if (err) throw err;4 console.log('Successfully connected');5 mongoose.disconnect(function (err) {6 if (err) throw err;7 console.log('Successfully disconnected');8 });9});

Full Screen

Using AI Code Generation

copy

Full Screen

1mongoose.disconnect(function (err) {2 if (err) {3 console.log(err);4 process.exit(1);5 }6 console.log('Mongoose disconnected');7 process.exit(0);8});9mongoose.connection.close(function (err) {10 if (err) {11 console.log(err);12 process.exit(1);13 }14 console.log('Mongoose disconnected');15 process.exit(0);16});17mongoose.connection.close(function (err) {18 if (err) {19 console.log(err);20 process.exit(1);21 }22 console.log('Mongoose disconnected');23 process.exit(0);24});25mongoose.disconnect(function (err) {26 if (err) {27 console.log(err);28 process.exit(1);29 }30 console.log('Mongoose disconnected');31 process.exit(0);32});33mongoose.connection.close(function (err) {34 if (err) {35 console.log(err);36 process.exit(1);37 }38 console.log('Mongoose disconnected');39 process.exit(0);40});41mongoose.disconnect(function (err) {42 if (err) {43 console.log(err);44 process.exit(1);45 }46 console.log('Mongoose disconnected');47 process.exit(0);48});49mongoose.connection.close(function (err) {50 if (err) {51 console.log(err);52 process.exit(1);53 }54 console.log('Mongoose disconnected');55 process.exit(0);56});57mongoose.disconnect(function (err) {58 if (err) {59 console.log(err);60 process.exit(1);61 }62 console.log('Mongoose disconnected');63 process.exit(0);64});65mongoose.connection.close(function (err) {66 if (err) {67 console.log(err);68 process.exit(1);69 }70 console.log('Mongoose disconnected');71 process.exit(0);72});73mongoose.disconnect(function (err) {

Full Screen

Using AI Code Generation

copy

Full Screen

1var mongoose = require('mongoose');2mongoose.disconnect(function(err) {3 if (err) throw err;4});5var mongoose = require('mongoose');6mongoose.connection.on('connected', function () {7});8var mongoose = require('mongoose');9var Schema = mongoose.Schema;10var UserSchema = new Schema({11 username: { type: String, required: true, index: { unique: true } },12 password: { type: String, required: true },13 meta: {14 },15});16var mongoose = require('mongoose');17var Schema = mongoose.Schema;18var UserSchema = new Schema({19 username: { type: String, required: true, index: { unique: true } },20 password: { type: String, required: true },21 meta: {22 },23});24var User = mongoose.model('User', UserSchema);

Full Screen

Using AI Code Generation

copy

Full Screen

1mongoose.disconnect(function (err) {2 if (err) throw err;3 console.log('Successfully disconnected');4});5mongoose.connection.close(function (err) {6 if (err) throw err;7 console.log('Successfully disconnected');8});9mongoose.disconnect(function (err) {10 if (err) throw err;11 console.log('Successfully disconnected');12});13mongoose.connection.close(function (err) {14 if (err) throw err;15 console.log('Successfully disconnected');16});17mongoose.disconnect(function (err) {18 if (err) throw err;19 console.log('Successfully disconnected');20});21mongoose.connection.close(function (err) {22 if (err) throw err;23 console.log('Successfully disconnected');24});25mongoose.disconnect(function (err) {26 if (err) throw err;27 console.log('Successfully disconnected');28});29mongoose.connection.close(function (err) {30 if (err) throw err;31 console.log('Successfully disconnected');32});33mongoose.disconnect(function (err) {34 if (err) throw err;35 console.log('Successfully disconnected');36});

Full Screen

Using AI Code Generation

copy

Full Screen

1mongoose.disconnect();2mongoose.connection.close();3const UserSchema = new mongoose.Schema({4 name: {5 },6 age: {7 }8}, { timestamps: true });9const User = mongoose.model('User', UserSchema);10const user = new User();11user.name = "John Doe";12user.age = 35;13user.save()14 .then(user => console.log(user))15 .catch(err => console.log(err));

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ava automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful