diff --git a/pages/cart/publishers/index.tsx b/pages/cart/publishers/index.tsx index b692d44..3a78e2d 100644 --- a/pages/cart/publishers/index.tsx +++ b/pages/cart/publishers/index.tsx @@ -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 ( @@ -204,8 +212,13 @@ function PublishersPage({ publishers = [] }: IProps) {
Import publishers
+ {/* export by calling excel helper .ExportPublishersToExcel() */} +
+ +
+
@@ -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({ diff --git a/server.js b/server.js index 601079c..7fbafbd 100644 --- a/server.js +++ b/server.js @@ -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); }); diff --git a/src/helpers/excel.js b/src/helpers/excel.js index 4bba712..cd45aa0 100644 --- a/src/helpers/excel.js +++ b/src/helpers/excel.js @@ -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 = [ "Понеделник", "Вторник",