diff --git a/controllers/usersControllers/user.js b/controllers/usersControllers/user.js index 802ff9e..810c1e8 100644 --- a/controllers/usersControllers/user.js +++ b/controllers/usersControllers/user.js @@ -711,3 +711,67 @@ export const getMe = async (req, res) => { response(500, null, "Error retrieving user data!", res); } }; + +export const sendExcelTemplate = async (req, res) => { + try { + const filePath = path.join( + process.cwd(), + "media/uploads/excel/excel template.xlsx" + ); + + if (!fs.existsSync(filePath)) { + return res.status(404).json({ + success: false, + message: "File not found!", + }); + } + + const fileName = "excel_template.xlsx"; + + res.setHeader("Content-Disposition", `attachment; filename=${fileName}`); + res.setHeader( + "Content-Type", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ); + + return res.sendFile(filePath); + } catch (error) { + console.error("Error sending file:", error); + return res.status(500).json({ + success: false, + message: "Internal Server Error", + }); + } +}; + +export const sendExcelExample = async (req, res) => { + try { + const filePath = path.join( + process.cwd(), + "media/uploads/excel/excel example.xlsx" + ); + + if (!fs.existsSync(filePath)) { + return res.status(404).json({ + success: false, + message: "File not found!", + }); + } + + const fileName = "excel_example.xlsx"; + + res.setHeader("Content-Disposition", `attachment; filename=${fileName}`); + res.setHeader( + "Content-Type", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ); + + return res.sendFile(filePath); + } catch (error) { + console.error("Error sending file:", error); + return res.status(500).json({ + success: false, + message: "Internal Server Error", + }); + } +}; diff --git a/media/uploads/excel/excel example.xlsx b/media/uploads/excel/excel example.xlsx new file mode 100644 index 0000000..7ef54f2 Binary files /dev/null and b/media/uploads/excel/excel example.xlsx differ diff --git a/media/uploads/excel/excel template.xlsx b/media/uploads/excel/excel template.xlsx index 291521c..cca4f3a 100644 Binary files a/media/uploads/excel/excel template.xlsx and b/media/uploads/excel/excel template.xlsx differ diff --git a/routes/user/user.js b/routes/user/user.js index ab95878..d2c3409 100644 --- a/routes/user/user.js +++ b/routes/user/user.js @@ -1,5 +1,5 @@ import express from "express"; -import { getUsers, getAdmins, getTeachers, getStudents, getUserById, getUserByName, updateUserById, updateUserPasswordById, deleteUserById, getMe } from "../../controllers/usersControllers/user.js"; +import { getUsers, getAdmins, getTeachers, getStudents, getUserById, getUserByName, updateUserById, updateUserPasswordById, deleteUserById, getMe, sendExcelTemplate, sendExcelExample } from "../../controllers/usersControllers/user.js"; import { verifyLoginUser, adminOnly, adminOrTeacherOnly } from "../../middlewares/User/authUser.js"; import handleUpload from "../../middlewares/User/uploadUser.js"; @@ -14,6 +14,10 @@ router.get("/user/teacher", verifyLoginUser, adminOrTeacherOnly, getTeachers); router.get("/user/student", verifyLoginUser, adminOrTeacherOnly, getStudents); +router.get("/user/sendExcelTemplate", verifyLoginUser, adminOrTeacherOnly, sendExcelTemplate); + +router.get("/user/sendExcelExample", verifyLoginUser, adminOrTeacherOnly, sendExcelExample); + router.get("/user/:id", verifyLoginUser, getUserById); router.get("/user/name/:name", verifyLoginUser, adminOrTeacherOnly, getUserByName);