-
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): get booking * chore: rename to byIdAndEmail to avoid collision
- Loading branch information
Showing
13 changed files
with
398 additions
and
11 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
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,4 @@ | ||
import type { BookingResponseResolvers } from "./../../types.generated.js"; | ||
export const BookingResponse: BookingResponseResolvers = { | ||
/* Implement BookingResponse resolver logic here */ | ||
}; |
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,4 @@ | ||
import type { GuestsResolvers } from "./../../types.generated.js"; | ||
export const Guests: GuestsResolvers = { | ||
/* Implement Guests resolver logic here */ | ||
}; |
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,19 @@ | ||
import type { QueryResolvers } from "./../../../types.generated.js"; | ||
export const booking: NonNullable<QueryResolvers["booking"]> = async ( | ||
_parent, | ||
{ data }, | ||
ctx, | ||
) => { | ||
const getBookingResult = await ctx.cabins.getBookingByIdAndEmail(ctx, data); | ||
if (!getBookingResult.ok) { | ||
switch (getBookingResult.error.name) { | ||
case "InternalServerError": | ||
throw getBookingResult.error; | ||
case "NotFoundError": | ||
return { | ||
booking: null, | ||
}; | ||
} | ||
} | ||
return getBookingResult.data; | ||
}; |
136 changes: 136 additions & 0 deletions
136
src/graphql/cabins/resolvers/Query/booking.unit.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,136 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { Booking } from "~/domain/cabins.js"; | ||
import { InternalServerError, NotFoundError } from "~/domain/errors.js"; | ||
import { createMockApolloServer } from "~/graphql/test-clients/mock-apollo-server.js"; | ||
import { graphql } from "~/graphql/test-clients/unit/gql.js"; | ||
import { Result } from "~/lib/result.js"; | ||
|
||
describe("Cabin queries", () => { | ||
describe("booking", () => { | ||
it("returns a booking", async () => { | ||
const { client, cabinService } = createMockApolloServer(); | ||
const booking = new Booking({ | ||
cabins: [], | ||
createdAt: faker.date.recent(), | ||
email: faker.internet.email(), | ||
endDate: faker.date.recent(), | ||
externalParticipantsCount: faker.number.int({ max: 10 }), | ||
feedback: faker.lorem.paragraph(), | ||
firstName: faker.person.firstName(), | ||
id: faker.string.uuid(), | ||
internalParticipantsCount: faker.number.int({ max: 10 }), | ||
lastName: faker.person.lastName(), | ||
phoneNumber: faker.phone.number(), | ||
questions: faker.lorem.paragraph(), | ||
startDate: faker.date.recent(), | ||
status: "PENDING", | ||
totalCost: faker.number.int({ max: 200 }), | ||
}); | ||
cabinService.getBookingByIdAndEmail.mockResolvedValue( | ||
Result.success({ | ||
booking, | ||
}), | ||
); | ||
|
||
const { data } = await client.query({ | ||
query: graphql(` | ||
query Booking($data: BookingInput!) { | ||
booking(data: $data) { | ||
booking { | ||
id | ||
guests { | ||
internal | ||
external | ||
} | ||
} | ||
} | ||
} | ||
`), | ||
variables: { | ||
data: { | ||
id: faker.string.uuid(), | ||
email: faker.internet.email(), | ||
}, | ||
}, | ||
}); | ||
|
||
expect(data).toEqual({ | ||
booking: { | ||
booking: { | ||
id: booking.id, | ||
guests: { | ||
internal: booking.internalParticipantsCount, | ||
external: booking.externalParticipantsCount, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it("returns null on NotFoundError", async () => { | ||
const { client, cabinService } = createMockApolloServer(); | ||
cabinService.getBookingByIdAndEmail.mockResolvedValue( | ||
Result.error(new NotFoundError("")), | ||
); | ||
|
||
const { data } = await client.query({ | ||
query: graphql(` | ||
query Booking($data: BookingInput!) { | ||
booking(data: $data) { | ||
booking { | ||
id | ||
guests { | ||
internal | ||
external | ||
} | ||
} | ||
} | ||
} | ||
`), | ||
variables: { | ||
data: { | ||
id: faker.string.uuid(), | ||
email: faker.internet.email(), | ||
}, | ||
}, | ||
}); | ||
|
||
expect(data).toEqual({ | ||
booking: { | ||
booking: null, | ||
}, | ||
}); | ||
}); | ||
|
||
it("throws on InternalServerError", async () => { | ||
const { client, cabinService } = createMockApolloServer(); | ||
cabinService.getBookingByIdAndEmail.mockResolvedValue( | ||
Result.error(new InternalServerError("")), | ||
); | ||
|
||
const { errors } = await client.query({ | ||
query: graphql(` | ||
query Booking($data: BookingInput!) { | ||
booking(data: $data) { | ||
booking { | ||
id | ||
guests { | ||
internal | ||
external | ||
} | ||
} | ||
} | ||
} | ||
`), | ||
variables: { | ||
data: { | ||
id: faker.string.uuid(), | ||
email: faker.internet.email(), | ||
}, | ||
}, | ||
}); | ||
|
||
expect(errors).toBeDefined(); | ||
}); | ||
}); | ||
}); |
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
100 changes: 100 additions & 0 deletions
100
src/services/cabins/__tests__/unit/get-booking-by-id-and-email.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,100 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { Booking } from "~/domain/cabins.js"; | ||
import { NotFoundError } from "~/domain/errors.js"; | ||
import { makeMockContext } from "~/lib/context.js"; | ||
import { Result } from "~/lib/result.js"; | ||
import { makeDependencies } from "./dependencies.js"; | ||
|
||
describe("Cabin service", () => { | ||
describe("#getBookingByIdAndEmail", () => { | ||
it("returns a booking if the email matches the booking", async () => { | ||
const { cabinService, cabinRepository } = makeDependencies(); | ||
const email = faker.internet.email(); | ||
|
||
const booking = new Booking({ | ||
id: faker.string.uuid(), | ||
cabins: [], | ||
createdAt: faker.date.recent(), | ||
email, | ||
endDate: faker.date.recent(), | ||
externalParticipantsCount: faker.number.int({ max: 10 }), | ||
feedback: faker.lorem.paragraph(), | ||
firstName: faker.person.firstName(), | ||
internalParticipantsCount: faker.number.int({ max: 10 }), | ||
lastName: faker.person.lastName(), | ||
phoneNumber: faker.phone.number(), | ||
questions: faker.lorem.paragraph(), | ||
startDate: faker.date.recent(), | ||
status: "PENDING", | ||
totalCost: faker.number.int({ max: 200 }), | ||
}); | ||
|
||
cabinRepository.getBookingById.mockResolvedValue( | ||
Result.success({ booking }), | ||
); | ||
|
||
const result = await cabinService.getBookingByIdAndEmail( | ||
makeMockContext(), | ||
{ | ||
id: faker.string.uuid(), | ||
email, | ||
}, | ||
); | ||
|
||
expect(result).toEqual(Result.success({ booking })); | ||
}); | ||
|
||
it("returns NotFound if the email does not match the booking", async () => { | ||
const { cabinService, cabinRepository } = makeDependencies(); | ||
|
||
const booking = new Booking({ | ||
id: faker.string.uuid(), | ||
cabins: [], | ||
createdAt: faker.date.recent(), | ||
email: faker.internet.email({ firstName: "test" }), | ||
endDate: faker.date.recent(), | ||
externalParticipantsCount: faker.number.int({ max: 10 }), | ||
feedback: faker.lorem.paragraph(), | ||
firstName: faker.person.firstName(), | ||
internalParticipantsCount: faker.number.int({ max: 10 }), | ||
lastName: faker.person.lastName(), | ||
phoneNumber: faker.phone.number(), | ||
questions: faker.lorem.paragraph(), | ||
startDate: faker.date.recent(), | ||
status: "PENDING", | ||
totalCost: faker.number.int({ max: 200 }), | ||
}); | ||
|
||
cabinRepository.getBookingById.mockResolvedValue( | ||
Result.success({ booking }), | ||
); | ||
|
||
const result = await cabinService.getBookingByIdAndEmail( | ||
makeMockContext(), | ||
{ | ||
id: faker.string.uuid(), | ||
email: faker.internet.email({ firstName: "not-test" }), | ||
}, | ||
); | ||
|
||
expect(result).toEqual(Result.error(expect.any(NotFoundError))); | ||
}); | ||
it("returns NotFound if the repository returns NotFound", async () => { | ||
const { cabinService, cabinRepository } = makeDependencies(); | ||
|
||
cabinRepository.getBookingById.mockResolvedValue( | ||
Result.error(new NotFoundError("")), | ||
); | ||
|
||
const result = await cabinService.getBookingByIdAndEmail( | ||
makeMockContext(), | ||
{ | ||
id: faker.string.uuid(), | ||
email: faker.internet.email(), | ||
}, | ||
); | ||
|
||
expect(result).toEqual(Result.error(expect.any(NotFoundError))); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.