Last active
May 30, 2020 04:31
-
-
Save radzserg/84a45002561f7456652b9128aa7a06d8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class DailyAttendanceReport { | |
| public constructor( | |
| private reportGenerator: DailyAttendanceReportGenerator, | |
| private reportSender: DailyAttendanceReportSender | |
| ) {} | |
| public generate = () => { | |
| const report = this.reportGenerator.generate(); | |
| this.reportSender.send(report); | |
| }; | |
| } | |
| class DailyAttendanceReportGenerator { | |
| public constructor(private knex: Knex) {} | |
| public generate = () => { | |
| const since = moment() | |
| .subtract(1, "days") | |
| .startOf("day"); | |
| const till = moment().startOf("day"); | |
| return this.knex("daily_attendance") | |
| .column("DATE(inserted_at)", "COUNT(*)") | |
| .groupByRaw("DATE(inserted_at)") | |
| .where("inserted_at", ">=", since) | |
| .where("inserted_at", "<", till); | |
| }; | |
| } | |
| class DailyAttendanceReportSender { | |
| public constructor( | |
| private knex: Knex, | |
| private viewRenderer: ViewRenderer, | |
| private emailTransport: EmailTransport | |
| ) {} | |
| public send = report => { | |
| const adminEmails = this.knex("users") | |
| .where({ team: "Marketing", role: "ADMIN" }) | |
| .map(admin => admin.email); | |
| const emailBody = this.viewRenderer.render("daily_attendance_report", { | |
| report | |
| }); | |
| adminEmails.forEach(adminEmail => { | |
| this.emailTransport.send( | |
| adminEmail, | |
| "Daily Attendance Report", | |
| emailBody | |
| ); | |
| }); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment