From 8fb5b81363bfe7c03d65fee256b1e7922e92d760 Mon Sep 17 00:00:00 2001 From: bhupeshbhatia Date: Wed, 31 Oct 2018 20:59:14 -0400 Subject: [PATCH] Insert: Auto-generate UserID if absent --- user/insert.go | 21 ++++++++++++--------- user/user_validations_test.go | 31 ------------------------------- 2 files changed, 12 insertions(+), 40 deletions(-) diff --git a/user/insert.go b/user/insert.go index 020756f..7c3a603 100644 --- a/user/insert.go +++ b/user/insert.go @@ -28,16 +28,19 @@ func Insert(collection *mongo.Collection, event *model.Event) *model.KafkaRespon } if user.UserID == (uuuid.UUID{}) { - err = errors.New("missing UserID") - err = errors.Wrap(err, "Insert") - log.Println(err) - return &model.KafkaResponse{ - AggregateID: event.AggregateID, - CorrelationID: event.CorrelationID, - Error: err.Error(), - ErrorCode: InternalError, - UUID: event.TimeUUID, + userID, err := uuuid.NewV4() + if err != nil { + err = errors.Wrap(err, "Insert: Error generating UserID") + log.Println(err) + return &model.KafkaResponse{ + AggregateID: event.AggregateID, + CorrelationID: event.CorrelationID, + Error: err.Error(), + ErrorCode: InternalError, + UUID: event.TimeUUID, + } } + user.UserID = userID } if user.FirstName == "" { err = errors.New("missing FirstName") diff --git a/user/user_validations_test.go b/user/user_validations_test.go index 5998f09..5685f45 100644 --- a/user/user_validations_test.go +++ b/user/user_validations_test.go @@ -65,37 +65,6 @@ var _ = Describe("UserAggregate", func() { } }) - It("should return error if userID is empty", func() { - user.UserID = uuuid.UUID{} - marshalUser, err := json.Marshal(user) - Expect(err).ToNot(HaveOccurred()) - - timeUUID, err := uuuid.NewV1() - Expect(err).ToNot(HaveOccurred()) - cid, err := uuuid.NewV4() - Expect(err).ToNot(HaveOccurred()) - uid, err := uuuid.NewV4() - Expect(err).ToNot(HaveOccurred()) - - mockEvent := &model.Event{ - Action: "insert", - CorrelationID: cid, - AggregateID: 1, - Data: marshalUser, - Timestamp: time.Now(), - UserUUID: uid, - TimeUUID: timeUUID, - Version: 3, - YearBucket: 2018, - } - kr := Insert(nil, mockEvent) - Expect(kr.AggregateID).To(Equal(mockEvent.AggregateID)) - Expect(kr.CorrelationID).To(Equal(mockEvent.CorrelationID)) - Expect(kr.Error).ToNot(BeEmpty()) - Expect(kr.ErrorCode).To(Equal(int16(InternalError))) - Expect(kr.UUID).To(Equal(mockEvent.TimeUUID)) - }) - It("should return error if firstName is empty", func() { user.FirstName = "" marshalUser, err := json.Marshal(user)