Skip to content

fix(invite): 邀请码增加使用情况和过期时间#66

Merged
yokowu merged 1 commit intochaitin:mainfrom
yokowu:fix-invite
Jul 11, 2025
Merged

fix(invite): 邀请码增加使用情况和过期时间#66
yokowu merged 1 commit intochaitin:mainfrom
yokowu:fix-invite

Conversation

@yokowu
Copy link
Copy Markdown
Collaborator

@yokowu yokowu commented Jul 11, 2025

fix: #58

@yokowu yokowu merged commit 31798c2 into chaitin:main Jul 11, 2025
2 checks passed
@yokowu yokowu deleted the fix-invite branch July 11, 2025 06:30
@safe1ine
Copy link
Copy Markdown
Collaborator

@MonkeyCode-AI 来 review

@MonkeyCode-AI
Copy link
Copy Markdown
Contributor

⏳ MonkeyCode-AI 正在分析,请稍等片刻...

Copy link
Copy Markdown
Contributor

@MonkeyCode-AI MonkeyCode-AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request 概述

  • 本次PR主要实现了邀请码的状态管理和过期机制,增强了系统的安全性和可用性。
  • 修改了邀请码的数据结构,增加了StatusExpiredAt字段,用于标识邀请码的状态(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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在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).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在CreateInviteCode函数中,设置了邀请码的过期时间为15分钟后。这个时间是否应该作为一个可配置的参数,而不是硬编码在代码中?这样可以提高代码的灵活性和可维护性。

Suggested change
SetStatus(consts.InviteCodeStatusPending).
// 可以考虑从配置文件中读取过期时间,例如:
// expireDuration := config.GetInviteCodeExpireDuration()
// SetExpiredAt(time.Now().Add(expireDuration))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

邀请链接可以重复使用,这不符合预期

3 participants