Skip to content

Commit 21a6587

Browse files
committed
2 parents 33810aa + f4ec5bc commit 21a6587

File tree

13 files changed

+99
-109
lines changed

13 files changed

+99
-109
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
# Express + MySQL
1+
# The Backend Server
2+
3+
This is stage in Developement.
4+
5+
Run Command Prompt or Terminal in director of project.
6+
7+
1. `npm install` or `yarn install`
8+
9+
2. Install XAMPP and Start Apache and MySQL for database in XAMPP Control Panel.
10+
11+
3. Create the database(`db_scope`) for server in XAMPP Apache server[http://localhost/phpmyadmin/].
12+
13+
4. `npm start` or `yarn start`
14+
15+
5. The server is running on http://localhost:5000
16+
17+
6. We get the url(https) for server from ngrok[https://dashboard.ngrok.com].
18+
19+
7. Final, we use this url for server.
20+
ex: `https://singularly-hot-cod.ngrok-free.app`

src/config/db.config.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
const dotenv = require("dotenv");
22

3+
// Load environment variables from .env file
34
dotenv.config();
45

6+
// Parse integer values for database pool configuration from environment variables
57
const max = parseInt(process.env.DB_POOL_MAX);
68
const min = parseInt(process.env.DB_POOL_MIN);
79
const acquire = parseInt(process.env.DB_POOL_ACQUIRE);
810
const idle = parseInt(process.env.DB_POOL_IDLE);
911

12+
// Database connection configuration object
1013
const config = {
11-
HOST: process.env.DB_HOST,
12-
USER: process.env.DB_USER,
13-
PASSWORD: process.env.DB_PASSWORD,
14-
DB: process.env.DB_DATABASE,
15-
dialect: process.env.DB_DIALECT,
14+
HOST: process.env.DB_HOST, // Database host address
15+
USER: process.env.DB_USER, // Database user
16+
PASSWORD: process.env.DB_PASSWORD, // Database user's password
17+
DB: process.env.DB_DATABASE, // Name of the database
18+
dialect: process.env.DB_DIALECT, // SQL dialect (e.g., 'mysql', 'postgres')
1619
pool: {
17-
max: max,
18-
min: min,
19-
acquire: acquire,
20-
idle: idle,
20+
// Pool configuration for managing database connections
21+
max: max, // Maximum number of connections in pool
22+
min: min, // Minimum number of connections in pool
23+
acquire: acquire, // Maximum time (ms) that pool will try to get connection before throwing error
24+
idle: idle, // Maximum time (ms) that a connection can be idle before being released
2125
},
2226
};
2327

28+
// Export the configuration object for use in other parts of the application
2429
module.exports = config;

src/controllers/service.controller.js

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

src/controllers/transaction.controller.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const update = async (req, res) => {
6363
const userId = req.user.id;
6464
const { content, amount, service } = req.body;
6565

66+
// Check if ServiceProvider exists
6667
const serviceProvider = await ServiceProvider.findOne({
6768
where: { name: service },
6869
});
@@ -86,6 +87,7 @@ const update = async (req, res) => {
8687
// Update transaction
8788
transaction.amount = amount;
8889
await transaction.save();
90+
8991
res.status(200).json({
9092
msg: "Transaction updated successfully.",
9193
transaction,
@@ -101,12 +103,14 @@ const deleteTrans = async (req, res) => {
101103
const userId = req.user.id;
102104
const { service } = req.body;
103105

106+
// Check if ServiceProvider exists
104107
const serviceProvider = await ServiceProvider.findOne({
105108
where: { name: service },
106109
});
107110
if (!serviceProvider) {
108111
return res.status(404).json({ msg: "ServiceProvider not found." });
109112
}
113+
110114
const serviceId = serviceProvider.id;
111115

112116
// Check if transaction exists

src/controllers/user.controller.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ const search = async (req, res) => {
227227
try {
228228
const { key } = req.query;
229229

230+
// Check if the service exists
230231
const searchedService = await ServiceProvider.findAll({
231232
where: {
232233
servicesProvided: {

src/controllers/verify.controller.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ const sendCode = async (req, res) => {
1010
try {
1111
const { email } = req.body;
1212

13-
const userAccount = await UserAccount.findOne({ where: { email } });
14-
1513
// Check if the user exists
14+
const userAccount = await UserAccount.findOne({ where: { email } });
1615
if (!userAccount) {
1716
return res.status(404).json({ msg: "User did not signup. Please signup." });
1817
}
@@ -37,6 +36,8 @@ const sendCode = async (req, res) => {
3736
pass: process.env.SMTP_PASSWORD,
3837
},
3938
});
39+
40+
// Defined transport object
4041
const mailOptions = {
4142
from: `${process.env.APP_NAME} <${process.env.EMAIL_FROM}>`,
4243
to: email,
@@ -47,6 +48,8 @@ const sendCode = async (req, res) => {
4748
<h1 style="padding: 12px; border-left: 4px solid #d0d0d0; font-style: italic;">${code}</h1>
4849
<p>Best wishes,<br>Scope Inc.</p>`,
4950
};
51+
52+
// send mail with defined transport object
5053
transporter.sendMail(mailOptions, (error, info) => {
5154
if (error) {
5255
res.status(500).json({ msg: error.message });

src/middlewares/requiredAdmin.js

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

src/middlewares/requiredAuth.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,34 @@ const secretOrKey = process.env.JWT_ACCESS_TOKEN_SECRET_PRIVATE;
88

99
module.exports = async (req, res, next) => {
1010
try {
11+
// Check if the user is authenticated
1112
if (!req.headers.authorization) {
1213
return res.status(401).json({ msg: "No authentication." });
1314
}
15+
16+
// Check if the token is existed.
1417
const token = req.headers.authorization;
1518
if (!token) {
1619
return res
1720
.status(401)
1821
.json({ msg: "No token, authentication denied." });
1922
}
23+
24+
// Verify token
2025
jwt.verify(token, secretOrKey, async (error, decoded) => {
2126
if (error) {
2227
return res.status(401).json({ msg: "Token is not valid." });
2328
}
2429

30+
// Check if the user exists
2531
const userAccount = await UserAccount.findOne({
2632
where: { email: decoded.email },
2733
});
28-
2934
if (!userAccount) {
3035
return res.status(404).json({ msg: "User does not exist." });
3136
}
3237

38+
// Add user to request
3339
req.user = userAccount;
3440
await next();
3541
});

src/models/index.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const dbConfig = require("../config/db.config.js");
1+
const dbConfig = require("../config/db.config.js"); // Import database configuration
22

33
const Sequelize = require("sequelize");
44

5+
// Initialize Sequelize instance with database credentials and pool settings
56
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
67
host: dbConfig.HOST,
78
dialect: dbConfig.dialect,
@@ -20,6 +21,7 @@ const db = {};
2021
db.Sequelize = Sequelize;
2122
db.sequelize = sequelize;
2223

24+
// Import and initialize all models
2325
db.useraccount = require("./useraccount.model.js")(sequelize, Sequelize);
2426
db.user = require("./user.model.js")(sequelize, Sequelize);
2527
db.serviceprovider = require("./serviceprovider.model.js")(
@@ -30,40 +32,49 @@ db.profile = require("./profile.model.js")(sequelize, Sequelize);
3032
db.review = require("./review.model.js")(sequelize, Sequelize);
3133
db.transaction = require("./transaction.model.js")(sequelize, Sequelize);
3234

35+
// Define associations between models
36+
37+
// One-to-One relationship: UserAccount -> User
3338
db.useraccount.hasOne(db.user, { foreignKey: "userAccountId" });
3439
db.user.belongsTo(db.useraccount, {
3540
foreignKey: "userAccountId",
3641
onDelete: "CASCADE",
3742
});
3843

39-
db.user.hasMany(db.profile, { foreginKey: "userId" });
40-
db.profile.belongsTo(db.user, { foreginKey: "userId", onDelete: "CASCADE" });
44+
// One-to-Many relationship: User -> Profile
45+
db.user.hasMany(db.profile, { foreignKey: "userId" });
46+
db.profile.belongsTo(db.user, { foreignKey: "userId", onDelete: "CASCADE" });
4147

42-
db.user.hasMany(db.review, { foreginKey: "userId" });
43-
db.review.belongsTo(db.user, { foreginKey: "userId", onDelete: "CASCADE" });
48+
// One-to-Many relationship: User -> Review
49+
db.user.hasMany(db.review, { foreignKey: "userId" });
50+
db.review.belongsTo(db.user, { foreignKey: "userId", onDelete: "CASCADE" });
4451

45-
db.user.hasMany(db.transaction, { foreginKey: "userId" });
52+
// One-to-Many relationship: User -> Transaction
53+
db.user.hasMany(db.transaction, { foreignKey: "userId" });
4654
db.transaction.belongsTo(db.user, {
47-
foreginKey: "userId",
55+
foreignKey: "userId",
4856
onDelete: "CASCADE",
4957
});
5058

51-
db.serviceprovider.hasMany(db.profile, { foreginKey: "serviceProviderId" });
59+
// One-to-Many relationship: ServiceProvider -> Profile
60+
db.serviceprovider.hasMany(db.profile, { foreignKey: "serviceProviderId" });
5261
db.profile.belongsTo(db.serviceprovider, {
5362
foreignKey: "serviceProviderId",
5463
onDelete: "CASCADE",
5564
});
5665

57-
db.serviceprovider.hasMany(db.review, { foreginKey: "serviceProviderId" });
66+
// One-to-Many relationship: ServiceProvider -> Review
67+
db.serviceprovider.hasMany(db.review, { foreignKey: "serviceProviderId" });
5868
db.review.belongsTo(db.serviceprovider, {
5969
foreignKey: "serviceProviderId",
6070
onDelete: "CASCADE",
6171
});
6272

63-
db.serviceprovider.hasMany(db.transaction, { foreginKey: "serviceProviderId" });
73+
// One-to-Many relationship: ServiceProvider -> Transaction
74+
db.serviceprovider.hasMany(db.transaction, { foreignKey: "serviceProviderId" });
6475
db.transaction.belongsTo(db.serviceprovider, {
6576
foreignKey: "serviceProviderId",
6677
onDelete: "CASCADE",
6778
});
6879

69-
module.exports = db;
80+
module.exports = db; // Export the configured `db` object for use in other parts of the application

src/models/useraccount.model.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,24 @@ module.exports = (sequelize, Sequelize) => {
4141
},
4242
});
4343

44+
// Define a beforeCreate hook on the UserAccount model
4445
UserAccount.beforeCreate(async (userAccount, options) => {
46+
// Check if password is provided and hash it using bcrypt
4547
if (userAccount.password) {
46-
const salt = await bcrypt.genSalt(10);
48+
const salt = await bcrypt.genSalt(10); // Generate salt with 10 rounds
4749
userAccount.password = await bcrypt.hash(
4850
userAccount.password,
4951
salt
50-
);
52+
); // Hash the password with the generated salt
5153
}
5254

55+
// Check if securityAnswer is provided and hash it using bcrypt
5356
if (userAccount.securityAnswer) {
54-
const salt = await bcrypt.genSalt(10);
57+
const salt = await bcrypt.genSalt(10); // Generate salt with 10 rounds
5558
userAccount.securityAnswer = await bcrypt.hash(
5659
userAccount.securityAnswer,
5760
salt
58-
);
61+
); // Hash the security answer with the generated salt
5962
}
6063
});
6164

0 commit comments

Comments
 (0)