Skip to content

Commit 5db7eaf

Browse files
committed
feat: integrate storage actions for user profile photo uploads and update related input schemas
1 parent 32061c4 commit 5db7eaf

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

src/app/api/auth/github/callback/route.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { GithubOAuthService } from "@/backend/services/oauth/GithubOAuthService";
22
import * as sessionActions from "@/backend/services/session.actions";
33
import * as userActions from "@/backend/services/user.action";
4+
import * as storageActions from "@/backend/services/storage.action";
45
import { NextResponse } from "next/server";
6+
import { DIRECTORY_NAME } from "@/backend/models/domain-models";
57

68
const githubOAuthService = new GithubOAuthService();
79

@@ -23,13 +25,26 @@ export async function GET(request: Request) {
2325
);
2426
}
2527

28+
const uploadedFileResponse = await storageActions.uploadByUrl({
29+
url: githubUser?.data?.avatar_url,
30+
key: `${DIRECTORY_NAME.USER_AVATARS}/${githubUser?.data?.login}.jpeg`,
31+
});
32+
2633
const bootedSocialUser = await userActions.bootSocialUser({
2734
service: "github",
2835
service_uid: githubUser?.data.id?.toString(),
2936
name: githubUser?.data?.login,
3037
username: githubUser?.data?.login,
3138
email: githubUser?.data.email,
32-
profile_photo: githubUser?.data?.avatar_url,
39+
profile_photo: uploadedFileResponse.success
40+
? {
41+
key: uploadedFileResponse.data.key,
42+
provider: uploadedFileResponse.data.provider as
43+
| "r2"
44+
| "cloudinary"
45+
| "direct",
46+
}
47+
: undefined,
3348
bio: githubUser?.data?.bio ?? "",
3449
});
3550

src/backend/services/inputs/article.input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const ArticleRepositoryInput = {
99
cover_image: z
1010
.object({
1111
key: z.string(),
12-
provider: z.enum(["cloudinary", "direct"]),
12+
provider: z.enum(["cloudinary", "direct", "r2"]),
1313
})
1414
.optional()
1515
.nullable(),

src/backend/services/inputs/user.input.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ export const UserActionInput = {
77
name: z.string(),
88
username: z.string(),
99
email: z.string().email(),
10-
profile_photo: z.string().url(),
10+
profile_photo: z
11+
.object({
12+
key: z.string(),
13+
provider: z.enum(["cloudinary", "direct", "r2"]),
14+
})
15+
.optional()
16+
.nullable(),
1117
bio: z.string().optional().nullable(),
1218
}),
1319
updateMyProfileInput: z.object({

src/backend/services/session.actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const validateSessionToken = async (
7575
}
7676

7777
await persistenceRepository.userSession.update({
78-
operationName: "validateSessionToken/userSession.update--",
78+
operationName: "validateSessionToken/userSession.update",
7979
where: eq("id", session.id),
8080
data: {
8181
last_action_at: new Date(),

src/backend/services/storage.action.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ export const uploadByUrl = async (_input: z.infer<typeof schema>) => {
2121
Body: body,
2222
});
2323
await s3Client.send(command);
24+
return {
25+
success: true as const,
26+
data: {
27+
key: input.key,
28+
provider: "r2",
29+
},
30+
};
2431
} catch (error) {
2532
return handleActionException(error);
2633
}

src/backend/services/user.action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function bootSocialUser(
4040
name: input.name,
4141
username: input.username,
4242
email: input.email,
43-
profile_photo: null,
43+
profile_photo: input.profile_photo,
4444
bio: input.bio ?? "",
4545
},
4646
])

0 commit comments

Comments
 (0)