Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
34 changes: 34 additions & 0 deletions src/email/email-report.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class EmailReportService {
constructor(private readonly prisma: PrismaService) {}

async recordDelivery(emailId: string, status: 'delivered' | 'bounced' | 'opened' | 'clicked') {
return this.prisma.emailReport.create({
data: {
emailId,
delivered: status === 'delivered',
bounced: status === 'bounced',
opened: status === 'opened',
clicked: status === 'clicked',
},
});
}

async getMetrics() {
const total = await this.prisma.emailReport.count();
const delivered = await this.prisma.emailReport.count({ where: { delivered: true } });
const bounced = await this.prisma.emailReport.count({ where: { bounced: true } });
const opened = await this.prisma.emailReport.count({ where: { opened: true } });
const clicked = await this.prisma.emailReport.count({ where: { clicked: true } });

return {
deliveryRate: delivered / total,
bounceRate: bounced / total,
openRate: opened / total,
clickRate: clicked / total,
};
}
}
12 changes: 12 additions & 0 deletions src/email/email.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { EmailReportService } from './email-report.service';

@Controller('emails')
export class EmailController {
constructor(private readonly reportService: EmailReportService) {}

@Get('reports')
async getReports() {
return this.reportService.getMetrics();
}
}
Empty file.
Empty file.
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions src/utils/observability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function logEmailEvent(emailId: string, event: string, correlationId?: string) {
console.log(JSON.stringify({
level: 'info',
type: 'email',
emailId,
event,
correlationId,
timestamp: new Date().toISOString(),
}));
}
Loading