From a3397f74af085549098d6092f51482c918492ee2 Mon Sep 17 00:00:00 2001 From: Eugene Makarihkin Date: Thu, 14 Sep 2023 16:25:03 +0200 Subject: [PATCH 1/6] Added the solution and unit tests --- internal/handlers/order_test.go | 6 ++ .../integration-tests/orders/order_test.go | 4 + internal/repositories/order.go | 19 +++- internal/repositories/order_test.go | 97 +++++++++++++------ swagger.yaml | 4 + 5 files changed, 99 insertions(+), 31 deletions(-) diff --git a/internal/handlers/order_test.go b/internal/handlers/order_test.go index 27861ecb..1786810e 100644 --- a/internal/handlers/order_test.go +++ b/internal/handlers/order_test.go @@ -811,11 +811,13 @@ func (s *orderTestSuite) TestOrder_UpdateOrder_RepoErr() { quantity := int64(10) rentStart := strfmt.DateTime(time.Now()) rentEnd := strfmt.DateTime(time.Now().Add(time.Hour * 24)) + status := domain.OrderStatusBlocked createOrder := &models.OrderUpdateRequest{ Description: &description, Quantity: &quantity, RentStart: &rentStart, RentEnd: &rentEnd, + Status: &status, } userID := 1 orderID := 2 @@ -846,11 +848,13 @@ func (s *orderTestSuite) TestOrder_UpdateOrder_MapErr() { quantity := int64(10) rentStart := strfmt.DateTime(time.Now()) rentEnd := strfmt.DateTime(time.Now().Add(time.Hour * 24)) + status := domain.OrderStatusInProgress createOrder := &models.OrderUpdateRequest{ Description: &description, Quantity: &quantity, RentStart: &rentStart, RentEnd: &rentEnd, + Status: &status, } userID := 1 orderID := 2 @@ -881,11 +885,13 @@ func (s *orderTestSuite) TestOrder_UpdateOrder_OK() { quantity := int64(10) rentStart := strfmt.DateTime(time.Now()) rentEnd := strfmt.DateTime(time.Now().Add(time.Hour * 24)) + status := domain.OrderStatusInReview createOrder := &models.OrderUpdateRequest{ Description: &description, Quantity: &quantity, RentStart: &rentStart, RentEnd: &rentEnd, + Status: &status, } userID := 1 orderID := 2 diff --git a/internal/integration-tests/orders/order_test.go b/internal/integration-tests/orders/order_test.go index 841be193..743dfced 100644 --- a/internal/integration-tests/orders/order_test.go +++ b/internal/integration-tests/orders/order_test.go @@ -542,6 +542,7 @@ func TestIntegration_UpdateOrder(t *testing.T) { eqID := equip.ID rentStart := strfmt.DateTime(time.Now()) rentEnd := strfmt.DateTime(time.Now().Add(time.Hour * 24)) + createParams.Data = &models.OrderCreateRequest{ Description: desc, EquipmentID: eqID, @@ -557,11 +558,13 @@ func TestIntegration_UpdateOrder(t *testing.T) { params := orders.NewUpdateOrderParamsWithContext(ctx) params.OrderID = *orderID desc = "new" + status := domain.OrderStatusApproved params.Data = &models.OrderUpdateRequest{ Description: &desc, Quantity: &quantity, RentStart: &rentStart, RentEnd: &rentEnd, + Status: &status, } res, err := client.Orders.UpdateOrder(params, auth) @@ -571,6 +574,7 @@ func TestIntegration_UpdateOrder(t *testing.T) { assert.Equal(t, quantity, *res.Payload.Quantity) rentEnd.Equal(*res.Payload.RentEnd) rentStart.Equal(*res.Payload.RentStart) + //status.Equal(*res.Payload.CurrentStatus) }) } diff --git a/internal/repositories/order.go b/internal/repositories/order.go index e448d729..48fee02f 100644 --- a/internal/repositories/order.go +++ b/internal/repositories/order.go @@ -203,11 +203,20 @@ func (r *orderRepository) Update(ctx context.Context, id int, data *models.Order if err != nil { return nil, err } + foundOrder, err := tx.Order.Get(ctx, id) if err != nil { return nil, err } + foundOrdersStatusName, err := tx.OrderStatusName. + Query(). + Where(orderstatusname.Status(*data.Status)). + Only(ctx) + if err != nil { + return nil, err + } + owner, err := foundOrder.QueryUsers().First(ctx) if err != nil { return nil, err @@ -242,13 +251,19 @@ func (r *orderRepository) Update(ctx context.Context, id int, data *models.Order SetRentEnd(*rentEnd). SetDescription(*data.Description). SetQuantity(*quantity). + SetCurrentStatus(foundOrdersStatusName). Save(ctx) if err != nil { return nil, err } - returnOrder, err := tx.Order.Query().Where(order.IDEQ(createdOrder.ID)). // get order with relations - WithUsers().WithOrderStatus().Only(ctx) + returnOrder, err := tx.Order. + Query(). + Where(order.IDEQ(createdOrder.ID)). + WithUsers(). + WithOrderStatus(). + WithCurrentStatus(). + Only(ctx) if err != nil { return nil, err } diff --git a/internal/repositories/order_test.go b/internal/repositories/order_test.go index a2ea0e51..07bf30af 100644 --- a/internal/repositories/order_test.go +++ b/internal/repositories/order_test.go @@ -3,13 +3,10 @@ package repositories import ( "context" "math" + "reflect" "testing" "time" - "github.com/go-openapi/strfmt" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" - "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/ent" "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/ent/enttest" "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/ent/order" @@ -18,6 +15,9 @@ import ( "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/middlewares" "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/utils" "git.epam.com/epm-lstr/epm-lstr-lc/be/pkg/domain" + "github.com/go-openapi/strfmt" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" ) type OrderSuite struct { @@ -31,6 +31,15 @@ type OrderSuite struct { equipments []*ent.Equipment } +// list of statuses with IDs. Amount of statuses is equal to amount of orders. +var statusNameMap = map[int]string{ + 1: "in review", // active + 2: "in progress", // active + 3: "rejected", // finished + 4: "closed", // finished + 8: "blocked", +} + func TestOrdersSuite(t *testing.T) { s := new(OrderSuite) suite.Run(t, s) @@ -104,13 +113,6 @@ func (s *OrderSuite) SetupTest() { s.equipments[i] = e } - // list of statuses with IDs. Amount of statuses is equal to amount of orders. - statusNameMap := map[int]string{ - 1: "in review", // active - 2: "in progress", // active - 3: "rejected", // finished - 4: "closed", // finished - } _, err = s.client.OrderStatusName.Delete().Exec(s.ctx) // clean up if err != nil { t.Fatal(err) @@ -777,6 +779,38 @@ func (s *OrderSuite) TestOrderRepository_List_StatusFilter() { } } +func Test_orderRepository_Update(t *testing.T) { + + type args struct { + ctx context.Context + id int + data *models.OrderUpdateRequest + userId int + } + tests := []struct { + name string + r *orderRepository + args args + want *ent.Order + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &orderRepository{} + got, err := r.Update(tt.args.ctx, tt.args.id, tt.args.data, tt.args.userId) + if (err != nil) != tt.wantErr { + t.Errorf("orderRepository.Update() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("orderRepository.Update() = %v, want %v", got, tt.want) + } + }) + } +} + func (s *OrderSuite) TestOrderRepository_Update_OK() { t := s.T() ctx := s.ctx @@ -798,25 +832,30 @@ func (s *OrderSuite) TestOrderRepository_Update_OK() { require.NoError(t, err) require.NoError(t, crtx.Commit()) - tx, err := s.client.Tx(ctx) - require.NoError(t, err) - ctx = context.WithValue(ctx, middlewares.TxContextKey, tx) - newDesc := "new desc" - newStartDate := strfmt.DateTime(time.Now().UTC()) - newEndDate := strfmt.DateTime(time.Now().UTC().Add(time.Hour * 24 * 10)) - newQuantity := int64(1) - req := &models.OrderUpdateRequest{ - Description: &newDesc, - Quantity: &newQuantity, - RentStart: &newStartDate, - RentEnd: &newEndDate, + for _, status := range statusNameMap { + tx, err := s.client.Tx(ctx) + require.NoError(t, err) + ctx = context.WithValue(ctx, middlewares.TxContextKey, tx) + newDesc := "new desc" + newStartDate := strfmt.DateTime(time.Now().UTC()) + newEndDate := strfmt.DateTime(time.Now().UTC().Add(time.Hour * 24 * 10)) + newQuantity := int64(1) + + req := &models.OrderUpdateRequest{ + Description: &newDesc, + Quantity: &newQuantity, + RentStart: &newStartDate, + RentEnd: &newEndDate, + Status: &status, + } + updated, err := s.orderRepository.Update(ctx, createdOrder.ID, req, s.user.ID) + require.NoError(t, err) + require.NoError(t, tx.Commit()) + require.Equal(t, newDesc, updated.Description) + require.Equal(t, updated.Edges.CurrentStatus.Status, status) + require.Equal(t, newEndDate, strfmt.DateTime(updated.RentEnd)) + require.Equal(t, newStartDate, strfmt.DateTime(updated.RentStart)) } - updated, err := s.orderRepository.Update(ctx, createdOrder.ID, req, s.user.ID) - require.NoError(t, err) - require.NoError(t, tx.Commit()) - require.Equal(t, newDesc, updated.Description) - require.Equal(t, newEndDate, strfmt.DateTime(updated.RentEnd)) - require.Equal(t, newStartDate, strfmt.DateTime(updated.RentStart)) } func (s *OrderSuite) TestOrderRepository_Update_MissingOrder() { diff --git a/swagger.yaml b/swagger.yaml index 02d78ce3..755cd2a4 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -3114,6 +3114,7 @@ definitions: - quantity - rent_start - rent_end + - status properties: description: type: string @@ -3126,6 +3127,9 @@ definitions: rent_end: type: string format: date-time + status: + type: string + maxLength: 255 Order: type: object required: From fed9223a905177bc6709a9d5ca6c49af8b801738 Mon Sep 17 00:00:00 2001 From: Eugene Makarihkin Date: Sun, 24 Sep 2023 23:01:17 +0200 Subject: [PATCH 2/6] Added the solution, updated unit tests --- internal/handlers/order.go | 8 +++-- .../integration-tests/orders/order_test.go | 4 +-- internal/repositories/order.go | 4 +-- internal/repositories/order_test.go | 33 ------------------- 4 files changed, 10 insertions(+), 39 deletions(-) diff --git a/internal/handlers/order.go b/internal/handlers/order.go index 9b96e973..0478b320 100644 --- a/internal/handlers/order.go +++ b/internal/handlers/order.go @@ -292,13 +292,17 @@ func (o Order) UpdateOrderFunc(repository domain.OrderRepository) orders.UpdateO order, err := repository.Update(ctx, orderID, p.Data, userID) if err != nil { o.logger.Error("update order failed", zap.Error(err)) - return orders.NewUpdateOrderDefault(http.StatusInternalServerError).WithPayload(buildErrorPayload(err)) + return orders. + NewUpdateOrderDefault(http.StatusInternalServerError). + WithPayload(buildErrorPayload(err)) } mappedOrder, err := mapOrder(order, o.logger) if err != nil { o.logger.Error("failed to map order", zap.Error(err)) - return orders.NewUpdateOrderDefault(http.StatusInternalServerError).WithPayload(buildErrorPayload(err)) + return orders. + NewUpdateOrderDefault(http.StatusInternalServerError). + WithPayload(buildErrorPayload(err)) } return orders.NewUpdateOrderOK().WithPayload(mappedOrder) diff --git a/internal/integration-tests/orders/order_test.go b/internal/integration-tests/orders/order_test.go index 743dfced..9942c877 100644 --- a/internal/integration-tests/orders/order_test.go +++ b/internal/integration-tests/orders/order_test.go @@ -558,13 +558,13 @@ func TestIntegration_UpdateOrder(t *testing.T) { params := orders.NewUpdateOrderParamsWithContext(ctx) params.OrderID = *orderID desc = "new" - status := domain.OrderStatusApproved + //status := domain.OrderStatusApproved params.Data = &models.OrderUpdateRequest{ Description: &desc, Quantity: &quantity, RentStart: &rentStart, RentEnd: &rentEnd, - Status: &status, + //Status: &status, } res, err := client.Orders.UpdateOrder(params, auth) diff --git a/internal/repositories/order.go b/internal/repositories/order.go index 48fee02f..bb4889f0 100644 --- a/internal/repositories/order.go +++ b/internal/repositories/order.go @@ -198,7 +198,7 @@ func (r *orderRepository) Create(ctx context.Context, data *models.OrderCreateRe return newOrder, nil } -func (r *orderRepository) Update(ctx context.Context, id int, data *models.OrderUpdateRequest, userId int) (*ent.Order, error) { +func (r *orderRepository) Update(ctx context.Context, id int, data *models.OrderUpdateRequest, userID int) (*ent.Order, error) { tx, err := middlewares.TxFromContext(ctx) if err != nil { return nil, err @@ -222,7 +222,7 @@ func (r *orderRepository) Update(ctx context.Context, id int, data *models.Order return nil, err } - if owner.ID != userId { + if owner.ID != userID { return nil, OrderAccessDenied{Err: errors.New("permission denied")} } diff --git a/internal/repositories/order_test.go b/internal/repositories/order_test.go index 07bf30af..408a95d1 100644 --- a/internal/repositories/order_test.go +++ b/internal/repositories/order_test.go @@ -3,7 +3,6 @@ package repositories import ( "context" "math" - "reflect" "testing" "time" @@ -779,38 +778,6 @@ func (s *OrderSuite) TestOrderRepository_List_StatusFilter() { } } -func Test_orderRepository_Update(t *testing.T) { - - type args struct { - ctx context.Context - id int - data *models.OrderUpdateRequest - userId int - } - tests := []struct { - name string - r *orderRepository - args args - want *ent.Order - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - r := &orderRepository{} - got, err := r.Update(tt.args.ctx, tt.args.id, tt.args.data, tt.args.userId) - if (err != nil) != tt.wantErr { - t.Errorf("orderRepository.Update() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("orderRepository.Update() = %v, want %v", got, tt.want) - } - }) - } -} - func (s *OrderSuite) TestOrderRepository_Update_OK() { t := s.T() ctx := s.ctx From ccecd5c631316e09ead3956eefb3fd51e458dd2e Mon Sep 17 00:00:00 2001 From: Eugene Makarihkin Date: Mon, 25 Sep 2023 00:43:14 +0200 Subject: [PATCH 3/6] Fixed go.sum --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index 9e77bec3..33f846cc 100644 --- a/go.sum +++ b/go.sum @@ -1239,7 +1239,6 @@ github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHN github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo= github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk= github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= -github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= @@ -1857,7 +1856,6 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.3.1-0.20221202221704-aa9f4b2f3d57 h1:/X0t/E4VxbZE7MLS7auvE7YICHeVvbIa9vkOVvYW/24= -golang.org/x/tools v0.3.1-0.20221202221704-aa9f4b2f3d57/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= From 0645ba06f4d9f1c75e39a20920aa188905dcddae Mon Sep 17 00:00:00 2001 From: Eugene Makarihkin Date: Mon, 25 Sep 2023 22:55:29 +0200 Subject: [PATCH 4/6] Reverted all changes --- go.sum | 2 + internal/handlers/order.go | 8 +-- internal/handlers/order_test.go | 6 -- .../integration-tests/orders/order_test.go | 4 -- internal/repositories/order.go | 23 ++----- internal/repositories/order_test.go | 64 +++++++++---------- swagger.yaml | 4 -- 7 files changed, 37 insertions(+), 74 deletions(-) diff --git a/go.sum b/go.sum index 33f846cc..9e77bec3 100644 --- a/go.sum +++ b/go.sum @@ -1239,6 +1239,7 @@ github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHN github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo= github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk= github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= +github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= @@ -1856,6 +1857,7 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.3.1-0.20221202221704-aa9f4b2f3d57 h1:/X0t/E4VxbZE7MLS7auvE7YICHeVvbIa9vkOVvYW/24= +golang.org/x/tools v0.3.1-0.20221202221704-aa9f4b2f3d57/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/handlers/order.go b/internal/handlers/order.go index d538b4ca..d721249c 100644 --- a/internal/handlers/order.go +++ b/internal/handlers/order.go @@ -379,17 +379,13 @@ func (o Order) UpdateOrderFunc(repository domain.OrderRepository) orders.UpdateO order, err := repository.Update(ctx, orderID, p.Data, userID) if err != nil { o.logger.Error("update order failed", zap.Error(err)) - return orders. - NewUpdateOrderDefault(http.StatusInternalServerError). - WithPayload(buildErrorPayload(err)) + return orders.NewUpdateOrderDefault(http.StatusInternalServerError).WithPayload(buildErrorPayload(err)) } mappedOrder, err := mapUserOrder(order, o.logger) if err != nil { o.logger.Error("failed to map order", zap.Error(err)) - return orders. - NewUpdateOrderDefault(http.StatusInternalServerError). - WithPayload(buildErrorPayload(err)) + return orders.NewUpdateOrderDefault(http.StatusInternalServerError).WithPayload(buildErrorPayload(err)) } return orders.NewUpdateOrderOK().WithPayload(mappedOrder) diff --git a/internal/handlers/order_test.go b/internal/handlers/order_test.go index 5cb59fe0..36595379 100644 --- a/internal/handlers/order_test.go +++ b/internal/handlers/order_test.go @@ -908,13 +908,11 @@ func (s *orderTestSuite) TestOrder_UpdateOrder_RepoErr() { quantity := int64(10) rentStart := strfmt.DateTime(time.Now()) rentEnd := strfmt.DateTime(time.Now().Add(time.Hour * 24)) - status := domain.OrderStatusBlocked createOrder := &models.OrderUpdateRequest{ Description: &description, Quantity: &quantity, RentStart: &rentStart, RentEnd: &rentEnd, - Status: &status, } userID := 1 orderID := 2 @@ -945,13 +943,11 @@ func (s *orderTestSuite) TestOrder_UpdateOrder_MapErr() { quantity := int64(10) rentStart := strfmt.DateTime(time.Now()) rentEnd := strfmt.DateTime(time.Now().Add(time.Hour * 24)) - status := domain.OrderStatusInProgress createOrder := &models.OrderUpdateRequest{ Description: &description, Quantity: &quantity, RentStart: &rentStart, RentEnd: &rentEnd, - Status: &status, } userID := 1 orderID := 2 @@ -982,13 +978,11 @@ func (s *orderTestSuite) TestOrder_UpdateOrder_OK() { quantity := int64(10) rentStart := strfmt.DateTime(time.Now()) rentEnd := strfmt.DateTime(time.Now().Add(time.Hour * 24)) - status := domain.OrderStatusInReview createOrder := &models.OrderUpdateRequest{ Description: &description, Quantity: &quantity, RentStart: &rentStart, RentEnd: &rentEnd, - Status: &status, } userID := 1 orderID := 2 diff --git a/internal/integration-tests/orders/order_test.go b/internal/integration-tests/orders/order_test.go index 06da46af..5b543081 100644 --- a/internal/integration-tests/orders/order_test.go +++ b/internal/integration-tests/orders/order_test.go @@ -624,7 +624,6 @@ func TestIntegration_UpdateOrder(t *testing.T) { eqID := equip.ID rentStart := strfmt.DateTime(time.Now()) rentEnd := strfmt.DateTime(time.Now().Add(time.Hour * 24)) - createParams.Data = &models.OrderCreateRequest{ Description: desc, EquipmentID: eqID, @@ -640,13 +639,11 @@ func TestIntegration_UpdateOrder(t *testing.T) { params := orders.NewUpdateOrderParamsWithContext(ctx) params.OrderID = *orderID desc = "new" - //status := domain.OrderStatusApproved params.Data = &models.OrderUpdateRequest{ Description: &desc, Quantity: &quantity, RentStart: &rentStart, RentEnd: &rentEnd, - //Status: &status, } res, err := client.Orders.UpdateOrder(params, auth) @@ -656,7 +653,6 @@ func TestIntegration_UpdateOrder(t *testing.T) { assert.Equal(t, quantity, *res.Payload.Quantity) rentEnd.Equal(*res.Payload.RentEnd) rentStart.Equal(*res.Payload.RentStart) - //status.Equal(*res.Payload.CurrentStatus) }) } diff --git a/internal/repositories/order.go b/internal/repositories/order.go index d8d72d5e..26f74f0e 100644 --- a/internal/repositories/order.go +++ b/internal/repositories/order.go @@ -208,31 +208,22 @@ func (r *orderRepository) Create(ctx context.Context, data *models.OrderCreateRe return newOrder, nil } -func (r *orderRepository) Update(ctx context.Context, id int, data *models.OrderUpdateRequest, userID int) (*ent.Order, error) { +func (r *orderRepository) Update(ctx context.Context, id int, data *models.OrderUpdateRequest, userId int) (*ent.Order, error) { tx, err := middlewares.TxFromContext(ctx) if err != nil { return nil, err } - foundOrder, err := tx.Order.Get(ctx, id) if err != nil { return nil, err } - foundOrdersStatusName, err := tx.OrderStatusName. - Query(). - Where(orderstatusname.Status(*data.Status)). - Only(ctx) - if err != nil { - return nil, err - } - owner, err := foundOrder.QueryUsers().First(ctx) if err != nil { return nil, err } - if owner.ID != userID { + if owner.ID != userId { return nil, OrderAccessDenied{Err: errors.New("permission denied")} } @@ -261,19 +252,13 @@ func (r *orderRepository) Update(ctx context.Context, id int, data *models.Order SetRentEnd(*rentEnd). SetDescription(*data.Description). SetQuantity(*quantity). - SetCurrentStatus(foundOrdersStatusName). Save(ctx) if err != nil { return nil, err } - returnOrder, err := tx.Order. - Query(). - Where(order.IDEQ(createdOrder.ID)). - WithUsers(). - WithOrderStatus(). - WithCurrentStatus(). - Only(ctx) + returnOrder, err := tx.Order.Query().Where(order.IDEQ(createdOrder.ID)). // get order with relations + WithUsers().WithOrderStatus().Only(ctx) if err != nil { return nil, err } diff --git a/internal/repositories/order_test.go b/internal/repositories/order_test.go index 46361d28..48207791 100644 --- a/internal/repositories/order_test.go +++ b/internal/repositories/order_test.go @@ -6,6 +6,10 @@ import ( "testing" "time" + "github.com/go-openapi/strfmt" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/ent" "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/ent/enttest" "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/ent/order" @@ -14,9 +18,6 @@ import ( "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/middlewares" "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/utils" "git.epam.com/epm-lstr/epm-lstr-lc/be/pkg/domain" - "github.com/go-openapi/strfmt" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" ) type OrderSuite struct { @@ -30,15 +31,6 @@ type OrderSuite struct { equipments []*ent.Equipment } -// list of statuses with IDs. Amount of statuses is equal to amount of orders. -var statusNameMap = map[int]string{ - 1: "in review", // active - 2: "in progress", // active - 3: "rejected", // finished - 4: "closed", // finished - 8: "blocked", -} - func TestOrdersSuite(t *testing.T) { s := new(OrderSuite) suite.Run(t, s) @@ -119,6 +111,13 @@ func (s *OrderSuite) SetupTest() { s.equipments[i] = e } + // list of statuses with IDs. Amount of statuses is equal to amount of orders. + statusNameMap := map[int]string{ + 1: "in review", // active + 2: "in progress", // active + 3: "rejected", // finished + 4: "closed", // finished + } _, err = s.client.OrderStatusName.Delete().Exec(s.ctx) // clean up if err != nil { t.Fatal(err) @@ -865,30 +864,25 @@ func (s *OrderSuite) TestOrderRepository_Update_OK() { require.NoError(t, err) require.NoError(t, crtx.Commit()) - for _, status := range statusNameMap { - tx, err := s.client.Tx(ctx) - require.NoError(t, err) - ctx = context.WithValue(ctx, middlewares.TxContextKey, tx) - newDesc := "new desc" - newStartDate := strfmt.DateTime(time.Now().UTC()) - newEndDate := strfmt.DateTime(time.Now().UTC().Add(time.Hour * 24 * 10)) - newQuantity := int64(1) - - req := &models.OrderUpdateRequest{ - Description: &newDesc, - Quantity: &newQuantity, - RentStart: &newStartDate, - RentEnd: &newEndDate, - Status: &status, - } - updated, err := s.orderRepository.Update(ctx, createdOrder.ID, req, s.user.ID) - require.NoError(t, err) - require.NoError(t, tx.Commit()) - require.Equal(t, newDesc, updated.Description) - require.Equal(t, updated.Edges.CurrentStatus.Status, status) - require.Equal(t, newEndDate, strfmt.DateTime(updated.RentEnd)) - require.Equal(t, newStartDate, strfmt.DateTime(updated.RentStart)) + tx, err := s.client.Tx(ctx) + require.NoError(t, err) + ctx = context.WithValue(ctx, middlewares.TxContextKey, tx) + newDesc := "new desc" + newStartDate := strfmt.DateTime(time.Now().UTC()) + newEndDate := strfmt.DateTime(time.Now().UTC().Add(time.Hour * 24 * 10)) + newQuantity := int64(1) + req := &models.OrderUpdateRequest{ + Description: &newDesc, + Quantity: &newQuantity, + RentStart: &newStartDate, + RentEnd: &newEndDate, } + updated, err := s.orderRepository.Update(ctx, createdOrder.ID, req, s.user.ID) + require.NoError(t, err) + require.NoError(t, tx.Commit()) + require.Equal(t, newDesc, updated.Description) + require.Equal(t, newEndDate, strfmt.DateTime(updated.RentEnd)) + require.Equal(t, newStartDate, strfmt.DateTime(updated.RentStart)) } func (s *OrderSuite) TestOrderRepository_Update_MissingOrder() { diff --git a/swagger.yaml b/swagger.yaml index bab4c492..4cdbe1ca 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -3212,7 +3212,6 @@ definitions: - quantity - rent_start - rent_end - - status properties: description: type: string @@ -3225,9 +3224,6 @@ definitions: rent_end: type: string format: date-time - status: - type: string - maxLength: 255 UserOrder: type: object required: From f378e7dd09160763426ffcd0e1b504b988e8c72e Mon Sep 17 00:00:00 2001 From: Eugene Makarihkin Date: Mon, 2 Oct 2023 12:18:14 +0200 Subject: [PATCH 5/6] Fixed incorrect behaviour for order status current date --- internal/repositories/order_status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/repositories/order_status.go b/internal/repositories/order_status.go index 5e84dbce..40aa7a49 100644 --- a/internal/repositories/order_status.go +++ b/internal/repositories/order_status.go @@ -96,7 +96,7 @@ func (r *orderStatusRepository) UpdateStatus(ctx context.Context, userID int, st } _, err = tx.OrderStatus.Create(). SetComment(*status.Comment). - SetCurrentDate(time.Time(*status.CreatedAt)). + SetCurrentDate(time.Now()). SetOrder(receivedOrder). SetOrderStatusName(statusName). SetUsers(receivedUser).Save(ctx) From 70cd73e5ff06b67bd486b018d6d57ba5ded44132 Mon Sep 17 00:00:00 2001 From: Eugene Makarihkin Date: Tue, 3 Oct 2023 23:13:47 +0200 Subject: [PATCH 6/6] Replaced filter from currentDate to ID --- internal/repositories/order_status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/repositories/order_status.go b/internal/repositories/order_status.go index 40aa7a49..09e63816 100644 --- a/internal/repositories/order_status.go +++ b/internal/repositories/order_status.go @@ -139,7 +139,7 @@ func (r *orderStatusRepository) GetOrderCurrentStatus(ctx context.Context, order WithOrder(func(query *ent.OrderQuery) { query.WithUsers() }). - Order(ent.Desc(orderstatus.FieldCurrentDate)).First(ctx) + Order(ent.Desc(orderstatus.FieldID)).First(ctx) if err != nil { return nil, fmt.Errorf("status history error, failed to get statuses: %s", err) }