Skip to content

Commit 2019e30

Browse files
codinghack30mm000810golden0810
committed
change db
Co-Authored-By: codinghack0810 <178182017+codinghack0810@users.noreply.github.com> Co-Authored-By: mm000810 <178183308+mm000810@users.noreply.github.com> Co-Authored-By: golden0810 <178187133+golden0810@users.noreply.github.com>
1 parent e76cca2 commit 2019e30

File tree

8 files changed

+117
-80
lines changed

8 files changed

+117
-80
lines changed

src/controllers/transaction.controller.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const db = require("../models");
22
const Transaction = db.transaction;
33
const User = db.user;
4-
const UserAccount = db.useraccount;
54
const ServiceProvider = db.serviceprovider;
65

76
const test = async (req, res) => {
@@ -14,8 +13,8 @@ const create = async (req, res) => {
1413
const { content, amount, service } = req.body;
1514

1615
// Check if User exists
17-
const userAccount = await UserAccount.findOne({ where: { id: userId } });
18-
if (!userAccount) {
16+
const user = await User.findOne({ where: { id: userId } });
17+
if (!user) {
1918
return res.status(404).json({ msg: "User not found." });
2019
}
2120

@@ -26,11 +25,15 @@ const create = async (req, res) => {
2625
if (!serviceProvider) {
2726
return res.status(404).json({ msg: "ServiceProvider not found." });
2827
}
29-
const serviceId = serviceProvider.id;
28+
const serviceProviderId = serviceProvider.id;
3029

3130
// Check if transaction exists
3231
const transaction = await Transaction.findOne({
33-
where: { content, userId: userId, serviceId: serviceId },
32+
where: {
33+
content,
34+
userId: userId,
35+
serviceProviderId: serviceProviderId,
36+
},
3437
});
3538
if (transaction) {
3639
return res.status(400).json({ msg: "Transaction already exists." });
@@ -41,7 +44,7 @@ const create = async (req, res) => {
4144
content,
4245
amount,
4346
userId: userId,
44-
serviceId: serviceId,
47+
serviceProviderId: serviceProviderId,
4548
});
4649

4750
res.status(201).json({
@@ -64,11 +67,15 @@ const update = async (req, res) => {
6467
if (!serviceProvider) {
6568
return res.status(404).json({ msg: "ServiceProvider not found." });
6669
}
67-
const serviceId = serviceProvider.id;
70+
const serviceProviderId = serviceProvider.id;
6871

6972
// Check if transaction exists
7073
const transaction = await Transaction.findOne({
71-
where: { content, userId: userId, serviceId: serviceId },
74+
where: {
75+
content,
76+
userId: userId,
77+
serviceProviderId: serviceProviderId,
78+
},
7279
});
7380
if (!transaction) {
7481
return res.status(404).json({ msg: "Transaction not found." });
@@ -101,13 +108,13 @@ const deleteTrans = async (req, res) => {
101108

102109
// Check if transaction exists
103110
const transaction = await Transaction.findOne({
104-
where: { user: userId, serviceId: serviceId },
111+
where: { user: userId, service: serviceId },
105112
});
106113
if (!transaction) {
107114
return res.status(404).json({ msg: "Transaction not found." });
108115
}
109116

110-
if(transaction.status) {
117+
if (transaction.status) {
111118
return res.status(400).json({ msg: "Transaction already paid." });
112119
}
113120

@@ -130,7 +137,7 @@ const paid = async (req, res) => {
130137
if (!transaction) {
131138
return res.status(404).json({ msg: "Transaction not found." });
132139
}
133-
140+
134141
// Check if transaction is paid
135142
if (transaction.status) {
136143
return res.status(400).json({ msg: "Transaction already paid." });

src/controllers/user.controller.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ const signup = async (req, res) => {
3131
securityAnswer,
3232
} = req.body;
3333

34+
if (
35+
!firstName ||
36+
!lastName ||
37+
!email ||
38+
!password ||
39+
!address1 ||
40+
!city ||
41+
!state ||
42+
!phone1 ||
43+
!securityQuestion ||
44+
!securityAnswer
45+
) {
46+
return res.status(400).json({ msg: "Please fill all data." });
47+
}
48+
3449
const userAccount = await UserAccount.findOne({ where: { email } });
3550
if (userAccount) {
3651
if (userAccount.active !== 1) {
@@ -52,7 +67,7 @@ const signup = async (req, res) => {
5267

5368
// Create the user
5469
await User.create({
55-
id: newUserAccount.id,
70+
userAccountId: newUserAccount.id,
5671
email: newUserAccount.email,
5772
firstName,
5873
lastName,
@@ -103,7 +118,7 @@ const signin = async (req, res) => {
103118
// const user = await User.findOne({
104119
// // include: {model: UserAccount, as: "user_account", attributes: ["isFirst"]},
105120
// include: { model: UserAccount, attributes: ["isFirst"] },
106-
// where: { id: userAccount.id },
121+
// where: { userAccountId: userAccount.id },
107122
// });
108123

109124
// Create JWT Payload
@@ -115,11 +130,18 @@ const signin = async (req, res) => {
115130
userAccount.loginTracking = true;
116131
const isFirst = await userAccount.isFirst;
117132

133+
//! remove this
118134
// Check if the user is first time login
119135
if (isFirst) {
120136
userAccount.isFirst = false;
121137
}
122138

139+
//TODO: use this
140+
// Check if the user is first time login
141+
// if (userAccount.isFirst) {
142+
// userAccount.isFirst = false;
143+
// }
144+
123145
// Sign Token
124146
jwt.sign(payload, SecurityOfKey, (err, token) => {
125147
if (err) throw err;
@@ -169,7 +191,7 @@ const updateUser = async (req, res) => {
169191
} = req.body;
170192

171193
// Check if the user exists
172-
const user = await User.findOne({ where: { id } });
194+
const user = await User.findOne({ where: { userAccountId: id } });
173195
if (!user) {
174196
return res.status(404).json({ msg: "User does not exist." });
175197
}
@@ -187,10 +209,12 @@ const updateUser = async (req, res) => {
187209
state,
188210
zip,
189211
},
190-
{ where: { id } }
212+
{ where: { userAccountId: id } }
191213
);
192214

193-
const updatedUser = await User.findOne({ where: { id } });
215+
const updatedUser = await User.findOne({
216+
where: { userAccountId: id },
217+
});
194218

195219
res.status(200).json({ msg: "Successfully.", updatedUser });
196220
} catch (error) {
@@ -254,8 +278,7 @@ const forgotPwd = async (req, res) => {
254278
//* POST /forgotpwd
255279
const forgotPwdAnswer = async (req, res) => {
256280
try {
257-
const { email, securityQuestion, securityAnswer } =
258-
req.body;
281+
const { email, securityQuestion, securityAnswer } = req.body;
259282
const userAccount = await UserAccount.findOne({
260283
where: { email, securityQuestion },
261284
});

src/models/index.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,40 @@ db.profile = require("./profile.model.js")(sequelize, Sequelize);
3030
db.review = require("./review.model.js")(sequelize, Sequelize);
3131
db.transaction = require("./transaction.model.js")(sequelize, Sequelize);
3232

33-
db.useraccount.hasOne(db.user, { foreignKey: "id" });
34-
db.user.belongsTo(db.useraccount, { foreignKey: "id" });
33+
db.useraccount.hasOne(db.user, { foreignKey: "userAccountId" });
34+
db.user.belongsTo(db.useraccount, {
35+
foreignKey: "userAccountId",
36+
onDelete: "CASCADE",
37+
});
3538

36-
db.useraccount.hasMany(db.profile, { foreginKey: "userId" });
37-
db.profile.belongsTo(db.useraccount, { foreginKey: "userId" });
39+
db.user.hasMany(db.profile, { foreginKey: "userId" });
40+
db.profile.belongsTo(db.user, { foreginKey: "userId", onDelete: "CASCADE" });
3841

39-
db.useraccount.hasMany(db.review, { foreginKey: "userId" });
40-
db.review.belongsTo(db.useraccount, { foreginKey: "userId" });
42+
db.user.hasMany(db.review, { foreginKey: "userId" });
43+
db.review.belongsTo(db.user, { foreginKey: "userId", onDelete: "CASCADE" });
4144

42-
db.useraccount.hasMany(db.transaction, { foreginKey: "userId" });
43-
db.transaction.belongsTo(db.useraccount, { foreginKey: "userId" });
45+
db.user.hasMany(db.transaction, { foreginKey: "userId" });
46+
db.transaction.belongsTo(db.user, {
47+
foreginKey: "userId",
48+
onDelete: "CASCADE",
49+
});
4450

45-
db.serviceprovider.hasMany(db.profile, { foreginKey: "serviceId" });
46-
db.profile.belongsTo(db.serviceprovider, { foreignKey: "serviceId" });
51+
db.serviceprovider.hasMany(db.profile, { foreginKey: "serviceProviderId" });
52+
db.profile.belongsTo(db.serviceprovider, {
53+
foreignKey: "serviceProviderId",
54+
onDelete: "CASCADE",
55+
});
4756

48-
db.serviceprovider.hasMany(db.review, { foreginKey: "serviceId" });
49-
db.review.belongsTo(db.serviceprovider, { foreignKey: "serviceId" });
57+
db.serviceprovider.hasMany(db.review, { foreginKey: "serviceProviderId" });
58+
db.review.belongsTo(db.serviceprovider, {
59+
foreignKey: "serviceProviderId",
60+
onDelete: "CASCADE",
61+
});
5062

51-
db.serviceprovider.hasMany(db.transaction, { foreginKey: "serviceId" });
52-
db.transaction.belongsTo(db.serviceprovider, { foreignKey: "serviceId" });
63+
db.serviceprovider.hasMany(db.transaction, { foreginKey: "serviceProviderId" });
64+
db.transaction.belongsTo(db.serviceprovider, {
65+
foreignKey: "serviceProviderId",
66+
onDelete: "CASCADE",
67+
});
5368

5469
module.exports = db;

src/models/profile.model.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
module.exports = (sequelize, Sequelize) => {
22
const Profile = sequelize.define("profile", {
3-
serviceId: {
3+
id: {
44
type: Sequelize.INTEGER,
5-
references: {
6-
model: "service_providers",
7-
key: "id",
8-
},
9-
onDelete: "CASCADE",
5+
primaryKey: true,
6+
autoIncrement: true,
107
},
11-
userId: {
8+
userId: { // Separate foreign key for users
129
type: Sequelize.INTEGER,
13-
references: {
14-
model: "user_accounts",
15-
key: "id",
16-
},
17-
onDelete: "CASCADE",
10+
allowNull: false, // Foreign key should not be null
11+
},
12+
serviceProviderId: { // Separate foreign key for service_providers
13+
type: Sequelize.INTEGER,
14+
allowNull: false, // Foreign key should not be null
1815
},
1916
rating: {
2017
type: Sequelize.FLOAT,

src/models/review.model.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
module.exports = (sequelize, Sequelize) => {
22
const Review = sequelize.define("review", {
3+
id: {
4+
type: Sequelize.INTEGER,
5+
primaryKey: true,
6+
autoIncrement: true,
7+
},
38
job: {
49
type: Sequelize.TEXT,
510
allowNull: false,
611
},
7-
userId: {
12+
userId: { // Separate foreign key for users
813
type: Sequelize.INTEGER,
9-
references: {
10-
model: "user_accounts",
11-
key: "id",
12-
},
13-
onDelete: "CASCADE",
14+
allowNull: false, // Foreign key should not be null
1415
},
1516
userRating: {
1617
type: Sequelize.INTEGER,
@@ -20,13 +21,9 @@ module.exports = (sequelize, Sequelize) => {
2021
type: Sequelize.TEXT,
2122
defaultValue: "",
2223
},
23-
serviceId: {
24+
serviceProviderId: { // Separate foreign key for service_providers
2425
type: Sequelize.INTEGER,
25-
references: {
26-
model: "service_providers",
27-
key: "id",
28-
},
29-
onDelete: "CASCADE",
26+
allowNull: false, // Foreign key should not be null
3027
},
3128
serviceRating: {
3229
type: Sequelize.INTEGER,

src/models/serviceprovider.model.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ module.exports = (sequelize, Sequelize) => {
1212
address: {
1313
type: Sequelize.TEXT,
1414
allowNull: false,
15-
// defaultValue: "",
1615
},
1716
areaOfOperation: {
1817
type: Sequelize.TEXT,
19-
// defaultValue: "",
2018
allowNull: false,
2119
},
2220
servicesProvided: {

src/models/transaction.model.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
module.exports = (sequelize, Sequelize) => {
22
const Transaction = sequelize.define("transaction", {
3+
id: {
4+
type: Sequelize.INTEGER,
5+
primaryKey: true,
6+
autoIncrement: true,
7+
},
38
content: {
49
type: Sequelize.TEXT,
510
allowNull: false,
611
},
7-
amount: {
8-
type: Sequelize.FLOAT,
9-
allowNull: false,
10-
},
11-
userId: {
12+
userId: { // Separate foreign key for users
1213
type: Sequelize.INTEGER,
13-
references: {
14-
model: "user_accounts",
15-
key: "id",
16-
},
17-
onDelete: "CASCADE",
14+
allowNull: false, // Foreign key should not be null
1815
},
19-
serviceId: {
16+
serviceProviderId: { // Separate foreign key for service_providers
2017
type: Sequelize.INTEGER,
21-
references: {
22-
model: "service_providers",
23-
key: "id",
24-
},
25-
onDelete: "CASCADE",
18+
allowNull: false, // Foreign key should not be null
19+
},
20+
amount: {
21+
type: Sequelize.FLOAT,
22+
allowNull: false,
2623
},
2724
status: {
2825
type: Sequelize.BOOLEAN,

src/models/user.model.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module.exports = (sequelize, Sequelize) => {
2-
const User = sequelize.define("user", {
2+
const User = sequelize.define("user", { // Use "user" for model name
33
id: {
44
type: Sequelize.INTEGER,
55
primaryKey: true,
6-
references: {
7-
model: "user_accounts",
8-
key: "id",
9-
},
10-
onDelete: "CASCADE",
6+
autoIncrement: true, // Auto-increment for primary key
7+
},
8+
userAccountId: { // Separate foreign key for user_accounts
9+
type: Sequelize.INTEGER,
10+
allowNull: false, // Foreign key should not be null
1111
},
1212
firstName: {
1313
type: Sequelize.STRING,
@@ -20,6 +20,9 @@ module.exports = (sequelize, Sequelize) => {
2020
email: {
2121
type: Sequelize.STRING,
2222
allowNull: false,
23+
validate: {
24+
isEmail: true, // Validate email format
25+
},
2326
},
2427
address1: {
2528
type: Sequelize.TEXT,

0 commit comments

Comments
 (0)