Skip to content

Commit 431ad73

Browse files
committed
Implemented user signin, signup, user searching
1 parent 921f8e6 commit 431ad73

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

src/domains/user/api.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
getById,
1010
updateById,
1111
deleteById,
12+
followUser,
1213
} = require('./service');
1314

1415
const {
@@ -56,6 +57,29 @@ const routes = () => {
5657
}
5758
);
5859

60+
router.get(
61+
'/:id/follow',
62+
logRequest({}),
63+
validateRequest({ schema: idSchema, isParam: true }),
64+
async (req, res, next) => {
65+
const currentUserId = req.user._id;
66+
try {
67+
if (currentUserId === req.params.id) {
68+
throw new AppError(
69+
'Cannot follow yourself',
70+
'Cannot follow yourself',
71+
400
72+
);
73+
}
74+
75+
const result = await followUser(currentUserId, req.params.id);
76+
res.status(200).json({ result });
77+
} catch (error) {
78+
next(error);
79+
}
80+
}
81+
);
82+
5983
router.post(
6084
'/',
6185
logRequest({}),

src/domains/user/schema.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ const schema = new mongoose.Schema({
7676
type: Boolean,
7777
default: false,
7878
},
79+
80+
// commitstreams related similar properties
81+
csFollowers: [
82+
{
83+
id: { type: mongoose.Schema.Types.ObjectId },
84+
date: { type: Date, default: Date.now },
85+
},
86+
],
87+
csFollowing: [
88+
{
89+
id: { type: mongoose.Schema.Types.ObjectId },
90+
date: { type: Date, default: Date.now },
91+
},
92+
],
7993
});
8094
schema.add(baseSchema);
8195

src/domains/user/service.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,47 @@ const getByGitHubId = async (id) => {
122122
} catch (error) {}
123123
};
124124

125+
const followUser = async (followerId, followedId) => {
126+
try {
127+
// Check existing following status (Optimized)
128+
const follower = await Model.findById(followerId);
129+
const existingFollowing = follower.csFollowing.find((item) =>
130+
item.id.equals(followedId)
131+
);
132+
133+
if (existingFollowing) {
134+
logger.info(
135+
`followUser(): User ${followerId} is already following user ${followedId}`
136+
);
137+
return false;
138+
}
139+
140+
// Perform the updates
141+
const [followedUserUpdate, followerUserUpdate] = await Promise.all([
142+
// Update csFollowers of the followed user
143+
Model.findByIdAndUpdate(followedId, {
144+
$push: { csFollowers: { id: followerId, date: Date.now() } }, // Add follow date
145+
}),
146+
147+
// Update csFollowing of the follower user
148+
Model.findByIdAndUpdate(followerId, {
149+
$push: { csFollowing: { id: followedId, date: Date.now() } },
150+
}),
151+
]);
152+
153+
logger.info(`followUser(): success`, {
154+
followedId,
155+
followedId,
156+
followedUserUpdate,
157+
followerUserUpdate,
158+
});
159+
return true;
160+
} catch (error) {
161+
logger.error(`followUser(): Failed to update follow status`, error);
162+
throw new AppError(`Failed to update follow status`, error.message);
163+
}
164+
};
165+
125166
module.exports = {
126167
create,
127168
search,
@@ -130,4 +171,5 @@ module.exports = {
130171
updateById,
131172
deleteById,
132173
getByGitHubId,
174+
followUser,
133175
};

src/libraries/error-handling/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const errorHandler = {
3939
if (!appError.isTrusted) {
4040
terminateHttpServerAndExit();
4141
}
42+
return appError;
4243
} catch (handlingError) {
4344
// No logger here since it might have failed
4445
process.stdout.write(

src/server.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,15 @@ function defineErrorHandlingMiddleware(expressApp) {
169169
}
170170
}
171171

172-
errorHandler.handleError(error);
173-
res.status(error?.HTTPStatus || 500).end();
172+
const appError = await errorHandler.handleError(error);
173+
res
174+
.status(error?.HTTPStatus || 500)
175+
.json(
176+
{ ...appError, errorMessage: appError.message } || {
177+
message: 'Internal server error',
178+
}
179+
)
180+
.end();
174181
});
175182
}
176183

0 commit comments

Comments
 (0)