Skip to content

Commit

Permalink
Merge pull request #26 from DucHuy2801/fix-bug
Browse files Browse the repository at this point in the history
API for Admin
  • Loading branch information
augustus281 authored May 20, 2024
2 parents 40a53ad + a23865b commit 8b67c93
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 104 deletions.
37 changes: 37 additions & 0 deletions server/src/controllers/admin.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const { StatusOrder } = require("../common/status")
const Order = require("../models/order.model")
const Tour = require("../models/tour.model")

class AdminController {
getCustomerNumber = async (req, res, next) => {
try {
const totalBookedNumber = await Tour.sum("booked_number")
return res.status(200).json({
message: "Get total booked number successfully!",
total: totalBookedNumber
})
} catch (error) {
return res.status(500).json({ message: error.message })
}
}

getTotalRevenue = async (req, res, next) => {
try {
const totalRevenue = await Order.sum("total_to_pay", {
where: {
status: StatusOrder.COMPLETE
}
})
return res.status(200).json({
message: "Get total revenue successfully!",
total_revenue: totalRevenue
})
} catch (error) {
return res.status(500).json({ message: error.message })
}
}
}

module.exports = new AdminController()
45 changes: 45 additions & 0 deletions server/src/controllers/tour_guide.controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict'

const { RoleUser } = require("../common/status");
const { BadRequestError } = require("../core/error.response");
const GuideTour = require("../models/guide_tour.model");
const TourGuide = require("../models/tour_guide.model");
const { findTourById } = require("../services/tour.service")
const bcrypt = require('bcrypt');
const { isAdmin } = require("../middlewares/authenticate");
const User = require("../models/user.model");

const role_user = {
ADMIN: 'admin',
Expand Down Expand Up @@ -95,6 +98,48 @@ class TourGuideController {
throw new Error("Error: ", error.message)
}
}

createTourGuideAccount = async (req, res, next) => {
try {
const {
email,
password,
lastname,
firstname,
gender,
dob,
phone_number
} = req.body

const access_token = req.headers['authorization']
const checkAdmin = await isAdmin(access_token)
if (!checkAdmin) throw new BadRequestError("Can't create tour guide account!")

const tourGuide = await User.findOne({
where: { email }
})
if (tourGuide) throw new BadRequestError("Email is existed!")
const newTourGuide = await User.create({
email,
password: await bcrypt.hash(password, 10),
lastname,
firstname,
gender,
dob,
phone_number,
role_user: RoleUser.GUIDER
})

if (!newTourGuide) throw new BadRequestError("Create new tour guide failed!")
return res.status(201).json({
message: "Create tour guide successfully!",
tour_guide: newTourGuide
})

} catch (error) {
return res.status(500).json({ message: error.message })
}
}
}

module.exports = new TourGuideController()
22 changes: 21 additions & 1 deletion server/src/middlewares/authenticate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const jwt = require('jsonwebtoken')
const { RoleUser } = require('../common/status')
const { AuthFailureError } = require('../core/error.response')

const HEADER = {
AUTHORIZATION: 'authorization'
Expand All @@ -20,13 +22,31 @@ const authenticate = async (req, res, next) => {
}
}

const authenticateAdmin = async (req, res, next) => {
try {
const token = req.headers[HEADER.AUTHORIZATION]
if (!token || token === null || token === 'undefined')
return res.status(400).json({ status: 'Unauthorized', message: "You don't have access"})

const decodeUser = jwt.decode(token)
if (decodeUser['role_user'] != RoleUser.ADMIN ) {
throw new AuthFailureError("Invalid request!")
}
req.user = decodeUser
return next()
} catch (error) {
return res.status(500).json({ status: 'Fail', message: error.message })
}
}

const isAdmin = async (token) => {
const decodeUser = jwt.decode(token)
const role_user = decodeUser['role_user']
if (role === role_user) return true;
if (RoleUser.ADMIN === role_user) return true;
}

module.exports = {
authenticate,
authenticateAdmin,
isAdmin
}
2 changes: 0 additions & 2 deletions server/src/models/schedule.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ Schedule.init({
}
}, { sequelize, modelName: "schedule"} )

// Schedule.belongsTo(Tour, { foreignKey: "tour_id" })

module.exports = Schedule
Loading

0 comments on commit 8b67c93

Please sign in to comment.