From e4a3c5464a571234db4f0c1fa3da541377fe0feb Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Thu, 25 Jan 2024 18:45:53 +0330 Subject: [PATCH] RollbackKindBoxRequestStatus --- repository/mysql/kind_box/kind_box.go | 9 ++++----- repository/mysql/kind_box_req/kind_box_req.go | 17 +++++++++++++++++ service/admin/kind_box_req/accept.go | 15 ++++++++++++--- service/admin/kind_box_req/service.go | 1 + 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index 4ccfb87..98a49f6 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,9 +2,8 @@ package mysqlkindbox import ( "context" - "log" - "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -49,7 +48,7 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] case err := <-errCh: if err != nil { if err := tx.Rollback(); err != nil { - log.Printf("Rollback error: %v", err) + logger.L().Error("Rollback error: ", err) } return nil, err @@ -58,7 +57,7 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] result = append(result, res) case <-ctx.Done(): if err := tx.Rollback(); err != nil { - log.Printf("Rollback error: %v", err) + logger.L().Error("Rollback error: ", err) } return nil, richerror.New(op).WithErr(ctx.Err()). @@ -67,7 +66,7 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] } if err := tx.Commit(); err != nil { - log.Printf("Commit error: %v", err) + logger.L().Error("Commit error: ", err) return nil, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index b0f8e15..02aca25 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -122,3 +122,20 @@ func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description return nil } + +func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error { + op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus") + statement, err := d.conn.Conn(). + Prepare(`update kind_box_reqs set status = ? where id = ?`) + defer statement.Close() + if err != nil { + return richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + _, eErr := statement.ExecContext(ctx, entity.KindBoxReqPendingStatus.String(), id) + if eErr != nil { + return richerror.New(op).WithErr(eErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + return nil +} diff --git a/service/admin/kind_box_req/accept.go b/service/admin/kind_box_req/accept.go index 12c2c5e..e284e02 100644 --- a/service/admin/kind_box_req/accept.go +++ b/service/admin/kind_box_req/accept.go @@ -2,12 +2,17 @@ package adminkindboxreqservice import ( "context" - + "git.gocasts.ir/ebhomengo/niki/logger" adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) +//@see +// When confirming a request, should the senderID field, which represents the person sending the kind-box to the beneficiary, +// be filled at the same time? Or is it acceptable to confirm the request first and fill in the senderID field later? +// + func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) (param.KindBoxReqAcceptResponse, error) { const op = "adminkindboxreqservice.Accept" err := s.repo.AcceptKindBoxReq(ctx, req.ID, req.CountAccepted) @@ -15,8 +20,6 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err) } - // fire new event to create a kind-box. - kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID) if gErr != nil { return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr) @@ -29,6 +32,12 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) Count: kindBoxReq.CountAccepted, }) if kErr != nil { + // rollback kind box request status + rErr := s.repo.RollbackKindBoxRequestStatus(ctx, req.ID) + if rErr != nil { + // log error + logger.L().Error(rErr.Error()) + } return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr) } diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index dd1de71..10b1788 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -17,6 +17,7 @@ type Repository interface { AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint) error GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error + RollbackKindBoxRequestStatus(ctx context.Context, id uint) error } type KindBoxClient interface {