-
Notifications
You must be signed in to change notification settings - Fork 17
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
PriyamAgrawal
wants to merge
2
commits into
cribbl:master
Choose a base branch
from
PriyamAgrawal:daily-events
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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` | ||
|
@@ -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() | ||
// 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
var eventCount = eventID.length | ||
|
@@ -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++ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
}) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 likefirebaseDB
would be more appropriate.