-
Notifications
You must be signed in to change notification settings - Fork 0
Optimize Booking Retrieval in Cron Job #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
89f5744
342f960
ad3ecad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { expect, it, describe, mock } from "bun:test"; | ||
| import { | ||
| TYPE_ROOM, | ||
| TYPE_FLAT, | ||
| TYPE_ADHYAYAN, | ||
| TYPE_FOOD, | ||
| TYPE_TRAVEL, | ||
| TYPE_UTSAV | ||
| } from '../config/constants.js'; | ||
|
|
||
| mock.module("../models/associations.js", () => ({ | ||
| RoomBooking: { findAll: async (q) => q.where.bookingid.map(id => ({ bookingid: id })) }, | ||
| FlatBooking: { findAll: async (q) => q.where.bookingid.map(id => ({ bookingid: id })) }, | ||
| ShibirBookingDb: { findAll: async (q) => q.where.bookingid.map(id => ({ bookingid: id })) }, | ||
| FoodDb: { findAll: async (q) => q.where.id.map(id => ({ id })) }, | ||
| TravelDb: { findAll: async (q) => q.where.bookingid.map(id => ({ bookingid: id })) }, | ||
| UtsavBooking: { findAll: async (q) => q.where.bookingid.map(id => ({ bookingid: id })) }, | ||
| CardDb: {}, | ||
| Transactions: {}, | ||
| ShibirDb: {}, | ||
| Departments: {}, | ||
| MaintenanceDb: {}, | ||
| GateRecord: {}, | ||
| CentreDb: {}, | ||
| BulkFoodBooking: {}, | ||
| FoodPhysicalPlate: {}, | ||
| RoomDb: {}, | ||
| FlatDb: {}, | ||
| UtsavDb: {}, | ||
| UtsavPackagesDb: {}, | ||
| AdminRoles: {}, | ||
| AdminUsers: {}, | ||
| Roles: {}, | ||
| Menu: {}, | ||
| Cities: {}, | ||
| States: {}, | ||
| Countries: {}, | ||
| GuestDb: {}, | ||
| GuestRelationship: {}, | ||
| RazorpayWebhook: {}, | ||
| RazorpaySettlement: {}, | ||
| SupportTickets: {}, | ||
| BlockDates: {}, | ||
| Updates: {}, | ||
| AdhyayanFeedback: {}, | ||
| RazorpaySettlementRecon: {}, | ||
| ShibirAttendanceDb: {} | ||
| })); | ||
|
|
||
| const { getBookings } = await import('../helpers/booking.helper.js'); | ||
|
|
||
| describe('getBookings', () => { | ||
| it('should return empty array for empty bookingids', async () => { | ||
| const result = await getBookings(TYPE_ROOM, []); | ||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it('should call RoomBooking.findAll for TYPE_ROOM', async () => { | ||
| const ids = ['room1']; | ||
| const result = await getBookings(TYPE_ROOM, ids); | ||
| expect(result).toHaveLength(1); | ||
| expect(result[0].bookingid).toBe('room1'); | ||
| }); | ||
|
|
||
| it('should call FoodDb.findAll for TYPE_FOOD', async () => { | ||
| const ids = [1]; | ||
| const result = await getBookings(TYPE_FOOD, ids); | ||
| expect(result).toHaveLength(1); | ||
| expect(result[0].id).toBe(1); | ||
| }); | ||
|
|
||
| it('should throw error for invalid booking type', async () => { | ||
| try { | ||
| await getBookings('INVALID', ['id1']); | ||
| expect(true).toBe(false); // Should not reach here | ||
| } catch (e) { | ||
| expect(e.message).toContain('Invalid booking type'); | ||
| } | ||
| }); | ||
|
Comment on lines
+72
to
+79
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test for checking exceptions can be simplified by using the it('should throw error for invalid booking type', async () => {
await expect(getBookings('INVALID', ['id1'])).rejects.toThrow(
'Invalid booking type'
);
});
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jules please take a look There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added these tests using Bun's test runner to verify the batch fetching logic and error handling for invalid booking types. The tests confirm that |
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getBookingsfunction can be made more concise and easier to maintain by using a map to associate booking types with their corresponding models and primary key fields. This avoids the repetitiveswitchstatement, reduces code duplication, and makes it simpler to add or modify booking types in the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jules please take a look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the
getBookingsfunction to uselet/constfor better adherence to modern JavaScript standards. I also added an early return for emptybookingidsto make the function more robust.