Loading gists…
A Text snippet, saved for reference.
| 1 | import prisma from '../config/prisma.ts'; |
| 2 | import QRCode from 'qrcode'; |
| 3 | import { v4 as uuidv4 } from 'uuid'; |
| 4 | import path from 'path'; |
| 5 | import fs from 'fs'; |
| 6 | import { AppError } from '../utils/appError.ts'; |
| 7 | import { TableType } from '@prisma/client'; |
| 8 | |
| 9 | export const generateQRService = async ({ tableCode }: { tableCode: string }) => { |
| 10 | if (!tableCode) throw new AppError('Table code is required', 400); |
| 11 | |
| 12 | // Check if table exists |
| 13 | let table = await prisma.table.findUnique({ |
| 14 | where: { tableCode }, |
| 15 | }); |
| 16 | |
| 17 | // Create if not exists |
| 18 | if (!table) { |
| 19 | table = await prisma.table.create({ |
| 20 | data: { |
| 21 | tableCode, |
| 22 | tableType: 'PHYSICAL', |
| 23 | qrToken: uuidv4(), |
| 24 | }, |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | // Format: FRONTEND_URL/menu?table=tableCode |
| 29 | const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:3000'; |
| 30 | const qrData = `${frontendUrl}/menu?table=${table.tableCode}`; |
| 31 | |
| 32 | // Generate QR Code File |
| 33 | const qrFileName = `${table.tableCode}.png`; |
| 34 | const qrDir = path.join(process.cwd(), 'public', 'qrcodes'); |
| 35 | const qrFilePath = path.join(qrDir, qrFileName); |
| 36 | |
| 37 | // Ensure directory exists |
| 38 | if (!fs.existsSync(qrDir)) { |
| 39 | fs.mkdirSync(qrDir, { recursive: true }); |
| 40 | } |
| 41 | |
| 42 | // Save to file |
| 43 | await QRCode.toFile(qrFilePath, qrData); |
| 44 | |
| 45 | // URL to access the file |
| 46 | const qrImage = `/public/qrcodes/${qrFileName}`; |
| 47 | |
| 48 | return { |
| 49 | message: 'QR Code generated successfully', |
| 50 | table: { |
| 51 | id: table.id, |
| 52 | tableCode: table.tableCode, |
| 53 | qrToken: table.qrToken, |
| 54 | qrCodeData: qrData, |
| 55 | qrImage, |
| 56 | }, |
| 57 | }; |
| 58 | }; |
| 59 | |
| 60 | export const getTableInfoService = async (tableCode: string) => { |
| 61 | if (!tableCode) throw new AppError('Table ID/Code is required', 400); |
| 62 | |
| 63 | // Try finding by tableCode first |
| 64 | const table = await prisma.table.findUnique({ |
| 65 | where: { tableCode }, |
| 66 | }); |
| 67 | |
| 68 | if (!table) throw new AppError(`Table with code '${tableCode}' not found`, 404); |
| 69 | |
| 70 | if (!table.isActive) throw new AppError('Table is currently inactive', 400); |
| 71 | |
| 72 | return { |
| 73 | table, |
| 74 | }; |
| 75 | }; |
| 76 | |
| 77 | export const initVirtualTableService = async (data: { type: TableType; identifier: string }) => { |
| 78 | const { type, identifier } = data; |
| 79 | if (!identifier) throw new AppError('Identifier (Name or Mobile) is required', 400); |
| 80 | |
| 81 | let table = await prisma.table.findUnique({ |
| 82 | where: { tableCode: identifier }, |
| 83 | }); |
| 84 | |
| 85 | if (!table) { |
| 86 | table = await prisma.table.create({ |
| 87 | data: { |
| 88 | tableCode: identifier, |
| 89 | tableType: type, |
| 90 | qrToken: uuidv4(), |
| 91 | }, |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | return { table }; |
| 96 | }; |
| 97 |