Skip to content

Instantly share code, notes, and snippets.

@meldsza
Forked from rahulCSENITTE/report
Last active August 16, 2020 17:48
Show Gist options
  • Select an option

  • Save meldsza/68ffa31bb309e8c9d853a7602006d6ce to your computer and use it in GitHub Desktop.

Select an option

Save meldsza/68ffa31bb309e8c9d853a7602006d6ce to your computer and use it in GitHub Desktop.

Revisions

  1. meldsza revised this gist Aug 16, 2020. 1 changed file with 32 additions and 47 deletions.
    79 changes: 32 additions & 47 deletions report
    Original file line number Diff line number Diff line change
    @@ -1,50 +1,35 @@
    router.post('/upreport/:ReId', multer.single('reportfile'), function (req, res, next) {
    if (!req.file) {
    req.flash('error', "Report file not selected")
    router.post('/upreport/:ReId', multer.single('reportfile'), async function (req, res, next) {
    try{
    if (!req.file) {
    req.flash('error', "Report file not selected")
    res.redirect('/hospital/report')
    } else {
    let doc = await db.collection("request").doc(req.params.ReId)
    let mailOptions = {
    from: '4nm17cs141@gmail.com',
    to: doc.data().email,
    subject: 'Sending Email using Node.js',
    html: '<h1>Report Gnerated</h1><p>Thank you for using Digilab!</p>',
    attachments: [{
    filename: req.file.originalname,
    path: "./public/reports/" + req.file.originalname
    }]
    }
    mail.sendMail(mailOptions)
    await bucket.upload("./public/reports/" + req.file.originalname, {
    destination: req.params.ReId
    });
    //assuming you are using require('fs/promises') https://nodejs.org/api/fs.html#fs_fs_promises_api
    await fs.unlink("./public/reports/" + req.file.originalname);
    await db.collection("request").doc(req.params.ReId).update({
    "status": 4
    });
    req.flash("success", "Report uploaded for request ID: " + req.params.ReId);
    res.redirect('/hospital/report');
    }
    } catch(e) {
    req.flash('error', "Report file not uploaded")
    res.redirect('/hospital/report')
    } else {
    db.collection("request").doc(req.params.ReId)
    .get()
    .then((doc) => {
    var mailOptions = {
    from: '4nm17cs141@gmail.com',
    to: doc.data().email,
    subject: 'Sending Email using Node.js',
    html: '<h1>Report Gnerated</h1><p>Thank you for using Digilab!</p>',
    attachments: [{
    filename: req.file.originalname,
    path: "./public/reports/" + req.file.originalname

    }]
    }
    mail.sendMail(mailOptions, function (error, info) {
    if (error) {
    console.log(error)
    }
    bucket.upload("./public/reports/" + req.file.originalname, {
    destination: req.params.ReId
    })
    .then(() => {
    fs.unlinkSync("./public/reports/" + req.file.originalname);
    db.collection("request").doc(req.params.ReId).update({
    "status": 4
    })
    .then(() => {
    req.flash("success", "Report uploaded for request ID: " + req.params.ReId);
    res.redirect('/hospital/report');
    })
    .catch(() => {
    req.flash('error', "Report file not uploaded")
    res.redirect('/hospital/report')
    })
    })
    .catch((er) => {
    console.log(er);
    req.flash('error', "Report file not uploaded")
    res.redirect('/hospital/report')
    })
    })

    })
    return;
    }
    });
  2. @rahulCSENITTE rahulCSENITTE created this gist Aug 16, 2020.
    50 changes: 50 additions & 0 deletions report
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    router.post('/upreport/:ReId', multer.single('reportfile'), function (req, res, next) {
    if (!req.file) {
    req.flash('error', "Report file not selected")
    res.redirect('/hospital/report')
    } else {
    db.collection("request").doc(req.params.ReId)
    .get()
    .then((doc) => {
    var mailOptions = {
    from: '4nm17cs141@gmail.com',
    to: doc.data().email,
    subject: 'Sending Email using Node.js',
    html: '<h1>Report Gnerated</h1><p>Thank you for using Digilab!</p>',
    attachments: [{
    filename: req.file.originalname,
    path: "./public/reports/" + req.file.originalname

    }]
    }
    mail.sendMail(mailOptions, function (error, info) {
    if (error) {
    console.log(error)
    }
    bucket.upload("./public/reports/" + req.file.originalname, {
    destination: req.params.ReId
    })
    .then(() => {
    fs.unlinkSync("./public/reports/" + req.file.originalname);
    db.collection("request").doc(req.params.ReId).update({
    "status": 4
    })
    .then(() => {
    req.flash("success", "Report uploaded for request ID: " + req.params.ReId);
    res.redirect('/hospital/report');
    })
    .catch(() => {
    req.flash('error', "Report file not uploaded")
    res.redirect('/hospital/report')
    })
    })
    .catch((er) => {
    console.log(er);
    req.flash('error', "Report file not uploaded")
    res.redirect('/hospital/report')
    })
    })

    })
    }
    });