Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Implemented CRON job sending reports every week

Yevhenii Kulisidi7fe3c823c350
diff --git a/src/task-manager/task-manager.service.ts b/src/task-manager/task-manager.service.ts
index d75f9398f..0e416a85d 100644
--- a/src/task-manager/task-manager.service.ts
+++ b/src/task-manager/task-manager.service.ts
@@ -723,6 +723,38 @@ export class TaskManagerService {
@Cron(CronExpression.EVERY_WEEK)
async handleWeeklyReports(): Promise<void> {
- // TODO: Implement
+ const subscribers = await this.projectService.getSubscribersForReports(
+ ReportFrequency.WEEKLY,
+ )
+ const now = dayjs.utc().format('DD.MM.YYYY')
+ const weekAgo = dayjs.utc().subtract(1, 'w').format('DD.MM.YYYY')
+ const date = `${weekAgo} - ${now}`
+ const tip = getRandomTip()
+
+ for (const subscriber of subscribers) {
+ const projects = await this.projectService.getSubscriberProjects(
+ subscriber.id,
+ )
+
+ const ids = projects.map(project => project.id)
+ const data = await this.analyticsService.getSummary(ids, 'w')
+
+ const result = {
+ type: 'w', // week
+ date,
+ projects: _map(ids, (pid, index) => ({
+ data: data[pid],
+ name: projects[index].name,
+ })),
+ tip,
+ }
+
+ await this.mailerService.sendEmail(
+ subscriber.email,
+ LetterTemplate.ProjectReport,
+ result,
+ 'broadcast',
+ )
+ }
}
}