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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions controllers/dailyeventpdf.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ th {
<center>
<div class="header">
<img class="left" style="max-width: 300px;" src="https://firebasestorage.googleapis.com/v0/b/mit-clubs-management.appspot.com/o/mit-logo.jpg?alt=media&token=06f3d93b-6218-458f-b137-89ab1f78bdf3">

<img class="right" src="https://firebasestorage.googleapis.com/v0/b/mit-clubs-management.appspot.com/o/Fq10VDgTdAf7t4o4sEUrgY08rGg2%2FprofilePic?alt=media&token=4545a6cc-524e-4e24-9c75-7b022ba6ccd2">
</div>

<div class="label">
Expand Down
85 changes: 47 additions & 38 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 @@ -117,51 +129,46 @@ 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
}
exports.generate_daily_events = async function (req, res) {
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()
// 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)
var eventCount = eventID.length
var i = 0
if (eventCount === 0) {
console.log('No events booked for this day')
res.status(200).send('No Events Booked')
if (snapshot.val() === null) {
console.log('no events are booked')
res.status(200).send('No events are booked at this day')
} else {
var html
var eventID = snapshotToArray(snapshot)
var firebaseDB = admin.database().ref()
let eventarr = []
let roomlist
const promises = []
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)
i++
if (i === eventCount) {
console.log(eventarr)
const p = firebaseDB.child('events/' + element).once('value', function (snapshot) {
if (snapshot.val() === null) {
console.log('Event does not exist')
} else {
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)
}
}
})
promises.push(p)
})
Promise.all(promises)
.then(() => {
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
Expand All @@ -182,7 +189,6 @@ exports.generate_daily_events = function (req, res) {
timeout: '30000',
border: '10'
}

pdf.create(html, options).toFile(function (err, result) {
if (err) {
console.log(err)
Expand All @@ -202,7 +208,10 @@ exports.generate_daily_events = function (req, res) {
})
}
})
})
.catch(error => {
console.log(error)
res.status(500).send('there was some error')
})
}
})
}
Expand Down