117 lines
3.0 KiB
TypeScript
117 lines
3.0 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { CreateTicketDto } from './dto/create-ticket.dto';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
import { MailService } from '../mail/mail.service';
|
|
import * as QRCode from 'qrcode'; // Import qrcode library
|
|
|
|
@Injectable()
|
|
export class TicketsService {
|
|
constructor(
|
|
private prisma: PrismaService,
|
|
private mailService: MailService,
|
|
) {}
|
|
|
|
async create(createTicketDto: CreateTicketDto) {
|
|
const event = await this.prisma.event.findUnique({
|
|
where: { id: createTicketDto.eventId },
|
|
});
|
|
|
|
if (!event) {
|
|
throw new NotFoundException(`Event with ID "${createTicketDto.eventId}" not found`);
|
|
}
|
|
|
|
const ticket = await this.prisma.ticket.create({
|
|
data: createTicketDto,
|
|
include: { event: true },
|
|
});
|
|
|
|
try {
|
|
// Generate QR code buffer with ticket ID only
|
|
const qrCodeBuffer = await QRCode.toBuffer(ticket.id, {
|
|
errorCorrectionLevel: 'H', // High error correction
|
|
type: 'png', // Output as PNG image
|
|
width: 250, // QR code width
|
|
color: {
|
|
dark: '#000000', // Black dots
|
|
light: '#FFFFFF', // White background
|
|
},
|
|
});
|
|
|
|
await this.mailService.sendTicket(
|
|
ticket.attendeeEmail,
|
|
ticket.attendeeName,
|
|
ticket.id,
|
|
ticket.event.name,
|
|
qrCodeBuffer, // Pass the QR code buffer
|
|
);
|
|
} catch (error) {
|
|
console.error('Failed to send ticket email:', error);
|
|
// Optionally throw error or just log it.
|
|
// For now, we log it so the ticket creation doesn't fail if email fails (unless strict requirement).
|
|
}
|
|
|
|
return ticket;
|
|
}
|
|
|
|
findAll(eventId: string) {
|
|
return this.prisma.ticket.findMany({
|
|
where: { eventId },
|
|
});
|
|
}
|
|
|
|
findOne(id: string) {
|
|
return this.prisma.ticket.findUnique({
|
|
where: { id },
|
|
include: { event: true },
|
|
});
|
|
}
|
|
|
|
async resendEmail(id: string) {
|
|
const ticket = await this.prisma.ticket.findUnique({
|
|
where: { id },
|
|
include: { event: true },
|
|
});
|
|
|
|
if (!ticket) {
|
|
throw new Error('Ticket not found');
|
|
}
|
|
|
|
try {
|
|
// Generate QR code buffer with ticket ID only
|
|
const qrCodeBuffer = await QRCode.toBuffer(ticket.id, {
|
|
errorCorrectionLevel: 'H',
|
|
type: 'png',
|
|
width: 250,
|
|
color: {
|
|
dark: '#000000',
|
|
light: '#FFFFFF',
|
|
},
|
|
});
|
|
|
|
await this.mailService.sendTicket(
|
|
ticket.attendeeEmail,
|
|
ticket.attendeeName,
|
|
ticket.id,
|
|
ticket.event.name,
|
|
qrCodeBuffer,
|
|
);
|
|
return { success: true, message: 'Email resent successfully' };
|
|
} catch (error) {
|
|
console.error('Failed to resend ticket email:', error);
|
|
throw new Error('Failed to send email');
|
|
}
|
|
}
|
|
|
|
updateStatus(id: string, status: string) {
|
|
return this.prisma.ticket.update({
|
|
where: { id },
|
|
data: { status },
|
|
});
|
|
}
|
|
|
|
remove(id: string) {
|
|
return this.prisma.ticket.delete({
|
|
where: { id },
|
|
});
|
|
}
|
|
} |