Guest User

Untitled

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