106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
import { Qr } from './qr.entity';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { CreateQrDto } from './dto/create-qr.dto';
|
|
import { UpdateQrDto } from './dto/update.qr.dto';
|
|
// @ts-ignore
|
|
import * as QRCode from 'qrcode';
|
|
|
|
@Injectable()
|
|
export class QrService {
|
|
constructor(@InjectRepository(Qr) private qrRepository: Repository<Qr>) {}
|
|
|
|
async generateQRCode(text: string): Promise<string> {
|
|
return await QRCode.toDataURL(text);
|
|
}
|
|
|
|
async generateAsistenciaQR(id_participante: number, id_evento: number): Promise<string> {
|
|
// Crear un objeto JSON que contenga los IDs
|
|
const qrData = {
|
|
id_participante,
|
|
id_evento
|
|
};
|
|
|
|
// Convertir el objeto a una cadena JSON
|
|
const jsonStr = JSON.stringify(qrData);
|
|
|
|
// Generar el código QR con la cadena JSON
|
|
return await QRCode.toDataURL(jsonStr);
|
|
}
|
|
|
|
async generateBuffer(text: string): Promise<Buffer> {
|
|
return await QRCode.toBuffer(text); // Devuelve un buffer para adjuntar en email o stream
|
|
}
|
|
|
|
// Para enviar el QR por email
|
|
/* const qrBuffer = await this.qrService.generateBuffer('Texto a codificar');
|
|
await transporter.sendMail({
|
|
to: 'destinatario@example.com',
|
|
subject: 'Tu código QR',
|
|
html: '<p>Escanea el siguiente código:</p><img src="cid:qrcode"/>',
|
|
attachments: [{
|
|
filename: 'qrcode.png',
|
|
content: qrBuffer,
|
|
cid: 'qrcode',
|
|
}],
|
|
}); */
|
|
|
|
async createQr(qr: CreateQrDto) {
|
|
const qrFound = await this.qrRepository.findOne({
|
|
where: {
|
|
id_qr: qr.id_participante_evento,
|
|
},
|
|
});
|
|
|
|
if (qrFound) {
|
|
return new HttpException('Qr already exists', HttpStatus.CONFLICT);
|
|
}
|
|
|
|
return this.qrRepository.save(qr);
|
|
}
|
|
|
|
getQrs() {
|
|
return this.qrRepository.find({});
|
|
}
|
|
|
|
async getQr(id_qr) {
|
|
const qrFound = await this.qrRepository.findOne({
|
|
where: {
|
|
id_qr,
|
|
},
|
|
});
|
|
|
|
if (!qrFound) {
|
|
return new HttpException('User not found', HttpStatus.NOT_FOUND);
|
|
}
|
|
|
|
return qrFound;
|
|
}
|
|
|
|
async deleteQr(id_qr: number) {
|
|
const result = await this.qrRepository.delete({ id_qr });
|
|
|
|
if (result.affected === 0) {
|
|
return new HttpException('User not found', HttpStatus.NOT_FOUND);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
async updateQr(id_qr: number, qr: UpdateQrDto) {
|
|
const qrFound = await this.qrRepository.findOne({
|
|
where: {
|
|
id_qr,
|
|
},
|
|
});
|
|
|
|
if (!qrFound) {
|
|
return new HttpException('User not found', HttpStatus.NOT_FOUND);
|
|
}
|
|
|
|
const updateQr = Object.assign(qrFound, qr);
|
|
return this.qrRepository.save(updateQr);
|
|
}
|
|
}
|