Files
shahikitchen-prod/application/messaging/whatsapp-message-builder.ts
T

93 lines
2.4 KiB
TypeScript

import type { BookingDetails } from '@/domain/booking/entities';
import { isBookingComplete } from '@/domain/booking/validation';
import type { Language } from '@/domain/language/entities';
import {
calculateOrderTotal,
formatOrderLinesForMessage,
type OrderLine,
} from '@/domain/shared/order-line';
import { PICKUP_LEAD_TIME_MINUTES } from '@/domain/shared/constants';
export interface CartDrawerCopy {
messageHello: string;
messageIntro: string;
messageConfirm: string;
messageTotal: string;
messageThanks: string;
}
export interface BookingLocationCopy {
askim: string;
backaplan: string;
}
export function getSuggestedPickupTime(language: Language, from: Date = new Date()): string {
const suggested = new Date(from.getTime() + PICKUP_LEAD_TIME_MINUTES * 60 * 1000);
const timeLocale =
language === 'sv' ? 'sv-SE' :
language === 'ar' ? 'ar-SA' :
language === 'tr' ? 'tr-TR' :
'en-GB';
return suggested.toLocaleTimeString(timeLocale, { hour: '2-digit', minute: '2-digit' });
}
export function buildCartWhatsAppMessage(
items: OrderLine[],
copy: CartDrawerCopy,
language: Language
): string | null {
if (items.length === 0) return null;
const lines = formatOrderLinesForMessage(items);
const total = calculateOrderTotal(items);
const suggestedTime = getSuggestedPickupTime(language);
return `${copy.messageHello}
${copy.messageIntro}
${lines}
${copy.messageConfirm.replace('{time}', suggestedTime)}
${copy.messageTotal}: ${total} kr
${copy.messageThanks}`;
}
export function buildBookingWhatsAppMessage(
booking: BookingDetails,
preOrder: OrderLine[],
locationCopy: BookingLocationCopy
): string | null {
if (!isBookingComplete(booking)) return null;
const locationLabel =
booking.location === 'askim' ? locationCopy.askim : locationCopy.backaplan;
const itemsText =
preOrder.length > 0
? formatOrderLinesForMessage(preOrder).replace(/ — /g, ' — ')
: 'None';
const totalText =
preOrder.length > 0 ? `\nPre-order total: ${calculateOrderTotal(preOrder)} kr\n` : '';
return `Hello Shahi Kitchen 👋
Table Reservation Inquiry:
Name: ${booking.name}
Phone: ${booking.phone}
Email: ${booking.email || 'N/A'}
Location: ${locationLabel}
Date: ${booking.date}
Time: ${booking.time}
Guests: ${booking.guests}
Special Requests: ${booking.notes || 'None'}
Pre-ordered items:
${itemsText}${totalText}
Please confirm table availability and pre-order.
Thank you!`;
}