feat: send excel template and example

This commit is contained in:
elangptra 2024-12-04 08:53:21 +07:00
parent 2da2950c84
commit 098d4b48f4
4 changed files with 69 additions and 1 deletions

View File

@ -711,3 +711,67 @@ export const getMe = async (req, res) => {
response(500, null, "Error retrieving user data!", 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",
});
}
};

Binary file not shown.

View File

@ -1,5 +1,5 @@
import express from "express"; 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 { verifyLoginUser, adminOnly, adminOrTeacherOnly } from "../../middlewares/User/authUser.js";
import handleUpload from "../../middlewares/User/uploadUser.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/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/:id", verifyLoginUser, getUserById);
router.get("/user/name/:name", verifyLoginUser, adminOrTeacherOnly, getUserByName); router.get("/user/name/:name", verifyLoginUser, adminOrTeacherOnly, getUserByName);