forked from ebhomengo/niki
1
0
Fork 0
niki/service/agent/kind_box_req/deliver.go

44 lines
1.7 KiB
Go

package agentkindboxreqservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
"time"
kindboxParam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Deliver(ctx context.Context, req param.DeliverKindBoxReqRequest) (param.DeliverKindBoxReqResponse, error) {
const op = "adminkindboxreqservice.Deliver"
if fieldErrors, vErr := s.vld.ValidateDeliver(ctx, req); vErr != nil {
return param.DeliverKindBoxReqResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr)
}
kindBoxReq, gErr := s.repo.GetByID(ctx, req.KindBoxReqID)
if gErr != nil {
return param.DeliverKindBoxReqResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
_, aErr := s.kindBoxSvc.AddBatchKindBox(ctx, kindboxParam.AddKindBoxRequest{
KindBoxReqID: kindBoxReq.ID,
BenefactorID: kindBoxReq.BenefactorID,
Type: kindBoxReq.KindBoxType,
DeliverReferTimeID: kindBoxReq.DeliverReferTimeID,
DeliverReferDate: kindBoxReq.DeliverReferDate,
DeliverAddressID: kindBoxReq.DeliverAddressID,
SenderAgentID: kindBoxReq.SenderAgentID,
DeliveredAt: time.Now(),
CountAccepted: kindBoxReq.CountAccepted,
SerialNumbers: req.SerialNumbers,
})
if aErr != nil {
return param.DeliverKindBoxReqResponse{}, richerror.New(op).WithErr(aErr).WithKind(richerror.KindUnexpected)
}
dErr := s.repo.DeliverKindBoxReq(ctx, req.KindBoxReqID)
if dErr != nil {
return param.DeliverKindBoxReqResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)
}
return param.DeliverKindBoxReqResponse{}, nil
}