@@ -1496,27 +1496,6 @@ func (c *Cockroach) NewAccountWithFreeSubscriptionPlan(ops *types.UserQueryOpts)
14961496 }
14971497 ops .UID = userUID
14981498 }
1499- freePlan , err := c .GetSubscriptionPlan (types .FreeSubscriptionPlanName )
1500- if err != nil {
1501- return nil , fmt .Errorf ("failed to get free plan: %w" , err )
1502- }
1503- userInfo := & types.UserInfo {}
1504- err = c .DB .Model (& types.UserInfo {}).Where (`"userUid" = ?` , ops .UID ).Find (userInfo ).Error
1505- if err != nil && err != gorm .ErrRecordNotFound {
1506- return nil , fmt .Errorf ("failed to get user info: %w" , err )
1507- }
1508- githubDetection := true
1509- if userInfo .Config != nil {
1510- if userInfo .Config .Github .CreatedAt != "" {
1511- createdAt , err := time .Parse (time .RFC3339 , userInfo .Config .Github .CreatedAt )
1512- if err != nil {
1513- return nil , fmt .Errorf ("failed to parse github created at: %w" , err )
1514- }
1515- if time .Since (createdAt ) < 7 * 24 * time .Hour {
1516- githubDetection = false
1517- }
1518- }
1519- }
15201499 now := time .Now ().UTC ()
15211500 account := & types.Account {
15221501 UserUID : ops .UID ,
@@ -1530,10 +1509,35 @@ func (c *Cockroach) NewAccountWithFreeSubscriptionPlan(ops *types.UserQueryOpts)
15301509 // 1. create credits
15311510 // 2. create account
15321511 // 3. create subscription
1533- err = c .DB .Transaction (func (tx * gorm.DB ) error {
1534- if err := tx .Where (& types.Account {UserUID : ops .UID }).FirstOrCreate (account ).Error ; err != nil {
1512+ err := c .DB .Transaction (func (tx * gorm.DB ) error {
1513+ result := tx .Where (& types.Account {UserUID : ops .UID }).FirstOrCreate (account )
1514+ if err := result .Error ; err != nil {
15351515 return fmt .Errorf ("failed to create account: %w" , err )
15361516 }
1517+ if result .RowsAffected == 0 {
1518+ return nil
1519+ }
1520+ freePlan , err := c .GetSubscriptionPlan (types .FreeSubscriptionPlanName )
1521+ if err != nil {
1522+ return fmt .Errorf ("failed to get free plan: %w" , err )
1523+ }
1524+ userInfo := & types.UserInfo {}
1525+ err = c .DB .Model (& types.UserInfo {}).Where (`"userUid" = ?` , ops .UID ).Find (userInfo ).Error
1526+ if err != nil && err != gorm .ErrRecordNotFound {
1527+ return fmt .Errorf ("failed to get user info: %w" , err )
1528+ }
1529+ githubDetection := true
1530+ if userInfo .Config != nil {
1531+ if userInfo .Config .Github .CreatedAt != "" {
1532+ createdAt , err := time .Parse (time .RFC3339 , userInfo .Config .Github .CreatedAt )
1533+ if err != nil {
1534+ return fmt .Errorf ("failed to parse github created at: %w" , err )
1535+ }
1536+ if time .Since (createdAt ) < 7 * 24 * time .Hour {
1537+ githubDetection = false
1538+ }
1539+ }
1540+ }
15371541 if freePlan .GiftAmount > 0 && githubDetection {
15381542 credits := & types.Credits {
15391543 ID : uuid .New (),
0 commit comments