From d26d4fc1794b5e2c0b1e5010fc72d1fd365562ed Mon Sep 17 00:00:00 2001 From: Iman Mirazimi Date: Mon, 16 Sep 2024 14:44:45 +0330 Subject: [PATCH] fix(niki): agent structure refactor --- param/{admin => agent}/kind_box/add.go | 2 +- repository/mysql/kind_box/add.go | 21 ------------------- service/admin/kind_box/service.go | 2 -- service/{admin => agent}/kind_box/add.go | 7 +++---- service/agent/kind_box/service.go | 1 + service/agent/kind_box_req/deliver.go | 4 ++-- .../kind_box_req/get_all_delivery_awaiting.go | 2 +- .../kind_box_req/get_delivery_awaiting.go | 2 +- service/agent/kind_box_req/service.go | 9 ++++---- service/service.go | 2 +- 10 files changed, 15 insertions(+), 37 deletions(-) rename param/{admin => agent}/kind_box/add.go (94%) rename service/{admin => agent}/kind_box/add.go (89%) diff --git a/param/admin/kind_box/add.go b/param/agent/kind_box/add.go similarity index 94% rename from param/admin/kind_box/add.go rename to param/agent/kind_box/add.go index 14cb8e4..cc383df 100644 --- a/param/admin/kind_box/add.go +++ b/param/agent/kind_box/add.go @@ -1,4 +1,4 @@ -package adminkindboxparam +package agentkindboxparam import ( "time" diff --git a/repository/mysql/kind_box/add.go b/repository/mysql/kind_box/add.go index 5c5d77c..f33ee21 100644 --- a/repository/mysql/kind_box/add.go +++ b/repository/mysql/kind_box/add.go @@ -8,29 +8,8 @@ import ( errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" querytransaction "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" - "git.gocasts.ir/ebhomengo/niki/repository/mysql" ) -func (d *DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) error { - const op = "mysqlkindbox.AddKindBox" - - query := `insert into kind_boxes(kind_box_req_id,benefactor_id,type,status,sender_agent_id) values (?,?,?,?,?)` - //nolint - stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxAdd, query) - if err != nil { - return richerror.New(op).WithErr(err). - WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected) - } - - _, err = stmt.ExecContext(ctx, kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.KindBoxType, entity.KindBoxDeliveredStatus, kindBox.SenderAgentID) - if err != nil { - return richerror.New(op).WithErr(err). - WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected) - } - - return nil -} - func (d *DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error { const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback" diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index 1476abd..bbf0735 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -9,8 +9,6 @@ import ( ) type Repository interface { - AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error - AddKindBox(ctx context.Context, kindBox entity.KindBox) error GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) AssignReceiverAgent(ctx context.Context, kindBox entity.KindBox) error GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error) diff --git a/service/admin/kind_box/add.go b/service/agent/kind_box/add.go similarity index 89% rename from service/admin/kind_box/add.go rename to service/agent/kind_box/add.go index b6a93f5..ede429e 100644 --- a/service/admin/kind_box/add.go +++ b/service/agent/kind_box/add.go @@ -1,15 +1,14 @@ -package adminkindboxservice +package agentkindboxservice import ( "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/agent/kind_box" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) func (s Service) AddBatchKindBox(ctx context.Context, req param.AddKindBoxRequest) (param.AddKindBoxResponse, error) { - const op = "adminkindboxservice.AddKindBox" + const op = "agentkindboxservice.AddKindBox" kindBoxes := make([]entity.KindBox, 0) for i := 0; i < int(req.CountAccepted); i++ { diff --git a/service/agent/kind_box/service.go b/service/agent/kind_box/service.go index 0915e9e..6a7f25a 100644 --- a/service/agent/kind_box/service.go +++ b/service/agent/kind_box/service.go @@ -12,6 +12,7 @@ type Repository interface { GetAwaitingReturnByAgent(ctx context.Context, kindBoxID uint, agentID uint) (entity.KindBox, error) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error) ReturnKindBox(ctx context.Context, kindBoxID uint, serialNumber string) error + AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error } type Service struct { diff --git a/service/agent/kind_box_req/deliver.go b/service/agent/kind_box_req/deliver.go index b665bf3..b77d2c9 100644 --- a/service/agent/kind_box_req/deliver.go +++ b/service/agent/kind_box_req/deliver.go @@ -2,15 +2,15 @@ package agentkindboxreqservice import ( "context" + kindboxParam "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" 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" + const op = "agentkindboxreqservice.Deliver" if fieldErrors, vErr := s.vld.ValidateDeliver(ctx, req); vErr != nil { return param.DeliverKindBoxReqResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) } diff --git a/service/agent/kind_box_req/get_all_delivery_awaiting.go b/service/agent/kind_box_req/get_all_delivery_awaiting.go index 6a07616..defcda2 100644 --- a/service/agent/kind_box_req/get_all_delivery_awaiting.go +++ b/service/agent/kind_box_req/get_all_delivery_awaiting.go @@ -8,7 +8,7 @@ import ( ) func (s Service) GetAllAwaitingDelivery(ctx context.Context, req param.DeliveryAwaitingGetAllRequest) (param.DeliveryAwaitingGetAllResponse, error) { - const op = "adminkindboxreqservice.GetAllAwaitingDelivery" + const op = "agentkindboxreqservice.GetAllAwaitingDelivery" if fieldErrors, vErr := s.vld.ValidateGetAllAwaitingDelivery(req); vErr != nil { return param.DeliveryAwaitingGetAllResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) diff --git a/service/agent/kind_box_req/get_delivery_awaiting.go b/service/agent/kind_box_req/get_delivery_awaiting.go index e44267c..cc5a93f 100644 --- a/service/agent/kind_box_req/get_delivery_awaiting.go +++ b/service/agent/kind_box_req/get_delivery_awaiting.go @@ -7,7 +7,7 @@ import ( ) func (s Service) GetAwaitingDelivery(ctx context.Context, req param.DeliveryAwaitingGetRequest) (param.DeliveryAwaitingGetResponse, error) { - const op = "adminkindboxreqservice.GetAwaitingDelivery" + const op = "agentkindboxreqservice.GetAwaitingDelivery" if fieldErrors, vErr := s.vld.ValidateGetAwaitingDeliveryRequest(req); vErr != nil { return param.DeliveryAwaitingGetResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) } diff --git a/service/agent/kind_box_req/service.go b/service/agent/kind_box_req/service.go index 1ee66d7..e88c6d6 100644 --- a/service/agent/kind_box_req/service.go +++ b/service/agent/kind_box_req/service.go @@ -2,7 +2,7 @@ package agentkindboxreqservice import ( "context" - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" "git.gocasts.ir/ebhomengo/niki/entity" params "git.gocasts.ir/ebhomengo/niki/param" @@ -26,9 +26,10 @@ type Service struct { vld validator.Validator } -func New(repo Repository, vld validator.Validator) Service { +func New(repo Repository, vld validator.Validator, kindBoxSvc KindBoxSvc) Service { return Service{ - repo: repo, - vld: vld, + repo: repo, + vld: vld, + kindBoxSvc: kindBoxSvc, } } diff --git a/service/service.go b/service/service.go index d94f248..b6e57a2 100644 --- a/service/service.go +++ b/service/service.go @@ -88,7 +88,7 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont AgentKindBoxVld = agentkindboxvalidator.New(kindBoxRepo) AgentKindBoxSvc = agentkindboxservice.New(kindBoxRepo, AgentKindBoxVld) AgentKindBoxReqVld = agentkindboxreqvalidator.New(kindBoxReqRepo) - AgentKindBoxReqSvc = agentkindboxreqservice.New(kindBoxReqRepo, AgentKindBoxReqVld) + AgentKindBoxReqSvc = agentkindboxreqservice.New(kindBoxReqRepo, AgentKindBoxReqVld, AgentKindBoxSvc) ) var ( BenefactorAuthSvc = auth.New(cfg.BenefactorAuth)