Skip to content

Commit a88ada2

Browse files
author
Víctor Martínez
authored
[First Release]
Add property joi validation (#44) (#46) * feat: edit/create property validation done * test: tests modified to pass the new validation resolves #45 #38
1 parent 410e8b8 commit a88ada2

File tree

9 files changed

+271
-203
lines changed

9 files changed

+271
-203
lines changed

src/controllers/property-controller.js

Lines changed: 119 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -9,175 +9,163 @@ async function searchProperty(req, res, next) {
99
const { uid } = req.employee;
1010
const filters = req.query;
1111

12-
const properties = await
13-
(filters.kind == "Home" ?
14-
db.Home.find({
15-
employee_id: uid,
16-
...buildPropertyBaseMatchingRules(filters),
17-
...buildHomeMatchingRules(filters)
18-
})
19-
: db.Office.find({
20-
employee_id: uid,
21-
...buildPropertyBaseMatchingRules(filters),
22-
...buildOfficeMatchingRules(filters)
23-
})
24-
)
25-
.sort({ created_at: -1 })
26-
.select("_id employee_id sold kind bedRooms bathRooms address price surface buildingUse images")
27-
.lean()
28-
.exec()
29-
.catch(next);
30-
31-
res.status(200).send({
32-
data: properties,
33-
error: null,
34-
});
12+
try {
13+
const properties = await
14+
(filters.kind == "Home" ?
15+
db.Home.find({
16+
employee_id: uid,
17+
...buildPropertyBaseMatchingRules(filters),
18+
...buildHomeMatchingRules(filters)
19+
})
20+
: db.Office.find({
21+
employee_id: uid,
22+
...buildPropertyBaseMatchingRules(filters),
23+
...buildOfficeMatchingRules(filters)
24+
})
25+
)
26+
.sort({ created_at: -1 })
27+
.select("_id employee_id sold kind bedRooms bathRooms address price surface buildingUse images")
28+
.lean()
29+
.exec()
30+
31+
res.status(200).send({
32+
data: properties,
33+
error: null,
34+
});
35+
} catch (err) {
36+
next(err);
37+
}
3538
}
3639

3740
async function getPropertyById(req, res, next) {
3841
const { uid } = req.employee;
3942
const propertyID = req.params.propertyID;
4043

41-
const property = await db.Property.findById(propertyID)
42-
.lean()
43-
.exec()
44-
.catch(next);
45-
46-
if (!property) {
47-
next({ statusCode: 404, message: "Property not found" })
48-
return;
49-
}
50-
if (property.employee_id != uid) {
51-
next({ statusCode: 403, message: "You cannot access this property" })
52-
return;
44+
try {
45+
const property = await db.Property.findById(propertyID)
46+
.lean()
47+
.exec();
48+
49+
if (!property) {
50+
return next({ statusCode: 404, message: "Property not found" });
51+
}
52+
if (property.employee_id != uid) {
53+
return next({ statusCode: 403, message: "You cannot access this property" });
54+
}
55+
56+
res.status(200).send({
57+
data: property,
58+
error: null,
59+
});
60+
} catch (err) {
61+
next(err);
5362
}
54-
55-
res.status(200).send({
56-
data: property,
57-
error: null,
58-
});
5963
}
6064

6165
async function deleteProperty(req, res, next) {
6266
const { uid } = req.employee;
6367
const propertyID = req.params.propertyID;
6468

65-
//get property by id
66-
const propertyFound = await db.Property.findById(propertyID)
67-
.lean()
68-
.exec()
69-
.catch(next);
70-
71-
if (!propertyFound) {
72-
next({ statusCode: 404, message: "Property not found" })
73-
return;
74-
}
75-
if (propertyFound.employee_id != uid) {
76-
next({ statusCode: 403, message: "You cannot access this property" })
77-
return;
78-
}
69+
try {
70+
const propertyFound = await db.Property.findById(propertyID)
71+
.lean()
72+
.exec();
7973

80-
const property = await db.Property.findByIdAndDelete(propertyID)
81-
.lean()
82-
.exec()
83-
.catch(next);
74+
if (!propertyFound) return next({ statusCode: 404, message: "Property not found" });
75+
if (propertyFound.employee_id != uid) return next({ statusCode: 403, message: "You cannot access this property" });
8476

85-
res.status(200).send({
86-
data: property,
87-
error: null,
88-
});
77+
const property = await db.Property.findByIdAndDelete(propertyID)
78+
.lean()
79+
.exec();
80+
res.status(200).send({
81+
data: property,
82+
error: null,
83+
});
84+
} catch (err) {
85+
next(err);
86+
}
8987
}
9088

9189
async function editProperty(req, res, next) {
9290
const { uid } = req.employee;
9391
const { kind } = req.body;
9492
const propertyID = req.params.propertyID;
9593

96-
//get property by id
97-
const propertyFound = await db.Property.findById(propertyID)
98-
.lean()
99-
.exec()
100-
.catch(next);
101-
102-
if (!propertyFound) {
103-
next({ statusCode: 404, message: "Property not found" })
104-
return;
105-
}
106-
if (propertyFound.employee_id != uid) {
107-
next({ statusCode: 403, message: "You cannot access this property" })
108-
return;
109-
}
110-
111-
const propertyData = { ...req.body };
112-
113-
const property = await
114-
(kind === "Home" ? db.Home : db.Office)
115-
.findByIdAndUpdate(propertyID, propertyData, {
116-
new: true,
117-
})
94+
try {
95+
const propertyFound = await db.Property.findById(propertyID)
11896
.lean()
119-
.exec()
120-
.catch(next);
121-
122-
res.status(200).send({
123-
data: property,
124-
error: null,
125-
});
97+
.exec();
98+
99+
if (!propertyFound) return next({ statusCode: 404, message: "Property not found" });
100+
if (propertyFound.employee_id != uid) return next({ statusCode: 403, message: "You cannot access this property" });
101+
102+
const propertyData = { ...req.body };
103+
104+
const property = await
105+
(kind === "Home" ? db.Home : db.Office)
106+
.findByIdAndUpdate(propertyID, propertyData, {
107+
new: true,
108+
})
109+
.lean()
110+
.exec();
111+
112+
res.status(200).send({
113+
data: property,
114+
error: null,
115+
});
116+
} catch (err) {
117+
next(err);
118+
}
126119
}
127120

128121
async function createProperty(req, res, next) {
129122
const { kind } = req.body;
130123
const { uid, email } = req.employee;
131124

132-
//get employee with db.Employee.findbyId ( uid)
133-
const employee = await db.Employee.findById(uid).lean().exec().catch(next);
134-
//grab email, id, phone
135-
//const contactinfo with those params
136-
const contactInfo = {
137-
phone: employee.phone,
138-
email: email,
139-
};
140-
141-
const propertyData = { employee_id: employee._id, ...req.body, contactInfo };
142-
143-
const property = await (
144-
kind === "Home"
145-
? db.Home.create(propertyData).catch(next)
146-
: db.Office.create(propertyData).catch(next)
147-
);
148-
149-
res.status(201).send({
150-
data: property,
151-
error: null,
152-
});
125+
try {
126+
const employee = await db.Employee.findById(uid).lean().exec();
127+
128+
const contactInfo = {
129+
phone: employee.phone,
130+
email: email,
131+
};
132+
const propertyData = { employee_id: employee._id, ...req.body, contactInfo };
133+
134+
const property =
135+
kind === "Home"
136+
? await db.Home.create(propertyData)
137+
: await db.Office.create(propertyData);
138+
139+
res.status(201).send({
140+
data: property,
141+
error: null,
142+
});
143+
} catch (err) {
144+
next(err);
145+
}
153146
}
154147

155148
async function setPropertyAsSold(req, res, next) {
156149
const { uid } = req.employee;
157150
const { propertyID } = req.params;
158151

159-
//get property by id
160-
const property = await db.Property.findById(propertyID)
161-
.exec()
162-
.catch(next);
152+
try {
153+
const property = await db.Property.findById(propertyID).exec();
163154

164-
if (!property) {
165-
next({ statusCode: 404, message: "Property not found" })
166-
return;
167-
}
168-
if (property.employee_id != uid) {
169-
next({ statusCode: 403, message: "You cannot access this property" })
170-
return;
171-
}
155+
if (!property) return next({ statusCode: 404, message: "Property not found" });
156+
if (property.employee_id != uid) return next({ statusCode: 403, message: "You cannot access this property" });
172157

173-
property.sold = true;
174-
property.soldDate = Date.now();
175-
await property.save().catch(next);
158+
property.sold = true;
159+
property.soldDate = Date.now();
160+
await property.save();
176161

177-
res.status(200).send({
178-
data: property.toObject(),
179-
error: null,
180-
});
162+
res.status(200).send({
163+
data: property.toObject(),
164+
error: null,
165+
});
166+
} catch (err) {
167+
next(err);
168+
}
181169
}
182170

183171
module.exports = {

0 commit comments

Comments
 (0)