Files
bab-app/src/stores/schedule.ts
Patrick Toal 66e2169f45
Some checks failed
Build BAB Application Deployment Artifact / build (push) Failing after 2m11s
Adapting to time blocks for bookings
2023-12-23 11:39:54 -05:00

200 lines
4.8 KiB
TypeScript

import { defineStore } from 'pinia';
import { ref } from 'vue';
import { Boat, useBoatStore } from './boat';
import { date } from 'quasar';
import { DateOptions } from 'quasar';
import {
Timestamp,
parseTimestamp,
TimestampArray,
} from '@quasar/quasar-ui-qcalendar';
import { timeStamp } from 'console';
export type StatusTypes = 'tentative' | 'confirmed' | 'pending' | undefined;
export type Reservation = {
id: number;
user: string;
start: Date;
end: Date;
resource: Boat;
reservationDate: Date;
status?: StatusTypes;
};
export type Timeblock = {
start: Timestamp;
end: Timestamp;
};
const sampleBlocks = [
{
start: { time: '09:00', hour: 9, minute: 0, hasDay: false, hasTime: true },
end: { time: '12:00', hour: 12, minute: 0, hasDay: false, hasTime: true },
},
{
start: { time: '12:00', hour: 12, minute: 0, hasDay: false, hasTime: true },
end: { time: '15:00', hour: 15, minute: 0, hasDay: false, hasTime: true },
},
{
start: { time: '15:00', hour: 15, minute: 0, hasDay: false, hasTime: true },
end: { time: '18:00', hour: 18, minute: 0, hasDay: false, hasTime: true },
},
{
start: { time: '18:00', hour: 18, minute: 0, hasDay: false, hasTime: true },
end: { time: '21:00', hour: 21, minute: 0, hasDay: false, hasTime: true },
},
] as Timeblock[];
function getSampleReservations(): Reservation[] {
const sampleData = [
{
id: 1,
user: 'John Smith',
start: '12:00',
end: '15:00',
boat: 1,
status: 'confirmed',
},
{
id: 2,
user: 'Bob Barker',
start: '18:00',
end: '21:00',
boat: 1,
status: 'confirmed',
},
{
id: 3,
user: 'Peter Parker',
start: '9:00',
end: '12:00',
boat: 2,
status: 'tentative',
},
{
id: 4,
user: 'Vince McMahon',
start: '15:00',
end: '18:00',
boat: 2,
status: 'pending',
},
{
id: 5,
user: 'Heather Graham',
start: '09:00',
end: '12:00',
boat: 3,
status: 'confirmed',
},
{
id: 6,
user: 'Lawrence Fishburne',
start: '18:00',
end: '21:00',
boat: 3,
},
];
const boatStore = useBoatStore();
const now = new Date();
const splitTime = (x: string): string[] => {
return x.split(':');
};
const makeOpts = (x: string[]): DateOptions => {
return {
hour: parseInt(x[0]),
minute: parseInt(x[1]),
seconds: 0,
milliseconds: 0,
};
};
return sampleData.map((entry): Reservation => {
const boat = <Boat>boatStore.boats.find((b) => b.id == entry.boat);
return {
id: entry.id,
user: entry.user,
start: date.adjustDate(now, makeOpts(splitTime(entry.start))),
end: date.adjustDate(now, makeOpts(splitTime(entry.end))),
resource: boat,
reservationDate: now,
status: entry.status as StatusTypes,
};
});
}
export const useScheduleStore = defineStore('schedule', () => {
// TODO: Implement functions to dynamically pull this data.
const reservations = ref<Reservation[]>(getSampleReservations());
const timeblocks = sampleBlocks;
const getTimeblocksForDate = (date: Date): Timeblock[] => timeblocks;
const getBoatReservations = (
boat: number | string,
curDate: Date
): Reservation[] => {
return reservations.value.filter((x) => {
return (
(x.start.getDate() == curDate.getDate() ||
x.end.getDate() == curDate.getDate()) &&
x.resource != undefined &&
(typeof boat == 'number'
? x.resource.id == boat
: x.resource.name == boat)
);
});
};
const getConflictingReservations = (
resource: Boat,
start: Date,
end: Date
): Reservation[] => {
const overlapped = reservations.value.filter(
(entry: Reservation) =>
entry.resource.id == resource.id &&
entry.start < end &&
entry.end > start
);
return overlapped;
};
const isResourceTimeOverlapped = (
resource: Boat,
start: Date,
end: Date
): boolean => {
return getConflictingReservations(resource, start, end).length > 0;
};
const isReservationOverlapped = (res: Reservation): boolean => {
return isResourceTimeOverlapped(res.resource, res.start, res.end);
};
const getNewId = () => {
// Trivial placeholder
return Math.max(...reservations.value.map((item) => item.id)) + 1;
};
const addOrCreateReservation = (reservation: Reservation) => {
const index = reservations.value.findIndex(
(res) => res.id == reservation.id
);
index != -1
? (reservations.value[index] = reservation)
: reservations.value.push(reservation);
};
return {
reservations,
getBoatReservations,
getConflictingReservations,
getTimeblocksForDate,
getNewId,
addOrCreateReservation,
isReservationOverlapped,
isResourceTimeOverlapped,
};
});