Spaces:
Sleeping
Sleeping
| import axios from 'axios'; | |
| import apiClient from '@/lib/api/axios-instance'; | |
| import { createHeaders } from '@/lib/api'; | |
| import { API_BASE_URL } from '../config'; | |
| import { | |
| MeetingRoom, | |
| MeetingRoomCreateRequest, | |
| MeetingRoomUpdateRequest, | |
| RoomBooking, | |
| RoomBookingCreateRequest, | |
| RoomBookingUpdateRequest, | |
| ResponseData | |
| } from '../types'; | |
| const BASE_URL = `${API_BASE_URL}/api`; | |
| // Meeting Room APIs | |
| export const getMeetingRooms = async (): Promise<MeetingRoom[]> => { | |
| const response = await apiClient.get(`${BASE_URL}/MeetingRoom`); | |
| return response.data; | |
| }; | |
| export const getMeetingRoomById = async (id: number): Promise<MeetingRoom> => { | |
| const response = await apiClient.get(`${BASE_URL}/MeetingRoom/${id}`); | |
| return response.data; | |
| }; | |
| export const createMeetingRoom = async (room: MeetingRoomCreateRequest): Promise<MeetingRoom> => { | |
| const response = await apiClient.post(`${BASE_URL}/MeetingRoom`, room); | |
| return response.data; | |
| }; | |
| export const updateMeetingRoom = async (room: MeetingRoomUpdateRequest): Promise<MeetingRoom> => { | |
| const response = await apiClient.put(`${BASE_URL}/MeetingRoom/${room.id}`, room); | |
| return response.data; | |
| }; | |
| export const deleteMeetingRoom = async (id: number): Promise<boolean> => { | |
| const response = await apiClient.delete(`${BASE_URL}/MeetingRoom/${id}`); | |
| return response.status === 200; | |
| }; | |
| // Room Booking APIs | |
| export const getRoomBookings = async (): Promise<RoomBooking[]> => { | |
| const response = await apiClient.get(`${BASE_URL}/RoomBooking`); | |
| return response.data; | |
| }; | |
| export const getRoomBookingById = async (id: number): Promise<RoomBooking> => { | |
| const response = await apiClient.get(`${BASE_URL}/RoomBooking/${id}`); | |
| return response.data; | |
| }; | |
| export const createRoomBooking = async (booking: RoomBookingCreateRequest): Promise<RoomBooking> => { | |
| const response = await apiClient.post(`${BASE_URL}/RoomBooking`, booking); | |
| return response.data; | |
| }; | |
| export const updateRoomBooking = async (booking: RoomBookingUpdateRequest): Promise<RoomBooking> => { | |
| const response = await apiClient.put(`${BASE_URL}/RoomBooking/${booking.id}`, booking); | |
| return response.data; | |
| }; | |
| export const deleteRoomBooking = async (id: number): Promise<boolean> => { | |
| const response = await apiClient.delete(`${BASE_URL}/RoomBooking/${id}`); | |
| return response.status === 200; | |
| }; | |
| // Additional helper functions | |
| export const getRoomBookingsByRoomId = async (roomId: number): Promise<RoomBooking[]> => { | |
| const bookings = await getRoomBookings(); | |
| return bookings.filter(booking => booking.roomId === roomId); | |
| }; | |
| export const getRoomBookingsByEmployeeId = async (employeeId: number): Promise<RoomBooking[]> => { | |
| const bookings = await getRoomBookings(); | |
| return bookings.filter(booking => booking.employeeId === employeeId); | |
| }; | |
| export const checkRoomAvailability = async ( | |
| roomId: number, | |
| startTime: string, | |
| endTime: string, | |
| excludeBookingId?: number | |
| ): Promise<boolean> => { | |
| const bookings = await getRoomBookingsByRoomId(roomId); | |
| // Convert input times to Date objects | |
| const start = new Date(startTime); | |
| const end = new Date(endTime); | |
| // Check if the requested time slot overlaps with any existing booking | |
| return !bookings.some(booking => { | |
| // Skip the current booking if we're checking for an update | |
| if (excludeBookingId && booking.id === excludeBookingId) { | |
| return false; | |
| } | |
| const bookingStart = new Date(booking.startTime); | |
| const bookingEnd = new Date(booking.endTime); | |
| // Check for overlap: | |
| // If the new start time is before the existing end time | |
| // AND the new end time is after the existing start time | |
| return start < bookingEnd && end > bookingStart; | |
| }); | |
| }; |