Skip to content

Commit acaf43d

Browse files
fix(follow): use boolean flag instead of reply.statusCode for success logging (#172)
reply.statusCode defaults to 200 before any response is sent, so the previous check always evaluated to true and logged failed follows as success. followGitHub now returns { success, response } so the caller can log based on the actual API outcome. Closes #148
1 parent 2108d70 commit acaf43d

1 file changed

Lines changed: 38 additions & 23 deletions

File tree

apps/backend/src/routes/follow.ts

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,23 @@ export async function followRoutes(app: FastifyInstance) {
3131
// Decrypt the stored token
3232
const accessToken = decrypt(oauthToken.accessToken);
3333

34-
try {
34+
try {
3535
let result;
36+
let succeeded = false;
37+
3638
switch (platform) {
3739
case 'github':
3840
result = await followGitHub(accessToken, targetUsername, reply);
41+
succeeded = result.success === true;
3942
break;
4043
default:
4144
return reply.status(400).send({
4245
error: `API follow not supported for ${platform}. Use WebView or link instead.`,
4346
});
4447
}
4548

46-
// If follow succeeded (or was handled by the function without throwing), log it
47-
if (reply.statusCode === 200 || reply.statusCode === 204) {
49+
// Log only genuine successes — not based on reply.statusCode default
50+
if (succeeded) {
4851
app.prisma.followLog.create({
4952
data: {
5053
followerId: userId,
@@ -56,7 +59,7 @@ export async function followRoutes(app: FastifyInstance) {
5659
}).catch(err => app.log.error('Failed to log follow:', err));
5760
}
5861

59-
return result;
62+
return result.response;
6063
} catch (err: any) {
6164
app.log.error(`Follow error for ${platform}:`, err);
6265

@@ -81,7 +84,7 @@ async function followGitHub(
8184
accessToken: string,
8285
targetUsername: string,
8386
reply: FastifyReply
84-
) {
87+
): Promise<{ success: boolean; response: FastifyReply }> {
8588
const response = await fetch(`https://api.github.com/user/following/${targetUsername}`, {
8689
method: 'PUT',
8790
headers: {
@@ -92,30 +95,42 @@ async function followGitHub(
9295
});
9396

9497
if (response.status === 204) {
95-
return reply.send({
96-
status: 'success',
97-
platform: 'github',
98-
targetUsername,
99-
message: `Now following ${targetUsername} on GitHub`,
100-
});
98+
return {
99+
success: true,
100+
response: reply.send({
101+
status: 'success',
102+
platform: 'github',
103+
targetUsername,
104+
message: `Now following ${targetUsername} on GitHub`,
105+
}),
106+
};
101107
}
102108

103109
if (response.status === 401 || response.status === 403) {
104-
return reply.status(401).send({
105-
error: 'GitHub token expired or insufficient permissions',
106-
requiresAuth: true,
107-
});
110+
return {
111+
success: false,
112+
response: reply.status(401).send({
113+
error: 'GitHub token expired or insufficient permissions',
114+
requiresAuth: true,
115+
}),
116+
};
108117
}
109118

110119
if (response.status === 404) {
111-
return reply.status(404).send({
112-
error: `GitHub user '${targetUsername}' not found`,
113-
});
120+
return {
121+
success: false,
122+
response: reply.status(404).send({
123+
error: `GitHub user '${targetUsername}' not found`,
124+
}),
125+
};
114126
}
115127

116128
const errorBody = await response.text();
117-
return reply.status(response.status).send({
118-
error: 'GitHub follow failed',
119-
details: errorBody,
120-
});
121-
}
129+
return {
130+
success: false,
131+
response: reply.status(response.status).send({
132+
error: 'GitHub follow failed',
133+
details: errorBody,
134+
}),
135+
};
136+
}

0 commit comments

Comments
 (0)