fix(niki): complete admin kind_box service

This commit is contained in:
imirazimi 2023-12-27 01:12:33 +03:30
parent 27634049bb
commit d49f1277cc
14 changed files with 104 additions and 23 deletions

View File

@ -3,6 +3,7 @@ package adminkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity" import entity "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxAddRequest struct { type KindBoxAddRequest struct {
BenefactorID uint
KindBoxReqID uint KindBoxReqID uint
SenderID uint SenderID uint
SerialNumber string SerialNumber string

View File

@ -1,6 +1,7 @@
package adminkindboxparam package adminkindboxparam
type KindBoxDeleteRequest struct { type KindBoxDeleteRequest struct {
BenefactorID uint
KindBoxID uint KindBoxID uint
} }

View File

@ -3,6 +3,7 @@ package adminkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity" import entity "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxGetRequest struct { type KindBoxGetRequest struct {
BenefactorID uint
KindBoxID uint KindBoxID uint
} }

View File

@ -3,6 +3,8 @@ package adminkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity" import entity "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxUpdateRequest struct { type KindBoxUpdateRequest struct {
BenefactorID uint
KindBoxID uint
TotalAmount uint TotalAmount uint
ReceiverID uint ReceiverID uint
SenderID uint SenderID uint

View File

@ -10,7 +10,18 @@ import (
func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) { func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) {
const op = "adminkindboxservice.Add" const op = "adminkindboxservice.Add"
// TODO: check validation
// exist, err := s.benefactorService.IsBenefactorExist(ctx, req.BenefactorID)
// if err != nil {
// return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
//}
//if !exist {
// return param.KindBoxAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid)
//}
kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{ kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{
BenefactorID: req.BenefactorID,
KindBoxReqID: req.KindBoxReqID, KindBoxReqID: req.KindBoxReqID,
SenderID: req.SenderID, SenderID: req.SenderID,
SerialNumber: req.SerialNumber, SerialNumber: req.SerialNumber,

View File

@ -3,10 +3,31 @@ package adminkindboxservice
import ( import (
"context" "context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) error { func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) {
// some code // TODO: Does business domain need to delete an kindbox ?
panic("not implemented") const op = "adminkindboxservice.Delete"
kb, gErr := s.repo.GetKindBox(ctx, req.KindBoxID)
if gErr != nil {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kb.Status != entity.KindBoxPendingSendStatus {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
if kb.BenefactorID != req.BenefactorID {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID)
if dErr != nil {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)
}
return param.KindBoxDeleteResponse{}, nil
} }

View File

@ -3,15 +3,22 @@ package adminkindboxservice
import ( import (
"context" "context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) { func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) {
const op = "adminkindboxservice.Get" const op = "adminkindboxservice.Get"
kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID) kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID)
if err != nil { if err != nil {
return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
// TODO : ref to service.Update()
if kindBox.BenefactorID != req.BenefactorID {
return param.KindBoxGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
return param.KindBoxGetResponse{KindBox: kindBox}, nil return param.KindBoxGetResponse{KindBox: kindBox}, nil
} }

View File

@ -8,7 +8,7 @@ import (
type Repository interface { type Repository interface {
AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error) AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error)
EditKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error) UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error)
DeleteKindBox(ctx context.Context, kindBoxID uint) error DeleteKindBox(ctx context.Context, kindBoxID uint) error
GetAllKindBox(ctx context.Context) ([]entity.KindBox, error) GetAllKindBox(ctx context.Context) ([]entity.KindBox, error)
GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error) GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error)
@ -18,6 +18,11 @@ type Service struct {
repo Repository repo Repository
} }
// TODO: check validation.
// type BenefactorService interface {
// IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error)
// }
func New(repository Repository) Service { func New(repository Repository) Service {
return Service{ return Service{
repo: repository, repo: repository,

View File

@ -3,10 +3,37 @@ package adminkindboxservice
import ( import (
"context" "context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) { func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) {
// some code // TODO: can benefactor update its Request ?
panic("not implement") // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ?
// TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected
const op = "adminkindboxservice.Update"
kb, gErr := s.repo.GetKindBox(ctx, req.KindBoxID)
if gErr != nil {
return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kb.BenefactorID != req.BenefactorID {
return param.KindBoxUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{
TotalAmount: req.TotalAmount,
ReceiverID: req.ReceiverID,
SenderID: req.SenderID,
SerialNumber: req.SerialNumber,
Status: req.Status,
})
if uErr != nil {
return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected)
}
return param.KindBoxUpdateResponse{KindBox: kindBox}, nil
} }

View File

@ -5,7 +5,6 @@ import (
entity "git.gocasts.ir/ebhomengo/niki/entity" entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
@ -13,13 +12,13 @@ func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param
const op = "adminkindboxreqservice.Add" const op = "adminkindboxreqservice.Add"
// TODO: check validation // TODO: check validation
kbr, err := s.repo.GetKindBoxReq(ctx, req.BenefactorID) // exist, err := s.benefactorService.IsBenefactorExist(ctx, req.BenefactorID)
if err != nil { // if err != nil {
return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) // return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} //}
if kbr.BenefactorID != req.BenefactorID { // if !exist {
return param.KindBoxReqAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid) // return param.KindBoxReqAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid)
} //}
kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{ kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{
BenefactorID: req.BenefactorID, BenefactorID: req.BenefactorID,

View File

@ -17,12 +17,12 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest)
if gErr != nil { if gErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
} }
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
if kbr.BenefactorID != req.BenefactorID { if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
} }
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID)
if dErr != nil { if dErr != nil {

View File

@ -20,6 +20,9 @@ func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest)
if gErr != nil { if gErr != nil {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
} }
if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
if kbr.Status != entity.KindBoxReqPendingStatus { if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid) return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
} }

View File

@ -17,12 +17,12 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest)
if gErr != nil { if gErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
} }
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
if kbr.BenefactorID != req.BenefactorID { if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
} }
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID)
if dErr != nil { if dErr != nil {

View File

@ -18,6 +18,9 @@ func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest)
if gErr != nil { if gErr != nil {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
} }
if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
if kbr.Status != entity.KindBoxReqPendingStatus { if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid) return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
} }