Skip to content

Commit be2d67c

Browse files
committed
Merge pull request #36 from softdev0810/feature
Feature
2 parents 058faba + f32a46d commit be2d67c

11 files changed

+81
-121
lines changed

src/controllers/transaction.controller.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ const deleteTrans = async (req, res) => {
8484

8585
// Check if transaction exists
8686
const transaction = await Transaction.findOne({
87-
where: { user: userId, service: serviceId, status: false },
87+
where: { user: userId, service: serviceId },
8888
});
8989
if (!transaction) {
90-
return res.status(404).json({ msg: "Transaction not found or already paid." });
90+
return res.status(404).json({ msg: "Transaction not found." });
91+
}
92+
93+
if(transaction.status) {
94+
return res.status(400).json({ msg: "Transaction already paid." });
9195
}
9296

9397
// Delete transaction

src/controllers/user.controller.js

Lines changed: 34 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ const signup = async (req, res) => {
2929
securityAnswer,
3030
} = req.body;
3131

32-
// Check if all fields are filled
33-
if (!email || !password || !securityQuestion || !securityAnswer) {
34-
return res.status(400).json({ msg: "Please fill in all fields." });
35-
}
36-
3732
const userAccount = await UserAccount.findOne({ where: { email } });
3833
if (userAccount) {
39-
return res.status(400).json({ msg: "User already exists." });
34+
if (userAccount.active !== 1) {
35+
return res
36+
.status(400)
37+
.json({ msg: "User is not verified. Please verify." });
38+
} else {
39+
return res.status(400).json({ msg: "User already exists." });
40+
}
4041
}
4142

4243
// Create the user account
@@ -61,7 +62,7 @@ const signup = async (req, res) => {
6162
});
6263

6364
// Respond with both created records
64-
res.status(201).json({ msg: "Successfully signed up." });
65+
res.status(201).json({ msg: "Successfully signed up. Please verify." });
6566
} catch (error) {
6667
res.status(500).json({ msg: error.message });
6768
}
@@ -77,9 +78,10 @@ const signin = async (req, res) => {
7778
return res.status(404).json({ msg: "User does not exist." });
7879
}
7980

80-
if (userAccount.loginTracking) {
81-
return res.status(400).json({ msg: "User is already logged in." });
82-
}
81+
// Check if the user is already logged in
82+
// if (userAccount.loginTracking) {
83+
// return res.status(400).json({ msg: "User is already logged in." });
84+
// }
8385

