diff --git a/internal/authDB.go b/internal/authDB.go index 7d91b1d..1e438e1 100644 --- a/internal/authDB.go +++ b/internal/authDB.go @@ -98,14 +98,16 @@ func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRe user, ok := s.repo.Customers[req.GetLogin()] if !ok { hash := s.getMD5Hash(req.GetPassword()) - tmpUser, eru := s.repo.NewCustomer(req.GetLogin(), hash, req.GetEmail(), strconv.Itoa(time.Now().Nanosecond())) - if eru != nil { - s.logger.Error(eru) + user = s.repo.NewCustomer(req.GetLogin(), hash, req.GetEmail(), strconv.Itoa(time.Now().Nanosecond())) - return nil, eru + // + erk := user.Add() + if erk != nil { + return nil, erk } - user = tmpUser + // Добавим в локальную копию + s.repo.Customers[req.GetLogin()] = user } else if user.Confirmed || time.Now().Before(user.Time) { return nil, errors.New("login already registered") } else { // Обновим время регистрации @@ -113,7 +115,7 @@ func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRe } // TODO - _, era := json.Marshal(user.MessageRegistration) + value, era := json.Marshal(user.MessageRegistration) if era != nil { return nil, era } @@ -121,15 +123,13 @@ func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRe // s.logger.Printf("publication code %s to %s ...", user.MessageRegistration.Code, user.MessageRegistration.Email) - // // - // err := s.kafkaWriter.WriteMessage([]byte(user.Login), value) - // if err != nil { - // s.logger.Error(err) + // Отправим уведомление о необходимости подтверждения + err := s.kafkaWriter.WriteMessage([]byte(user.Login), value) + if err != nil { + s.logger.Error(err) - // return nil, err - // } else { - s.repo.Customers[req.Login] = user - // } + return nil, err + } // s.logger.Printf("publication code %s to %s completed", user.MessageRegistration.Code, user.MessageRegistration.Email) diff --git a/internal/customer/amodel.go b/internal/customer/amodel.go index cf1453e..e803283 100644 --- a/internal/customer/amodel.go +++ b/internal/customer/amodel.go @@ -34,7 +34,7 @@ type SimpleRow struct { func NewSimpleRow(ctx context.Context, database driver.Database, collection driver.Collection) *SimpleRow { now := time.Now() nowStr := now.Format(StampTemplate) - key := strconv.Itoa(now.Nanosecond()) + key := strconv.Itoa(now.Nanosecond() * -1) return &SimpleRow{ Row: Row{ diff --git a/internal/customer/customer.go b/internal/customer/customer.go index d4e7cbe..506a645 100644 --- a/internal/customer/customer.go +++ b/internal/customer/customer.go @@ -16,6 +16,17 @@ type Customer struct { Time time.Time } +func (c *Customer) Add() error { + meta, err := c.collection.CreateDocument(c.ctx, c) + if err != nil { + return err + } else { + c.key = meta.Key + } + + return nil +} + func (c *Customer) Update() error { _, err := c.collection.UpdateDocument(c.ctx, c.key, c) diff --git a/internal/customer/repository.go b/internal/customer/repository.go index 04cf2c4..e182de9 100644 --- a/internal/customer/repository.go +++ b/internal/customer/repository.go @@ -35,8 +35,8 @@ func NewCustomerRepository(ctx context.Context, database *arango_db.DataBase) *C } // Новый покупатель -func (r *CustomerRepository) NewCustomer(login string, password string, email string, code string) (Customer, error) { - customer := Customer{ +func (r *CustomerRepository) NewCustomer(login string, password string, email string, code string) Customer { + return Customer{ SimpleRow: *NewSimpleRow(r.ctx, r.database, r.collection), Login: login, Password: password, @@ -46,16 +46,6 @@ func (r *CustomerRepository) NewCustomer(login string, password string, email st Code: code, }, } - - // - meta, err := r.collection.CreateDocument(r.ctx, customer) - if err != nil { - return customer, err - } else { - customer.key = meta.Key - } - - return customer, nil } func (r *CustomerRepository) FetchAll() error {