Repository navigation
feat(db): 退部するとロールが外れるようにする(U-6) #84
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
Open
yufoxda
wants to merge
2
commits into
develop
Choose a base branch
from
claude/epic-wright-9zzc8u-u6
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -202,12 +202,26 @@ const normalizeApplication = (input: { | |
| allergyDetails: input.someAllergy ? input.allergyDetails?.trim() || null : null, | ||
| }) | ||
|
|
||
| export const conflictForUniqueMemberField = (error: unknown) => { | ||
| let databaseError = error as { code?: string; constraint?: string; cause?: unknown } | ||
| for (let depth = 0; depth < 4 && !databaseError.code && databaseError.cause; depth += 1) { | ||
| databaseError = databaseError.cause as { code?: string; constraint?: string; cause?: unknown } | ||
| // PostgreSQL がエラーの種類を表す 5 桁のコード(SQLSTATE)。 | ||
| // 一覧:https://www.postgresql.org/docs/current/errcodes-appendix.html | ||
| const UNIQUE_VIOLATION = '23505' // 一意の決まりに反した(同じ値がすでにある) | ||
| const CHECK_VIOLATION = '23514' // 検査に反した(check 制約や、トリガーでの確認) | ||
|
|
||
| type DatabaseError = { code?: string; constraint?: string; message?: string; cause?: unknown } | ||
|
|
||
| // Drizzle は、データベースのエラーを別のエラーで包んで投げることがある。 | ||
| // 包まれた元のエラーは cause に入っているので、コードを持つエラーが見つかるまで最大 4 段たどる。 | ||
| const findDatabaseError = (error: unknown): DatabaseError => { | ||
| let current = error as DatabaseError | ||
| for (let depth = 0; depth < 4 && !current.code && current.cause; depth += 1) { | ||
| current = current.cause as DatabaseError | ||
| } | ||
| if (databaseError.code !== '23505') return null | ||
| return current | ||
| } | ||
|
|
||
| export const conflictForUniqueMemberField = (error: unknown) => { | ||
| const databaseError = findDatabaseError(error) | ||
| if (databaseError.code !== UNIQUE_VIOLATION) return null | ||
| if (databaseError.constraint === 'members_student_id_unique') { | ||
| return { error: 'Student ID is already registered', code: 'student_id_conflict' } | ||
| } | ||
|
|
@@ -217,6 +231,16 @@ export const conflictForUniqueMemberField = (error: unknown) => { | |
| return { error: 'Application conflicts with an existing record', code: 'application_conflict' } | ||
| } | ||
|
|
||
| // 最後の管理者を退部させようとしたときのエラー(keep_a_role_manager トリガー)を、409 の応答に変える。 | ||
| // 退部でロールが外れる(U-6)と、role.manage を持つ人が誰もいなくなるため、データベースが取り消す。 | ||
| // トリガーは「検査に反した」(23514)としてエラーを出す。同じコードは他の確認でも使うので、メッセージでも見分ける。 | ||
| export const conflictForLastRoleManager = (error: unknown) => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 実装の筋が悪い気がします。 |
||
| const databaseError = findDatabaseError(error) | ||
| if (databaseError.code !== CHECK_VIOLATION) return null | ||
| if (!databaseError.message?.includes('at least one member must keep role.manage')) return null | ||
| return { error: 'At least one member must keep role.manage', code: 'last_role_manager' } | ||
| } | ||
|
|
||
| // Every write to a member row does the same thing: update only while the row is | ||
| // still in the status and version the caller saw, and treat anything else as a | ||
| // conflict. Keeping that in one place means a caller only states what it is | ||
|
|
@@ -928,6 +952,8 @@ export const updateAdminMemberService: RouteHandler<typeof updateAdminMemberRout | |
| } | ||
| const uniqueConflict = conflictForUniqueMemberField(error) | ||
| if (uniqueConflict) return c.json(uniqueConflict, 409) | ||
| const lastManager = conflictForLastRoleManager(error) | ||
| if (lastManager) return c.json(lastManager, 409) | ||
| throw error | ||
| } | ||
|
|
||
|
|
||
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
32 changes: 32 additions & 0 deletions
32
supabase/migrations/20261110090000_remove_roles_on_withdrawal.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,32 @@ | ||
| -- U-6(FR-4.7):退部するとロールが外れる | ||
| -- | ||
| -- 退部の処理(部員の状態を withdrawn にする)だけでロールが外れるよう、トリガーで行う。 | ||
| -- アプリの手順に頼らないので、どの API から退部させても外れる。 | ||
| -- 申請と部員を分けないと決めた(components.md Q-4)ため、これが無いと再入部で権限が戻ってしまう。 | ||
| begin; | ||
|
|
||
| -- 退部の操作は member.edit を持つ人ができるが、ロールを外すには role.manage が要る。 | ||
| -- 承認時に基本ロールを付けるトリガー(grant_base_member_role)と同じく、データベースの権限で行う。 | ||
| -- 外した記録は member_role_history に「退部させた人」の名前で残る(record_member_role_change)。 | ||
| -- 最後の管理者を退部させようとすると、keep_a_role_manager が拒み、退部の操作ごと取り消される。 | ||
| create function app_private.remove_roles_on_withdrawal() | ||
| returns trigger | ||
| language plpgsql | ||
| security definer | ||
| set search_path = pg_catalog, public | ||
| as $$ | ||
| begin | ||
| delete from public.member_roles where member_id = new.member_id; | ||
| return new; | ||
| end; | ||
| $$; | ||
|
|
||
| revoke all on function app_private.remove_roles_on_withdrawal() from public; | ||
|
|
||
| create trigger members_remove_roles_on_withdrawal | ||
| after update of member_status on public.members | ||
| for each row | ||
| when (new.member_status = 'withdrawn' and old.member_status is distinct from 'withdrawn') | ||
| execute function app_private.remove_roles_on_withdrawal(); | ||
|
|
||
| commit; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なんか筋が悪くないですか