-
-
Notifications
You must be signed in to change notification settings - Fork 10
refactor(phase4): migrate account components to src/features/account #3324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
88302f3
feat: add AtCoderAccount model and migrate data from User
river0525 a8beb67
refactor: update users.ts and hooks.server.ts to use AtCoderAccount r…
river0525 82e8362
refactor: move AtCoder verification logic to src/features/account/ser…
river0525 b332671
refactor(phase4): migrate account components to src/features/account
river0525 0a9af83
fix: address CodeRabbit review feedback on atcoder-account-model
river0525 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
prisma/migrations/20260328002556_split_atcoder_account/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| -- CreateTable: must be created before data migration and column drop | ||
| CREATE TABLE "atcoder_account" ( | ||
| "userId" TEXT NOT NULL, | ||
| "handle" TEXT NOT NULL DEFAULT '', | ||
| "isValidated" BOOLEAN NOT NULL DEFAULT false, | ||
| "validationCode" TEXT NOT NULL DEFAULT '', | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
|
||
| CONSTRAINT "atcoder_account_pkey" PRIMARY KEY ("userId") | ||
| ); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "atcoder_account" ADD CONSTRAINT "atcoder_account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- MigrateData: copy AtCoder fields from user to atcoder_account (only for users with a registered handle) | ||
| INSERT INTO "atcoder_account" ("userId", "handle", "isValidated", "validationCode", "createdAt", "updatedAt") | ||
| SELECT | ||
| "id", | ||
| "atcoder_username", | ||
| COALESCE("atcoder_validation_status", false), | ||
| "atcoder_validation_code", | ||
| NOW(), | ||
| NOW() | ||
| FROM "user" | ||
| WHERE "atcoder_username" != ''; | ||
|
|
||
| -- AlterTable: drop AtCoder columns after data has been migrated | ||
| ALTER TABLE "user" DROP COLUMN "atcoder_username", | ||
| DROP COLUMN "atcoder_validation_code", | ||
| DROP COLUMN "atcoder_validation_status"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { default as db } from '$lib/server/database'; | ||
| import { sha256 } from '$lib/utils/hash'; | ||
|
|
||
| const EXTERNAL_API_TIMEOUT_MS = 5000; | ||
|
|
||
| /** Calls the external API to check if the validation code appears in the user's AtCoder affiliation. */ | ||
| async function confirmWithExternalApi(handle: string, validationCode: string): Promise<boolean> { | ||
| const controller = new AbortController(); | ||
| const timeoutId = setTimeout(() => controller.abort(), EXTERNAL_API_TIMEOUT_MS); | ||
|
|
||
| try { | ||
| const baseUrl = process.env.CONFIRM_API_URL; | ||
| if (!baseUrl) { | ||
| throw new Error('CONFIRM_API_URL is not set.'); | ||
| } | ||
| const url = `${baseUrl}?user=${handle}`; | ||
| const response = await fetch(url, { signal: controller.signal }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error('Network response was not ok.'); | ||
| } | ||
|
|
||
| try { | ||
| const jsonData = await response.json(); | ||
| return jsonData.contents?.some((item: string) => item === validationCode) ?? false; | ||
| } catch { | ||
| // Invalid JSON from external API — treat as unconfirmed | ||
| return false; | ||
| } | ||
| } finally { | ||
| clearTimeout(timeoutId); | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Generates a SHA256 validation code, stores it in AtCoderAccount, and returns the code. | ||
| * Creates the AtCoderAccount record if it does not exist yet. | ||
| */ | ||
| export async function generate(username: string, handle: string): Promise<string> { | ||
| const date = new Date().toISOString(); | ||
| const validationCode = await sha256(username + date); | ||
|
|
||
| const user = await db.user.findUniqueOrThrow({ where: { username } }); | ||
|
|
||
| await db.atCoderAccount.upsert({ | ||
| where: { userId: user.id }, | ||
| create: { userId: user.id, handle, validationCode, isValidated: false }, | ||
| update: { handle, validationCode, isValidated: false }, | ||
| }); | ||
|
|
||
| return validationCode; | ||
| } | ||
|
|
||
| /** | ||
| * Checks the external API and, if confirmed, marks the AtCoderAccount as validated. | ||
| * @returns true if validation succeeded, false otherwise. | ||
| */ | ||
| export async function validate(username: string): Promise<boolean> { | ||
| const user = await db.user.findUniqueOrThrow({ | ||
| where: { username }, | ||
| include: { atCoderAccount: true }, | ||
| }); | ||
|
|
||
| if (!user.atCoderAccount) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!user.atCoderAccount.validationCode) { | ||
| return false; | ||
| } | ||
|
|
||
| let confirmed: boolean; | ||
|
|
||
| try { | ||
| confirmed = await confirmWithExternalApi( | ||
| user.atCoderAccount.handle, | ||
| user.atCoderAccount.validationCode, | ||
| ); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| throw new Error(`Failed to confirm AtCoder affiliation for ${username}: ${error}`); | ||
| } | ||
|
|
||
| if (!confirmed) { | ||
| return false; | ||
| } | ||
|
|
||
| await db.atCoderAccount.update({ | ||
| where: { userId: user.id }, | ||
| data: { validationCode: '', isValidated: true }, | ||
| }); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** Deletes the AtCoderAccount record, effectively resetting the verification state. */ | ||
| export async function reset(username: string): Promise<void> { | ||
| const user = await db.user.findUniqueOrThrow({ where: { username } }); | ||
| await db.atCoderAccount.deleteMany({ where: { userId: user.id } }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,19 @@ | ||
| import { default as db } from '$lib/server/database'; | ||
| import type { User } from '@prisma/client'; | ||
|
|
||
| export async function getUser(username: string) { | ||
| const user = await db.user.findUnique({ | ||
| where: { | ||
| username: username, | ||
| }, | ||
| return await db.user.findUnique({ | ||
| where: { username }, | ||
| include: { atCoderAccount: true }, | ||
| }); | ||
| return user; | ||
| } | ||
|
|
||
| export async function getUserById(userId: string) { | ||
| const user = await db.user.findUnique({ | ||
| where: { | ||
| id: userId, | ||
| }, | ||
| return await db.user.findUnique({ | ||
| where: { id: userId }, | ||
| include: { atCoderAccount: true }, | ||
| }); | ||
| return user; | ||
| } | ||
|
|
||
| export async function deleteUser(username: string) { | ||
| const user = await db.user.delete({ | ||
| where: { | ||
| username: username, | ||
| }, | ||
| }); | ||
| return user; | ||
| } | ||
|
|
||
| export async function updateValicationCode( | ||
KATO-Hiro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| username: string, | ||
| atcoder_id: string, | ||
| validationCode: string, | ||
| ) { | ||
| try { | ||
| const user: User | null = await db.user.update({ | ||
| where: { | ||
| username: username, | ||
| }, | ||
|
|
||
| data: { | ||
| atcoder_validation_code: validationCode, | ||
| atcoder_username: atcoder_id, | ||
| }, | ||
| }); | ||
|
|
||
| return user; | ||
| } catch { | ||
| console.log('user update error'); | ||
| } | ||
| return await db.user.delete({ where: { username } }); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.