added access variable to exams soo we can distinguish private, public and confidential exams and also bugfixes and improvements
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import {WithLabeledEntities} from "@/interfaces/entity";
|
||||
import {User} from "@/interfaces/user";
|
||||
import {USER_TYPE_LABELS} from "@/resources/user";
|
||||
import {capitalize} from "lodash";
|
||||
import { WithLabeledEntities } from "@/interfaces/entity";
|
||||
import { User } from "@/interfaces/user";
|
||||
import { USER_TYPE_LABELS } from "@/resources/user";
|
||||
import { capitalize } from "lodash";
|
||||
import moment from "moment";
|
||||
import ExcelJS from "exceljs";
|
||||
|
||||
export interface UserListRow {
|
||||
name: string;
|
||||
@@ -17,6 +18,22 @@ export interface UserListRow {
|
||||
gender: string;
|
||||
}
|
||||
|
||||
const indexToLetter = (index: number): string => {
|
||||
// Base case: if the index is less than 0, return an empty string
|
||||
if (index < 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Calculate the quotient for recursion (number of times the letter sequence repeats)
|
||||
const quotient = Math.floor(index / 26);
|
||||
|
||||
// Calculate the remainder for the current letter
|
||||
const remainder = index % 26;
|
||||
|
||||
// Recursively call indexToLetter for the quotient and append the current letter
|
||||
return indexToLetter(quotient - 1) + String.fromCharCode(65 + remainder);
|
||||
};
|
||||
|
||||
export const exportListToExcel = (rowUsers: WithLabeledEntities<User>[]) => {
|
||||
const rows: UserListRow[] = rowUsers.map((user) => ({
|
||||
name: user.name,
|
||||
@@ -33,10 +50,31 @@ export const exportListToExcel = (rowUsers: WithLabeledEntities<User>[]) => {
|
||||
gender: user.demographicInformation?.gender ? capitalize(user.demographicInformation.gender) : "N/A",
|
||||
verified: user.isVerified?.toString() || "FALSE",
|
||||
}));
|
||||
const header = "Name,Email,Type,Entities,Expiry Date,Country,Phone,Employment/Department,Gender,Verification";
|
||||
const rowsString = rows.map((x) => Object.values(x).join(",")).join("\n");
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
const worksheet = workbook.addWorksheet("User Data");
|
||||
const border: Partial<ExcelJS.Borders> = { top: { style: 'thin' as ExcelJS.BorderStyle }, left: { style: 'thin' as ExcelJS.BorderStyle }, bottom: { style: 'thin' as ExcelJS.BorderStyle }, right: { style: 'thin' as ExcelJS.BorderStyle } }
|
||||
const header = ['Name', 'Email', 'Type', 'Entities', 'Expiry Date', 'Country', 'Phone', 'Employment/Department', 'Gender', 'Verification'].forEach((item, index) => {
|
||||
const cell = worksheet.getCell(`${indexToLetter(index)}1`);
|
||||
const column = worksheet.getColumn(index + 1);
|
||||
column.width = item.length * 2;
|
||||
cell.value = item;
|
||||
cell.font = { bold: true, size: 16 };
|
||||
cell.border = border;
|
||||
|
||||
return `${header}\n${rowsString}`;
|
||||
});
|
||||
rows.forEach((x, index) => {
|
||||
(Object.keys(x) as (keyof UserListRow)[]).forEach((key, i) => {
|
||||
const cell = worksheet.getCell(`${indexToLetter(i)}${index + 2}`);
|
||||
cell.value = x[key];
|
||||
if (index === 0) {
|
||||
const column = worksheet.getColumn(i + 1);
|
||||
column.width = Math.max(column.width ?? 0, x[key].toString().length * 2);
|
||||
}
|
||||
cell.border = border;
|
||||
});
|
||||
})
|
||||
|
||||
return workbook.xlsx.writeBuffer();
|
||||
};
|
||||
|
||||
export const getUserName = (user?: User) => {
|
||||
|
||||
Reference in New Issue
Block a user