fix(invite): 邀请码增加使用情况和过期时间#66
Merged
yokowu merged 1 commit intochaitin:mainfrom Jul 11, 2025
Merged
Conversation
Collaborator
|
@MonkeyCode-AI 来 review |
Contributor
|
⏳ MonkeyCode-AI 正在分析,请稍等片刻... |
Contributor
MonkeyCode-AI
left a comment
There was a problem hiding this comment.
Pull Request 概述
- 本次PR主要实现了邀请码的状态管理和过期机制,增强了系统的安全性和可用性。
- 修改了邀请码的数据结构,增加了
Status和ExpiredAt字段,用于标识邀请码的状态(pending/used)和过期时间。 - 更新了数据库模式,添加了相应的列,并添加了数据库迁移脚本。
- 在验证邀请码时增加了状态检查和过期检查,并在验证成功后将邀请码标记为已使用。
- 创建邀请码时设置了初始状态为
pending,并设置15分钟的过期时间。
Pull Request 变更详情
| 文件路径 | 变更类型 | 变更内容 |
|---|---|---|
| backend/consts/user.go | 添加 | 添加了InviteCodeStatus类型,包含pending和used两个值 |
| backend/db/invitecode.go | 修改 | 修改了InviteCode结构体,添加了Status和ExpiredAt字段 |
| backend/db/migrate/schema.go | 修改 | 更新了数据库schema,添加了status和expired_at列 |
| backend/ent/schema/invitecode.go | 修改 | 更新了数据库schema,添加了status和expired_at列 |
| backend/internal/user/repo/user.go | 修改 | 增强了ValidateInviteCode函数,增加了状态检查和过期检查 |
| backend/migration/000005_alter_invite_codes_table.up.sql | 添加 | 添加了数据库迁移脚本,用于添加新的列 |
| func (r *UserRepo) ValidateInviteCode(ctx context.Context, code string) (*db.InviteCode, error) { | ||
| return r.db.InviteCode.Query().Where(invitecode.Code(code)).Only(ctx) | ||
| var res *db.InviteCode | ||
| err := entx.WithTx(ctx, r.db, func(tx *db.Tx) error { |
Contributor
There was a problem hiding this comment.
在ValidateInviteCode函数中,使用了数据库事务来确保邀请码的状态更新和检查是原子操作,这提高了代码的可靠性。但需要注意的是,在高并发场景下,这种实现可能导致性能问题。建议考虑使用数据库层面的原子操作或者分布式锁来进一步优化。
Suggested change
| err := entx.WithTx(ctx, r.db, func(tx *db.Tx) error { | |
| // 可以考虑使用数据库的原子操作来替代事务,例如: | |
| // UPDATE invite_codes SET status = 'used' WHERE code = $1 AND status = 'pending' AND expired_at > NOW() RETURNING * |
| return r.db.InviteCode.Create(). | ||
| SetAdminID(adminID). | ||
| SetCode(code). | ||
| SetStatus(consts.InviteCodeStatusPending). |
Contributor
There was a problem hiding this comment.
在CreateInviteCode函数中,设置了邀请码的过期时间为15分钟后。这个时间是否应该作为一个可配置的参数,而不是硬编码在代码中?这样可以提高代码的灵活性和可维护性。
Suggested change
| SetStatus(consts.InviteCodeStatusPending). | |
| // 可以考虑从配置文件中读取过期时间,例如: | |
| // expireDuration := config.GetInviteCodeExpireDuration() | |
| // SetExpiredAt(time.Now().Add(expireDuration)) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fix: #58