Skip to content

Commit 637d151

Browse files
committed
fix: correct re-link account
1 parent 8c3193a commit 637d151

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

app/sephirah/internal/biz/biztiphereth/tiphereth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type TipherethRepo interface {
2525
ListUsers(context.Context, model.Paging, []model.InternalID,
2626
[]libauth.UserType, []modeltiphereth.UserStatus, []model.InternalID,
2727
*model.InternalID) ([]*modeltiphereth.User, int64, error)
28-
CreateAccount(context.Context, modeltiphereth.Account, model.InternalID) error
28+
LinkAccount(context.Context, modeltiphereth.Account, model.InternalID) error
2929
UnLinkAccount(context.Context, modeltiphereth.Account, model.InternalID) error
3030
ListLinkAccounts(context.Context, model.Paging, model.InternalID) ([]*modeltiphereth.Account, int64, error)
3131
GetUser(context.Context, model.InternalID) (*modeltiphereth.User, error)
@@ -328,7 +328,7 @@ func (t *Tiphereth) LinkAccount(
328328
}}); err != nil {
329329
return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error())
330330
}
331-
if err := t.repo.CreateAccount(ctx, a, claims.InternalID); err != nil {
331+
if err := t.repo.LinkAccount(ctx, a, claims.InternalID); err != nil {
332332
return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error())
333333
}
334334
if err := t.pullAccount.Publish(ctx, modeltiphereth.PullAccountInfo{

app/sephirah/internal/data/tiphereth.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/tuihub/librarian/app/sephirah/internal/biz/biztiphereth"
88
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/converter"
9+
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent"
910
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/account"
1011
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/user"
1112
"github.com/tuihub/librarian/app/sephirah/internal/model/modeltiphereth"
@@ -114,16 +115,37 @@ func (t tipherethRepo) GetUser(ctx context.Context, id model.InternalID) (*model
114115
return converter.ToBizUser(u), nil
115116
}
116117

117-
func (t tipherethRepo) CreateAccount(ctx context.Context, a modeltiphereth.Account, u model.InternalID) error {
118-
return t.data.db.Account.Create().
119-
SetBindUserID(u).
120-
SetID(a.ID).
121-
SetPlatform(converter.ToEntAccountPlatform(a.Platform)).
122-
SetPlatformAccountID(a.PlatformAccountID).
123-
SetName(a.Name).
124-
SetAvatarURL(a.AvatarURL).
125-
SetProfileURL(a.ProfileURL).
126-
Exec(ctx)
118+
func (t tipherethRepo) LinkAccount(ctx context.Context, a modeltiphereth.Account, u model.InternalID) error {
119+
return t.data.WithTx(ctx, func(tx *ent.Tx) error {
120+
acc, err := tx.Account.Query().Where(
121+
account.PlatformEQ(converter.ToEntAccountPlatform(a.Platform)),
122+
account.PlatformAccountIDEQ(a.PlatformAccountID),
123+
).Only(ctx)
124+
if ent.IsNotFound(err) {
125+
return t.data.db.Account.Create().
126+
SetBindUserID(u).
127+
SetID(a.ID).
128+
SetPlatform(converter.ToEntAccountPlatform(a.Platform)).
129+
SetPlatformAccountID(a.PlatformAccountID).
130+
SetName(a.Name).
131+
SetAvatarURL(a.AvatarURL).
132+
SetProfileURL(a.ProfileURL).
133+
Exec(ctx)
134+
}
135+
if err != nil {
136+
return err
137+
}
138+
exist, err := acc.QueryBindUser().Exist(ctx)
139+
if err != nil {
140+
return err
141+
}
142+
if exist {
143+
return errors.New("account already bound to an user")
144+
}
145+
return t.data.db.Account.UpdateOneID(acc.ID).
146+
SetBindUserID(u).
147+
Exec(ctx)
148+
})
127149
}
128150

129151
func (t tipherethRepo) UnLinkAccount(ctx context.Context, a modeltiphereth.Account, u model.InternalID) error {

0 commit comments

Comments
 (0)