feat(backend): QR-Code-System (Modul 22)
QrModule mit GET /qr/:entityType/:id.svg fuer colonies (Deep-Link direkt zu /inspections/new), apiaries, queens, honey-batches und storage-containers. Nutzt pdfkit-Nachbar qrcode zur SVG-Erzeugung.
This commit is contained in:
1 parent
a7ded02015
commit
dd88b6a92c
9 files changed
+427
-15
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import {
|
||||
Injectable,
|
||||
BadRequestException,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import * as QRCode from 'qrcode';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
const ENTITY_TYPES = [
|
||||
'colonies',
|
||||
'apiaries',
|
||||
'queens',
|
||||
'honey-batches',
|
||||
'storage-containers',
|
||||
] as const;
|
||||
type EntityType = (typeof ENTITY_TYPES)[number];
|
||||
|
||||
function isEntityType(value: string): value is EntityType {
|
||||
return (ENTITY_TYPES as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class QrService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async generateSvg(entityType: string, id: string): Promise<string> {
|
||||
if (!isEntityType(entityType)) {
|
||||
throw new BadRequestException(
|
||||
`Unsupported QR entity type: ${entityType}`,
|
||||
);
|
||||
}
|
||||
|
||||
const exists = await this.entityExists(entityType, id);
|
||||
if (!exists) {
|
||||
throw new NotFoundException('Object not found');
|
||||
}
|
||||
|
||||
const frontendOrigin =
|
||||
process.env.FRONTEND_ORIGIN ?? 'http://localhost:3000';
|
||||
const url = `${frontendOrigin}${this.deepLink(entityType, id)}`;
|
||||
return QRCode.toString(url, { type: 'svg', margin: 1, width: 256 });
|
||||
}
|
||||
|
||||
private async entityExists(
|
||||
entityType: EntityType,
|
||||
id: string,
|
||||
): Promise<boolean> {
|
||||
switch (entityType) {
|
||||
case 'colonies':
|
||||
return (
|
||||
(await this.prisma.colony.findUnique({ where: { id } })) !== null
|
||||
);
|
||||
case 'apiaries':
|
||||
return (
|
||||
(await this.prisma.apiary.findUnique({ where: { id } })) !== null
|
||||
);
|
||||
case 'queens':
|
||||
return (await this.prisma.queen.findUnique({ where: { id } })) !== null;
|
||||
case 'honey-batches':
|
||||
return (
|
||||
(await this.prisma.honeyBatch.findUnique({ where: { id } })) !== null
|
||||
);
|
||||
case 'storage-containers':
|
||||
return (
|
||||
(await this.prisma.storageContainer.findUnique({ where: { id } })) !==
|
||||
null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private deepLink(entityType: EntityType, id: string): string {
|
||||
switch (entityType) {
|
||||
case 'colonies':
|
||||
return `/inspections/new?colonyId=${id}`;
|
||||
case 'apiaries':
|
||||
return `/apiaries/${id}`;
|
||||
case 'queens':
|
||||
return `/queens/${id}`;
|
||||
case 'honey-batches':
|
||||
return `/honey-batches/${id}`;
|
||||
case 'storage-containers':
|
||||
return `/storage`;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user