Guest User

Untitled

a guest
Jun 29th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.01 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 request = 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 { Service } = require('../models/service');
  14. const { Tech } = require('../models/tech');
  15. const { Blog } = require('../models/blog');
  16. const { Review } = require('../models/review');
  17. const { Contact } = require('../models/contact');
  18. const { SubCat } = require('../models/subcategory');
  19. const { Portfolio } = require('../models/portfolio');
  20. const { Reference } = require('../models/reference');
  21. const { Solution } = require('../models/solution');
  22. const { GA } = require('../models/ga');
  23. const { BASE_URL } = require('../Config');
  24.  
  25. var download = function(uri, filename, callback){
  26. request.head(uri, function(err, res, body){
  27. console.log('content-type:', res.headers['content-type']);
  28. console.log('content-length:', res.headers['content-length']);
  29.  
  30. request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
  31. });
  32. };
  33.  
  34.  
  35.  
  36. let OTP = [];
  37. const generateOTP = () => {
  38. let text = "";
  39. let possible = "0123456789";
  40.  
  41. for (let i = 0; i < 6; i++)
  42. text += possible.charAt(Math.floor(Math.random() * possible.length));
  43.  
  44. return text;
  45. }
  46.  
  47. const sendMail = (toEmail, sub, msg) => {
  48. let transporter = nodemailer.createTransport({
  49. service: 'gmail',
  50. auth: {
  51. user: 'tecish.co@gmail.com',
  52. pass: 'PrateekMathur'
  53. }
  54. });
  55.  
  56. let mailOptions = {
  57. from: 'tecish.co@gmail.com',
  58. to: toEmail,
  59. subject: sub,
  60. text: msg
  61. };
  62.  
  63.  
  64. transporter.sendMail(mailOptions, function(error, info){
  65. if (error) {
  66. return false;
  67. } else {
  68. return true;
  69. }
  70. });
  71.  
  72. return true;
  73. }
  74.  
  75.  
  76. const StorageProfileImages = multer.diskStorage({
  77. // destination
  78. destination: (req, file, cb) => {
  79. cb(null, './public/ProfileImages');
  80. },
  81. filename: (req, file, cb) => {
  82. cb(null, Date.now()+'-'+file.originalname);
  83. }
  84. });
  85.  
  86. // Multer Configuration
  87. const StorageBlogImage = multer.diskStorage({
  88. // destination
  89. destination: (req, file, cb) => {
  90. cb(null, './public/BlogImages');
  91. },
  92. filename: (req, file, cb) => {
  93. cb(null, Date.now()+'-'+file.originalname);
  94. }
  95. });
  96.  
  97. const StorageLogo = multer.diskStorage({
  98. // destination
  99. destination: (req, file, cb) => {
  100. cb(null, './public/CompanyLogo');
  101. },
  102. filename: (req, file, cb) => {
  103. cb(null, Date.now()+'-'+file.originalname);
  104. }
  105. });
  106.  
  107. // Multer Configuration
  108. const StoragePortfolio = multer.diskStorage({
  109. // destination
  110. destination: (req, file, cb) => {
  111. cb(null, './public/PortfolioImages');
  112. },
  113. filename: (req, file, cb) => {
  114. cb(null, Date.now()+'-'+file.originalname);
  115. }
  116. });
  117.  
  118. const StorageSoluLogo = multer.diskStorage({
  119. // destination
  120. destination: (req, file, cb) => {
  121. cb(null, './public/SolutionLogo');
  122. },
  123. filename: (req, file, cb) => {
  124. cb(null, Date.now()+'-'+file.originalname);
  125. }
  126. });
  127.  
  128. const uploadProfileImages = multer({storage: StorageProfileImages});
  129. const uploadBlogImage = multer({ storage: StorageBlogImage });
  130. const uploadCompanyLogo = multer({ storage: StorageLogo });
  131. const uploadPortfolioImage = multer({storage: StoragePortfolio});
  132. const uploadSolutionLogo = multer({ storage: StorageSoluLogo });
  133.  
  134. router.get('/',(req,res) => {
  135. res.end('Welcome Admin');
  136. });
  137.  
  138. // User Get and Add.
  139. router.get('/user',(req,res) => {
  140. User.find({}, (err, data) => {
  141. if(err) {
  142. res.send({"status":"error"});
  143. console.log(err);
  144. } else {
  145. res.send({data, ...{status: "ok"}});
  146. }
  147. })
  148. });
  149.  
  150. router.get('/user/:userId',(req,res) => {
  151. User.find({_id: req.params.userId},(err,data) => {
  152. if(err) {
  153. res.send({"status":"error"});
  154. return false;
  155. } else {
  156. res.send({data, ...{status: "ok"}})
  157. }
  158. });
  159. });
  160.  
  161. router.get('/reg',(req,res) => {
  162. Reg.find({}).populate('userId').exec((err, data) => {
  163. if(err) {
  164. res.send({"status":"error"});
  165. console.log(err);
  166. } else {
  167. res.send({data, ...{status: "ok"}});
  168. }
  169. });
  170. });
  171.  
  172.  
  173. router.get('/user/email/:email',(req,res) => {
  174. User.find({_id: req.params.email},(err,data) => {
  175. if(err) {
  176. res.send({"status":"error"});
  177. return false;
  178. } else {
  179. res.send({data, ...{status: "ok"}})
  180. }
  181. });
  182. });
  183.  
  184. router.post("/user/add/linkedin",(req, res) => {
  185. const email = req.body.email;
  186. download(req.body.profilePicUrl, `./public/ProfileImages/${req.body.mobNo}.jpg`, function(){
  187. console.log('done');
  188. let user = new User({
  189. name: req.body.name,
  190. title: req.body.title,
  191. location: req.body.location,
  192. industry: req.body.industry,
  193. linkedinProfile: req.body.linkedinProfile,
  194. twitterProfile: req.body.twitterProfile,
  195. bio: req.body.bio,
  196. mobNo: req.body.mobNo,
  197. imgUrl: req.body.mobNo+'.jpg'
  198. });
  199.  
  200. user.save().then((data) => {
  201. let response = data;
  202. let reg = new Reg({
  203. userId: data._id,
  204. email: req.body.email.toLowerCase(),
  205. pass: req.body.pass
  206. });
  207.  
  208. reg.save().then((data) => {
  209. res.send({response, ...{status: "ok"}});
  210. sendMail(email,"Thanks for creating an account",`Your account has been created, your password is ${req.body.pass}`);
  211. }, (e) => {
  212. console.log('Error in email');
  213. User.findOneAndRemove({_id: response._id}, (err, data) => {
  214. console.log('inside rollback feature');
  215. if(err) {
  216. console.log({status: "error while performing rollback"});
  217. } else {
  218. console.log('Deleted successfully');
  219. }
  220. });
  221. if(e.name === "BulkWriteError")
  222. res.send({"status":"error","msg":"Email alredy taken"});
  223. else
  224. res.send({"status":"error", msg: "Something went Wrong"});
  225. return false;
  226. });
  227. }, (e) => {
  228. console.log(e);
  229. if(e.name === "BulkWriteError")
  230. res.send({"status":"error","msg":"mobile number alredy taken"});
  231. else
  232. res.send({"status":"error", msg: "SOmething went wrong"});
  233. return false;
  234. });
  235. });
  236. });
  237.  
  238. // Data should be multipart/formdata encoded
  239. router.post("/user/add/form", uploadProfileImages.single('imgUrl'), (req, res) => {
  240. const email = req.body.email;
  241. let user = new User({
  242. name: req.body.name,
  243. title: req.body.title,
  244. location: req.body.location,
  245. industry: req.body.industry,
  246. linkedinProfile: req.body.linkedinProfile,
  247. twitterProfile: req.body.twitterProfile,
  248. bio: req.body.bio,
  249. mobNo: req.body.mobNo,
  250. imgUrl: req.file.name
  251. });
  252.  
  253. user.save().then((data) => {
  254. let response = data;
  255. let reg = new Reg({
  256. userId: data._id,
  257. email: req.body.email.toLowerCase(),
  258. pass: req.body.pass
  259. });
  260.  
  261. reg.save().then((data) => {
  262. res.send({response, ...{status: "ok"}});
  263. sendMail(email,"Thanks for creating an account",`Your account has been created, your password is ${req.body.pass}`);
  264. }, (e) => {
  265. console.log('Error in email');
  266. User.findOneAndRemove({_id: response._id}, (err, data) => {
  267. console.log('inside rollback feature');
  268. if(err) {
  269. console.log({status: "error while performing rollback"});
  270. } else {
  271. console.log('Deleted successfully');
  272. }
  273. });
  274. if(e.name === "BulkWriteError")
  275. res.send({"status":"error","msg":"Email alredy taken"});
  276. else
  277. res.send({"status":"error", msg: "Something went Wrong"});
  278. return false;
  279. });
  280. }, (e) => {
  281. console.log(e);
  282. if(e.name === "BulkWriteError")
  283. res.send({"status":"error","msg":"mobile number alredy taken"});
  284. else
  285. res.send({"status":"error", msg: "SOmething went wrong"});
  286. return false;
  287. });
  288. });
  289. });
  290.  
  291. router.post("/user/edit", (req, res) => {
  292. const email = "";
  293. delete req.body.userId;
  294. let update = req.body;
  295.  
  296. User.findOneAndUpdate({_id: req.body.userId}, {$set:update}, {new: true},(err, data) => {
  297. if(err) {
  298. res.send({"status":"error"});
  299. return false;
  300. } else {
  301. sendMail(email,"Information Updated","Updated successfully");
  302. res.send({data, ...{status: "ok"}})
  303. }
  304. });
  305. });
  306.  
  307.  
  308. router.post('/login', (req,res) => {
  309. Reg.find({$and: [{email: req.body.email},{pass: req.body.pass}]}, (err,data) => {
  310. if(err) {
  311. console.log(err);
  312. res.send({"status":"error"});
  313. return false;
  314. } else {
  315. if(data.length === 0) {
  316. // Failed Login
  317. res.json({
  318. login: false
  319. });
  320. } else {
  321. User.find({_id: data[0].userId}, (err, data) => {
  322. if(err) {
  323. console.log(err);
  324. res.send({"status":"error"});
  325. return false;
  326. }
  327. res.send({data, ...{status: "ok"}})
  328. });
  329. }
  330. }
  331. });
  332. });
  333.  
  334.  
  335. // Company Get and Add.
  336. router.get('/company',(req,res) => {
  337. Company.find({approved: true}).populate('userId').exec((err,data) => {
  338. if(err) {
  339. res.send({"status":"error"});
  340. return false;
  341. }
  342. else res.send({data, ...{status: "ok"}})
  343. });
  344. });
  345.  
  346. router.get('/company/all',(req,res) => {
  347. Company.find({}, (err,data) => {
  348. if(err) {
  349. res.send({"status":"error"});
  350. return false;
  351. }
  352. else res.send({data, ...{status: "ok"}})
  353. });
  354. });
  355.  
  356.  
  357. router.post('/company/approve', (req, res) => {
  358. let update = {
  359. approved: true
  360. }
  361. Company.findOneAndUpdate({_id: req.body.companyId}, {$set:update}, {new: true},(err, data) => {
  362. if(err) {
  363. console.log(err);
  364. res.send({"status":"error"});
  365. return false;
  366. } else {
  367. res.send({data, ...{status: "ok"}})
  368. }
  369. });
  370. });
  371.  
  372.  
  373. router.post('/company/edit', (req, res) => {
  374. const id = req.body.companyId;
  375. delete req.body.companyId;
  376. let update = req.body;
  377. Company.findOneAndUpdate({_id: id}, {$set:update}, {new: true},(err, data) => {
  378. if(err) {
  379. console.log(err);
  380. res.send({"status":"error"});
  381. return false;
  382. } else {
  383. res.send({data, ...{status: "ok"}})
  384. }
  385. });
  386. });
  387.  
  388.  
  389. router.get('/company/:companyId',(req,res) => {
  390. Company.find({$and: [
  391. {_id: req.params.companyId},
  392. {approved: true}
  393. ]}).populate('userId').exec((err,data) => {
  394. if(err) {
  395. res.send({"status":"error"});
  396. return false;
  397. }
  398. else res.send({data, ...{status: "ok"}})
  399. });
  400. });
  401.  
  402. router.post("/company/add", uploadCompanyLogo.single('logo') ,(req, res) => {
  403. let email = "";
  404. Reg.find({userId: req.body.userId}, (err, data) => {
  405. if(err) {
  406. res.send({"status":"error"});
  407. return false;
  408. }
  409. email = data[0].email;
  410. let company = new Company({
  411. userId: req.body.userId,
  412. name: req.body.name,
  413. ownerName: req.body.ownerName,
  414. logo: req.file.filename,
  415. tagline: req.body.tagline,
  416. founded: req.body.founded,
  417. noOfEmp: req.body.noOfEmp,
  418. minProjectPrice: req.body.minProjectPrice,
  419. avgPricePerHour: req.body.avgPricePerHour,
  420. websiteLink: req.body.websiteLink,
  421. emailTechSupport: req.body.emailTechSupport,
  422. emailAdmin: req.body.emailAdmin,
  423. twitterProfile: req.body.twitterProfile,
  424. facebookProfile: req.body.facebookProfile,
  425. summary: req.body.summary,
  426. client: req.body.client,
  427. country: req.body.country || "",
  428. street: req.body.street,
  429. city: req.body.city || "",
  430. state: req.body.state || "",
  431. postalCode: req.body.postalCode,
  432. mobNo: req.body.mobNo,
  433. cert: req.body.cert,
  434. accolades: req.body.accolades,
  435. detailedDes: req.body.detailedDes,
  436. services: req.body.services,
  437. tech: req.body.tech,
  438. approved: false
  439. });
  440.  
  441. company.save().then((data) => {
  442. res.send({data, ...{status: "ok"}})
  443. sendMail(email,"Company under approval","Your company will be verified and updated in 48 hours");
  444. }, (e) => {
  445. res.send({"status":"error"});
  446. return false;
  447. });
  448. });
  449. });
  450.  
  451. // Rating Get and Add.
  452. router.get('/rating',(req,res) => {
  453. Rating.find({}, (err,data) => {
  454. if(err) {
  455. res.send({"status":"error"});
  456. return false;
  457. }
  458. else res.json({data, ...{status: "ok"}});
  459. });
  460. });
  461.  
  462. router.get('/rating/:catId',(req,res) => {
  463. Rating.find({}).populate('company_id').exec((err,data) => {
  464. if(err) {
  465. res.send({"status":"error"});
  466. return false;
  467. }
  468. else {
  469. let result = [];
  470. for(let i=0; i < data.length; i++) {
  471. if(req.params.catId in JSON.parse(data[i].ratingList)) {
  472. result.push(data[i]);
  473. }
  474. }
  475. res.send(result);
  476. }
  477. });
  478. });
  479.  
  480.  
  481. router.post("/rating/add", (req, res) => {
  482. const company_id = req.body.company_id || null;
  483. const solution_id = req.body.solution_id || null;
  484. const type = req.body.type;
  485. const userId = req.body.userId;
  486. if('company_id' in req.body)
  487. delete req.body.company_id;
  488. if('solution_id' in req.body)
  489. delete req.body.solution_id;
  490. delete req.body.type;
  491. delete req.body.userId;
  492. let rating = new Rating({
  493. company_id: company_id,
  494. solution_id: solution_id,
  495. type: type,
  496. userId: userId,
  497. ratingList: JSON.stringify(req.body)
  498. });
  499.  
  500.  
  501. rating.save().then((data) => {
  502. if(type == "Company".toLowerCase())
  503. res.send({data, ...{status: "ok"}})
  504. else
  505. res.send({data, ...{status: "ok"}})
  506. }, (e) => {
  507. res.send({"status":"error"});
  508. return false;
  509. });
  510. });
  511.  
  512.  
  513. // Category Get and Add.
  514. router.get('/category', (req,res) => {
  515. Category.find({}, (err,data) => {
  516. if(err) {
  517. res.send({"status":"error"});
  518. return false;
  519. }
  520. else res.send({data, ...{status: "ok"}})
  521. });
  522.  
  523. });
  524.  
  525. router.get('/category/:categoryId',(req,res) => {
  526. // res.end(req.params.categoryId);
  527. Category.find({_id: req.params.categoryId}, (err,data) => {
  528. if(err) {
  529. res.send({"status":"error"});
  530. return false;
  531. }
  532. else res.send({data, ...{status: "ok"}})
  533. });
  534. });
  535.  
  536. router.post("/category/add", (req, res) => {
  537. console.log(req.body);
  538. let category = new Category({
  539. name: req.body.name
  540. });
  541.  
  542. category.save().then((data) => {
  543. res.send({data, ...{status: "ok"}})
  544. }, (e) => {
  545. res.send({"status":"error"});
  546. return false;
  547. });
  548. });
  549.  
  550.  
  551. // Service Get and Add.
  552. router.get('/service', (req,res) => {
  553. Service.find({}, (err,data) => {
  554. if(err) {
  555. res.send({"status":"error"});
  556. return false;
  557. }
  558. else res.send({data, ...{status: "ok"}})
  559. });
  560.  
  561. });
  562.  
  563. router.get('/service/:serviceId',(req,res) => {
  564. Service.find({_id: req.params.serviceId}, (err,data) => {
  565. if(err) {
  566. res.send({"status":"error"});
  567. return false;
  568. }
  569. else res.send({data, ...{status: "ok"}})
  570. });
  571. });
  572.  
  573. router.post("/service/add", (req, res) => {
  574. console.log(req.body);
  575. let service = new Service({
  576. name: req.body.name
  577. });
  578.  
  579. Service.save().then((data) => {
  580. res.send({data, ...{status: "ok"}})
  581. }, (e) => {
  582. res.send({"status":"error"});
  583. return false;
  584. });
  585. });
  586.  
  587. router.post("/service/approve", (req, res) => {
  588. let update = {
  589. approved: true
  590. }
  591. Service.findOneAndUpdate({_id: req.body.serviceId}, {$set:update}, {new: true},(err, data) => {
  592. if(err) {
  593. console.log(err);
  594. res.send({"status":"error"});
  595. return false;
  596. } else {
  597. res.send({data, ...{status: "ok"}})
  598. }
  599. });
  600. });
  601.  
  602.  
  603. router.post('/service/edit', (req, res) => {
  604. const id = req.body.serviceId;
  605. delete req.body.serviceId;
  606. let update = req.body;
  607. Service.findOneAndUpdate({_id: id}, {$set:update}, {new: true},(err, data) => {
  608. if(err) {
  609. console.log(err);
  610. res.send({"status":"error"});
  611. return false;
  612. } else {
  613. res.send({data, ...{status: "ok"}})
  614. }
  615. });
  616. });
  617.  
  618. router.get('/tech', (req,res) => {
  619. Tech.find({}, (err,data) => {
  620. if(err) {
  621. res.send({"status":"error"});
  622. return false;
  623. }
  624. else res.send({data, ...{status: "ok"}})
  625. });
  626.  
  627. });
  628.  
  629. router.get('/tech/:techId',(req,res) => {
  630. Tech.find({_id: req.params.techId}, (err,data) => {
  631. if(err) {
  632. res.send({"status":"error"});
  633. return false;
  634. }
  635. else res.send({data, ...{status: "ok"}})
  636. });
  637. });
  638.  
  639. router.post("/tech/add", (req, res) => {
  640. console.log(req.body);
  641. let tech = new Tech({
  642. name: req.body.name
  643. });
  644.  
  645. tech.save().then((data) => {
  646. res.send({data, ...{status: "ok"}})
  647. }, (e) => {
  648. res.send({"status":"error"});
  649. return false;
  650. });
  651. });
  652.  
  653. // Review Get and Add.
  654. router.get('/review', (req,res) => {
  655. Review.find({approved: true}, (err,data) => {
  656. if(err) {
  657. res.send({"status":"error"});
  658. return false;
  659. }
  660. res.json({data, ...{status: "ok"}});
  661. });
  662. });
  663.  
  664. router.get('/review/all', (req,res) => {
  665. Review.find({}, (err,data) => {
  666. if(err) {
  667. res.send({"status":"error"});
  668. return false;
  669. }
  670. res.json({data, ...{status: "ok"}});
  671. });
  672. });
  673.  
  674. router.get('/review/company/:companyId', (req,res) => {
  675. Review.find({$and: [
  676. {companyId: req.params.companyId},
  677. {approved: true},
  678. ]}, (err,data) => {
  679. if(err) {
  680. res.send({"status":"error"});
  681. return false;
  682. }
  683. let items = 0;
  684. let total = 0;
  685. data = data.map(item => {
  686. items++;
  687. total += item.rating.overallRating.rating;
  688. return item;
  689. });
  690. let obj = {reviewsArray:data, avgRating: total/items}
  691. res.send(obj);
  692. });
  693. });
  694.  
  695. router.get('/review/company/filter', (req,res) => {
  696. let mySort = {};
  697. if(id === 1)
  698. mySort = {updatedAt: -1}
  699. if(id === 2)
  700. mySort = {rating: {overallRating: {rating: 1}}}
  701. if(id === 3)
  702. mySort = {rating: {overallRating: {rating: -1}}}
  703. Review.find({$and: [
  704. {companyId: req.params.companyId},
  705. {approved: true},
  706. ]},[],{sort: mySort},(err,data) => {
  707. if(err) {
  708. res.send({"status":"error"});
  709. return false;
  710. }
  711. let items = 0;
  712. let total = 0;
  713. data = data.map(item => {
  714. items++;
  715. total = item.rating.overallRating.rating;
  716. return item;
  717. });
  718. let obj = {reviewsArray:data, avgRating: total/items}
  719. res.send(obj);
  720. });
  721.  
  722. });
  723.  
  724.  
  725. router.get('/review/filter', (req,res) => {
  726. let mySort = {};
  727. if(id === 1)
  728. mySort = {updatedAt: -1}
  729. if(id === 2)
  730. mySort = {rating: {overallRating: {rating: 1}}}
  731. if(id === 3)
  732. mySort = {rating: {overallRating: {rating: -1}}}
  733. Review.find({$and: [
  734. {companyId: req.params.companyId},
  735. {approved: true},
  736. ]},[],{sort: mySort},(err,data) => {
  737. if(err) {
  738. res.send({"status":"error"});
  739. return false;
  740. }
  741. let items = 0;
  742. let total = 0;
  743. data = data.filter(item => {
  744. items++;
  745. total = item.rating.overallRating.rating;
  746. return parseInt(item.cost) > parseInt(req.query.cost);
  747. });
  748. let obj = {reviewsArray:data, avgRating: total/items}
  749. res.send(obj);
  750. });
  751.  
  752. });
  753.  
  754.  
  755.  
  756. router.get('/review/:userId', (req,res) => {
  757. // res.end(req.params.reviewId);
  758. Review.find({$and: [
  759. {userId: req.params.userId},
  760. {approved: true},
  761. ]}).populate('companyId').exec((err,data) => {
  762. if(err) {
  763. res.send({"status":"error"});
  764. return false;
  765. }
  766. if(data.length === 0) res.json({success:false});
  767. else res.json({...{success: true}, data});
  768. });
  769. });
  770.  
  771.  
  772. router.post("/review/add", (req, res) => {
  773. const email = req.body.email;
  774. let review = new Review({
  775. companyId: req.body.companyId,
  776. typeOfProject: req.body.typeOfProject,
  777. projectTitle: req.body.projectTitle,
  778. industry: req.body.industry,
  779. cost: req.body.cost,
  780. startDate: req.body.startDate,
  781. endDate: req.body.endDate,
  782. userId: req.body.userId,
  783. background: req.body.background,
  784. challenge: {
  785. service: req.body.cService,
  786. goal: req.body.cGoal
  787. },
  788. solution: {
  789. vendor: req.body.vendor,
  790. projectDetail: req.body.projectDetail,
  791. teamComposition: req.body.teamComposition
  792. },
  793. result: {
  794. outcome: req.body.outcome,
  795. effective: req.body.effective,
  796. keyFeature: req.body.keyFeature,
  797. improvements: req.body.improvements
  798. },
  799. rating: {
  800. quality: {
  801. rating: req.body.qRating,
  802. detail: req.body.qDetail
  803. },
  804. schedule: {
  805. rating: req.body.sRating,
  806. detail: req.body.sDetail
  807. },
  808. cost: {
  809. rating: req.body.cRating,
  810. detail: req.body.cDetail
  811. },
  812. refer: {
  813. rating: req.body.rRating,
  814. detail: req.body.rDetail
  815. },
  816. overallRating: {
  817. rating: req.body.oRating,
  818. detail: req.body.oDetail
  819. }
  820. },
  821. reviewer: {
  822. fullName: req.body.fullName,
  823. position: req.body.position,
  824. companyName: req.body.companyName,
  825. companySize: req.body.companySize,
  826. country: req.body.country,
  827. email: req.body.email,
  828. mobNo: req.body.mobNo
  829. },
  830. approved: false
  831. });
  832.  
  833. review.save().then((data) => {
  834. res.send({data, ...{status: "ok"}})
  835. sendMail(email,"Review Under approval","Your review will be updated in 48 hours");
  836. }, (e) => {
  837. res.send({"status":"error"});
  838. return false;
  839. });
  840. });
  841.  
  842.  
  843. // Subcategory
  844. router.get('/subcategory', (req,res) => {
  845. SubCat.find({}).populate('cat_id').exec((err,data) => {
  846. if(err) {
  847. res.send({"status":"error"});
  848. return false;
  849. }
  850. else res.json({data, ...{status: "ok"}});
  851. });
  852. });
  853.  
  854. router.post('/subcategory/add', (req,res) => {
  855. let subcat = new SubCat({
  856. cat_id: req.body.cat_id,
  857. name: req.body.name
  858. });
  859.  
  860. subcat.save().then((data) => {
  861. res.json({data, ...{status: "ok"}});
  862. }, (e) => {
  863. res.send({"status":"error"});
  864. return false;
  865. });
  866. });
  867.  
  868.  
  869. router.get('/portfolio', (req,res) => {
  870. Portfolio.find({}, (err,data) => {
  871. if(err) {
  872. res.send({"status":"error"});
  873. return false;
  874. }
  875. else res.send({data, ...{status: "ok"}})
  876. });
  877. });
  878.  
  879. router.post("/portfolio/add", uploadPortfolioImage.single('img') ,(req, res) => {
  880. let portfolio = new Portfolio({
  881. userId: req.body.userId,
  882. title: req.body.title,
  883. companyId: req.body.companyId,
  884. description: req.body.description,
  885. imageName : req.file.filename
  886. });
  887.  
  888. portfolio.save().then((data) => {
  889. res.send({data, ...{status: "ok"}})
  890. }, (e) => {
  891. res.send({"status":"error"});
  892. return false;
  893. });
  894. });
  895.  
  896. router.get('/portfolio/:companyId',(req,res) => {
  897. Portfolio.find({companyId: req.params.companyId}, (err,data) => {
  898. if(err) {
  899. res.send({"status":"error"});
  900. return false;
  901. }
  902. else res.send({data, ...{status: "ok"}})
  903. });
  904. });
  905.  
  906.  
  907. // Reference
  908. router.get('/reference', (req,res) => {
  909. Reference.find({}, (err,data) => {
  910. if(err) {
  911. res.send({"status":"error"});
  912. return false;
  913. }
  914. else res.json({data, ...{status: "ok"}});
  915. });
  916. });
  917.  
  918. router.get('/reference/user/:userId',(req,res) => {
  919. Reference.find({userId: req.params.userId}, (err,data) => {
  920. if(err) {
  921. res.send({"status":"error"});
  922. return false;
  923. }
  924. else res.send({data, ...{status: "ok"}})
  925. });
  926. });
  927.  
  928.  
  929. router.post("/reference/add",(req, res) => {
  930. let reference = new Reference({
  931. userId: req.body.userId,
  932. companyId: req.body.companyId,
  933. fName: req.body.fName,
  934. lName: req.body.lName,
  935. company: req.body.company,
  936. positionAtCompany: req.body.positionAtCompany,
  937. mobNo: req.body.mobNo,
  938. email: req.body.email,
  939. city: req.body.city,
  940. country: req.body.country,
  941. projectCost: req.body.projectCost,
  942. projectLength: req.body.projectLength,
  943. projectDescription: req.body.projectDescription
  944. });
  945.  
  946. reference.save().then((data) => {
  947. res.send({data, ...{status: "ok"}})
  948. }, (e) => {
  949. res.send({"status":"error"});
  950. return false;
  951. });
  952. });
  953.  
  954.  
  955. router.get('/solution/all', (req,res) => {
  956. Solution.find({}, (err,data) => {
  957. if(err) {
  958. res.send({"status":"error"});
  959. return false;
  960. }
  961. res.json({data, ...{status: "ok"}});
  962. })
  963. });
  964.  
  965. router.get('/solution', (req,res) => {
  966. Solution.find({approved: true}, (err,data) => {
  967. if(err) {
  968. res.send({"status":"error"});
  969. return false;
  970. }
  971. res.json({data, ...{status: "ok"}});
  972. })
  973. });
  974.  
  975. router.post("/solution/add", uploadSolutionLogo.single('logo') ,(req, res) => {
  976. let solu = new Solution({
  977. userId: req.body.userId,
  978. name: req.body.name,
  979. softwareCat: req.body.softwareCat,
  980. logo: req.file.filename,
  981. tagline: req.body.tagline,
  982. des: req.body.des,
  983. softwareSummary: req.body.softwareSummary,
  984. noOfEmp: req.body.noOfEmp,
  985. introText: req.body.introText,
  986. optionToStart: req.body.optionToStart,
  987. websiteLink: req.body.websiteLink,
  988. client: req.body.client,
  989. priceRange: req.body.priceRange,
  990. pricingOptions: req.body.pricingOptions,
  991. summary: req.body.summary,
  992. pricingPage: req.body.pricingPage,
  993. services: req.body.services,
  994. approved: false
  995. });
  996.  
  997. solu.save().then((data) => {
  998. res.send({data, ...{status: "ok"}})
  999. }, (e) => {
  1000. res.send({"status":"error"});
  1001. return false;
  1002. });
  1003. });
  1004.  
  1005.  
  1006. // Google Analytics Get and Add.
  1007. router.get('/ga', (req,res) => {
  1008. GA.find({}, (err,data) => {
  1009. if(err) {
  1010. res.send({"status":"error"});
  1011. return false;
  1012. }
  1013. else res.send({data, ...{status: "ok"}})
  1014. });
  1015.  
  1016. });
  1017.  
  1018. router.post("/ga/add", (req, res) => {
  1019. let ga = new GA({
  1020. companyId: req.body.company_id,
  1021. details: req.body.details
  1022. });
  1023.  
  1024. ga.save().then((data) => {
  1025. res.send({data, ...{status: "ok"}})
  1026. }, (e) => {
  1027. res.send({"status":"error"});
  1028. return false;
  1029. });
  1030. });
  1031.  
  1032. router.get('/search', (req,res) => {
  1033. // Search Company and
  1034. Company.find({
  1035. $and: [
  1036. {$or: [
  1037. {name: { "$regex": req.body.name, "$options": "i" }},
  1038. {services: {"$regex": req.body.name, "$options": "i" }},
  1039. {tech: {"$regex": req.body.name, "$options": "i" }},
  1040. ]},
  1041. {approved: true}
  1042. ]}, (err,data) => {
  1043. if(err) {
  1044. res.send({"status":"error"});
  1045. return false;
  1046. }
  1047. else res.json({data, ...{status: "ok"}});
  1048. });
  1049. });
  1050.  
  1051. router.get('/company/user/:userId', (req, res) => {
  1052. Company.find({$and: [
  1053. {userId: req.params.userId},
  1054. {approved: true},
  1055. ]}, (err, data) => {
  1056. if(err) {
  1057. res.send({"status":"error"});
  1058. return false;
  1059. }
  1060. else res.send({data, ...{status: "ok"}})
  1061. });
  1062. });
  1063.  
  1064. router.post('/user/forget', (req, res) => {
  1065. const givenEmail = req.body.email.toLowerCase();
  1066. Reg.find({email: givenEmail}, (err, data) => {
  1067. if( err ) {
  1068. res.send({"status":"error"});
  1069. return false;
  1070. }
  1071. if(data.length !== 1) {
  1072. res.send({"status":"No such file exists"});
  1073. return false;
  1074. }
  1075. let regId = data[0]._id;
  1076. let otp = generateOTP();
  1077. let transporter = nodemailer.createTransport({
  1078. service: 'gmail',
  1079. auth: {
  1080. user: 'tecish.co@gmail.com',
  1081. pass: 'PrateekMathur'
  1082. }
  1083. });
  1084.  
  1085. let mailOptions = {
  1086. from: 'tecish.co@gmail.com',
  1087. to: givenEmail,
  1088. subject: 'OTP for Tecish',
  1089. text: `Your OTP for password reset request is: ${otp}`
  1090. };
  1091.  
  1092.  
  1093. transporter.sendMail(mailOptions, function(error, info){
  1094. if (error) {
  1095. console.log(error);
  1096. } else {
  1097. console.log('Email sent: ' + info.response);
  1098. OTP.push(
  1099. {
  1100. regId: regId,
  1101. email: req.body.email,
  1102. otp: otp
  1103. }
  1104. );
  1105. console.log("inside",OTP);
  1106. res.send({status:"SENT", otp: OTP});
  1107. }
  1108. });
  1109. });
  1110. });
  1111.  
  1112.  
  1113. router.post('/user/verify/otp', (req, res) => {
  1114. const result = OTP.filter((instance) => {
  1115. return instance.otp === req.body.otp;
  1116. });
  1117. if (result.length === 1) {
  1118. res.json({"success": true,"regId" : result[0].regId});
  1119. } else {
  1120. res.json({"success": false});
  1121. }
  1122. });
  1123.  
  1124. router.post('/user/change/password', (req, res) => {
  1125. Reg.findOneAndUpdate({_id: req.body.regId}, {$set:{pass:req.body.pass}}, {new: true}, function(err, data){
  1126. if(err){
  1127. res.send({"status":"error"});
  1128. return false;
  1129. }
  1130. res.send({data, ...{status: "ok"}})
  1131. });
  1132. });
  1133.  
  1134. router.get('/company/service/:service', (req, res) => {
  1135. const result = [];
  1136. Company.find({approved: true}, (err, data) => {
  1137. if(err) {
  1138. res.send({"status":"error"});
  1139. return false;
  1140. }
  1141. else {
  1142. data.map(row => {
  1143. let servicesArray = row.services.split(',');
  1144. if(servicesArray.includes(req.params.service)) {
  1145. result.push(row);
  1146. }
  1147. });
  1148. res.send(result);
  1149. }
  1150. })
  1151. });
  1152.  
  1153. router.get('/company/name', (req, res) => {
  1154. Company.find({approved: true},'name',(err, data) => {
  1155. if(err) {
  1156. res.send({"status":"error"});
  1157. return false;
  1158. }
  1159. else res.send({data, ...{status: "ok"}})
  1160. });
  1161. });
  1162.  
  1163.  
  1164. router.post('/filter', (req, res) => {
  1165. Company.find({
  1166. $and: [
  1167. {$or: [
  1168. {name: { "$regex": req.body.name, "$options": "i" }},
  1169. {services: {"$regex": req.body.name, "$options": "i" }},
  1170. {tech: {"$regex": req.body.name, "$options": "i" }},
  1171. ]},
  1172. {approved: true}
  1173. ]}, (err,data) => {
  1174. if(err) {
  1175. res.send({"status":"error"});
  1176. return false;
  1177. }
  1178. else {
  1179. data = data.filter(company => {
  1180. const minPrice = parseInt(company.minProjectPrice);
  1181. const emp = parseInt(company.noOfEmp);
  1182. const hourPrice = parseInt(company.avgPricePerHour);
  1183. let flag = true;
  1184. const serviceArray = company.services.split(',');
  1185. if(req.body.industryFocus.length !== 1) {
  1186. flag = false;
  1187. serviceArray.map(item => {
  1188. if(req.body.industryFocus.includes(item)) {
  1189. flag = true;
  1190. }
  1191. });
  1192. }
  1193. return flag && minPrice >= parseInt(req.body.minPrice) && hourPrice >= parseInt(req.body.hourPrice) && emp >= parseInt(req.body.emp);
  1194. });
  1195. res.send({data, ...{status: "ok"}})
  1196. }
  1197. }
  1198. );
  1199. });
  1200.  
  1201. router.get('/leader/matrix',(req, res) => {
  1202. Company.find({approved: true},[],{skip: 0, last:10}).populate('userId').exec((err,data) => {
  1203. if(err) {
  1204. res.send({"status":"error"});
  1205. return false;
  1206. }
  1207. else res.send({data, ...{status: "ok"}})
  1208. });
  1209. });
  1210.  
  1211.  
  1212. router.get('/blog', (req, res) => {
  1213. Blog.find({}).populate('category').exec((err,data) => {
  1214. if(err) {
  1215. res.send({status: "error"});
  1216. return;
  1217. } else {
  1218. res.send({data, ...{status: "ok"}})
  1219. }
  1220. })
  1221. });
  1222.  
  1223. // Category here means serivce
  1224. router.get('/blog/:categoryId', (req, res) => {
  1225. Blog.find({category: req.params.categoryId}).populate('category').exec((err,data) => {
  1226. if(err) {
  1227. res.send({status: "error"});
  1228. return;
  1229. } else {
  1230. res.send({data, ...{status: "ok"}})
  1231. }
  1232. })
  1233. });
  1234.  
  1235. router.post("/blog/add", uploadCompanyLogo.single('img') ,(req, res) => {
  1236. let blog = new Blog({
  1237. title: req.body.title,
  1238. img: req.body.img,
  1239. content: req.body.content,
  1240. date: req.body.date,
  1241. category: req.body.category
  1242. });
  1243.  
  1244. blog.save().then((data) => {
  1245. res.send({data, ...{status: "ok"}})
  1246. }, (e) => {
  1247. res.send({"status":"error"});
  1248. return false;
  1249. });
  1250. });
  1251.  
  1252.  
  1253.  
  1254. router.get('/ contact', (req,res) => {
  1255. Contact.find({}, (err,data) => {
  1256. if(err) {
  1257. res.send({"status":"error"});
  1258. return false;
  1259. }
  1260. else res.send({data, ...{status: "ok"}})
  1261. });
  1262.  
  1263. });
  1264.  
  1265. router.get('/contact/:contactId',(req,res) => {
  1266. // res.end(req.params.contactId);
  1267. Contact.find({_id: req.params.contactId}, (err,data) => {
  1268. if(err) {
  1269. res.send({"status":"error"});
  1270. return false;
  1271. }
  1272. else res.send({data, ...{status: "ok"}})
  1273. });
  1274. });
  1275.  
  1276. router.post("/contact/add", (req, res) => {
  1277. console.log(req.body);
  1278. let contact = new Contact({
  1279. name: req.body.name
  1280. });
  1281.  
  1282. contact.save().then((data) => {
  1283. res.send({data, ...{status: "ok"}})
  1284. }, (e) => {
  1285. res.send({"status":"error"});
  1286. return false;
  1287. });
  1288. });
  1289.  
  1290.  
  1291. module.exports = router;
Add Comment
Please, Sign In to add comment