Skip to content

Commit 7024085

Browse files
authored
Merge pull request #43 from Capstone-C2SE02-TI/fix-old-function
fix follow and profile
2 parents b6d31c6 + a3a94de commit 7024085

File tree

13 files changed

+137
-143
lines changed

13 files changed

+137
-143
lines changed

package-lock.json

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"license": "MIT",
1919
"homepage": "https://github.com/Capstone-C2SE02-TI/backend-node-mongodb#readme",
2020
"dependencies": {
21+
"@matg97/mongoose-plugin-autoinc": "^1.1.10",
2122
"and": "^0.0.3",
2223
"bcrypt": "^5.1.0",
2324
"build": "^0.1.4",

src/controllers/Auth.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
import dotenv from "dotenv";
22
dotenv.config();
33

4-
import { createNewUser } from "../services/crudDatabase/user.js";
5-
import {
6-
cryptWalletAddress,
7-
decryptWalletAddress,
8-
encryptWalletAddress
9-
} from "../helpers/index.js";
4+
import { createNewUser} from "../services/crudDatabase/user.js";
105

116
const TI_AUTH_COOKIE = process.env.TI_AUTH_COOKIE;
127

138
function AuthController() {
149
this.signup = async (req, res, next) => {
1510
const { walletAddress } = req.body;
1611

17-
const hashWallet = encryptWalletAddress(walletAddress);
18-
19-
const detailCreated = await createNewUser(hashWallet.encryptedData);
12+
const detailCreated = await createNewUser(walletAddress);
2013

2114
detailCreated.created
2215
? res.status(200).json({

src/controllers/Display.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ function DisplayController() {
160160
};
161161

162162
this.getSharks = async (req, res, next) => {
163-
const { userId } = req.query;
164-
await getListOfSharks(userId)
163+
const { walletAddress } = req.query;
164+
await getListOfSharks(walletAddress)
165165
.then((datas) =>
166166
datas.length === 0
167167
? res.status(400).json({

src/controllers/User.js

+30-31
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ function UserController() {
4949
};
5050

5151
this.updateUserProfile = async (req, res, next) => {
52-
let userId = req.query.userId;
52+
let walletAddress = req.query.walletAddress;
5353

54-
if (!userId) userId = null;
54+
if (!walletAddress) walletAddress = null;
5555
else {
56-
const userIdCheck = _.toString(userId);
57-
if (_.isNaN(userIdCheck)) userId = undefined;
58-
else userId = Number(userIdCheck);
56+
const walletAddressCheck = _.toString(walletAddress);
57+
if (_.isNaN(walletAddressCheck)) walletAddress = undefined;
5958
}
6059

6160
const { status, error } = await validateUpdateProfileBody(req, res, next);
@@ -64,7 +63,7 @@ function UserController() {
6463
return res.status(400).json({ message: error, error: error });
6564
else {
6665
const updateInfo = req.body;
67-
await updateUserProfile(userId, updateInfo)
66+
await updateUserProfile(walletAddress, updateInfo)
6867
.then((data) =>
6968
data === "success"
7069
? res.status(200).json({
@@ -86,15 +85,14 @@ function UserController() {
8685
};
8786

8887
this.upgradePremiumAccount = async (req, res, next) => {
89-
let userId = req.body.userId;
88+
let walletAddress = req.body.walletAddress;
9089

91-
if (!userId) userId = null;
90+
if (!walletAddress) walletAddress = null;
9291
else {
93-
if (isNaN(userId)) userId = undefined;
94-
else userId = Number(userId);
92+
if (isNaN(walletAddress)) walletAddress = undefined;
9593
}
9694

97-
await upgradeUserPremiumAccount(userId)
95+
await upgradeUserPremiumAccount(walletAddress)
9896
.then((data) =>
9997
data === "success"
10098
? res.status(200).json({
@@ -115,12 +113,11 @@ function UserController() {
115113
};
116114

117115
this.followSharkWallet = async (req, res, next) => {
118-
let { userId, sharkId } = req.body;
116+
let { walletAddress, sharkId } = req.body;
119117

120-
if (!userId) userId = null;
118+
if (!walletAddress) walletAddress = null;
121119
else {
122-
if (isNaN(userId)) userId = undefined;
123-
else userId = Number(userId);
120+
if (isNaN(walletAddress)) walletAddress = undefined;
124121
}
125122

126123
if (!sharkId) sharkId = null;
@@ -129,7 +126,7 @@ function UserController() {
129126
else sharkId = Number(sharkId);
130127
}
131128

132-
await followWalletOfShark(userId, sharkId)
129+
await followWalletOfShark(walletAddress, sharkId)
133130
.then((data) => {
134131
if (data.message === "success")
135132
return res.status(200).json({
@@ -152,20 +149,19 @@ function UserController() {
152149
};
153150

154151
this.unfollowSharkWallet = async (req, res, next) => {
155-
let { userId, sharkId } = req.body;
152+
let { walletAddress, sharkId } = req.body;
156153

157-
if (!userId) userId = null;
154+
if (!walletAddress) walletAddress = null;
158155
else {
159-
if (isNaN(userId)) userId = undefined;
160-
else userId = Number(userId);
156+
if (isNaN(walletAddress)) walletAddress = undefined;
161157
}
162158

163159
if (!sharkId) sharkId = null;
164160
else {
165161
if (isNaN(sharkId)) sharkId = undefined;
166162
else sharkId = Number(sharkId);
167163
}
168-
await unfollowWalletOfShark(userId, sharkId)
164+
await unfollowWalletOfShark(walletAddress, sharkId)
169165
.then((data) => {
170166
if (data.message === "success")
171167
return res.status(200).json({
@@ -188,15 +184,14 @@ function UserController() {
188184
};
189185

190186
this.getSharkFollowed = async (req, res, next) => {
191-
let userId = req.query.userId;
187+
let walletAddress = req.query.userId;
192188

193-
if (!userId) userId = null;
189+
if (!walletAddress) walletAddress = null;
194190
else {
195-
if (isNaN(userId)) userId = undefined;
196-
else userId = Number(userId);
191+
if (isNaN(walletAddress)) walletAddress = undefined;
197192
}
198193

199-
await getListOfSharkFollowed(userId)
194+
await getListOfSharkFollowed(walletAddress)
200195
.then((data) => {
201196
data.message === "success"
202197
? res.status(200).json({
@@ -223,15 +218,19 @@ function UserController() {
223218
};
224219

225220
this.addNewShark = async (req, res, next) => {
226-
let { walletAddress, userId } = req.body;
221+
let { walletAddress, sharkWalletAddress } = req.body;
227222

228-
if (!userId) userId = null;
223+
if (!walletAddress) walletAddress = null;
229224
else {
230-
if (isNaN(userId)) userId = undefined;
231-
else userId = Number(userId);
225+
if (isNaN(walletAddress)) walletAddress = undefined;
226+
}
227+
228+
if (!sharkWalletAddress) sharkWalletAddress = null;
229+
else {
230+
if (isNaN(sharkWalletAddress)) sharkWalletAddress = undefined;
232231
}
233232

234-
await addNewShark(walletAddress, userId)
233+
await addNewShark(walletAddress, sharkWalletAddress)
235234
.then((data) => {
236235
data.isAdded
237236
? res.status(200).json({

src/helpers/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ export const decryptWalletAddress = (encryptedData) => {
2424
const ivDecrypt = Buffer.from(iv.toString("hex"), "hex");
2525
const encryptedText = Buffer.from(encryptedData, "hex");
2626

27-
const decipher = crypto.createDecipheriv(algorithm, Buffer.from(key), ivDecrypt);
27+
const decipher = crypto.createDecipheriv(
28+
algorithm,
29+
Buffer.from(key),
30+
ivDecrypt
31+
);
2832

2933
let decrypted = decipher.update(encryptedText);
3034
decrypted = Buffer.concat([decrypted, decipher.final()]);

src/models/Investor.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import mongoose from "mongoose";
2-
import Inc from "mongoose-sequence";
3-
const AutoIncrement = Inc(mongoose);
42

53
const InvestorSchema = new mongoose.Schema(
64
{
@@ -53,7 +51,5 @@ const InvestorSchema = new mongoose.Schema(
5351
{ versionKey: false }
5452
);
5553

56-
InvestorSchema.plugin(AutoIncrement, { inc_field: "sharkId" });
57-
5854
const InvestorModel = mongoose.model("Investor", InvestorSchema);
5955
export default InvestorModel;

src/models/User.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import mongoose from "mongoose";
22

33
const UserSchema = new mongoose.Schema(
44
{
5+
userId: {
6+
type: Number,
7+
unique: true
8+
},
59
walletAddress: {
610
type: String,
711
trim: true,
@@ -40,6 +44,5 @@ const UserSchema = new mongoose.Schema(
4044
{ timestamps: true, versionKey: false }
4145
);
4246

43-
4447
const UserModel = mongoose.model("User", UserSchema);
4548
export default UserModel;

src/models/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export {
99
InvestorModel,
1010
CoinModel,
1111
TagModel,
12-
TransactionModel
12+
TransactionModel,
1313
};

src/services/crudDatabase/admin.js

+17-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { cryptWalletAddress } from "../../helpers/index.js";
21
import { UserModel } from "../../models/index.js";
32
import { checkExistedWalletAddress } from "./user.js";
43

@@ -18,36 +17,27 @@ export const getListOfUsers = async () => {
1817

1918
export const getUserProfile = async (walletAddress) => {
2019
if (walletAddress) {
21-
const projection = {
22-
fullName: 1,
23-
avatar: 1,
24-
website: 1,
25-
premiumAccount: 1,
26-
sharksFollowed: 1,
27-
addedSharks: 1,
28-
createdAt: 1
29-
};
30-
const user = await UserModel.findOne(
31-
{ walletAddress: hashAddress },
32-
projection
33-
).lean();
3420

35-
if (!user) return {};
36-
else return user;
21+
const projection = {
22+
fullName: 1,
23+
avatar: 1,
24+
website: 1,
25+
premiumAccount: 1,
26+
sharksFollowed: 1,
27+
addedSharks: 1,
28+
createdAt: 1
29+
};
30+
const user = await UserModel.findOne(
31+
{ walletAddress: walletAddress },
32+
projection
33+
).lean();
34+
35+
if (!user) return {};
36+
else return user;
3737
}
3838
return {};
3939
};
4040

41-
export const checkExistedUsernameForUpdateProfile = async (
42-
userId,
43-
username
44-
) => {
45-
const user = await UserModel.findOne({ username: username }).lean();
46-
47-
if (user && user.userId !== userId) return true;
48-
else return false;
49-
};
50-
5141
export const checkExistedEmailForUpdateProfile = async (userId, email) => {
5242
const user = await UserModel.findOne({ email: email }).lean();
5343

@@ -72,6 +62,7 @@ export const updateUserProfile = async (walletAddress, updateInfo) => {
7262
avatar: avatar === "" ? undefined : avatar
7363
};
7464

65+
7566
await UserModel.findOneAndUpdate(
7667
{ walletAddress: walletAddress },
7768
newUpdateInfo
@@ -116,18 +107,6 @@ export const upgradeUserPremiumAccount = async (walletAddress) => {
116107
}
117108
};
118109

119-
export const checkExistedUsername = async (username) => {
120-
const isExisted = await AdminModel.exists({ username: username }).lean();
121-
return Boolean(isExisted);
122-
};
123-
124-
export const getPasswordByUsername = async (username) => {
125-
const admin = await AdminModel.findOne({ username: username })
126-
.select("password -_id")
127-
.lean();
128-
return admin?.password || null;
129-
};
130-
131110
export const getAdminByUsername = async (username) => {
132111
return await AdminModel.findOne({ username: username }).lean();
133112
};

0 commit comments

Comments
 (0)