From 5efb2ad133252ec251d7a1525f35b8e6fac087b4 Mon Sep 17 00:00:00 2001 From: Jaya Singh Date: Thu, 12 Nov 2020 10:02:06 +0530 Subject: [PATCH] added professional profile added --- package.json | 20 ++++++++++ server/package.json | 4 +- server/src/db/mongoose.js | 2 +- server/src/middleware/auth.js | 35 ++++++++++++----- server/src/models/signup_workers.js | 3 ++ server/src/routers/api.js | 59 ++++++++++++++++++++++++++++- 6 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..61f41ed --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "job-provider-site", + "version": "1.0.0", + "description": "> A job provoding and posting website", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/dscnitp/job-provider-site.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/dscnitp/job-provider-site/issues" + }, + "homepage": "https://github.com/dscnitp/job-provider-site#readme" +} diff --git a/server/package.json b/server/package.json index 6e83700..d773df3 100644 --- a/server/package.json +++ b/server/package.json @@ -1,7 +1,6 @@ { "name": "server", "version": "1.0.0", - "description": "", "main": "index.js", "scripts": { "start": "node --path src/App.js", @@ -28,5 +27,6 @@ "devDependencies": { "nodemon": "^2.0.6", "prettier": "^2.1.2" - } + }, + "description": "" } diff --git a/server/src/db/mongoose.js b/server/src/db/mongoose.js index b9e3995..0a31db8 100644 --- a/server/src/db/mongoose.js +++ b/server/src/db/mongoose.js @@ -2,7 +2,7 @@ const mongoose = require("mongoose"); require("dotenv").config(); mongoose - .connect(process.env.MainDB, { + .connect(MainDB = "mongodb+srv://dscnitp_webdept_worklink:dscnitp_webdept_worklink@cluster0.glrlk.gcp.mongodb.net/worklinkDB?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, diff --git a/server/src/middleware/auth.js b/server/src/middleware/auth.js index 45efd5e..0157d12 100644 --- a/server/src/middleware/auth.js +++ b/server/src/middleware/auth.js @@ -1,22 +1,39 @@ const jwt = require('jsonwebtoken') const Customer = require('../models/Customer') +const Worker = require('../models/signup_workers') const auth = async (req,res,next) => { try { + console.log("enetered try") const token = req.header('Authorization').replace('Bearer ', '') - const decoded = jwt.verify(token, process.env.JWT_SECRET) - const customer = await Customer.findOne({_id: decoded._id, 'tokens.token': token }) - + const decoded = jwt.verify(token, "thisismynewcourse") + const customer = await Customer.findOne({_id: decoded._id, 'tokens.token': token}) + console.log(token); + if(customer) + { + req.token = token + req.customer = customer + console.log("customer found") + next() + } if(!customer) { - throw new error() + console.log("customer not found") + const worker = await Worker.findOne({_id: decoded._id, 'tokens.token':token}); + if(!worker) + { + throw new Error(); + console.log("worker not found") + } + req.token = token; + req.worker = worker; + next(); } - req.token = token - req.customer = customer - next() - } catch (e) { + + } + catch (e) { res.status(401).send({error: 'Please authenticate'}) } } -module.exports = auth \ No newline at end of file +module.exports = auth diff --git a/server/src/models/signup_workers.js b/server/src/models/signup_workers.js index 07808f5..b334def 100644 --- a/server/src/models/signup_workers.js +++ b/server/src/models/signup_workers.js @@ -78,6 +78,9 @@ const workerSchema = new mongoose.Schema({ }, }, ], + avatar: { + type: Buffer + } }); workerSchema.methods.generateAuthToken = async function () { diff --git a/server/src/routers/api.js b/server/src/routers/api.js index 0727649..ddfb3c7 100644 --- a/server/src/routers/api.js +++ b/server/src/routers/api.js @@ -3,7 +3,9 @@ const router = express.Router(); const multer = require('multer') const sharp = require('sharp') const Customer = require("../models/Customer"); -const auth = require('../middleware/auth') +const auth = require('../middleware/auth'); +const Worker = require("../models/signup_workers"); + // Testing Purpose // router.get('/signup_customer', (req, res) => { @@ -94,4 +96,59 @@ router.get('/customer/:id/avatar', async (req, res) => { } }) +//editing proffessional profile:- + +router.patch('/worker/me', auth , async (req, res) => { + const updates = Object.keys(req.body) + const allowedUpdates = ['name','contact', 'email','location', 'password','cost_of_work','type_of_work','experience','address'] + const isValidOperation = updates.every((update) => allowedUpdates.includes(update)) + + if (!isValidOperation) { + return res.status(400).send({ error: 'Invalid update!' }) + } + + try { + + updates.forEach((update) => req.worker[update] = req.body[update]) + await req.worker.save() + + res.send(req.worker) + } catch (e) { + res.status(400).send(e) + } +}) + + +router.post('/worker/me/avatar',auth, upload.single('avatar'),async (req,res) => { + const buffer = await sharp(req.file.buffer).resize({ width: 250, height: 250 }).png().toBuffer() + req.worker.avatar = buffer + await req.worker.save() + res.send() +}, (error, req, res, next) => { + res.status(400).send({ error: error.message}) +}) + +router.delete('/worker/me/avatar',auth,async (req,res) => { + req.worker.avatar = undefined + await req.worker.save() + res.send() +}); + +router.get('/worker/:id/avatar', async (req, res) => { + try { + console.log("Worker avatar") + const worker = await Worker.findById(req.params.id) + + if (!worker || !worker.avatar) { + throw new Error() + } + console.log("reached here") + res.set('Content-Type', 'image/png') + res.send(worker.avatar) + } catch (e) { + res.status(404).send() + } +}) + + module.exports = router;