Skip to content

Commit

Permalink
add locker download
Browse files Browse the repository at this point in the history
  • Loading branch information
birdpump committed Jul 20, 2024
1 parent 27a10ec commit 165d1b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
19 changes: 12 additions & 7 deletions routes/admin/adminData.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,17 @@ adminRouter.post('/db-action/clear-users', async (req, res, next) => {
adminRouter.get('/csv-action/gen-locker-csv', async (req, res, next) => {
try {
await generateLockerCSV();
res.sendStatus(200);
const filePath = path.join(process.cwd(), 'data-temp/locker.csv');

setTimeout(() => {
res.download(filePath, 'locker.csv', (err) => {
if (err) {
console.error('Error sending file:', err);
res.status(500).send('Error downloading file');
}
});
}, 500); //add a delay to prevent fuck-ups

} catch (error) {
next(error);
}
Expand All @@ -233,11 +243,7 @@ adminRouter.get('/csv-action/gen-locker-csv', async (req, res, next) => {
adminRouter.get('/csv-action/gen-user-csv', async (req, res, next) => {
try {
await generateUserCSV();
console.log("Generated CSV");

const filePath = path.join(process.cwd(), 'data-temp/user.csv');
console.log("File path:", filePath);


setTimeout(() => {
res.download(filePath, 'users.csv', (err) => {
Expand All @@ -246,10 +252,9 @@ adminRouter.get('/csv-action/gen-user-csv', async (req, res, next) => {
res.status(500).send('Error downloading file');
}
});
}, 2000); // todo see if this actually fixed the issue
}, 500); //add a delay to prevent fuck-ups

} catch (error) {
console.error('Error generating CSV:', error);
next(error);
}
});
Expand Down
48 changes: 21 additions & 27 deletions utils/admin/csvgen/generateCSV.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {createObjectCsvWriter} from "csv-writer";

//generate a csv from database matching import format for the locker locator
export async function generateLockerCSV() {

Locker.findAll().then(lockers => {
Locker.findAll().then(async lockers => {
const filteredData = lockers.map(locker => ({
Num: locker.lockerNumber,
Building: locker.location.Building,
Expand All @@ -27,7 +26,7 @@ export async function generateLockerCSV() {
],
});

csvWriter.writeRecords(filteredData);
await csvWriter.writeRecords(filteredData);
});
}

Expand All @@ -36,31 +35,26 @@ export async function generateLockerCSV() {
//todo im not sure how useful is is to export userData, better to export Users in the format of UserData, to be able to import as as student locator
//generate a csv from database matching import format for the student locator
export async function generateUserCSV() {
try {
User.findAll().then(async users => {
const filteredData = users.map(user => ({
studentId: user.studentId,
name: user.name,
grade: user.grade,
email: user.email,
permissions: user.permissions
}));

const csvWriter = createObjectCsvWriter({
path: './data-temp/user.csv',
header: [
{id: 'studentId', title: 'studentId'},
{id: 'name', title: 'name'},
{id: 'grade', title: 'grade'},
{id: 'email', title: 'email'},
{id: 'permissions', title: 'permissions'},
],
});
User.findAll().then(async users => {
const filteredData = users.map(user => ({
studentId: user.studentId,
name: user.name,
grade: user.grade,
email: user.email,
permissions: user.permissions
}));

await csvWriter.writeRecords(filteredData);
const csvWriter = createObjectCsvWriter({
path: './data-temp/user.csv',
header: [
{id: 'studentId', title: 'studentId'},
{id: 'name', title: 'name'},
{id: 'grade', title: 'grade'},
{id: 'email', title: 'email'},
{id: 'permissions', title: 'permissions'},
],
});
} catch (error) {
console.log(error);
}

await csvWriter.writeRecords(filteredData);
});
}

0 comments on commit 165d1b7

Please sign in to comment.