Skip to content

Commit 7b2881f

Browse files
author
Víctor Martínez
committed
Merge branch 'dev' into main
2 parents 8734be0 + 5488b72 commit 7b2881f

File tree

12 files changed

+174
-31
lines changed

12 files changed

+174
-31
lines changed

src/controllers/bookings-controller.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const db = require("../models");
2-
const { getPropertyById } = require("./property-controller");
2+
const { getPropertyById } = require("../utils/properties");
33

44
async function bookProperty(req, res, next) {
55
const { uid } = req.user;
@@ -74,5 +74,38 @@ async function updateBookingAddress(req, res, next) {
7474
next(err);
7575
}
7676
}
77-
78-
module.exports = { getBookings, bookProperty, cancelBooking, updateBookingAddress };
77+
async function getBookingsByEmployeeId(req, res, next) {
78+
const { eid } = req.params;
79+
try {
80+
const bookings = await db.Bookings
81+
.find({ employeeId: eid })
82+
.select("-__v -contactInfo")
83+
.sort({ createdAt: -1 })
84+
.lean()
85+
.exec();
86+
res.status(200).send({
87+
data: bookings,
88+
error: null
89+
});
90+
} catch (err) {
91+
next(err);
92+
}
93+
}
94+
async function setStatus(req, res, next) {
95+
const { propertyId } = req.params;
96+
const { status } = req.body;
97+
try {
98+
const booking = await db.Bookings
99+
.findByIdAndUpdate(propertyId, { status }, { new: true })
100+
.select("-__v -contactInfo")
101+
.lean()
102+
.exec();
103+
res.status(200).send({
104+
data: booking,
105+
error: null
106+
});
107+
} catch (err) {
108+
next(err);
109+
}
110+
}
111+
module.exports = { getBookings, bookProperty, cancelBooking, updateBookingAddress, getBookingsByEmployeeId, setStatus };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const fetch = require("node-fetch");
2+
const config = require("../config");
3+
const { getPropertyById, searchAdminProperties } = require("../utils/properties");
4+
5+
async function getProperty(req, res, next) {
6+
const { propertyId } = req.params;
7+
try {
8+
const response = await getPropertyById(propertyId);
9+
res.status(200).send(response);
10+
} catch (err) {
11+
next(err);
12+
}
13+
}
14+
15+
async function searchProperties(req, res, next) {
16+
try {
17+
const response = await searchAdminProperties(req.query);
18+
res.status(200).send(response);
19+
} catch (err) {
20+
next(err);
21+
}
22+
}
23+
24+
module.exports = { getProperty, searchProperties };

src/middleware/stsauth-middleware.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/middleware/validators/bookings-validator.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const updateBookingAddressSchema = Joi.object({
1515
state: Joi.string().trim().required(),
1616
country: Joi.string().trim().required(),
1717
});
18+
const setStatusSchema = Joi.object({
19+
status: Joi.string().trim().required().valid("pending", "rejected", "accepted"),
20+
});
1821

