forked from ebhomengo/niki
minor change
This commit is contained in:
parent
430bd9a95c
commit
bc9a625c03
|
@ -58,5 +58,5 @@ func InitAdminKindBoxService(db *mysql.DB) adminkindboxservice.Service {
|
|||
}
|
||||
|
||||
func InitAdminKindBoxReqService(db *mysql.DB) adminkindboxreqservice.Service {
|
||||
return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db), InitAdminKindBoxService(db))
|
||||
return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db))
|
||||
}
|
||||
|
|
|
@ -1,12 +1 @@
|
|||
package adminkindboxparam
|
||||
|
||||
import entity "git.gocasts.ir/ebhomengo/niki/entity"
|
||||
|
||||
type KindBoxAddAfterAcceptingReqRequest struct {
|
||||
BenefactorID uint
|
||||
KindBoxReqID uint
|
||||
Type entity.KindBoxType
|
||||
Count uint
|
||||
}
|
||||
|
||||
type KindBoxAddAfterAcceptingReqResponse struct{}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package mysqlkindboxreq
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
||||
)
|
||||
|
@ -9,10 +11,18 @@ func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) {
|
|||
var kindBoxReq entity.KindBoxReq
|
||||
var kindBoxStatus string
|
||||
var kindBoxType string
|
||||
err := scanner.Scan(&kindBoxReq.ID, &kindBoxReq.BenefactorID, &kindBoxType, &kindBoxReq.AddressID, &kindBoxReq.CountRequested, &kindBoxReq.CountAccepted,
|
||||
&kindBoxReq.Description,
|
||||
var countAccept sql.NullInt64
|
||||
var desc sql.NullString
|
||||
err := scanner.Scan(&kindBoxReq.ID, &kindBoxReq.BenefactorID, &kindBoxType, &kindBoxReq.AddressID, &kindBoxReq.CountRequested, &countAccept,
|
||||
&desc,
|
||||
&kindBoxReq.ReferDate, &kindBoxStatus, &kindBoxReq.CreatedAt)
|
||||
|
||||
if countAccept.Valid {
|
||||
kindBoxReq.CountAccepted = uint(countAccept.Int64)
|
||||
}
|
||||
if desc.Valid {
|
||||
kindBoxReq.Description = desc.String
|
||||
}
|
||||
kindBoxReq.Status = entity.MapToKindBoxReqStatus(kindBoxStatus)
|
||||
kindBoxReq.KindBoxType = entity.MapToKindBoxType(kindBoxType)
|
||||
|
||||
|
|
|
@ -1,31 +1 @@
|
|||
package adminkindboxservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
entity "git.gocasts.ir/ebhomengo/niki/entity"
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
"github.com/oklog/ulid/v2"
|
||||
)
|
||||
|
||||
func (s Service) AddKindBoxAfterAcceptingRequest(ctx context.Context, req param.KindBoxAddAfterAcceptingReqRequest) (param.KindBoxAddAfterAcceptingReqResponse, error) {
|
||||
const op = "adminkindboxservice.AddKindBoxAfterAcceptingRequest"
|
||||
|
||||
var kindBoxes []entity.KindBox
|
||||
for i := 0; i < int(req.Count); i++ {
|
||||
kindBoxes = append(kindBoxes, entity.KindBox{
|
||||
KindBoxReqID: req.KindBoxReqID,
|
||||
BenefactorID: req.BenefactorID,
|
||||
Type: req.Type,
|
||||
Status: entity.KindBoxPendingSendStatus,
|
||||
SerialNumber: ulid.Make().String(),
|
||||
})
|
||||
}
|
||||
err := s.repo.AddBatchKindBox(ctx, kindBoxes)
|
||||
if err != nil {
|
||||
return param.KindBoxAddAfterAcceptingReqResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
|
||||
return param.KindBoxAddAfterAcceptingReqResponse{}, nil
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@ 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"
|
||||
)
|
||||
|
@ -26,23 +24,6 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest)
|
|||
return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr)
|
||||
}
|
||||
|
||||
_, aErr := s.kindBoxClient.AddKindBoxAfterAcceptingRequest(ctx, adminkindboxparam.KindBoxAddAfterAcceptingReqRequest{
|
||||
BenefactorID: kindBoxReq.BenefactorID,
|
||||
KindBoxReqID: kindBoxReq.ID,
|
||||
Type: kindBoxReq.KindBoxType,
|
||||
Count: kindBoxReq.CountAccepted,
|
||||
})
|
||||
if aErr != 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(aErr)
|
||||
}
|
||||
|
||||
return param.KindBoxReqAcceptResponse{
|
||||
KindBoxReqID: kindBoxReq.ID,
|
||||
KindBoxReqStatus: kindBoxReq.Status,
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
|
@ -15,24 +14,12 @@ type Repository interface {
|
|||
GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error)
|
||||
}
|
||||
|
||||
type KindBoxClient interface {
|
||||
AddKindBoxAfterAcceptingRequest(ctx context.Context, param adminkindboxparam.KindBoxAddAfterAcceptingReqRequest) (adminkindboxparam.KindBoxAddAfterAcceptingReqResponse, error)
|
||||
}
|
||||
|
||||
// TODO: check validation.
|
||||
// type BenefactorService interface {
|
||||
// IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error)
|
||||
// }
|
||||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
// benefactorService BenefactorService
|
||||
kindBoxClient KindBoxClient
|
||||
}
|
||||
|
||||
func New(repository Repository, kindBoxClient KindBoxClient) Service {
|
||||
func New(repository Repository) Service {
|
||||
return Service{
|
||||
repo: repository,
|
||||
kindBoxClient: kindBoxClient,
|
||||
repo: repository,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue