-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(cabins): add occupied days query (#510)
* ✨ feat(cabins): add occupied days query * chore: address code smell
- Loading branch information
Showing
10 changed files
with
355 additions
and
2 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 |
---|---|---|
@@ -1,4 +1,14 @@ | ||
import type { CabinResolvers } from "./../../types.generated.js"; | ||
export const Cabin: CabinResolvers = { | ||
/* Implement Cabin resolver logic here */ | ||
occupiedDays: async ({ id }, _args, ctx) => { | ||
const occupiedDates = await ctx.cabins.getOccupiedDates(ctx, { | ||
cabinId: id, | ||
}); | ||
if (!occupiedDates.ok) { | ||
throw occupiedDates.error; | ||
} | ||
|
||
return occupiedDates.data.days; | ||
}, | ||
}; |
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
88 changes: 88 additions & 0 deletions
88
src/repositories/cabins/__tests__/integration/find-many-bookings.test.ts
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,88 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { DateTime } from "luxon"; | ||
import { makeMockContext } from "~/lib/context.js"; | ||
import { makeDependencies } from "./dependencies.js"; | ||
|
||
describe("CabinRepository", () => { | ||
describe("#findManyBookings", () => { | ||
it("returns all bookings for a cabin", async () => { | ||
const { | ||
cabinRepository, | ||
oksen, | ||
oksenBooking, | ||
bothBooking, | ||
bjørnenBooking, | ||
} = await makeDependencies(); | ||
|
||
const result = await cabinRepository.findManyBookings(makeMockContext(), { | ||
cabinId: oksen.id, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
ok: true, | ||
data: { | ||
bookings: expect.arrayContaining([oksenBooking, bothBooking]), | ||
}, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
ok: true, | ||
data: { | ||
bookings: expect.not.arrayContaining([bjørnenBooking]), | ||
}, | ||
}); | ||
}); | ||
|
||
it("returns empty array if cabin with the id does not exist", async () => { | ||
const { cabinRepository } = await makeDependencies(); | ||
|
||
const result = await cabinRepository.findManyBookings(makeMockContext(), { | ||
cabinId: faker.string.uuid(), | ||
}); | ||
|
||
expect(result).toEqual({ | ||
ok: true, | ||
data: { | ||
bookings: [], | ||
}, | ||
}); | ||
}); | ||
|
||
it("returns only bookings matching status", async () => { | ||
const { cabinRepository, makeBooking, oksen } = await makeDependencies(); | ||
|
||
const confirmedBooking = await makeBooking({ | ||
cabins: [{ id: oksen.id }], | ||
status: "CONFIRMED", | ||
endDate: DateTime.now().plus({ days: 4 }).toJSDate(), | ||
startDate: DateTime.now().plus({ days: 1 }).toJSDate(), | ||
}); | ||
|
||
const pendingBooking = await makeBooking({ | ||
cabins: [{ id: oksen.id }], | ||
status: "PENDING", | ||
endDate: DateTime.now().plus({ days: 4 }).toJSDate(), | ||
startDate: DateTime.now().plus({ days: 1 }).toJSDate(), | ||
}); | ||
|
||
const result = await cabinRepository.findManyBookings(makeMockContext(), { | ||
cabinId: oksen.id, | ||
bookingStatus: "CONFIRMED", | ||
}); | ||
|
||
expect(result).toEqual({ | ||
ok: true, | ||
data: { | ||
bookings: expect.arrayContaining([confirmedBooking]), | ||
}, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
ok: true, | ||
data: { | ||
bookings: expect.not.arrayContaining([pendingBooking]), | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
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
60 changes: 60 additions & 0 deletions
60
src/services/cabins/__tests__/unit/get-occupied-dates.test.ts
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,60 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { type DeepMockProxy, mock, mockDeep } from "jest-mock-extended"; | ||
import { range } from "lodash-es"; | ||
import { DateTime } from "luxon"; | ||
import type { BookingType } from "~/domain/cabins.js"; | ||
import { makeMockContext } from "~/lib/context.js"; | ||
import { CabinService, type ICabinRepository } from "../../service.js"; | ||
|
||
describe("CabinService", () => { | ||
let cabinService: CabinService; | ||
let mockCabinRepository: DeepMockProxy<ICabinRepository>; | ||
|
||
beforeAll(() => { | ||
mockCabinRepository = mockDeep<ICabinRepository>(); | ||
cabinService = new CabinService(mockCabinRepository, mock(), mock()); | ||
}); | ||
|
||
describe("#getOccupiedDates", () => { | ||
it("returns all occupied dates for a cabin", async () => { | ||
const booking1 = { | ||
...mock<BookingType>(), | ||
startDate: DateTime.now().plus({ days: 1 }).toJSDate(), | ||
endDate: DateTime.now().plus({ days: 3 }).toJSDate(), | ||
}; | ||
|
||
const booking2 = { | ||
...mock<BookingType>(), | ||
startDate: DateTime.now().plus({ days: 5 }).toJSDate(), | ||
endDate: DateTime.now().plus({ days: 7 }).toJSDate(), | ||
}; | ||
|
||
mockCabinRepository.findManyBookings.mockResolvedValue({ | ||
ok: true, | ||
data: { | ||
bookings: [booking1, booking2], | ||
}, | ||
}); | ||
|
||
const result = await cabinService.getOccupiedDates(makeMockContext(), { | ||
cabinId: faker.string.uuid(), | ||
}); | ||
|
||
if (!result.ok) throw result.error; | ||
|
||
const { days } = result.data; | ||
|
||
for (const day of range(1, 9)) { | ||
const date = DateTime.now() | ||
.plus({ days: day }) | ||
.startOf("day") | ||
.toJSDate(); | ||
if (day === 4 || day === 8) { | ||
expect(days).not.toContainEqual(date); | ||
} else { | ||
expect(days).toContainEqual(date); | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.