1922
async function validateNewBooking(req, res, next) {
2023
try {
@@ -33,5 +36,13 @@ async function validateUpdateBookingAddress(req, res, next) {
3336
next({ statusCode: 400, message: err.details });
3437
}
3538
}
39+
async function validateSetStatus(req, res, next) {
40+
try {
41+
req.body = await setStatusSchema.validateAsync(req.body);
42+
next();
43+
} catch (err) {
44+
next({ statusCode: 400, message: err.details });
45+
}
46+
}
3647

37-
module.exports = { validateNewBooking, validateUpdateBookingAddress };
48+
module.exports = { validateNewBooking, validateUpdateBookingAddress, validateSetStatus };

src/mock/middleware/auth-middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const mockAuth = (clientId, mail) => {
2-
return (req, _, next) => {
2+
return () => (req, _, next) => {
33
req.user = {
44
uid: clientId,
55
email: mail

src/routes/__tests__/bookings-routes.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const setupTestDB = require("../../mock/seedTestDB");
1010

1111
const mockHome = setupTestDB.getHome();
1212

13-
jest.mock("../../controllers/property-controller", () => ({
13+
jest.mock("../../utils/properties", () => ({
1414
getPropertyById: () => mockHome
1515
}))
1616

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Mock Firebase Auth
2+
const MOCK_CLIENT_ID = "5d6ede6a0ba62570afcedd3a";
3+
const mockAuth = require("../../mock/middleware/auth-middleware")(MOCK_CLIENT_ID, "[email protected]");
4+
jest.mock('../../middleware/auth-middleware.js', () => mockAuth);
5+
// Require everything else
6+
const supertest = require("supertest");
7+
const testServer = require("../../mock/db-test-server");
8+
const app = require("../../server");
9+
const setupTestDB = require("../../mock/seedTestDB");
10+
const { parseQueryParams } = require("../../utils/properties");
11+
12+
const request = supertest(app);
13+
14+
beforeAll(async () => {
15+
await testServer.initTestServer();
16+
await setupTestDB.seedTestBookingsDB();
17+
});
18+
19+
afterAll(async () => {
20+
await testServer.clearCollections();
21+
await testServer.stopTestServer();
22+
});
23+
24+
describe("Properties routes", () => {
25+
it("Query params conversion", () => {
26+
const queryParams = {
27+
name: "Patata",
28+
age: 19,
29+
homeType: ["patata", "queso", "macarrones"],
30+
names: ["Victor", "Martinez", "Montané"],
31+
sold: true
32+
};
33+
//insert into db test_property with create property
34+
const string = parseQueryParams(queryParams);
35+
expect(string).toEqual("name=Patata&age=19&homeType[]=patata&homeType[]=queso&homeType[]=macarrones&names[]=Victor&names[]=Martinez&names[]=Montané&sold=true&");
36+
});
37+
});

src/routes/bookings-routes.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
const { Router } = require("express");
22
const auth = require("../middleware/auth-middleware");
33
const validateJWT = require("../middleware/jwt-middleware");
4-
const { validateNewBooking, validateUpdateBookingAddress } = require("../middleware/validators/bookings-validator");
4+
const { validateNewBooking, validateUpdateBookingAddress, validateSetStatus } = require("../middleware/validators/bookings-validator");
55

66
const bookingsRouter = Router();
77

88
const {
99
getBookings,
1010
cancelBooking,
1111
bookProperty,
12-
updateBookingAddress
12+
updateBookingAddress,
13+
getBookingsByEmployeeId,
14+
setStatus
1315
} = require("../controllers/bookings-controller");
1416

15-
bookingsRouter.post("/", auth, validateNewBooking, bookProperty);
16-
bookingsRouter.delete("/:propertyId", auth, cancelBooking);
17-
bookingsRouter.get("/all", auth, getBookings);
17+
bookingsRouter.post("/", auth(), validateNewBooking, bookProperty);
18+
bookingsRouter.delete("/:propertyId", auth(), cancelBooking);
19+
bookingsRouter.get("/all", auth(), getBookings);
1820

19-
bookingsRouter.patch("/:propertyId", validateUpdateBookingAddress, updateBookingAddress);
21+
bookingsRouter.get("/employees/:eid", validateJWT, getBookingsByEmployeeId);
22+
bookingsRouter.post("/:propertyId", validateJWT, validateSetStatus, setStatus);
23+
bookingsRouter.patch("/:propertyId", validateJWT, validateUpdateBookingAddress, updateBookingAddress);
2024

2125
module.exports = bookingsRouter;

src/routes/properties-routes.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { Router } = require("express");
2+
const auth = require("../middleware/auth-middleware");
3+
4+
const router = Router();
5+
6+
const {
7+
getProperty,
8+
searchProperties
9+
} = require("../controllers/properties-controller");
10+
11+
router.get("/:propertyId", auth(), getProperty);
12+
router.get("/", auth(), searchProperties);
13+
14+
module.exports = router;

src/routes/user-routes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const {
1212
} = require("../controllers/user-controller");
1313

1414
userRouter.post("/register", validateRegisterData, register);
15-
userRouter.post("/login", auth, login);
16-
userRouter.delete("/", auth, deleteUser);
17-
userRouter.patch("/profile", auth, validateUpdateData, update);
15+
userRouter.post("/login", auth(), login);
16+
userRouter.delete("/", auth(), deleteUser);
17+
userRouter.patch("/profile", auth(), validateUpdateData, update);
1818

1919
module.exports = userRouter;

0 commit comments

Comments
 (0)