From 17b7c4beb286c8640c7e91f9fa6eddc45ee49e78 Mon Sep 17 00:00:00 2001 From: Iman Mirazimi Date: Wed, 21 Feb 2024 00:04:51 +0330 Subject: [PATCH] feat(service): add admin kind_box_req assign sender agent endpoint. --- delivery/http_server/admin/kind_box/add.go | 1 - delivery/http_server/admin/kind_box/get.go | 1 - .../http_server/admin/kind_box/get_all.go | 1 - delivery/http_server/admin/kind_box/update.go | 1 - .../http_server/admin/kind_box_req/add.go | 1 - .../admin/kind_box_req/assign_sender_agent.go | 46 +++++++++++++++++++ .../http_server/admin/kind_box_req/get.go | 1 - .../http_server/admin/kind_box_req/route.go | 1 + .../http_server/admin/kind_box_req/update.go | 1 - .../benefactor/kind_box_req/get_all.go | 1 - .../benefactor/kind_box_req/update.go | 1 - param/admin/admin/get.go | 13 ++++++ .../admin/kind_box_req/assign_sender_agent.go | 8 ++++ pkg/err_msg/message.go | 32 +++++++------ repository/mysql/admin/exist_admin.go | 23 ++++++++++ .../mysql/benefactor/exist_benefactor.go | 8 +--- .../mysql/kind_box_req/assign_sender_agent.go | 23 ++++++++++ ..._insert_admins_table_super_admin_value.sql | 4 +- service/admin/admin/get.go | 19 ++++++++ service/admin/admin/service.go | 1 + service/admin/kind_box/delete.go | 1 - service/admin/kind_box/get.go | 1 - service/admin/kind_box/get_all.go | 1 - service/admin/kind_box/update.go | 1 - service/admin/kind_box_req/add.go | 1 - .../admin/kind_box_req/assign_sender_agent.go | 19 ++++++++ service/admin/kind_box_req/delete.go | 1 - service/admin/kind_box_req/get.go | 1 - service/admin/kind_box_req/service.go | 3 +- service/admin/kind_box_req/update.go | 1 - service/benefactor/kind_box_req/add.go | 1 + service/benefactor/kind_box_req/delete.go | 1 - service/benefactor/kind_box_req/get_all.go | 1 - service/benefactor/kind_box_req/update.go | 1 - validator/admin/kind_box/add.go | 1 - validator/admin/kind_box/delete.go | 1 - validator/admin/kind_box/get.go | 1 - validator/admin/kind_box/update.go | 1 - validator/admin/kind_box_req/add.go | 1 - .../admin/kind_box_req/assign_sender_agent.go | 40 ++++++++++++++++ validator/admin/kind_box_req/delete.go | 1 - validator/admin/kind_box_req/get.go | 1 - validator/admin/kind_box_req/update.go | 1 - validator/admin/kind_box_req/validator.go | 40 +++++++++++++++- 44 files changed, 256 insertions(+), 53 deletions(-) delete mode 100644 delivery/http_server/admin/kind_box/add.go delete mode 100644 delivery/http_server/admin/kind_box/get.go delete mode 100644 delivery/http_server/admin/kind_box/get_all.go delete mode 100644 delivery/http_server/admin/kind_box/update.go delete mode 100644 delivery/http_server/admin/kind_box_req/add.go create mode 100644 delivery/http_server/admin/kind_box_req/assign_sender_agent.go delete mode 100644 delivery/http_server/admin/kind_box_req/get.go delete mode 100644 delivery/http_server/admin/kind_box_req/update.go delete mode 100644 delivery/http_server/benefactor/kind_box_req/get_all.go delete mode 100644 delivery/http_server/benefactor/kind_box_req/update.go create mode 100644 param/admin/admin/get.go create mode 100644 param/admin/kind_box_req/assign_sender_agent.go create mode 100644 repository/mysql/kind_box_req/assign_sender_agent.go create mode 100644 service/admin/admin/get.go delete mode 100644 service/admin/kind_box/delete.go delete mode 100644 service/admin/kind_box/get.go delete mode 100644 service/admin/kind_box/get_all.go delete mode 100644 service/admin/kind_box/update.go delete mode 100644 service/admin/kind_box_req/add.go create mode 100644 service/admin/kind_box_req/assign_sender_agent.go delete mode 100644 service/admin/kind_box_req/delete.go delete mode 100644 service/admin/kind_box_req/get.go delete mode 100644 service/admin/kind_box_req/update.go delete mode 100644 service/benefactor/kind_box_req/delete.go delete mode 100644 service/benefactor/kind_box_req/get_all.go delete mode 100644 service/benefactor/kind_box_req/update.go delete mode 100644 validator/admin/kind_box/add.go delete mode 100644 validator/admin/kind_box/delete.go delete mode 100644 validator/admin/kind_box/get.go delete mode 100644 validator/admin/kind_box/update.go delete mode 100644 validator/admin/kind_box_req/add.go create mode 100644 validator/admin/kind_box_req/assign_sender_agent.go delete mode 100644 validator/admin/kind_box_req/delete.go delete mode 100644 validator/admin/kind_box_req/get.go delete mode 100644 validator/admin/kind_box_req/update.go diff --git a/delivery/http_server/admin/kind_box/add.go b/delivery/http_server/admin/kind_box/add.go deleted file mode 100644 index 3cc696d..0000000 --- a/delivery/http_server/admin/kind_box/add.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxhandler diff --git a/delivery/http_server/admin/kind_box/get.go b/delivery/http_server/admin/kind_box/get.go deleted file mode 100644 index 3cc696d..0000000 --- a/delivery/http_server/admin/kind_box/get.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxhandler diff --git a/delivery/http_server/admin/kind_box/get_all.go b/delivery/http_server/admin/kind_box/get_all.go deleted file mode 100644 index 3cc696d..0000000 --- a/delivery/http_server/admin/kind_box/get_all.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxhandler diff --git a/delivery/http_server/admin/kind_box/update.go b/delivery/http_server/admin/kind_box/update.go deleted file mode 100644 index 3cc696d..0000000 --- a/delivery/http_server/admin/kind_box/update.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxhandler diff --git a/delivery/http_server/admin/kind_box_req/add.go b/delivery/http_server/admin/kind_box_req/add.go deleted file mode 100644 index b5635e7..0000000 --- a/delivery/http_server/admin/kind_box_req/add.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqhandler diff --git a/delivery/http_server/admin/kind_box_req/assign_sender_agent.go b/delivery/http_server/admin/kind_box_req/assign_sender_agent.go new file mode 100644 index 0000000..337a11b --- /dev/null +++ b/delivery/http_server/admin/kind_box_req/assign_sender_agent.go @@ -0,0 +1,46 @@ +package adminkindboxreqhandler + +import ( + "net/http" + "strconv" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + echo "github.com/labstack/echo/v4" +) + +func (h Handler) AssignSenderAgent(c echo.Context) error { + var req param.AssignSenderRequest + + if bErr := c.Bind(&req); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + + id, cErr := strconv.ParseUint(c.Param("id"), 10, 64) + + if cErr != nil { + return c.JSON(http.StatusBadRequest, errmsg.ErrorMsgInvalidInput) + } + + req.KindBoxReqID = uint(id) + + if fieldErrors, err := h.adminKindBoxReqVld.ValidateAssignSenderAgent(req); err != nil { + msg, code := httpmsg.Error(err) + + return c.JSON(code, echo.Map{ + "message": msg, + "errors": fieldErrors, + }) + } + + resp, sErr := h.adminKindBoxReqSvc.AssignSenderAgent(c.Request().Context(), req) + + if sErr != nil { + msg, code := httpmsg.Error(sErr) + + return echo.NewHTTPError(code, msg) + } + + return c.JSON(http.StatusOK, resp) +} diff --git a/delivery/http_server/admin/kind_box_req/get.go b/delivery/http_server/admin/kind_box_req/get.go deleted file mode 100644 index b5635e7..0000000 --- a/delivery/http_server/admin/kind_box_req/get.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqhandler diff --git a/delivery/http_server/admin/kind_box_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index 98c12ca..0787eae 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -12,5 +12,6 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.PATCH("/accept-kind-box-req/:id", h.Accept) r.PATCH("/reject-kind-box-req/:id", h.Reject) r.PATCH("/deliver-kind-box-req/:id", h.Deliver) + r.PATCH("/assign-sender-agent/:id", h.AssignSenderAgent) r.GET("/", h.GetAll, middleware.Auth(h.authSvc, h.authConfig)) } diff --git a/delivery/http_server/admin/kind_box_req/update.go b/delivery/http_server/admin/kind_box_req/update.go deleted file mode 100644 index b5635e7..0000000 --- a/delivery/http_server/admin/kind_box_req/update.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqhandler diff --git a/delivery/http_server/benefactor/kind_box_req/get_all.go b/delivery/http_server/benefactor/kind_box_req/get_all.go deleted file mode 100644 index 9ea2099..0000000 --- a/delivery/http_server/benefactor/kind_box_req/get_all.go +++ /dev/null @@ -1 +0,0 @@ -package benefactorkindboxreqhandler diff --git a/delivery/http_server/benefactor/kind_box_req/update.go b/delivery/http_server/benefactor/kind_box_req/update.go deleted file mode 100644 index 9ea2099..0000000 --- a/delivery/http_server/benefactor/kind_box_req/update.go +++ /dev/null @@ -1 +0,0 @@ -package benefactorkindboxreqhandler diff --git a/param/admin/admin/get.go b/param/admin/admin/get.go new file mode 100644 index 0000000..c55e720 --- /dev/null +++ b/param/admin/admin/get.go @@ -0,0 +1,13 @@ +package adminserviceparam + +import ( + "git.gocasts.ir/ebhomengo/niki/entity" +) + +type AdminExistByIDRequest struct { + AdminID uint +} + +type AdminExistByIDResponse struct { + Admin entity.Admin +} diff --git a/param/admin/kind_box_req/assign_sender_agent.go b/param/admin/kind_box_req/assign_sender_agent.go new file mode 100644 index 0000000..b63144a --- /dev/null +++ b/param/admin/kind_box_req/assign_sender_agent.go @@ -0,0 +1,8 @@ +package adminkindboxreqparam + +type AssignSenderRequest struct { + KindBoxReqID uint `json:"kind_box_req_id"` + SenderAgentID uint `json:"sender_agent_id"` +} + +type AssignSenderResponse struct{} diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 1e14da6..be587b0 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -1,21 +1,23 @@ package errmsg const ( - ErrorMsgNotFound = "record not found" - ErrorMsgSomethingWentWrong = "something went wrong" - ErrorMsgInvalidInput = "invalid input" - ErrorMsgInvalidStatus = "invalid status" - ErrorMsgPhoneNumberIsNotUnique = "phone number is not unique" - ErrorMsgEmailIsNotUnique = "email is not unique" - ErrorMsgPhoneNumberIsNotValid = "phone number is not valid" - ErrorMsgUserNotAllowed = "user not allowed" - ErrorMsgUserNotFound = "benefactor not found" - ErrorMsgOtpCodeExist = "please wait a little bit" - ErrorMsgOtpCodeIsNotValid = "verification code is not valid" - ErrorMsgCantScanQueryResult = "can't scan query result" - ErrorMsgPhoneNumberOrPassIsIncorrect = "phone number or password is incorrect" + ErrorMsgNotFound = "record not found" + ErrorMsgSomethingWentWrong = "something went wrong" + ErrorMsgInvalidInput = "invalid input" + ErrorMsgInvalidStatus = "invalid status" + ErrorMsgPhoneNumberIsNotUnique = "phone number is not unique" + ErrorMsgEmailIsNotUnique = "email is not unique" + ErrorMsgPhoneNumberIsNotValid = "phone number is not valid" + ErrorMsgUserNotAllowed = "user not allowed" + ErrorMsgUserNotFound = "benefactor not found" + ErrorMsgOtpCodeExist = "please wait a little bit" + ErrorMsgOtpCodeIsNotValid = "verification code is not valid" + ErrorMsgCantScanQueryResult = "can't scan query result" + ErrorMsgPhoneNumberOrPassIsIncorrect = "phone number or password is incorrect" ErrBadRequest = "Bad request" - ErrorMsgAcceptKindBoxReqStatus = "only pending requests will have the ability to be confirmed" - ErrorMsgRejectKindBoxReqStatus = "only pending requests will have the ability to be rejected" + ErrorMsgAcceptKindBoxReqStatus = "only pending requests will have the ability to be confirmed" + ErrorMsgRejectKindBoxReqStatus = "only pending requests will have the ability to be rejected" + ErrorMsgAssignSenderAgentKindBoxReqStatus = "only accepted kind_box_reqs will have the ability to be assign sender agent" ErrorMsgDeliverKindBoxReqStatus = "only assigned requests will have the ability to be delivered" + ErrorMsgAdminIsNotAgent = "admin is not agent" ) diff --git a/repository/mysql/admin/exist_admin.go b/repository/mysql/admin/exist_admin.go index 85dc0ef..fe8e244 100644 --- a/repository/mysql/admin/exist_admin.go +++ b/repository/mysql/admin/exist_admin.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" + entity "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -52,3 +53,25 @@ func (d DB) AdminExistByEmail(ctx context.Context, email string) (bool, error) { return true, nil } + +func (d DB) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) { + const op = "mysqladmin.GetAdminByID" + + row := d.conn.Conn().QueryRowContext(ctx, `select * from admins where id = ?`, adminID) + + admin, err := scanAdmin(row) + if err != nil { + sErr := sql.ErrNoRows + //TODO-errorsas: second argument to errors.As should not be *error + //nolint + if errors.As(err, &sErr) { + return entity.Admin{}, nil + } + + // TODO - log unexpected error for better observability + return entity.Admin{}, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + + return admin, nil +} diff --git a/repository/mysql/benefactor/exist_benefactor.go b/repository/mysql/benefactor/exist_benefactor.go index cbbc49d..ddb0c61 100644 --- a/repository/mysql/benefactor/exist_benefactor.go +++ b/repository/mysql/benefactor/exist_benefactor.go @@ -91,18 +91,14 @@ func mapNotNullToBenefactor(data nullableFields, benefactor *entity.Benefactor) if data.lastName.Valid { benefactor.LastName = data.lastName.String } - //if data.address.Valid { - // benefactor.Address = data.address.String - //} + if data.description.Valid { benefactor.Description = data.description.String } if data.email.Valid { benefactor.Email = data.email.String } - //if data.city.Valid { - // benefactor.City = data.city.String - //} + if data.genderStr.Valid { benefactor.Gender = entity.MapToGender(data.genderStr.String) } diff --git a/repository/mysql/kind_box_req/assign_sender_agent.go b/repository/mysql/kind_box_req/assign_sender_agent.go new file mode 100644 index 0000000..90136b3 --- /dev/null +++ b/repository/mysql/kind_box_req/assign_sender_agent.go @@ -0,0 +1,23 @@ +package mysqlkindboxreq + +import ( + "context" + "time" + + entity "git.gocasts.ir/ebhomengo/niki/entity" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (d DB) AssignSenderAgentToKindBoxReq(ctx context.Context, kindBoxReqID, senderAgentID uint) error { + const op = "mysqlkindboxreq.AssignSenderAgentToKindBoxReq" + + _, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set sender_agent_id = ?, status = ?, updated_at= ? where id = ?`, + senderAgentID, entity.KindBoxReqAssignedSenderAgentStatus.String(), time.Now(), kindBoxReqID) + if err != nil { + return richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + return nil +} diff --git a/repository/mysql/migration/1708712563_insert_admins_table_super_admin_value.sql b/repository/mysql/migration/1708712563_insert_admins_table_super_admin_value.sql index 8de5ae3..950adee 100644 --- a/repository/mysql/migration/1708712563_insert_admins_table_super_admin_value.sql +++ b/repository/mysql/migration/1708712563_insert_admins_table_super_admin_value.sql @@ -1,8 +1,8 @@ -- +migrate Up -- what can we do for password? INSERT INTO `admins` (`id`, `phone_number`, `email`,`password`,`role`,`status`) - VALUES - (1, '09122702856', 'keshvari@gmail.com','Abc123456','super-admin','active'); +VALUES + (1, '09122702856', 'keshvari@gmail.com','Abc123456','super-admin','active'); -- +migrate Down DELETE diff --git a/service/admin/admin/get.go b/service/admin/admin/get.go new file mode 100644 index 0000000..fb135d5 --- /dev/null +++ b/service/admin/admin/get.go @@ -0,0 +1,19 @@ +package adminservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) AdminExistByID(ctx context.Context, req param.AdminExistByIDRequest) (param.AdminExistByIDResponse, error) { + const op = "adminservice.AdminExistByID" + + admin, err := s.repo.GetAdminByID(ctx, req.AdminID) + if err != nil { + return param.AdminExistByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.AdminExistByIDResponse{Admin: admin}, nil +} diff --git a/service/admin/admin/service.go b/service/admin/admin/service.go index a5fe3dc..7eb4113 100644 --- a/service/admin/admin/service.go +++ b/service/admin/admin/service.go @@ -17,6 +17,7 @@ type AuthGenerator interface { type Repository interface { AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) + GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) } type Service struct { diff --git a/service/admin/kind_box/delete.go b/service/admin/kind_box/delete.go deleted file mode 100644 index 9b0bb5d..0000000 --- a/service/admin/kind_box/delete.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxservice diff --git a/service/admin/kind_box/get.go b/service/admin/kind_box/get.go deleted file mode 100644 index 9b0bb5d..0000000 --- a/service/admin/kind_box/get.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxservice diff --git a/service/admin/kind_box/get_all.go b/service/admin/kind_box/get_all.go deleted file mode 100644 index 9b0bb5d..0000000 --- a/service/admin/kind_box/get_all.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxservice diff --git a/service/admin/kind_box/update.go b/service/admin/kind_box/update.go deleted file mode 100644 index 9b0bb5d..0000000 --- a/service/admin/kind_box/update.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxservice diff --git a/service/admin/kind_box_req/add.go b/service/admin/kind_box_req/add.go deleted file mode 100644 index 475ca43..0000000 --- a/service/admin/kind_box_req/add.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqservice diff --git a/service/admin/kind_box_req/assign_sender_agent.go b/service/admin/kind_box_req/assign_sender_agent.go new file mode 100644 index 0000000..d4b41b4 --- /dev/null +++ b/service/admin/kind_box_req/assign_sender_agent.go @@ -0,0 +1,19 @@ +package adminkindboxreqservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) AssignSenderAgent(ctx context.Context, req param.AssignSenderRequest) (param.AssignSenderResponse, error) { + const op = "adminkindboxreqservice.AssignSenderAgent" + + err := s.repo.AssignSenderAgentToKindBoxReq(ctx, req.KindBoxReqID, req.SenderAgentID) + if err != nil { + return param.AssignSenderResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.AssignSenderResponse{}, nil +} diff --git a/service/admin/kind_box_req/delete.go b/service/admin/kind_box_req/delete.go deleted file mode 100644 index 475ca43..0000000 --- a/service/admin/kind_box_req/delete.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqservice diff --git a/service/admin/kind_box_req/get.go b/service/admin/kind_box_req/get.go deleted file mode 100644 index 475ca43..0000000 --- a/service/admin/kind_box_req/get.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqservice diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index beac3d8..2128d05 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -3,7 +3,7 @@ package adminkindboxreqservice import ( "context" - entity "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/entity" paginationparam "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" ) @@ -14,6 +14,7 @@ type Repository interface { RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error RollbackKindBoxRequestStatus(ctx context.Context, id uint) error GetAllKindBoxReq(ctx context.Context, pagination paginationparam.PaginationRequest) ([]entity.KindBoxReq, paginationparam.PaginationResponse, error) + AssignSenderAgentToKindBoxReq(ctx context.Context, kindBoxReqID uint, senderAgentID uint) error DeliverKindBoxReq(ctx context.Context, kindBoxReqID uint) error } diff --git a/service/admin/kind_box_req/update.go b/service/admin/kind_box_req/update.go deleted file mode 100644 index 475ca43..0000000 --- a/service/admin/kind_box_req/update.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqservice diff --git a/service/benefactor/kind_box_req/add.go b/service/benefactor/kind_box_req/add.go index 3acaecc..0baab21 100644 --- a/service/benefactor/kind_box_req/add.go +++ b/service/benefactor/kind_box_req/add.go @@ -2,6 +2,7 @@ package benefactorkindboxreqservice import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" diff --git a/service/benefactor/kind_box_req/delete.go b/service/benefactor/kind_box_req/delete.go deleted file mode 100644 index cf248cf..0000000 --- a/service/benefactor/kind_box_req/delete.go +++ /dev/null @@ -1 +0,0 @@ -package benefactorkindboxreqservice diff --git a/service/benefactor/kind_box_req/get_all.go b/service/benefactor/kind_box_req/get_all.go deleted file mode 100644 index cf248cf..0000000 --- a/service/benefactor/kind_box_req/get_all.go +++ /dev/null @@ -1 +0,0 @@ -package benefactorkindboxreqservice diff --git a/service/benefactor/kind_box_req/update.go b/service/benefactor/kind_box_req/update.go deleted file mode 100644 index cf248cf..0000000 --- a/service/benefactor/kind_box_req/update.go +++ /dev/null @@ -1 +0,0 @@ -package benefactorkindboxreqservice diff --git a/validator/admin/kind_box/add.go b/validator/admin/kind_box/add.go deleted file mode 100644 index 7ba0d92..0000000 --- a/validator/admin/kind_box/add.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxvalidator diff --git a/validator/admin/kind_box/delete.go b/validator/admin/kind_box/delete.go deleted file mode 100644 index 7ba0d92..0000000 --- a/validator/admin/kind_box/delete.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxvalidator diff --git a/validator/admin/kind_box/get.go b/validator/admin/kind_box/get.go deleted file mode 100644 index 7ba0d92..0000000 --- a/validator/admin/kind_box/get.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxvalidator diff --git a/validator/admin/kind_box/update.go b/validator/admin/kind_box/update.go deleted file mode 100644 index 7ba0d92..0000000 --- a/validator/admin/kind_box/update.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxvalidator diff --git a/validator/admin/kind_box_req/add.go b/validator/admin/kind_box_req/add.go deleted file mode 100644 index 7e69606..0000000 --- a/validator/admin/kind_box_req/add.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqvalidator diff --git a/validator/admin/kind_box_req/assign_sender_agent.go b/validator/admin/kind_box_req/assign_sender_agent.go new file mode 100644 index 0000000..b7dbca0 --- /dev/null +++ b/validator/admin/kind_box_req/assign_sender_agent.go @@ -0,0 +1,40 @@ +package adminkindboxreqvalidator + +import ( + "errors" + + 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" + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +func (v Validator) ValidateAssignSenderAgent(req param.AssignSenderRequest) (map[string]string, error) { + const op = "adminkindboxreqvalidator.ValidateAssignSenderAgent" + + if err := validation.ValidateStruct(&req, + + validation.Field(&req.KindBoxReqID, validation.Required, validation.By(v.doesKindBoxRequestExist), validation.By(v.checkKindBoxReqStatusForAssigningSenderAgent)), + + validation.Field(&req.SenderAgentID, validation.Required, validation.By(v.doesAgentAdminExist))); err != nil { + + fieldErrors := make(map[string]string) + + var errV validation.Errors + if errors.As(err, &errV) { + for key, value := range errV { + if value != nil { + fieldErrors[key] = value.Error() + } + } + } + + return fieldErrors, richerror.New(op). + WithMessage(errmsg.ErrorMsgInvalidInput). + WithKind(richerror.KindInvalid). + WithMeta(map[string]interface{}{"req": req}). + WithErr(err) + } + + return map[string]string{}, nil +} diff --git a/validator/admin/kind_box_req/delete.go b/validator/admin/kind_box_req/delete.go deleted file mode 100644 index 7e69606..0000000 --- a/validator/admin/kind_box_req/delete.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqvalidator diff --git a/validator/admin/kind_box_req/get.go b/validator/admin/kind_box_req/get.go deleted file mode 100644 index 7e69606..0000000 --- a/validator/admin/kind_box_req/get.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqvalidator diff --git a/validator/admin/kind_box_req/update.go b/validator/admin/kind_box_req/update.go deleted file mode 100644 index 7e69606..0000000 --- a/validator/admin/kind_box_req/update.go +++ /dev/null @@ -1 +0,0 @@ -package adminkindboxreqvalidator diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index 6ec8339..e9b615c 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -6,6 +6,7 @@ import ( "fmt" "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" ) @@ -19,8 +20,13 @@ type Repository interface { GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) } +type AdminSvc interface { + AdminExistByID(ctx context.Context, req param.AdminExistByIDRequest) (param.AdminExistByIDResponse, error) +} + type Validator struct { - repo Repository + repo Repository + adminSvc AdminSvc } func New(repo Repository) Validator { @@ -76,6 +82,38 @@ func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error { return nil } +func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(value interface{}) error { + kindboxreqID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + kindBoxReq, err := v.repo.GetByID(context.Background(), kindboxreqID) + if err != nil { + return err + } + if kindBoxReq.Status != entity.KindBoxReqAcceptedStatus { + return fmt.Errorf(errmsg.ErrorMsgAssignSenderAgentKindBoxReqStatus) + } + + return nil +} + +func (v Validator) doesAgentAdminExist(value interface{}) error { + adminID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + resp, err := v.adminSvc.AdminExistByID(context.Background(), param.AdminExistByIDRequest{AdminID: adminID}) + if err != nil { + return err + } + if resp.Admin.Role != entity.AdminAgentRole { + return fmt.Errorf(errmsg.ErrorMsgAdminIsNotAgent) + } + + return nil +} + func (v Validator) checkKindBoxReqStatusForDelivering(value interface{}) error { kindboxreqID, ok := value.(uint) if !ok {