8486
// Check if the password is correct
8587
const passwordMatch = bcrypt.compareSync(
@@ -125,18 +127,9 @@ const signin = async (req, res) => {
125127

126128
const signout = async (req, res) => {
127129
try {
128-
const email = req.user.email;
129-
console.log("user => ", req.user);
130-
console.log("email => ", email);
131-
132-
const userAccount = await UserAccount.findOne({
133-
where: { email: email },
134-
});
135-
if (!userAccount) {
136-
return res.status(404).json({ msg: "User does not exist." });
137-
}
130+
const userAccount = req.user;
138131
userAccount.loginTracking = false;
139-
userAccount.save().then(() => {
132+
await userAccount.save().then(() => {
140133
res.status(200).json({ msg: "Successfully signed out." });
141134
});
142135
} catch (error) {
@@ -146,12 +139,13 @@ const signout = async (req, res) => {
146139

147140
const updateUser = async (req, res) => {
148141
try {
149-
const id = req.user.id;
142+
const userAccount = req.user;
143+
const { id } = userAccount;
150144

151145
const {
152146
firstName,
153147
lastName,
154-
email,
148+
// email,
155149
address1,
156150
address2,
157151
phone1,
@@ -163,30 +157,24 @@ const updateUser = async (req, res) => {
163157
securityAnswer,
164158
} = req.body;
165159

166-
// Check if the user exists
167-
const userAccount = await UserAccount.findOne({ where: { id: id } });
168-
if (!userAccount) {
169-
return res.status(404).json({ msg: "User does not exist." });
170-
}
171-
172-
if (email) {
173-
// Check if the email is already taken
174-
const emailTaken = await UserAccount.findOne({
175-
where: { email },
176-
});
177-
if (emailTaken && emailTaken.id !== id) {
178-
return res.status(400).json({ msg: "Email is already taken." });
179-
}
180-
}
160+
// if (email) {
161+
// // Check if the email is already taken
162+
// const emailTaken = await UserAccount.findOne({
163+
// where: { email },
164+
// });
165+
// if (emailTaken && emailTaken.id !== id) {
166+
// return res.status(400).json({ msg: "Email is already taken." });
167+
// }
168+
// }
181169

182170
// Update the userAccount
183171
await UserAccount.update(
184172
{
185-
email,
173+
// email,
186174
securityQuestion,
187175
securityAnswer,
188176
},
189-
{ where: { id: id } }
177+
{ where: { id } }
190178
);
191179

192180
userAccount.isFirst = false;
@@ -197,7 +185,7 @@ const updateUser = async (req, res) => {
197185
{
198186
firstName,
199187
lastName,
200-
email,
188+
// email,
201189
address1,
202190
address2,
203191
phone1,
@@ -206,62 +194,22 @@ const updateUser = async (req, res) => {
206194
state,
207195
zip,
208196
},
209-
{ where: { id: id } }
197+
{ where: { id } }
210198
);
211199

212-
const updatedUser = await User.findOne({ where: { id: id } });
200+
const updatedUser = await User.findOne({ where: { id } });
213201

214202
res.status(200).json({ msg: "Successfully.", updatedUser });
215203
} catch (error) {
216204
res.status(500).json({ msg: error.message });
217205
}
218206
};
219207

220-
const fillUser = async (req, res) => {
221-
try {
222-
const { id } = req.user.id;
223-
const {
224-
firstName,
225-
lastName,
226-
address1,
227-
address2,
228-
phone1,
229-
phone2,
230-
city,
231-
state,
232-
zip,
233-
} = req.body;
234-
const user = await User.findOne({ where: { id } });
235-
if (!user) {
236-
return res.status(404).json({ msg: "User not found." });
237-
}
238-
239-
const filledUser = await User.update(
240-
{
241-
firstName,
242-
lastName,
243-
address1,
244-
address2,
245-
phone1,
246-
phone2,
247-
city,
248-
state,
249-
zip,
250-
},
251-
{ where: { id } }
252-
);
253-
const userAccount = await UserAccount.findOne({ where: { id } });
254-
userAccount.isFirst = false;
255-
await userAccount.save();
256-
res.status(200).json({ msg: "Successfully filled.", filledUser });
257-
} catch (error) {
258-
res.status(500).json({ msg: error.message });
259-
}
260-
};
261-
262208
const search = async (req, res) => {
263209
try {
264-
const { key } = req.params;
210+
const { key } = req.query;
211+
212+
console.log(key);
265213

266214
const searchedService = await ServiceProvider.findAll({
267215
where: {
@@ -289,7 +237,6 @@ module.exports = {
289237
signup,
290238
signin,
291239
signout,
292-
fillUser,
293240
updateUser,
294241
search,
295242
};

src/controllers/verify.controller.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const sendCode = async (req, res) => {
1313

1414
// Check if the user exists
1515
if (!userAccount) {
16-
return res.status(404).json({ msg: "User does not exist." });
16+
return res.status(404).json({ msg: "User did not signup. Please signup." });
1717
}
1818

1919
// Check if the user is already verified
@@ -61,11 +61,14 @@ const sendCode = async (req, res) => {
6161
const verifyCode = async (req, res) => {
6262
try {
6363
const { email, code } = req.body;
64+
6465
const userAccount = await UserAccount.findOne({ where: { email } });
6566

6667
// Check if the user exists
6768
if (!userAccount) {
68-
return res.status(404).json({ msg: "User does not exist." });
69+
return res
70+
.status(404)
71+
.json({ msg: "User did not signup. Please signup." });
6972
}
7073

7174
// Check if the user is already verified
@@ -76,16 +79,16 @@ const verifyCode = async (req, res) => {
7679
if (userAccount.active == 0) {
7780
return res
7881
.status(400)
79-
.json({ msg: "Don't send code. Please resend." });
82+
.json({ msg: "Please resend." });
8083
}
8184
if (userAccount.active == code) {
8285
userAccount.active = 1;
8386
await userAccount.save();
84-
res.status(200).json({ msg: "Email verified." });
87+
res.status(200).json({ msg: "User verified. Please sign in" });
8588
} else {
8689
userAccount.active = 0;
8790
await userAccount.save();
88-
res.status(400).json({ msg: "Invalid code." });
91+
res.status(400).json({ msg: "Invalid code. Please resend." });
8992
}
9093
} catch (error) {
9194
res.status(500).json({ msg: error.message });

src/middlewares/requiredAuth.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ const secretOrKey = process.env.JWT_ACCESS_TOKEN_SECRET_PRIVATE;
99
module.exports = async (req, res, next) => {
1010
try {
1111
if (!req.headers.authorization) {
12-
return res
13-
.status(401)
14-
.json({ msg: "No authentication." });
12+
return res.status(401).json({ msg: "No authentication." });
1513
}
1614
const token = req.headers.authorization;
1715
if (!token) {
@@ -25,10 +23,15 @@ module.exports = async (req, res, next) => {
2523
}
2624

2725
const userAccount = await UserAccount.findOne({
28-
where: { id: decoded.id },
26+
where: { email: decoded.email },
2927
});
28+
29+
if (!userAccount) {
30+
return res.status(404).json({ msg: "User does not exist." });
31+
}
32+
3033
req.user = userAccount;
31-
next();
34+
await next();
3235
});
3336
} catch (error) {
3437
console.error("Something wrong with auth middleware.", error);

src/middlewares/requiredVerify.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = async (req, res, next) => {
88
// Check if the user exists
99
const userAccount = await UserAccount.findOne({ where: { email } });
1010
if (!userAccount) {
11-
return res.status(404).json({ msg: "User does not exist." });
11+
return res.status(404).json({ msg: "User does not exist. Please signed up." });
1212
}
1313

1414
// Check if the user is already verified

src/models/profile.model.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ module.exports = (sequelize, Sequelize) => {
1717
onDelete: "CASCADE",
1818
},
1919
rating: {
20-
type: Sequelize.INTEGER,
21-
allowNull: false,
20+
type: Sequelize.FLOAT,
21+
defaultValue: 0,
2222
},
2323
comment: {
24-
type: Sequelize.TEXT,
25-
allowNull: false,
24+
type: Sequelize.STRING,
25+
defaultValue: "",
2626
},
2727
userFeedback: {
28-
type: Sequelize.TEXT,
28+
type: Sequelize.STRING,
2929
defaultValue: "",
3030
},
3131
serviceFeedback: {
32-
type: Sequelize.TEXT,
32+
type: Sequelize.STRING,
3333
defaultValue: "",
3434
},
3535
});

src/models/serviceprovider.model.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = (sequelize, Sequelize) => {
1111
},
1212
address: {
1313
type: Sequelize.TEXT,
14-
allowNull: false,
14+
// allowNull: false,
15+
defaultValue: "",
1516
},
1617
areaOfOperation: {
1718
type: Sequelize.TEXT,

src/models/transaction.model.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module.exports = (sequelize, Sequelize) => {
1919
model: "user_accounts",
2020
key: "id",
2121
},
22-
allowNull: false,
2322
onDelete: "CASCADE",
2423
},
2524
service: {
@@ -28,7 +27,6 @@ module.exports = (sequelize, Sequelize) => {
2827
model: "service_providers",
2928
key: "id",
3029
},
31-
allowNull: false,
3230
onDelete: "CASCADE",
3331
},
3432
status: {

0 commit comments

Comments
 (0)