43 lines
1.7 KiB
JavaScript
43 lines
1.7 KiB
JavaScript
const express = require('express');
|
|
const multer = require('multer');
|
|
const path = require('path');
|
|
const { register, me, updateMe, removeMe, exchangeZohoToken } = require('../controllers/userController');
|
|
const auth = require('../middlewares/auth');
|
|
const { registerSchema, updateSchema } = require('../validators/userValidator');
|
|
const Joi = require('joi');
|
|
|
|
const router = express.Router();
|
|
|
|
const storage = multer.diskStorage({
|
|
destination: (req, file, cb) => cb(null, path.join(process.cwd(), 'uploads')),
|
|
filename: (req, file, cb) => cb(null, `${Date.now()}-${file.originalname}`)
|
|
});
|
|
const upload = multer({ storage });
|
|
|
|
function validate(schema) {
|
|
return (req, res, next) => {
|
|
const toValidate = req.method === 'GET' ? req.query : req.body;
|
|
const { error, value } = schema.validate(toValidate, { abortEarly: false, stripUnknown: true });
|
|
if (error) {
|
|
return res.status(400).json({ status: 'error', message: 'Validation failed', errorCode: 'VALIDATION_ERROR', details: error.details, timestamp: new Date().toISOString() });
|
|
}
|
|
if (req.method === 'GET') req.query = value; else req.body = value;
|
|
next();
|
|
};
|
|
}
|
|
|
|
router.post('/register', validate(registerSchema), register);
|
|
router.get('/me', auth, me);
|
|
router.put('/me', auth, upload.single('profilePicture'), validate(updateSchema), updateMe);
|
|
router.delete('/me', auth, removeMe);
|
|
|
|
// OAuth token exchange (Zoho request currently)
|
|
const zohoTokenSchema = Joi.object({
|
|
authorization_code: Joi.string().required(),
|
|
id: Joi.number().required(),
|
|
service_name: Joi.string().valid('zoho', 'keka', 'bamboohr', 'hubspot', 'other').required()
|
|
});
|
|
router.post('/zoho/token', auth, validate(zohoTokenSchema), exchangeZohoToken);
|
|
|
|
module.exports = router;
|