-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
242 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { | ||
Id, | ||
NullableId, | ||
Paginated, | ||
Params, | ||
ServiceMethods, | ||
} from "@feathersjs/feathers"; | ||
import { Application } from "../../declarations"; | ||
import { BadRequest } from "@feathersjs/errors"; | ||
|
||
interface Data {} | ||
|
||
interface ServiceOptions {} | ||
|
||
export class AvailableRooms implements ServiceMethods<Data> { | ||
app: Application; | ||
options: ServiceOptions; | ||
|
||
constructor(options: ServiceOptions = {}, app: Application) { | ||
this.options = options; | ||
this.app = app; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async find(params?: Params): Promise<Data[] | Paginated<Data>> { | ||
/* | ||
* @description | ||
* This Endpoint returns all the available rooms (status == Available ) between a startDate & | ||
* endDate. The Handler queries (agg.) the db to search for rooms between startDate and endDate and | ||
* sends the result to client as response. | ||
* */ | ||
|
||
interface QueryInterface { | ||
startDate?: Date; | ||
endDate?: Date; | ||
} | ||
|
||
const { startDate, endDate } = params?.query as QueryInterface; | ||
|
||
if (!startDate || !endDate) | ||
throw new BadRequest("Please provide startDate and endDate"); | ||
|
||
const start = new Date(startDate); | ||
const end = new Date(endDate); | ||
|
||
if (isNaN(start.getTime()) || isNaN(end.getTime())) { | ||
throw new BadRequest("Invalid date format"); | ||
} | ||
|
||
const roomsModel = this.app.service("rooms").Model; | ||
|
||
// rooms that are not booked during the specified dates | ||
const roomsWithOrWithoutBookings = await roomsModel | ||
.aggregate([ | ||
{ | ||
$lookup: { | ||
from: "bookings", | ||
localField: "_id", | ||
foreignField: "room", | ||
as: "bookings", | ||
}, | ||
}, | ||
{ | ||
$match: { | ||
$or: [ | ||
{ bookings: { $size: 0 } }, | ||
{ | ||
bookings: { | ||
$not: { | ||
$elemMatch: { | ||
dates: { | ||
$not: { | ||
$elemMatch: { | ||
$lt: end, | ||
$gte: start, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
]) | ||
.exec(); | ||
|
||
const resp = roomsWithOrWithoutBookings.map((room: any) => { | ||
if (room.bookings.length === 0) { | ||
delete room.bookings; | ||
return room; | ||
} | ||
}); | ||
|
||
return resp; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async get(id: Id, params?: Params): Promise<Data> { | ||
return { | ||
id, | ||
text: `A new message with ID: ${id}!`, | ||
}; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async create(data: Data, params?: Params): Promise<Data> { | ||
if (Array.isArray(data)) { | ||
return Promise.all(data.map((current) => this.create(current, params))); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async update(id: NullableId, data: Data, params?: Params): Promise<Data> { | ||
return data; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async patch(id: NullableId, data: Data, params?: Params): Promise<Data> { | ||
return data; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async remove(id: NullableId, params?: Params): Promise<Data> { | ||
return { id }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { HooksObject } from '@feathersjs/feathers'; | ||
import * as authentication from '@feathersjs/authentication'; | ||
// Don't remove this comment. It's needed to format import lines nicely. | ||
|
||
const { authenticate } = authentication.hooks; | ||
|
||
export default { | ||
before: { | ||
all: [ authenticate('jwt') ], | ||
find: [], | ||
get: [], | ||
create: [], | ||
update: [], | ||
patch: [], | ||
remove: [] | ||
}, | ||
|
||
after: { | ||
all: [], | ||
find: [], | ||
get: [], | ||
create: [], | ||
update: [], | ||
patch: [], | ||
remove: [] | ||
}, | ||
|
||
error: { | ||
all: [], | ||
find: [], | ||
get: [], | ||
create: [], | ||
update: [], | ||
patch: [], | ||
remove: [] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Initializes the `available-rooms` service on path `/available-rooms` | ||
import { ServiceAddons } from '@feathersjs/feathers'; | ||
import { Application } from '../../declarations'; | ||
import { AvailableRooms } from './available-rooms.class'; | ||
import hooks from './available-rooms.hooks'; | ||
|
||
// Add this service to the service type index | ||
declare module '../../declarations' { | ||
interface ServiceTypes { | ||
'available-rooms': AvailableRooms & ServiceAddons<any>; | ||
} | ||
} | ||
|
||
export default function (app: Application): void { | ||
const options = { | ||
paginate: app.get('paginate') | ||
}; | ||
|
||
// Initialize our service with any options it requires | ||
app.use('/available-rooms', new AvailableRooms(options, app)); | ||
|
||
// Get our initialized service so that we can register hooks | ||
const service = app.service('available-rooms'); | ||
|
||
service.hooks(hooks); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import BookingStatus from '../../constants/booking-status.enum'; | ||
|
||
const isValidRoomStatusMove = (currentStatus: string, newStatus: string): boolean => { | ||
// Define the valid status transitions | ||
const validTransitions: Record<string, string[]> = { | ||
[BookingStatus.PENDING]: [BookingStatus.APPROVED,BookingStatus.CANCELLED], | ||
[BookingStatus.CANCELLED]: [], | ||
[BookingStatus.APPROVED]: [], | ||
}; | ||
return validTransitions[currentStatus].includes(newStatus); | ||
}; | ||
|
||
export default isValidRoomStatusMove; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import app from '../../src/app'; | ||
|
||
describe('\'available-rooms\' service', () => { | ||
it('registered the service', () => { | ||
const service = app.service('available-rooms'); | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |