Added reservation and username lookup
All checks were successful
Build BAB Application Deployment Artifact / build (push) Successful in 2m13s

This commit is contained in:
2024-05-13 10:49:03 -04:00
parent 4a273ccb2f
commit 78211a33ae
17 changed files with 180 additions and 72 deletions

View File

@@ -1,10 +1,11 @@
import { defineStore } from 'pinia';
import { ID, account } from 'boot/appwrite';
import { OAuthProvider, type Models } from 'appwrite';
import { ID, account, functions } from 'boot/appwrite';
import { ExecutionMethod, OAuthProvider, type Models } from 'appwrite';
import { ref } from 'vue';
export const useAuthStore = defineStore('auth', () => {
const currentUser = ref<Models.User<Models.Preferences> | null>(null);
const userNames = ref<Record<string, string>>({});
async function init() {
try {
@@ -31,9 +32,39 @@ export const useAuthStore = defineStore('auth', () => {
currentUser.value = await account.get();
}
function getUserNameById(id: string) {
try {
if (!userNames.value[id]) {
userNames.value[id] = '';
functions
.createExecution(
'664038294b5473ef0c8d',
'',
false,
'/userinfo/' + id,
ExecutionMethod.GET
)
.then(
(res) => (userNames.value[id] = JSON.parse(res.responseBody).name)
);
}
} catch (e) {
console.log('Failed to get username. Error: ' + e);
}
return userNames.value[id];
}
function logout() {
return account.deleteSession('current').then((currentUser.value = null));
}
return { currentUser, register, login, googleLogin, logout, init };
return {
currentUser,
getUserNameById,
register,
login,
googleLogin,
logout,
init,
};
});

View File

@@ -39,5 +39,9 @@ export const useBoatStore = defineStore('boat', () => {
}
}
return { boats, fetchBoats };
const getBoatById = (id: string): Boat | null => {
return boats.value.find((b) => b.$id === id) || null;
};
return { boats, fetchBoats, getBoatById };
});

View File

@@ -69,7 +69,7 @@ export function getSampleReservations(): Reservation[] {
user: 'John Smith',
start: '7:00',
end: '10:00',
boat: '1',
boat: '66359729003825946ae1',
status: 'confirmed',
},
{
@@ -77,7 +77,7 @@ export function getSampleReservations(): Reservation[] {
user: 'Bob Barker',
start: '16:00',
end: '19:00',
boat: '1',
boat: '66359729003825946ae1',
status: 'confirmed',
},
{
@@ -85,7 +85,7 @@ export function getSampleReservations(): Reservation[] {
user: 'Peter Parker',
start: '7:00',
end: '13:00',
boat: '4',
boat: '663597030029b71c7a9b',
status: 'tentative',
},
{
@@ -93,7 +93,7 @@ export function getSampleReservations(): Reservation[] {
user: 'Vince McMahon',
start: '10:00',
end: '13:00',
boat: '2',
boat: '663597030029b71c7a9b',
status: 'pending',
},
{
@@ -101,7 +101,7 @@ export function getSampleReservations(): Reservation[] {
user: 'Heather Graham',
start: '13:00',
end: '19:00',
boat: '4',
boat: '663596b9000235ffea55',
status: 'confirmed',
},
{
@@ -109,7 +109,7 @@ export function getSampleReservations(): Reservation[] {
user: 'Lawrence Fishburne',
start: '13:00',
end: '16:00',
boat: '3',
boat: '663596b9000235ffea55',
},
];
const boatStore = useBoatStore();
@@ -131,9 +131,11 @@ export function getSampleReservations(): Reservation[] {
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,
start: date
.adjustDate(now, makeOpts(splitTime(entry.start)))
.toISOString(),
end: date.adjustDate(now, makeOpts(splitTime(entry.end))).toISOString(),
resource: boat.$id,
reservationDate: now,
status: entry.status as StatusTypes,
};

View File

@@ -108,19 +108,32 @@ export const useScheduleStore = defineStore('schedule', () => {
});
};
async function fetchReservations() {
try {
const response = await databases.listDocuments(
AppwriteIds.databaseId,
AppwriteIds.collection.reservation
);
reservations.value = response.documents as Reservation[];
} catch (error) {
console.error('Failed to fetch timeblocks', error);
}
}
const getBoatReservations = (
searchDate: Timestamp,
boat?: string
): Reservation[] => {
return reservations.value.filter((x) => {
const result = reservations.value.filter((x) => {
return (
((parseDate(x.start)?.date == searchDate.date ||
parseDate(x.end)?.date == searchDate.date) && // Part of reservation falls on day
((parsed(x.start)?.date == searchDate.date ||
parsed(x.end)?.date == searchDate.date) && // Part of reservation falls on day
x.resource != undefined && // A boat is defined
!boat) ||
x.resource.$id == boat // A specific boat has been passed, and matches
x.resource == boat // A specific boat has been passed, and matches
);
});
return result;
};
async function fetchIntervals() {
@@ -170,21 +183,21 @@ export const useScheduleStore = defineStore('schedule', () => {
// };
const getConflictingReservations = (
resource: Boat,
resource: string,
start: Date,
end: Date
): Reservation[] => {
const overlapped = reservations.value.filter(
(entry: Reservation) =>
entry.resource.$id == resource.$id &&
entry.start < end &&
entry.end > start
entry.resource == resource &&
new Date(entry.start) < end &&
new Date(entry.end) > start
);
return overlapped;
};
const isResourceTimeOverlapped = (
resource: Boat,
resource: string,
start: Date,
end: Date
): boolean => {
@@ -192,7 +205,11 @@ export const useScheduleStore = defineStore('schedule', () => {
};
const isReservationOverlapped = (res: Reservation): boolean => {
return isResourceTimeOverlapped(res.resource, res.start, res.end);
return isResourceTimeOverlapped(
res.resource,
new Date(res.start),
new Date(res.end)
);
};
const getNewId = (): string => {
@@ -235,7 +252,6 @@ export const useScheduleStore = defineStore('schedule', () => {
{ ...interval, $id: undefined }
);
timeblocks.value.push(response as Interval);
console.log(interval, response);
} else {
console.error('Update interval called without an ID');
}
@@ -315,19 +331,20 @@ export const useScheduleStore = defineStore('schedule', () => {
timeblockTemplates,
getBoatReservations,
getConflictingReservations,
addOrCreateReservation,
isReservationOverlapped,
isResourceTimeOverlapped,
fetchReservations,
getIntervalsForDate,
getIntervals,
fetchIntervals,
fetchIntervalTemplates,
getNewId,
addOrCreateReservation,
createInterval,
updateInterval,
deleteInterval,
createIntervalTemplate,
deleteIntervalTemplate,
updateIntervalTemplate,
isReservationOverlapped,
isResourceTimeOverlapped,
};
});

View File

@@ -1,16 +1,14 @@
import { Models } from 'appwrite';
import type { Boat } from './boat';
export type StatusTypes = 'tentative' | 'confirmed' | 'pending' | undefined;
export interface Reservation {
id: string;
export type Reservation = Partial<Models.Document> & {
user: string;
start: Date;
end: Date;
resource: Boat;
reservationDate: Date;
start: string; // ISODate
end: string; //ISODate
resource: string; // Boat ID
status?: StatusTypes;
}
};
// 24 hrs in advance only 2 weekday, and 1 weekend slot
// Within 24 hrs, any available slot
/* TODO: Figure out how best to separate out where qcalendar bits should be.