publishers try ex cel export (wip)
This commit is contained in:
@ -16,7 +16,7 @@ import ProtectedRoute from '../../../components/protectedRoute';
|
||||
import ConfirmationModal from '../../../components/ConfirmationModal';
|
||||
import { relative } from "path";
|
||||
import { set } from "lodash";
|
||||
|
||||
import { excel } from "../../../src/helpers/excel";
|
||||
|
||||
|
||||
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 (
|
||||
<Layout>
|
||||
<ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER]}>
|
||||
@ -204,7 +212,12 @@ function PublishersPage({ publishers = [] }: IProps) {
|
||||
<div className="flex justify-center m-4">
|
||||
<a href="/cart/publishers/import" className="btn">Import publishers</a>
|
||||
</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 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" >
|
||||
@ -245,12 +258,12 @@ function PublishersPage({ publishers = [] }: IProps) {
|
||||
|
||||
export default PublishersPage;
|
||||
|
||||
//import { set } from "date-fns";
|
||||
//import {set} from "date-fns";
|
||||
|
||||
export const getServerSideProps = async (context) => {
|
||||
// const axios = await axiosServer(context);
|
||||
// //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
|
||||
const prisma = common.getPrismaClient();
|
||||
let publishers = await prisma.publisher.findMany({
|
||||
|
@ -574,7 +574,7 @@ nextApp
|
||||
|
||||
// --------------- EXCEL EXPORT ROUTE ----------------
|
||||
server.get("/generatexcel/:year/:month/:process", async (req, res) => {
|
||||
await excel.GenerateExcel(req, res);
|
||||
await excel.ScheduleGenerateExcel(req, res);
|
||||
});
|
||||
|
||||
|
||||
|
@ -15,7 +15,7 @@ const data = require("./data");
|
||||
// for nodejs
|
||||
//const api = require("./pages/api/index");
|
||||
|
||||
exports.GenerateExcel = async function (req, res) {
|
||||
exports.ScheduleGenerateExcel = async function (req, res) {
|
||||
|
||||
const prisma = common.getPrismaClient();
|
||||
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 = [
|
||||
"Понеделник",
|
||||
"Вторник",
|
||||
|
Reference in New Issue
Block a user