From 62364baa1d2603beb4e48daaa288ebf105d200e6 Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 24 Apr 2022 19:02:37 +0000 Subject: [PATCH] Don't block main thread waiting for email to build and send --- lib/Test.js | 30 +++++++++++++++--------------- lib/TestResult.js | 18 +++++++++--------- lib/TestTemplate.js | 21 +++++++++++---------- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lib/Test.js b/lib/Test.js index a71c0b0..85f25b7 100644 --- a/lib/Test.js +++ b/lib/Test.js @@ -173,23 +173,23 @@ class Test { mark ); - const parents = await tr.student.getParents(); + const body = 'Your result has been added for ' + + `the test "${this.template.name}" that you ` + + `took on ${this.getDateString()}\n\n` + + `You scored ${mark}/${this.template.maxMark} ` + + `(${tr.percentage}%) which is a grade ` + + `${tr.grade}`; - const email = new EmailBuilder() - .addTo([ tr.student, ...parents ] - .map(u => u.getEmail())) - .setSubject('Stratos - Test result added') - .setBody( - 'Your result has been added for ' + - `the test "${this.template.name}" that you ` + - `took on ${this.getDateString()}\n\n` + - `You scored ${mark}/${this.template.maxMark} ` + - `(${tr.percentage}%) which is a grade ` + - `${tr.grade}` - ); + tr.student.getParents().then(parents => { + const email = new EmailBuilder() + .addTo([ tr.student, ...parents ] + .map(u => u.getEmail())) + .setSubject('Stratos - Test result added') + .setBody(body); - const emailer = new Emailer(); - await emailer.sendEmail(email); + const emailer = new Emailer(); + emailer.sendEmail(email); + }); return tr; } diff --git a/lib/TestResult.js b/lib/TestResult.js index 8c2f8d7..dce42d1 100644 --- a/lib/TestResult.js +++ b/lib/TestResult.js @@ -114,16 +114,16 @@ class TestResult { `(${this.percentage}%) which is a grade ` + `${this.grade}`; - const parents = await this.student.getParents(); + this.student.getParents().then(parents => { + const email = new EmailBuilder() + .addTo([ this.student, ...parents ] + .map(u => u.getEmail())) + .setSubject('Stratos - Test result changed') + .setBody(body); - const email = new EmailBuilder() - .addTo([ this.student, ...parents ] - .map(u => u.getEmail())) - .setSubject('Stratos - Test result changed') - .setBody(body); - - const emailer = new Emailer(); - await emailer.sendEmail(email); + const emailer = new Emailer(); + emailer.sendEmail(email); + }); } get percentage() { diff --git a/lib/TestTemplate.js b/lib/TestTemplate.js index 9d3f9e5..27cb82f 100644 --- a/lib/TestTemplate.js +++ b/lib/TestTemplate.js @@ -139,17 +139,18 @@ class TestTemplate { const t = await new Test(this.#conn, id); - const email = (await EmailBuilder.generateFromClass(c)) - .setSubject('Stratos - New Test') - .setBody( - `New test added for class ${c.name}:\n` + - `Test name: ${this.name}\n` + - `Test date: ${t.getDateString()}\n` + - `Maximum mark: ${this.maxMark}` - ); + const body = `New test added for class ${c.name}:\n` + + `Test name: ${this.name}\n` + + `Test date: ${t.getDateString()}\n` + + `Maximum mark: ${this.maxMark}`; - const emailer = new Emailer(); - await emailer.sendEmail(email); + EmailBuilder.generateFromClass(c).then(email => { + email.setSubject('Stratos - New Test') + .setBody(body); + + const emailer = new Emailer(); + emailer.sendEmail(email); + }); return t; }