publishers try ex cel export (wip)

This commit is contained in:
Dobromir Popov
2024-09-14 19:03:48 +03:00
parent 51544873dd
commit ed3222a428
3 changed files with 67 additions and 5 deletions

View File

@ -16,7 +16,7 @@ import ProtectedRoute from '../../../components/protectedRoute';
import ConfirmationModal from '../../../components/ConfirmationModal'; import ConfirmationModal from '../../../components/ConfirmationModal';
import { relative } from "path"; import { relative } from "path";
import { set } from "lodash"; import { set } from "lodash";
import { excel } from "../../../src/helpers/excel";
interface IProps { interface IProps {
@ -172,6 +172,14 @@ function PublishersPage({ publishers = [] }: IProps) {
} }
}; };
const exportPublishers = async () => {
try {
await excel.ExportPublishersToExcel();
} catch (error) {
console.error(JSON.stringify(error));
}
}
return ( return (
<Layout> <Layout>
<ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER]}> <ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER]}>
@ -204,8 +212,13 @@ function PublishersPage({ publishers = [] }: IProps) {
<div className="flex justify-center m-4"> <div className="flex justify-center m-4">
<a href="/cart/publishers/import" className="btn">Import publishers</a> <a href="/cart/publishers/import" className="btn">Import publishers</a>
</div> </div>
{/* export by calling excel helper .ExportPublishersToExcel() */}
<div className="flex justify-center m-4">
<button className="btn" onClick={exportPublishers}>Export to Excel</button>
</div>
</div> </div>
<div className="z-60 sticky top-0" style={{ zIndex: 60, position: relative }}> <div className="z-60 sticky top-0" style={{ zIndex: 60, position: relative }}>
<div name="filters" className="flex items-center justify-center space-x-4 m-4 bg-gray-100 p-2" > <div name="filters" className="flex items-center justify-center space-x-4 m-4 bg-gray-100 p-2" >
<label htmlFor="filter">Filter:</label> <label htmlFor="filter">Filter:</label>
@ -245,12 +258,12 @@ function PublishersPage({ publishers = [] }: IProps) {
export default PublishersPage; export default PublishersPage;
//import { set } from "date-fns"; //import {set} from "date-fns";
export const getServerSideProps = async (context) => { export const getServerSideProps = async (context) => {
// const axios = await axiosServer(context); // const axios = await axiosServer(context);
// //ToDo: refactor all axios calls to use axiosInstance and this URL // //ToDo: refactor all axios calls to use axiosInstance and this URL
// const { data: publishers } = await axios.get('/api/data/publishers?select=id,firstName,lastName,email,isActive,isTrained,isImported,assignments.shift.startTime,availabilities.startTime&dev=fromuseefect'); // const {data: publishers } = await axios.get('/api/data/publishers?select=id,firstName,lastName,email,isActive,isTrained,isImported,assignments.shift.startTime,availabilities.startTime&dev=fromuseefect');
//use prisma instead of axios //use prisma instead of axios
const prisma = common.getPrismaClient(); const prisma = common.getPrismaClient();
let publishers = await prisma.publisher.findMany({ let publishers = await prisma.publisher.findMany({

View File

@ -574,7 +574,7 @@ nextApp
// --------------- EXCEL EXPORT ROUTE ---------------- // --------------- EXCEL EXPORT ROUTE ----------------
server.get("/generatexcel/:year/:month/:process", async (req, res) => { server.get("/generatexcel/:year/:month/:process", async (req, res) => {
await excel.GenerateExcel(req, res); await excel.ScheduleGenerateExcel(req, res);
}); });

View File

@ -15,7 +15,7 @@ const data = require("./data");
// for nodejs // for nodejs
//const api = require("./pages/api/index"); //const api = require("./pages/api/index");
exports.GenerateExcel = async function (req, res) { exports.ScheduleGenerateExcel = async function (req, res) {
const prisma = common.getPrismaClient(); const prisma = common.getPrismaClient();
const year = req.params.year; const year = req.params.year;
@ -611,6 +611,55 @@ exports.ReadDocxFileForMonth = async function (filePath, buffer, month, year, pr
} }
}; };
exports.ExportPublishersToExcel = async function (req, res) {
const prisma = common.getPrismaClient();
const publishers = await prisma.publisher.findMany({
// where: { isActive: true, },
include: {
// availabilities: { where: { isActive: true, }, },
// assignments: { include: { shift: true, }, },
congregation: true,
},
});
const ExcelJS = require("exceljs");
const xjswb = new ExcelJS.Workbook();
const sheet = xjswb.addWorksheet("Publishers");
sheet.columns = [
{ header: "Name", key: "name", width: 32 },
{ header: "Trained", key: "trained", width: 10 },
{ header: "Email", key: "email", width: 32 },
{ header: "Phone", key: "phone", width: 32 },
{ header: "Role", key: "role", width: 32 },
{ header: "Congregation", key: "congregationName", width: 32 },
{ header: "Last Login", key: "lastLogin", width: 32 },
{ header: "Type", key: "PublisherTypeText", width: 32 },
{ header: "Active", key: "isActive", width: 10 },
{ header: "Created At", key: "createdAt", width: 32 },
{ header: "Updated At", key: "updatedAt", width: 32 },
];
publishers.forEach((publisher) => {
sheet.addRow({
name: publisher.firstName + " " + publisher.lastName,
trained: publisher.isTrained,
email: publisher.email,
phone: publisher.phone,
role: publisher.role,
congregationName: publisher.congregation.name,
lastLogin: publisher.lastLogin,
PublisherTypeText: publisher.PublisherTypeText,
isActive: publisher.isActive,
createdAt: publisher.createdAt,
updatedAt: publisher.updatedAt,
});
});
res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
res.setHeader("Content-Disposition", "attachment; filename=" + encodeURI("Publishers.xlsx"));
xjswb.xlsx.write(res);
}
const weekNames = [ const weekNames = [
"Понеделник", "Понеделник",
"Вторник", "Вторник",