feat: send excel template and example
This commit is contained in:
parent
2da2950c84
commit
098d4b48f4
|
|
@ -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",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
BIN
media/uploads/excel/excel example.xlsx
Normal file
BIN
media/uploads/excel/excel example.xlsx
Normal file
Binary file not shown.
Binary file not shown.
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user