70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import { PICKUP_LEAD_TIME_MINUTES } from '@/domain/shared/constants';
|
|
|
|
export type FulfillmentMode = 'pickup' | 'delivery';
|
|
|
|
export type InquiryBranch = 'askim' | 'backaplan' | '';
|
|
|
|
export const DEFAULT_INQUIRY_BRANCH: InquiryBranch = 'backaplan';
|
|
|
|
export type InquiryPreferredDate = 'today' | 'tomorrow' | '';
|
|
|
|
export type InquiryScheduleValidation =
|
|
| 'ok'
|
|
| 'incomplete'
|
|
| 'too_soon'
|
|
| 'invalid_time';
|
|
|
|
export interface InquirySchedule {
|
|
preferredDate: InquiryPreferredDate;
|
|
preferredTime: string;
|
|
}
|
|
|
|
export interface PickupInquiryDetails extends InquirySchedule {
|
|
name: string;
|
|
email: string;
|
|
branch: InquiryBranch;
|
|
}
|
|
|
|
export interface DeliveryInquiryDetails extends PickupInquiryDetails {
|
|
address: string;
|
|
}
|
|
|
|
export function isInquiryBranchOnlineEnabled(branch: InquiryBranch): boolean {
|
|
return branch === 'backaplan';
|
|
}
|
|
|
|
export function getMinInquiryTimeForToday(now: Date = new Date()): string {
|
|
const min = new Date(now.getTime() + PICKUP_LEAD_TIME_MINUTES * 60 * 1000);
|
|
return `${String(min.getHours()).padStart(2, '0')}:${String(min.getMinutes()).padStart(2, '0')}`;
|
|
}
|
|
|
|
export function validateInquirySchedule(
|
|
schedule: InquirySchedule,
|
|
now: Date = new Date(),
|
|
): InquiryScheduleValidation {
|
|
const hasDate = schedule.preferredDate === 'today' || schedule.preferredDate === 'tomorrow';
|
|
const hasTime = schedule.preferredTime.trim().length > 0;
|
|
|
|
if (!hasDate && !hasTime) return 'ok';
|
|
if (hasDate !== hasTime) return 'incomplete';
|
|
|
|
const [hours, minutes] = schedule.preferredTime.split(':').map(Number);
|
|
if (Number.isNaN(hours) || Number.isNaN(minutes)) return 'invalid_time';
|
|
|
|
const scheduled = new Date(now);
|
|
if (schedule.preferredDate === 'tomorrow') {
|
|
scheduled.setDate(scheduled.getDate() + 1);
|
|
}
|
|
scheduled.setHours(hours, minutes, 0, 0);
|
|
|
|
const minTime = new Date(now.getTime() + PICKUP_LEAD_TIME_MINUTES * 60 * 1000);
|
|
if (scheduled.getTime() < minTime.getTime()) return 'too_soon';
|
|
|
|
return 'ok';
|
|
}
|
|
|
|
export function hasInquirySchedule(schedule: InquirySchedule): boolean {
|
|
return validateInquirySchedule(schedule) === 'ok' &&
|
|
schedule.preferredDate !== '' &&
|
|
schedule.preferredTime.trim() !== '';
|
|
} |