Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate daily events fixed #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 62 additions & 59 deletions controllers/eventController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ function getRoomList (rooms) {
roomlist = roomlist.replace(/,\s*$/, '')
return roomlist
}

function snapshotToArray (snapshot) {
var returnArr = []

snapshot.forEach(function (childSnapshot) {
var item = childSnapshot.val()
returnArr.push(item)
})

return returnArr
}

exports.generate_pdf = function (req, res) {
var eventID = req.query.eventID
var filename = `${eventID}.pdf`
Expand Down Expand Up @@ -118,26 +130,12 @@ exports.generate_pdf = function (req, res) {
}

exports.generate_daily_events = function (req, res) {
function snapshotToArray (snapshot) {
var returnArr = []

snapshot.forEach(function (childSnapshot) {
var item = childSnapshot.val()
returnArr.push(item)
})

return returnArr
}
var date = req.query.date
var filename = path.join(__dirname, `events-${date}.pdf`)
console.log(date)
var eventsRef = admin.database().ref('to-be-held/' + date)
var eventRef = admin.database().ref()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventRef is a misleading variable name in this case. Something like firebaseDB would be more appropriate.

// console.log(eventsRef);
eventsRef.once('value', function (snapshot) {
// if(snapshot.val()===null) {
// console.log("No events Booked for this day")
// }
var html
var eventID = snapshotToArray(snapshot)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could check the length of the array and respond if it is 0 here itself instead of using the counter i.

var eventCount = eventID.length
Expand All @@ -150,56 +148,61 @@ exports.generate_daily_events = function (req, res) {
let roomlist
eventID.forEach(function (element) {
eventRef.child('events/' + element).once('value', function (snapshot) {
console.log(snapshot.val().clubName)
console.log(element)
let rooms = snapshot.child('rooms/').val()
roomlist = getRoomList(rooms)
let eventObj = []
eventObj.push(element)
eventObj.push(snapshot.val().clubName)
eventObj.push(roomlist)
eventarr.push(eventObj)
if (snapshot.val().SO_appr === 'approved') {
let rooms = snapshot.child('rooms/').val()
roomlist = getRoomList(rooms)
let eventObj = []
eventObj.push(element)
eventObj.push(snapshot.val().clubName)
eventObj.push(roomlist)
eventarr.push(eventObj)
}
i++
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try not to use the counter.

if (i === eventCount) {
console.log(eventarr)
ejs.renderFile(path.join(__dirname, '/dailyeventpdf.ejs'), {
events: eventarr,
date: date
}, function (err, result) {
if (result) {
html = result
} else {
res.end('An error occurred')
console.log(err)
if (eventarr.length === 0) {
console.log('No approved events for this day')
res.status(200).send('No approved events')
} else {
ejs.renderFile(path.join(__dirname, '/dailyeventpdf.ejs'), {
events: eventarr,
date: date
}, function (err, result) {
if (result) {
html = result
} else {
res.end('An error occurred')
console.log(err)
}
})
var options = {
filename: filename,
height: '870px',
width: '650px',
orientation: 'portrait',
type: 'pdf',
timeout: '30000',
border: '10'
}
})
var options = {
filename: filename,
height: '870px',
width: '650px',
orientation: 'portrait',
type: 'pdf',
timeout: '30000',
border: '10'
}

pdf.create(html, options).toFile(function (err, result) {
if (err) {
console.log(err)
} else {
res.download(path.join(filename), function (err, result) {
if (err) {
console.log('Error downloading file: ' + err)
} else {
console.log('File downloaded successfully')
fs.unlink(filename, (err) => {
if (err) throw err
console.log(filename + ' was deleted from local server')
})
}
})
}
})
pdf.create(html, options).toFile(function (err, result) {
if (err) {
console.log(err)
} else {
res.download(path.join(filename), function (err, result) {
if (err) {
console.log('Error downloading file: ' + err)
} else {
console.log('File downloaded successfully')
fs.unlink(filename, (err) => {
if (err) throw err
console.log(filename + ' was deleted from local server')
})
}
})
}
})
}
}
})
})
Expand Down