Skip to content

Commit 239bdb8

Browse files
committed
2 parents eadb419 + 878af9d commit 239bdb8

4 files changed

Lines changed: 70 additions & 12 deletions

File tree

src/controllers/terminal.controller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export const createTerminal = async (req, res, next) => {
1313
// Get all terminals
1414
export const getAllTerminals = async (req, res, next) => {
1515
try {
16-
const terminals = await terminalService.getAllTerminals();
16+
const { airportId } = req.query;
17+
18+
const terminals = await terminalService.getAllTerminals(airportId);
1719
res.status(200).json({success:true, message:"Terminals fetched successfully", data: terminals, error: null});
1820
} catch (error) {
1921
next(error);

src/controllers/test/terminal.controller.test.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
req = {
1818
body: {},
1919
params: {},
20+
query: {},
2021
};
2122
res = {
2223
status: jest.fn().mockReturnThis(),
@@ -65,10 +66,12 @@ import {
6566
{ id: 1, name: "Terminal 1", type: "Domestic" },
6667
{ id: 2, name: "Terminal 2", type: "International" },
6768
];
68-
terminalService.getAllTerminals.mockResolvedValue(terminals);
69-
69+
req.query.airportId = "123"; // Add query parameter to the existing mock
70+
terminalService.getAllTerminals.mockResolvedValue(terminals); // Mock service
71+
7072
await getAllTerminals(req, res, next);
71-
73+
74+
expect(terminalService.getAllTerminals).toHaveBeenCalledWith("123"); // Ensure airportId is passed
7275
expect(res.status).toHaveBeenCalledWith(200);
7376
expect(res.json).toHaveBeenCalledWith({
7477
success: true,
@@ -77,17 +80,20 @@ import {
7780
error: null,
7881
});
7982
});
80-
83+
8184
test("should handle error when retrieving terminals", async () => {
8285
const errorMessage = "Failed to fetch terminals";
83-
terminalService.getAllTerminals.mockRejectedValue(new Error(errorMessage));
84-
86+
req.query.airportId = "123"; // Add query parameter to the existing mock
87+
terminalService.getAllTerminals.mockRejectedValue(new Error(errorMessage)); // Mock service
88+
8589
await getAllTerminals(req, res, next);
86-
90+
91+
expect(terminalService.getAllTerminals).toHaveBeenCalledWith("123"); // Ensure airportId is passed
8792
expect(next).toHaveBeenCalledWith(expect.any(Error));
8893
expect(next).toHaveBeenCalledWith(expect.objectContaining({ message: errorMessage }));
8994
});
9095
});
96+
9197

9298
describe("getTerminalById", () => {
9399
test("should return a terminal by ID with 200 status", async () => {

src/services/terminal.service.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@ export const createTerminal = async (data) => {
2222
};
2323

2424
// Get all terminals
25-
export const getAllTerminals = async () => {
25+
export const getAllTerminals = async (airportId=null) => {
2626
try {
27+
let where = {};
28+
29+
if (airportId) {
30+
where = {
31+
airportId: { contains: airportId },
32+
};
33+
}
34+
2735
const terminals = await prisma.terminal.findMany({
28-
include: { Airport: true },
36+
where,
37+
include: { Airport: true },
2938
});
39+
3040
return terminals;
3141
} catch (error) {
3242
console.error("Error fetching terminals:", error);

src/services/test/terminal.service.test.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,40 @@ describe("Terminal Service", () => {
8282
expect(result).toEqual(terminals);
8383
expect(prismaMock.terminal.findMany).toHaveBeenCalledWith({
8484
include: { Airport: true },
85+
where: {},
86+
});
87+
});
88+
89+
it("should fetch all terminals with search", async () => {
90+
const terminals = [
91+
{
92+
id: 1,
93+
name: "Terminal 1",
94+
type: "Domestic",
95+
airportId: "JFK",
96+
Airport: { iataCode: "JFK" },
97+
},
98+
{
99+
id: 2,
100+
name: "Terminal 2",
101+
type: "International",
102+
airportId: "JFK",
103+
Airport: { iataCode: "JFK"},
104+
},
105+
];
106+
107+
const airportId = "JFK";
108+
109+
prismaMock.terminal.findMany.mockResolvedValue(terminals); // Mock Prisma behavior
110+
111+
const result = await terminalService.getAllTerminals(airportId); // Pass airportId to the service
112+
113+
expect(result).toEqual(terminals); // Ensure the returned data matches the mock data
114+
expect(prismaMock.terminal.findMany).toHaveBeenCalledWith({
115+
include: { Airport: true }, // Ensure the query includes related Airport data
116+
where: {
117+
airportId: { contains: airportId }, // Ensure the query filters by airportId
118+
},
85119
});
86120
});
87121

@@ -116,14 +150,20 @@ describe("Terminal Service", () => {
116150
expect(result).toEqual(terminal);
117151
expect(prismaMock.terminal.findUnique).toHaveBeenCalledWith({
118152
where: { id: 1 },
119-
include: { Airport: true, FlightsDeparture: true, FlightsArrival: true },
153+
include: {
154+
Airport: true,
155+
FlightsDeparture: true,
156+
FlightsArrival: true,
157+
},
120158
});
121159
});
122160

123161
it("should throw an error if the terminal is not found", async () => {
124162
prismaMock.terminal.findUnique.mockResolvedValue(null);
125163

126-
await expect(terminalService.getTerminalById(1)).rejects.toThrow(AppError);
164+
await expect(terminalService.getTerminalById(1)).rejects.toThrow(
165+
AppError
166+
);
127167
expect(console.error).toHaveBeenCalledWith(
128168
"Error fetching terminal:",
129169
expect.any(Error)

0 commit comments

Comments
 (0)