53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { Injectable, NotFoundException } from "@nestjs/common";
|
|
import QR from "qrcode";
|
|
import { PrismaService } from "../../prisma/prisma.service";
|
|
|
|
@Injectable()
|
|
export class QrcodeService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
findAll(tenantId: string) {
|
|
return this.prisma.qRCode.findMany({
|
|
where: { tenantId },
|
|
orderBy: { createdAt: "desc" },
|
|
include: {
|
|
registration: { include: { attendee: true, event: true } }
|
|
}
|
|
});
|
|
}
|
|
|
|
async createForRegistration(tenantId: string, registrationId: string) {
|
|
const registration = await this.prisma.registration.findFirst({
|
|
where: { id: registrationId, tenantId },
|
|
include: { attendee: true, event: true }
|
|
});
|
|
if (!registration) throw new NotFoundException("Registration not found");
|
|
|
|
const payload = {
|
|
code: registration.code,
|
|
eventId: registration.eventId,
|
|
tenantId: registration.tenantId,
|
|
registrationId: registration.id
|
|
};
|
|
|
|
const qrCode = await this.prisma.qRCode.upsert({
|
|
where: { registrationId: registration.id },
|
|
create: {
|
|
tenantId,
|
|
registrationId: registration.id,
|
|
code: registration.code,
|
|
payload
|
|
},
|
|
update: {
|
|
code: registration.code,
|
|
payload,
|
|
status: "active"
|
|
},
|
|
include: { registration: { include: { attendee: true, event: true } } }
|
|
});
|
|
|
|
const dataUrl = await QR.toDataURL(JSON.stringify(payload), { errorCorrectionLevel: "M", margin: 1, width: 320 });
|
|
return { qrCode, dataUrl };
|
|
}
|
|
}
|