|
| 1 | +'use server' |
| 2 | + |
| 3 | +import { prisma } from "@/lib/prisma" |
| 4 | +import { z } from "zod" |
| 5 | +import { revalidatePath } from "next/cache" |
| 6 | + |
| 7 | +// Schema validation for Kiosk Booking |
| 8 | +const kioskBookingSchema = z.object({ |
| 9 | + name: z.string().min(2, "Nome é obrigatório (mínimo 2 caracteres)"), |
| 10 | + phone: z.string().optional(), |
| 11 | + instagram: z.string().optional(), |
| 12 | + artistId: z.string().min(1, "Selecione um artista"), |
| 13 | + scheduledFor: z.string().refine((val) => !isNaN(Date.parse(val)), "Data inválida"), |
| 14 | + duration: z.number().min(30, "Duração mínima é de 30 minutos").max(480, "Duração máxima de 8 horas"), |
| 15 | + type: z.string().min(2, "Tipo de trabalho é obrigatório"), |
| 16 | + notes: z.string().optional(), |
| 17 | + honeypot: z.string().optional(), |
| 18 | +}).refine((data) => { |
| 19 | + const hasPhone = data.phone && data.phone.trim().length > 0; |
| 20 | + const hasInstagram = data.instagram && data.instagram.trim().length > 0; |
| 21 | + return hasPhone || hasInstagram; |
| 22 | +}, { |
| 23 | + message: "Forneça pelo menos um meio de contato: Instagram ou Telefone", |
| 24 | + path: ["instagram"], |
| 25 | +}); |
| 26 | + |
| 27 | +/** |
| 28 | + * Retrieve all active artists associated with a workspace for the Kiosk selection. |
| 29 | + */ |
| 30 | +export async function getKioskArtists() { |
| 31 | + try { |
| 32 | + const artists = await prisma.artist.findMany({ |
| 33 | + where: { |
| 34 | + isActive: true, |
| 35 | + workspaceId: { not: null } |
| 36 | + }, |
| 37 | + include: { |
| 38 | + user: { |
| 39 | + select: { |
| 40 | + name: true, |
| 41 | + image: true, |
| 42 | + } |
| 43 | + }, |
| 44 | + workspace: { |
| 45 | + select: { |
| 46 | + id: true, |
| 47 | + name: true, |
| 48 | + primaryColor: true, |
| 49 | + } |
| 50 | + } |
| 51 | + }, |
| 52 | + orderBy: { |
| 53 | + user: { |
| 54 | + name: 'asc' |
| 55 | + } |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + return { success: true, artists } |
| 60 | + } catch (error) { |
| 61 | + console.error('Error fetching kiosk artists:', error) |
| 62 | + return { success: false, error: 'Erro ao buscar artistas do estúdio' } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Calculates available time slots for a specific artist on a given date. |
| 68 | + * Business hours: 09:00 to 20:00 (America/Sao_Paulo). |
| 69 | + */ |
| 70 | +export async function getKioskAvailableSlots(artistId: string, dateStr: string, durationMin: number) { |
| 71 | + try { |
| 72 | + if (!artistId || !dateStr) { |
| 73 | + return { success: false, error: 'Dados insuficientes' } |
| 74 | + } |
| 75 | + |
| 76 | + const artist = await prisma.artist.findUnique({ |
| 77 | + where: { id: artistId }, |
| 78 | + include: { workspace: true } |
| 79 | + }) |
| 80 | + |
| 81 | + if (!artist || !artist.workspaceId || !artist.workspace) { |
| 82 | + return { success: false, error: 'Artista ou Workspace não encontrado' } |
| 83 | + } |
| 84 | + |
| 85 | + const workspace = artist.workspace |
| 86 | + const capacity = workspace.capacity || 3 |
| 87 | + |
| 88 | + // Fetch all bookings for the artist on the selected date |
| 89 | + const dayStart = new Date(`${dateStr}T00:00:00-03:00`) |
| 90 | + const dayEnd = new Date(`${dateStr}T23:59:59-03:00`) |
| 91 | + |
| 92 | + // All active bookings for the artist today |
| 93 | + const artistBookings = await prisma.booking.findMany({ |
| 94 | + where: { |
| 95 | + artistId, |
| 96 | + status: { in: ['OPEN', 'CONFIRMED', 'COMPLETED'] }, |
| 97 | + scheduledFor: { |
| 98 | + gte: dayStart, |
| 99 | + lte: dayEnd |
| 100 | + } |
| 101 | + }, |
| 102 | + include: { |
| 103 | + slot: true |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + // All active slots in the workspace today to check studio capacity |
| 108 | + const workspaceSlots = await prisma.slot.findMany({ |
| 109 | + where: { |
| 110 | + workspaceId: artist.workspaceId, |
| 111 | + isActive: true, |
| 112 | + startTime: { |
| 113 | + gte: dayStart, |
| 114 | + lte: dayEnd |
| 115 | + } |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + // Business Hours slots: 09:00 to 19:00 hourly |
| 120 | + const hours = ['09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00'] |
| 121 | + const availableSlots: string[] = [] |
| 122 | + |
| 123 | + for (const hr of hours) { |
| 124 | + const slotStart = new Date(`${dateStr}T${hr}:00-03:00`) |
| 125 | + const slotEnd = new Date(slotStart.getTime() + durationMin * 60 * 1000) |
| 126 | + |
| 127 | + // 1. Check if artist has a conflict |
| 128 | + const hasArtistConflict = artistBookings.some((booking) => { |
| 129 | + const bStart = new Date(booking.scheduledFor) |
| 130 | + const bEnd = new Date(bStart.getTime() + booking.duration * 60 * 1000) |
| 131 | + return slotStart < bEnd && slotEnd > bStart |
| 132 | + }) |
| 133 | + |
| 134 | + if (hasArtistConflict) continue |
| 135 | + |
| 136 | + // 2. Check if workspace capacity is exceeded |
| 137 | + const activeSlotsInInterval = workspaceSlots.filter((slot) => { |
| 138 | + const sStart = new Date(slot.startTime) |
| 139 | + const sEnd = new Date(slot.endTime) |
| 140 | + return slotStart < sEnd && slotEnd > sStart |
| 141 | + }) |
| 142 | + |
| 143 | + if (activeSlotsInInterval.length >= capacity) continue |
| 144 | + |
| 145 | + // If no conflict, slot is free |
| 146 | + availableSlots.push(hr) |
| 147 | + } |
| 148 | + |
| 149 | + return { success: true, slots: availableSlots } |
| 150 | + } catch (error) { |
| 151 | + console.error('Error calculating available slots:', error) |
| 152 | + return { success: false, error: 'Erro ao calcular horários livres' } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * Creates a booking request from the public Kiosk form. |
| 158 | + */ |
| 159 | +export async function createKioskBooking(rawData: any) { |
| 160 | + // Anti-bot Protection: Honeypot field |
| 161 | + if (rawData.honeypot && rawData.honeypot.trim().length > 0) { |
| 162 | + console.warn('🤖 Bot detected in honeypot field. Silent rejection.') |
| 163 | + return { success: true, message: 'Agendamento recebido, aguardando confirmação' } |
| 164 | + } |
| 165 | + |
| 166 | + // Zod Validation |
| 167 | + const result = kioskBookingSchema.safeParse(rawData) |
| 168 | + if (!result.success) { |
| 169 | + return { success: false, error: result.error.issues[0].message } |
| 170 | + } |
| 171 | + |
| 172 | + const data = result.data |
| 173 | + const phone = data.phone?.trim() |
| 174 | + const instagram = data.instagram?.trim() |
| 175 | + |
| 176 | + try { |
| 177 | + // 1. Look up artist and workspace details |
| 178 | + const artist = await prisma.artist.findUnique({ |
| 179 | + where: { id: data.artistId }, |
| 180 | + include: { workspace: true, user: true } |
| 181 | + }) |
| 182 | + |
| 183 | + if (!artist || !artist.workspaceId) { |
| 184 | + return { success: false, error: 'Artista selecionado não possui estúdio associado.' } |
| 185 | + } |
| 186 | + |
| 187 | + // 2. Find or Create Client |
| 188 | + let client = null |
| 189 | + |
| 190 | + // Match criteria: Telefone or email derived from Instagram |
| 191 | + const instagramEmail = instagram ? `${instagram.replace('@', '').toLowerCase()}@instagram.com` : undefined |
| 192 | + |
| 193 | + if (phone) { |
| 194 | + client = await prisma.user.findFirst({ |
| 195 | + where: { phone, role: 'CLIENT' } |
| 196 | + }) |
| 197 | + } |
| 198 | + |
| 199 | + if (!client && instagramEmail) { |
| 200 | + client = await prisma.user.findFirst({ |
| 201 | + where: { email: instagramEmail, role: 'CLIENT' } |
| 202 | + }) |
| 203 | + } |
| 204 | + |
| 205 | + if (!client) { |
| 206 | + // Create a sovereign CLIENT user profile |
| 207 | + client = await prisma.user.create({ |
| 208 | + data: { |
| 209 | + name: data.name, |
| 210 | + phone: phone || null, |
| 211 | + email: instagramEmail || null, |
| 212 | + role: 'CLIENT' |
| 213 | + } |
| 214 | + }) |
| 215 | + console.log(`✅ [Kiosk] Novo cliente cadastrado: ${client.name}`) |
| 216 | + } |
| 217 | + |
| 218 | + // 3. Allocate a free table/maca in the workspace |
| 219 | + const scheduledStart = new Date(data.scheduledFor) |
| 220 | + const scheduledEnd = new Date(scheduledStart.getTime() + data.duration * 60 * 1000) |
| 221 | + |
| 222 | + const workspaceSlots = await prisma.slot.findMany({ |
| 223 | + where: { |
| 224 | + workspaceId: artist.workspaceId, |
| 225 | + isActive: true, |
| 226 | + startTime: { lt: scheduledEnd }, |
| 227 | + endTime: { gt: scheduledStart } |
| 228 | + } |
| 229 | + }) |
| 230 | + |
| 231 | + const capacity = artist.workspace?.capacity || 3 |
| 232 | + const occupiedMacas = new Set(workspaceSlots.map(s => s.macaId)) |
| 233 | + |
| 234 | + let allocatedMacaId = null |
| 235 | + for (let m = 1; m <= capacity; m++) { |
| 236 | + if (!occupiedMacas.has(m)) { |
| 237 | + allocatedMacaId = m |
| 238 | + break |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + if (!allocatedMacaId) { |
| 243 | + return { success: false, error: 'Não há macas disponíveis no horário escolhido. Por favor, selecione outro horário.' } |
| 244 | + } |
| 245 | + |
| 246 | + // Create the Slot |
| 247 | + const slot = await prisma.slot.create({ |
| 248 | + data: { |
| 249 | + workspaceId: artist.workspaceId, |
| 250 | + macaId: allocatedMacaId, |
| 251 | + startTime: scheduledStart, |
| 252 | + endTime: scheduledEnd, |
| 253 | + isActive: true |
| 254 | + } |
| 255 | + }) |
| 256 | + |
| 257 | + // Create the Booking |
| 258 | + // status: OPEN, value: 0 (artist defines rate later) |
| 259 | + const booking = await prisma.booking.create({ |
| 260 | + data: { |
| 261 | + artistId: artist.id, |
| 262 | + clientId: client.id, |
| 263 | + workspaceId: artist.workspaceId, |
| 264 | + slotId: slot.id, |
| 265 | + value: 0, |
| 266 | + discountValue: 0, |
| 267 | + finalValue: 0, |
| 268 | + studioShare: 0, |
| 269 | + artistShare: 0, |
| 270 | + status: 'OPEN', |
| 271 | + type: data.type, |
| 272 | + scheduledFor: scheduledStart, |
| 273 | + duration: data.duration, |
| 274 | + notes: data.notes || '', |
| 275 | + syncedToGoogle: false |
| 276 | + } |
| 277 | + }) |
| 278 | + |
| 279 | + console.log(`✅ [Kiosk] Agendamento criado com sucesso. ID: ${booking.id}`) |
| 280 | + |
| 281 | + // Create Kiosk Entry logging |
| 282 | + await prisma.kioskEntry.create({ |
| 283 | + data: { |
| 284 | + workspaceId: artist.workspaceId, |
| 285 | + name: data.name, |
| 286 | + phone: phone || null, |
| 287 | + instagram: instagram || null, |
| 288 | + type: 'CLIENT', |
| 289 | + intent: data.type, |
| 290 | + artistId: artist.id, |
| 291 | + marketingOptIn: true |
| 292 | + } |
| 293 | + }) |
| 294 | + |
| 295 | + revalidatePath('/artist/agenda') |
| 296 | + |
| 297 | + return { success: true, message: 'Agendamento recebido, aguardando confirmação' } |
| 298 | + } catch (error: any) { |
| 299 | + console.error('Error creating Kiosk booking:', error) |
| 300 | + return { success: false, error: error.message || 'Erro ao processar agendamento' } |
| 301 | + } |
| 302 | +} |
0 commit comments