Skip to content

Commit

Permalink
added professional profile added
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaya Singh committed Nov 12, 2020
1 parent 8752d93 commit 5efb2ad
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 13 deletions.
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node --path src/App.js",
Expand All @@ -28,5 +27,6 @@
"devDependencies": {
"nodemon": "^2.0.6",
"prettier": "^2.1.2"
}
},
"description": ""
}
2 changes: 1 addition & 1 deletion server/src/db/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const mongoose = require("mongoose");
require("dotenv").config();

mongoose
.connect(process.env.MainDB, {
.connect(MainDB = "mongodb+srv://dscnitp_webdept_worklink:[email protected]/worklinkDB?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
Expand Down
35 changes: 26 additions & 9 deletions server/src/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -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
module.exports = auth
3 changes: 3 additions & 0 deletions server/src/models/signup_workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ const workerSchema = new mongoose.Schema({
},
},
],
avatar: {
type: Buffer
}
});

workerSchema.methods.generateAuthToken = async function () {
Expand Down
59 changes: 58 additions & 1 deletion server/src/routers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;

0 comments on commit 5efb2ad

Please sign in to comment.