forked from ebhomengo/niki
1
0
Fork 0

RollbackKindBoxRequestStatus

This commit is contained in:
Abolfazl Nourzad 2024-01-25 18:45:53 +03:30
parent a18da64cf9
commit e4a3c5464a
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
4 changed files with 34 additions and 8 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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)
}

View File

@ -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 {