forked from ebhomengo/niki
1
0
Fork 0
niki/service/admin/kind_box_req/accept.go

53 lines
1.8 KiB
Go
Raw Normal View History

package adminkindboxreqservice
import (
"context"
2024-01-25 15:15:53 +00:00
"git.gocasts.ir/ebhomengo/niki/logger"
2024-01-23 07:39:58 +00:00
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"
)
2024-01-25 15:15:53 +00:00
//@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)
if err != nil {
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err)
}
kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID)
if gErr != nil {
2024-01-23 07:39:58 +00:00
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr)
}
_, kErr := s.kindBoxClient.AddKindBoxAfterAcceptingRequest(ctx, adminkindboxparam.KindBoxAddAfterAcceptingReqRequest{
2024-01-23 07:39:58 +00:00
BenefactorID: kindBoxReq.BenefactorID,
KindBoxReqID: kindBoxReq.ID,
Type: kindBoxReq.KindBoxType,
Count: kindBoxReq.CountAccepted,
2024-01-23 07:39:58 +00:00
})
if kErr != nil {
2024-01-25 15:15:53 +00:00
// rollback kind box request status
rErr := s.repo.RollbackKindBoxRequestStatus(ctx, req.ID)
if rErr != nil {
// log error
logger.L().Error(rErr.Error())
}
2024-01-23 07:39:58 +00:00
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr)
}
return param.KindBoxReqAcceptResponse{
KindBoxReqID: kindBoxReq.ID,
KindBoxReqStatus: kindBoxReq.Status,
CountRequested: kindBoxReq.CountRequested,
CountAccepted: kindBoxReq.CountAccepted,
ReferDate: kindBoxReq.ReferDate,
AddressID: kindBoxReq.AddressID,
}, nil
}