Guest User

Route.js

a guest
Jun 26th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.21 KB | None | 0 0
  1. const express = require('express');
  2. const router = express.Router();
  3. const multer = require('multer');
  4. const fs = require('fs');
  5. const reqest = require('request');
  6. const nodemailer = require('nodemailer');
  7.  
  8. const { User } = require('../models/user');
  9. const { Reg } = require('../models/registeration');
  10. const { Company } = require('../models/company');
  11. const { Rating } = require('../models/rating');
  12. const { Category } = require('../models/category');
  13. const { Review } = require('../models/review');
  14. const { SubCat } = require('../models/subcategory');
  15. const { Portfolio } = require('../models/portfolio');
  16. const { Reference } = require('../models/reference');
  17. const { Solution } = require('../models/solution');
  18.  
  19. const download = function(uri, filename, callback){
  20. request.head(uri, function(err, res, body){
  21. console.log('content-type:', res.headers['content-type']);
  22. console.log('content-length:', res.headers['content-length']);
  23.  
  24. request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
  25. });
  26. };
  27.  
  28.  
  29.  
  30. let OTP = [];
  31. const generateOTP = () => {
  32. let text = "";
  33. let possible = "0123456789";
  34.  
  35. for (let i = 0; i < 6; i++)
  36. text += possible.charAt(Math.floor(Math.random() * possible.length));
  37.  
  38. return text;
  39. }
  40.  
  41. const sendMail = (toEmail, sub, msg) => {
  42. let transporter = nodemailer.createTransport({
  43. service: 'gmail',
  44. auth: {
  45. user: 'tecish.co@gmail.com',
  46. pass: 'PrateekMathur'
  47. }
  48. });
  49.  
  50. let mailOptions = {
  51. from: 'tecish.co@gmail.com',
  52. to: toEmail,
  53. subject: sub,
  54. text: msg`
  55. };
  56.  
  57.  
  58. transporter.sendMail(mailOptions, function(error, info){
  59. if (error) {
  60. return false;
  61. } else {
  62. return true;
  63. }
  64. });
  65.  
  66. return true;
  67. }
  68.  
  69.  
  70. const { GA } = require('../models/ga');
  71. const { BASE_URL } = require('../Config');
  72.  
  73. // Multer Configuration
  74. const StorageImage = multer.diskStorage({
  75. // destination
  76. destination: (req, file, cb) => {
  77. cb(null, './public/ProfileImages');
  78. },
  79. filename: (req, file, cb) => {
  80. cb(null, Date.now()+'-'+file.originalname);
  81. }
  82. });
  83.  
  84. const StorageLogo = multer.diskStorage({
  85. // destination
  86. destination: (req, file, cb) => {
  87. cb(null, './public/CompanyLogo');
  88. },
  89. filename: (req, file, cb) => {
  90. cb(null, Date.now()+'-'+file.originalname);
  91. }
  92. });
  93.  
  94. // Multer Configuration
  95. const StoragePortfolio = multer.diskStorage({
  96. // destination
  97. destination: (req, file, cb) => {
  98. cb(null, './public/PortfolioImages');
  99. },
  100. filename: (req, file, cb) => {
  101. cb(null, Date.now()+'-'+file.originalname);
  102. }
  103. });
  104.  
  105. const StorageSoluLogo = multer.diskStorage({
  106. // destination
  107. destination: (req, file, cb) => {
  108. cb(null, './public/SolutionLogo');
  109. },
  110. filename: (req, file, cb) => {
  111. cb(null, Date.now()+'-'+file.originalname);
  112. }
  113. });
  114.  
  115. const uploadProfileImage = multer({ storage: StorageImage });
  116. const uploadCompanyLogo = multer({ storage: StorageLogo });
  117. const uploadPortfolioImage = multer({storage: StoragePortfolio});
  118. const uploadSolutionLogo = multer({ storage: StorageSoluLogo });
  119.  
  120. router.get('/',(req,res) => {
  121. res.end('Welcome Admin');
  122. });
  123.  
  124. // User Get and Add.
  125. router.get('/user',(req,res) => {
  126. User.find({}, (err, data) => {
  127. if(err) {
  128. console.log(err);
  129. res.send("failure");
  130. } else {
  131. res.send(data);
  132. }
  133. })
  134. });
  135.  
  136. router.get('/user/:userId',(req,res) => {
  137. User.find({_id: req.params.userId}).populate('regId').exec((err,data) => {
  138. if(err) {
  139. res.send(err);
  140. return false;
  141. } else {
  142. res.send(data[0]);
  143. }
  144. });
  145. });
  146.  
  147. router.post("/adduser",(req, res) => {
  148. download(req.body.profilePicUrl,`/public/ProfileImages/${req.body.regId}.jpg`,() => {
  149. console.log('Done');
  150. });
  151. let user = new User({
  152. name: req.body.name,
  153. regId: req.body.regId,
  154. title: req.body.title,
  155. location: req.body.location,
  156. industry: req.body.industry,
  157. linkedinProfile: req.body.linkedinProfile,
  158. twitterProfile: req.body.twitterProfile,
  159. bio: req.body.bio,
  160. mobNo: req.body.mobNo
  161. });
  162.  
  163. user.save().then((data) => {
  164. // res.send(data);
  165. res.redirect(301,`${BASE_URL}/index.php`);
  166. }, (e) => {
  167. return res.status(400).send(`Bad request ${e}`);
  168. });
  169. });
  170.  
  171.  
  172.  
  173. router.post("/updateuser", (req, res) => {
  174. let update = {
  175. name: req.body.name,
  176. mobNo: req.body.mobNo,
  177. title: req.body.title,
  178. location: req.body.location,
  179. industry: req.body.industry,
  180. linkedinProfile: req.body.linkedinProfile,
  181. twitterProfile: req.body.twitterProfile,
  182. bio: req.body.bio,
  183. };
  184.  
  185. User.findOneAndUpdate({_id: req.body.userId}, {$set:update}, {new: true},(err, data) => {
  186. if(err) {
  187. res.res.redirect(301,`${BASE_URL}/edit-profile.php?profile=0`);
  188. } else {
  189. res.redirect(301,`${BASE_URL}/edit-profile.php?profile=1`);
  190. }
  191. });
  192. });
  193.  
  194. // Registeration.
  195. router.get('/reg',(req,res) => {
  196. Reg.find({}, (err,data)=> {
  197. if(err) res.send(err);
  198. else res.send(data);
  199. });
  200. });
  201.  
  202. router.get('/reg/:regId',(req,res) => {
  203. Reg.find({_id: req.params.regId}, (err,data) => {
  204. if(err) {
  205. res.send(err);
  206. return false;
  207. } else {
  208. res.send(data[0]);
  209. }
  210. });
  211. });
  212.  
  213. router.post("/registeration", (req, res) => {
  214. let reg = new Reg({
  215. email: req.body.email.toLowerCase(),
  216. pass: req.body.pass
  217. });
  218.  
  219. reg.save().then((doc) => {
  220. res.send(doc);
  221. }, (e) => {
  222. return res.status(400).send(`Bad request ${e}`);
  223. });
  224. });
  225.  
  226. router.post('/updateinfo', (req,res) => {
  227. Reg.find({_id: req.body.regId}, (err,data) => {
  228. if(err) {
  229. res.send(err);
  230. return false;
  231. } else {
  232. const PASS = data[0].pass;
  233. if(req.body.currPass != PASS) {
  234. res.redirect(301,`${BASE_URL}/edit-profile.php?password=0`);
  235. return false;
  236. }
  237. let update = {
  238. email: req.body.email.toLowerCase(),
  239. pass: req.body.pass
  240. }
  241. Reg.findOneAndUpdate({_id: req.body.regId}, {$set:update}, {new: true},(err, data) => {
  242. if(err) res.send(err);
  243. else res.redirect(301,`${BASE_URL}/edit-profile.php?password=1`);
  244. });
  245. }
  246. });
  247. })
  248.  
  249. router.post('/login', (req,res) => {
  250. Reg.find({$and: [{email: req.body.email},{pass: req.body.pass}]}, (err,data) => {
  251. if(err) {
  252. res.send(err);
  253. } else {
  254. if(data.length === 0) {
  255. // Failed Login
  256. res.json({
  257. login: false
  258. });
  259. } else {
  260. const id = data[0]._id;
  261. User.find({regId: id}).populate('regId').exec((error, doc) => {
  262. if(error) res.send(error);
  263. else {
  264. if(doc.length === 0) {
  265. // First Time Login
  266. res.json({
  267. login: true,
  268. firstTimeLogin: true,
  269. regId: id,
  270. });
  271. } else {
  272. // Editing User Data
  273. res.json({
  274. login: true,
  275. firstTimeLogin: false,
  276. userId: doc[0]._id,
  277. name: doc[0].name,
  278. regId: id
  279. });
  280. }
  281. }
  282. });
  283. }
  284. }
  285. });
  286. });
  287.  
  288.  
  289. // Company Get and Add.
  290. router.get('/company',(req,res) => {
  291. Company.find({}).populate('userId').exec((err,data) => {
  292. if(err) res.send(err);
  293. else res.send(data);
  294. });
  295. });
  296.  
  297. router.get('/company/plain',(req,res) => {
  298. Company.find({}, (err,data) => {
  299. if(err) res.send(err);
  300. else res.send(data);
  301. });
  302. });
  303.  
  304.  
  305. router.get('/company/:companyId',(req,res) => {
  306. Company.find({_id: req.params.companyId}).populate('userId').exec((err,data) => {
  307. if(err) res.send(err);
  308. else res.send(data[0]);
  309. });
  310. });
  311.  
  312. router.post("/addcompany", uploadCompanyLogo.single('logo') ,(req, res) => {
  313. // console.log(req.body.userId);
  314. let company = new Company({
  315. userId: req.body.userId,
  316. name: req.body.name,
  317. ownerName: req.body.ownerName,
  318. logo: req.file.filename,
  319. tagline: req.body.tagline,
  320. founded: req.body.founded,
  321. noOfEmp: req.body.noOfEmp,
  322. minProjectPrice: req.body.minProjectPrice,
  323. avgPricePerHour: req.body.avgPricePerHour,
  324. websiteLink: req.body.websiteLink,
  325. emailTechSupport: req.body.emailTechSupport,
  326. emailAdmin: req.body.emailAdmin,
  327. twitterProfile: req.body.twitterProfile,
  328. facebookProfile: req.body.facebookProfile,
  329. summary: req.body.summary,
  330. client: req.body.client,
  331. country: req.body.country || "",
  332. street: req.body.street,
  333. city: req.body.city || "",
  334. state: req.body.state || "",
  335. postalCode: req.body.postalCode,
  336. mobNo: req.body.mobNo,
  337. cert: req.body.cert,
  338. accolades: req.body.accolades,
  339. detailedDes: req.body.detailedDes,
  340. services: req.body.services,
  341. tech: req.body.tech,
  342. approved: false
  343. });
  344.  
  345. company.save().then((data) => {
  346. res.redirect(301,`${BASE_URL}/add-company.php?success=1`);
  347. }, (e) => {
  348. return res.status(400).send(`Bad request ${e}`);
  349. });
  350. });
  351.  
  352.  
  353. // Rating Get and Add.
  354. router.get('/rating',(req,res) => {
  355. Rating.find({}, (err,data) => {
  356. if(err) res.send(err);
  357. else res.json(data);
  358. });
  359. });
  360.  
  361. router.get('/rating/:catId',(req,res) => {
  362. Rating.find({}).populate('company_id').exec((err,data) => {
  363. if(err) res.send(err);
  364. else {
  365. let result = [];
  366. for(let i=0; i < data.length; i++) {
  367. if(req.params.catId in JSON.parse(data[i].ratingList)) {
  368. result.push(data[i]);
  369. }
  370. }
  371. res.send(result);
  372. }
  373. });
  374. });
  375.  
  376.  
  377. router.post("/addrating", (req, res) => {
  378. const company_id = req.body.company_id || null;
  379. const solution_id = req.body.solution_id || null;
  380. const type = req.body.type;
  381. const userId = req.body.userId;
  382. if('company_id' in req.body)
  383. delete req.body.company_id;
  384. if('solution_id' in req.body)
  385. delete req.body.solution_id;
  386. delete req.body.type;
  387. delete req.body.userId;
  388. let rating = new Rating({
  389. company_id: company_id,
  390. solution_id: solution_id,
  391. type: type,
  392. userId: userId,
  393. ratingList: JSON.stringify(req.body)
  394. });
  395.  
  396.  
  397. rating.save().then((doc) => {
  398. if(type == "Company".toLowerCase())
  399. res.redirect(301,`${BASE_URL}/company-step-3.php?companyId=${company_id}`);
  400. else
  401. res.redirect(301,`${BASE_URL}/solution-profile-3.php?solutionId=${solution_id}`);
  402. }, (e) => {
  403. return res.status(400).send(`Bad request ${e}`);
  404. });
  405. });
  406.  
  407.  
  408. // Category Get and Add.
  409. router.get('/category', (req,res) => {
  410. Category.find({}, (err,data) => {
  411. if(err) res.send(err);
  412. else res.send(data);
  413. });
  414.  
  415. });
  416.  
  417. router.get('/category/:categoryId',(req,res) => {
  418. res.end(req.params.categoryId);
  419. });
  420.  
  421. router.post("/addcategory", (req, res) => {
  422. console.log(req.body);
  423. let category = new Category({
  424. name: req.body.name
  425. });
  426.  
  427. category.save().then((doc) => {
  428. res.send(doc);
  429. }, (e) => {
  430. return res.status(400).send(`Bad request ${e}`);
  431. });
  432. });
  433.  
  434.  
  435. // Review Get and Add.
  436. router.get('/review', (req,res) => {
  437. Review.find({}, (err,data) => {
  438. if(err) throw err;
  439. res.json(data);
  440. });
  441. });
  442.  
  443. router.get('/reviewCompany/:companyId', (req,res) => {
  444. Review.find({companyId: req.params.companyId}, (err,data) => {
  445. if(err) res.send(err);
  446. let items = 0;
  447. let total = 0;
  448. data = data.map(item => {
  449. items++;
  450. total = item.rating.overallRating.rating;
  451. return item;
  452. });
  453. let obj = {reviewsArray:data, avgRating: total/items}
  454. res.send(obj);
  455. });
  456. });
  457.  
  458. router.get('/reviewCompanyfilter', (req,res) => {
  459. let mySort = {};
  460. if(id === 1)
  461. mySort = {updatedAt: -1}
  462. if(id === 2)
  463. mySort = {rating: {overallRating: {rating: 1}}}
  464. if(id === 3)
  465. mySort = {rating: {overallRating: {rating: -1}}}
  466. Review.find({companyId: req.query.companyId},[],{sort: mySort},(err,data) => {
  467. if(err) res.send(err);
  468. let items = 0;
  469. let total = 0;
  470. data = data.map(item => {
  471. items++;
  472. total = item.rating.overallRating.rating;
  473. return item;
  474. });
  475. let obj = {reviewsArray:data, avgRating: total/items}
  476. res.send(obj);
  477. });
  478.  
  479. });
  480.  
  481.  
  482. router.get('/reviewfilter', (req,res) => {
  483. let mySort = {};
  484. if(id === 1)
  485. mySort = {updatedAt: -1}
  486. if(id === 2)
  487. mySort = {rating: {overallRating: {rating: 1}}}
  488. if(id === 3)
  489. mySort = {rating: {overallRating: {rating: -1}}}
  490. Review.find({companyId: req.query.companyId},[],{sort: mySort},(err,data) => {
  491. if(err) res.send(err);
  492. let items = 0;
  493. let total = 0;
  494. data = data.filter(item => {
  495. items++;
  496. total = item.rating.overallRating.rating;
  497. return parseInt(item.cost) > parseInt(req.query.cost);
  498. });
  499. let obj = {reviewsArray:data, avgRating: total/items}
  500. res.send(obj);
  501. });
  502.  
  503. });
  504.  
  505.  
  506.  
  507. router.get('/review/:userId', (req,res) => {
  508. // res.end(req.params.reviewId);
  509. Review.find({userId: req.params.userId}).populate('companyId').exec((err,data) => {
  510. if(err) res.send(err);
  511. if(data.length === 0) res.json({success:false});
  512. else res.json({...{success: true}, ...data[0]._doc});
  513. });
  514. });
  515.  
  516.  
  517. router.post("/addreview", (req, res) => {
  518. let review = new Review({
  519. companyId: req.body.companyId,
  520. typeOfProject: req.body.typeOfProject,
  521. projectTitle: req.body.projectTitle,
  522. industry: req.body.industry,
  523. cost: req.body.cost,
  524. startDate: req.body.startDate,
  525. endDate: req.body.endDate,
  526. userId: req.body.userId,
  527. background: req.body.background,
  528. challenge: {
  529. service: req.body.cService,
  530. goal: req.body.cGoal
  531. },
  532. solution: {
  533. vendor: req.body.vendor,
  534. projectDetail: req.body.projectDetail,
  535. teamComposition: req.body.teamComposition
  536. },
  537. result: {
  538. outcome: req.body.outcome,
  539. effective: req.body.effective,
  540. keyFeature: req.body.keyFeature,
  541. improvements: req.body.improvements
  542. },
  543. rating: {
  544. quality: {
  545. rating: req.body.qRating,
  546. detail: req.body.qDetail
  547. },
  548. schedule: {
  549. rating: req.body.sRating,
  550. detail: req.body.sDetail
  551. },
  552. cost: {
  553. rating: req.body.cRating,
  554. detail: req.body.cDetail
  555. },
  556. refer: {
  557. rating: req.body.rRating,
  558. detail: req.body.rDetail
  559. },
  560. overallRating: {
  561. rating: req.body.oRating,
  562. detail: req.body.oDetail
  563. }
  564. },
  565. reviewer: {
  566. fullName: req.body.fullName,
  567. position: req.body.position,
  568. companyName: req.body.companyName,
  569. companySize: req.body.companySize,
  570. country: req.body.country,
  571. email: req.body.email,
  572. mobNo: req.body.mobNo
  573. },
  574. });
  575.  
  576. review.save().then((doc) => {
  577. res.redirect(301,`${BASE_URL}/review.php?success=1`);
  578. }, (e) => {
  579. return res.status(400).send(`Bad request ${e}`);
  580. });
  581. });
  582.  
  583.  
  584. // Subcategory.
  585. router.get('/subcategory', (req,res) => {
  586. SubCat.find({}).populate('cat_id').exec((err,data) => {
  587. if(err) res.send(err);
  588. else res.json(data);
  589. });
  590. });
  591.  
  592. router.post('/addsubcat', (req,res) => {
  593. let subcat = new SubCat({
  594. cat_id: req.body.cat_id,
  595. name: req.body.name
  596. });
  597.  
  598. subcat.save().then((doc) => {
  599. res.json(doc);
  600. }, (e) => {
  601. return res.status(400).send(`Bad request ${e}`);
  602. });
  603. });
  604.  
  605.  
  606. router.get('/portfolio', (req,res) => {
  607. Portfolio.find({}, (err,data) => {
  608. if(err) res.send(err);
  609. else res.send(data);
  610. });
  611. });
  612.  
  613. router.post("/addportfolio", uploadPortfolioImage.single('img') ,(req, res) => {
  614. let portfolio = new Portfolio({
  615. userId: req.body.userId,
  616. title: req.body.title,
  617. companyId: req.body.companyId,
  618. description: req.body.description,
  619. imageName : req.file.filename
  620. });
  621.  
  622. portfolio.save().then((doc) => {
  623. res.redirect(301,`${BASE_URL}/add-portfolio.php?success=1`);
  624. }, (e) => {
  625. return res.status(400).send(`Bad request ${e}`);
  626. });
  627. });
  628.  
  629. router.get('/portfolio/:companyId',(req,res) => {
  630. Portfolio.find({companyId: req.params.companyId}, (err,data) => {
  631. if(err) res.send(err);
  632. else res.send(data);
  633. });
  634. });
  635.  
  636.  
  637. // Reference
  638. router.get('/reference', (req,res) => {
  639. Reference.find({}, (err,data) => {
  640. if(err) res.send(err);
  641. else res.json(data);
  642. });
  643. });
  644.  
  645. router.get('/myReferences/:userId',(req,res) => {
  646. Reference.find({userId: req.params.userId}, (err,data) => {
  647. if(err) res.send(err);
  648. else res.send(data);
  649. });
  650. });
  651.  
  652.  
  653. router.post("/addreference",(req, res) => {
  654. let reference = new Reference({
  655. userId: req.body.userId,
  656. companyId: req.body.companyId,
  657. fName: req.body.fName,
  658. lName: req.body.lName,
  659. company: req.body.company,
  660. positionAtCompany: req.body.positionAtCompany,
  661. mobNo: req.body.mobNo,
  662. email: req.body.email,
  663. city: req.body.city,
  664. country: req.body.country,
  665. projectCost: req.body.projectCost,
  666. projectLength: req.body.projectLength,
  667. projectDescription: req.body.projectDescription
  668. });
  669.  
  670. reference.save().then((doc) => {
  671. // res.send(doc);
  672. res.redirect(301,`${BASE_URL}/add-reference.php?success=1`);
  673. }, (e) => {
  674. return res.status(400).send(`Bad request ${e}`);
  675. });
  676. });
  677.  
  678.  
  679. router.get('/solution', (req,res) => {
  680. Solution.find({}, (err,data) => {
  681. if(err) throw err;
  682. res.json(data);
  683. })
  684. });
  685.  
  686. router.post("/addsolution", uploadSolutionLogo.single('logo') ,(req, res) => {
  687. let solu = new Solution({
  688. userId: req.body.userId,
  689. name: req.body.name,
  690. softwareCat: req.body.softwareCat,
  691. logo: req.file.filename,
  692. tagline: req.body.tagline,
  693. des: req.body.des,
  694. softwareSummary: req.body.softwareSummary,
  695. noOfEmp: req.body.noOfEmp,
  696. introText: req.body.introText,
  697. optionToStart: req.body.optionToStart,
  698. websiteLink: req.body.websiteLink,
  699. client: req.body.client,
  700. priceRange: req.body.priceRange,
  701. pricingOptions: req.body.pricingOptions,
  702. summary: req.body.summary,
  703. pricingPage: req.body.pricingPage,
  704. services: req.body.services
  705. });
  706.  
  707. solu.save().then((data) => {
  708. res.redirect(301,`${BASE_URL}/add-solution.php?solutionId=${data._id}&success=1`);
  709. }, (e) => {
  710. return res.status(400).send(`Bad request ${e}`);
  711. });
  712. });
  713.  
  714.  
  715. // Google Analytics Get and Add.
  716. router.get('/ga', (req,res) => {
  717. GA.find({}, (err,data) => {
  718. if(err) res.send(err);
  719. else res.send(data);
  720. });
  721.  
  722. });
  723.  
  724. router.post("/addga", (req, res) => {
  725. let ga = new GA({
  726. companyId: req.body.company_id,
  727. details: req.body.details
  728. });
  729.  
  730. ga.save().then((data) => {
  731. res.send(data);
  732. }, (e) => {
  733. return res.status(400).send(`Bad request ${e}`);
  734. });
  735. });
  736.  
  737. router.get('/search', (req,res) => {
  738. // Search Company and
  739. Company.find({
  740. $or: [
  741. {name: { "$regex": req.query.name, "$options": "i" }},
  742. {services: {"$regex": req.query.name, "$options": "i" }},
  743. {tech: {"$regex": req.query.name, "$options": "i" }},
  744. ]
  745. }, (err,data) => {
  746. if(err) res.send(err);
  747. else res.json(data);
  748. });
  749. });
  750.  
  751. router.get('/mycompany/:userId', (req, res) => {
  752. Company.find({userId: req.params.userId}, (err, data) => {
  753. if(err) res.send(err);
  754. else res.send(data);
  755. });
  756. });
  757.  
  758. router.post('/forget', (req, res) => {
  759. const givenEmail = req.body.email.toLowerCase();
  760. Reg.find({email: givenEmail}, (err, data) => {
  761. if( err ) {
  762. res.send(err);
  763. return false;
  764. }
  765. if(data.length !== 1) {
  766. res.send("No such user exists");
  767. return;
  768. }
  769. let regId = data[0]._id;
  770. let otp = generateOTP();
  771. let transporter = nodemailer.createTransport({
  772. service: 'gmail',
  773. auth: {
  774. user: 'tecish.co@gmail.com',
  775. pass: 'PrateekMathur'
  776. }
  777. });
  778.  
  779. let mailOptions = {
  780. from: 'tecish.co@gmail.com',
  781. to: givenEmail,
  782. subject: 'OTP for Tecish',
  783. text: `Your OTP for password reset request is: ${otp}`
  784. };
  785.  
  786.  
  787. transporter.sendMail(mailOptions, function(error, info){
  788. if (error) {
  789. console.log(error);
  790. } else {
  791. console.log('Email sent: ' + info.response);
  792. OTP.push(
  793. {
  794. regId: regId,
  795. email: req.body.email,
  796. otp: otp
  797. }
  798. );
  799. console.log("inside",OTP);
  800. res.send("SENT");
  801. }
  802. });
  803. });
  804. });
  805.  
  806.  
  807. router.post('/verifyotp', (req, res) => {
  808. const result = OTP.filter((instance) => {
  809. return instance.otp === req.body.otp;
  810. });
  811. if (result.length === 1) {
  812. res.json({"success": true,"regId" : result[0].regId});
  813. } else {
  814. res.json({"success": false});
  815. }
  816. });
  817.  
  818. router.post('/changepassword', (req, res) => {
  819. Reg.findOneAndUpdate({_id: req.body.regId}, {$set:{pass:req.body.pass}}, {new: true}, function(err, data){
  820. if(err){
  821. res.send(err);
  822. return;
  823. }
  824. res.send(data);
  825. });
  826. });
  827.  
  828. router.get('/company/service/:service', (req, res) => {
  829. const result = [];
  830. Company.find({}, (err, data) => {
  831. if(err)
  832. res.send(err);
  833. else {
  834. data.map(row => {
  835. let servicesArray = row.services.split(',');
  836. if(servicesArray.includes(req.params.service)) {
  837. console.log("kuch bhi");
  838. result.push(row);
  839. }
  840. });
  841. res.send(result);
  842. }
  843. })
  844. });
  845.  
  846. router.get('/companylimited', (req, res) => {
  847. Company.find({},'name',(err, data) => {
  848. if(err) res.send(err);
  849. else res.send(data);
  850. });
  851. });
  852.  
  853.  
  854. router.post('/filter', (req, res) => {
  855. Company.find({
  856. $or: [
  857. {name: { "$regex": req.body.name, "$options": "i" }},
  858. {services: {"$regex": req.body.name, "$options": "i" }},
  859. {tech: {"$regex": req.body.name, "$options": "i" }},
  860. ]
  861. }, (err,data) => {
  862. if(err) res.send(err);
  863. else {
  864. data = data.filter(company => {
  865. const minPrice = parseInt(company.minProjectPrice);
  866. const emp = parseInt(company.noOfEmp);
  867. const hourPrice = parseInt(company.avgPricePerHour);
  868. let flag = true;
  869. const serviceArray = company.services.split(',');
  870. if(req.body.industryFocus.length !== 1) {
  871. flag = false;
  872. serviceArray.map(item => {
  873. if(req.body.industryFocus.includes(item)) {
  874. flag = true;
  875. }
  876. });
  877. }
  878. return flag && minPrice >= parseInt(req.body.minPrice) && hourPrice >= parseInt(req.body.hourPrice) && emp >= parseInt(req.body.emp);
  879. });
  880. res.send(data);
  881. }
  882. });
  883. });
  884.  
  885. router.get('/leadermatrix',(req, res) => {
  886. Company.find({},[],{skip: 0, last:10}).populate('userId').exec((err,data) => {
  887. if(err) res.send(err);
  888. else res.send(data);
  889. });
  890. });
  891.  
  892.  
  893. module.exports = router;
Add Comment
Please, Sign In to add comment