From feee5c43ff9a5702c1ca5588c631d92a5f7c1d33 Mon Sep 17 00:00:00 2001 From: Ruhollah Date: Sat, 27 Jul 2024 19:47:13 +0330 Subject: [PATCH 1/2] feat(niki): partially implemented kind box req accepted notification --- .../kavenegar/notification/adapter.go | 4 +-- config.yml | 2 +- .../http_server/admin/kind_box_req/accept.go | 3 ++- .../http_server/admin/kind_box_req/handler.go | 5 +++- delivery/http_server/server.go | 4 ++- internal/initial/service.go | 14 ++++++++-- main.go | 8 ++++-- param/admin/benefactor/get.go | 10 +++++++ param/notification.go | 5 ++++ pkg/sms_msg/message.go | 5 ++++ .../mysql/benefactor/exist_benefactor.go | 22 ++++++++++++++++ service/admin/benefactor/get.go | 12 ++++++++- service/admin/benefactor/service.go | 1 + service/notification/kindboxreq_accepted.go | 19 ++++++++++++++ service/notification/service.go | 26 ++++++++++++++++--- 15 files changed, 125 insertions(+), 15 deletions(-) create mode 100644 param/admin/benefactor/get.go create mode 100644 param/notification.go create mode 100644 pkg/sms_msg/message.go create mode 100644 service/notification/kindboxreq_accepted.go diff --git a/adapter/sms_provider/kavenegar/notification/adapter.go b/adapter/sms_provider/kavenegar/notification/adapter.go index 1d98f2b..3f2210c 100644 --- a/adapter/sms_provider/kavenegar/notification/adapter.go +++ b/adapter/sms_provider/kavenegar/notification/adapter.go @@ -6,6 +6,6 @@ type Adapter struct { adapter *kavenegar.Adapter } -func New(adapter *kavenegar.Adapter) Adapter { - return Adapter{adapter: adapter} +func New(adapter *kavenegar.Adapter) *Adapter { + return &Adapter{adapter: adapter} } diff --git a/config.yml b/config.yml index 1efe382..dce7f19 100644 --- a/config.yml +++ b/config.yml @@ -28,7 +28,7 @@ benefactor_service: length_of_otp_code: 5 kavenegar_sms_provider: - api_key: insert_your_api_key + api_key: 68556765576F785033342F774C336A7A5A574C7863497457706656364B6848534E63373661736A676B65553D sender: "" otp_template_new_user: ebhomeverify otp_template_registered_user: ebhomeverify diff --git a/delivery/http_server/admin/kind_box_req/accept.go b/delivery/http_server/admin/kind_box_req/accept.go index e51346b..0208912 100644 --- a/delivery/http_server/admin/kind_box_req/accept.go +++ b/delivery/http_server/admin/kind_box_req/accept.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" + params "git.gocasts.ir/ebhomengo/niki/param" 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" @@ -46,6 +47,6 @@ func (h Handler) Accept(c echo.Context) error { return echo.NewHTTPError(code, msg) } - + go h.notificationSvc.KindBoxReqAccepted(params.NotificationKindBoxReqAccepted{KindBoxReqID: req.ID}) return c.JSON(http.StatusOK, resp) } diff --git a/delivery/http_server/admin/kind_box_req/handler.go b/delivery/http_server/admin/kind_box_req/handler.go index 7e65063..d6f3aea 100644 --- a/delivery/http_server/admin/kind_box_req/handler.go +++ b/delivery/http_server/admin/kind_box_req/handler.go @@ -4,6 +4,7 @@ import ( adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization" adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" authservice "git.gocasts.ir/ebhomengo/niki/service/auth" + "git.gocasts.ir/ebhomengo/niki/service/notification" adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" ) @@ -13,11 +14,12 @@ type Handler struct { adminKindBoxReqSvc adminkindboxreqservice.Service adminKindBoxReqVld adminkindboxreqvalidator.Validator adminAuthorizeSvc adminauthorizationservice.Service + notificationSvc notification.Service } func New(authConfig authservice.Config, authSvc authservice.Service, adminKindBoxReqSvc adminkindboxreqservice.Service, adminKindBoxReqVld adminkindboxreqvalidator.Validator, - adminAuthorizeSvc adminauthorizationservice.Service, + adminAuthorizeSvc adminauthorizationservice.Service, notificationSvc notification.Service, ) Handler { return Handler{ authConfig: authConfig, @@ -25,5 +27,6 @@ func New(authConfig authservice.Config, authSvc authservice.Service, adminKindBoxReqSvc: adminKindBoxReqSvc, adminKindBoxReqVld: adminKindBoxReqVld, adminAuthorizeSvc: adminAuthorizeSvc, + notificationSvc: notificationSvc, } } diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 82c407f..064b379 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -2,6 +2,7 @@ package httpserver import ( "fmt" + "git.gocasts.ir/ebhomengo/niki/service/notification" "git.gocasts.ir/ebhomengo/niki/config" adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin" @@ -70,6 +71,7 @@ func New( adminKindBoxVld adminkindboxvalidator.Validator, agentKindBoxSvc agentkindboxservice.Service, agentKindBoxVld agentkindboxvalidator.Validator, + notificationSvc notification.Service, ) Server { return Server{ Router: echo.New(), @@ -79,7 +81,7 @@ func New( benefactorAddressHandler: benefactoraddresshandler.New(cfg.Auth, benefactorAuthSvc, benefactorAddressSvc, benefactorAddressVld), benefactorKindBoxHandler: benefactorkindboxhandler.New(cfg.Auth, benefactorAuthSvc, benefactorKindBoxSvc, benefactorKindBoxVld), adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld, adminAuthorizeSvc), - adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, adminAuthSvc, adminKinBoxReqSvc, adminKinBoxReqVld, adminAuthorizeSvc), + adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, adminAuthSvc, adminKinBoxReqSvc, adminKinBoxReqVld, adminAuthorizeSvc, notificationSvc), adminKindBoxHandler: adminKindBoxHandler.New(cfg.Auth, adminAuthSvc, adminKindBoxSvc, adminKindBoxVld, adminAuthorizeSvc), agentKindBoxHandler: agentkindboxhandler.New(cfg.AdminAuth, adminAuthSvc, agentKindBoxSvc, agentKindBoxVld, adminAuthorizeSvc), } diff --git a/internal/initial/service.go b/internal/initial/service.go index 91b6ff0..eacb7c8 100644 --- a/internal/initial/service.go +++ b/internal/initial/service.go @@ -3,6 +3,7 @@ package initial import ( "git.gocasts.ir/ebhomengo/niki/adapter/redis" smsprovider "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar" + kavenegarnotification "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar/notification" kavenegarotp "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar/otp" "git.gocasts.ir/ebhomengo/niki/config" "git.gocasts.ir/ebhomengo/niki/repository/mysql" @@ -21,6 +22,7 @@ import ( benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" + "git.gocasts.ir/ebhomengo/niki/service/notification" ) type Services struct { @@ -33,11 +35,15 @@ type Services struct { AdminSvc adminservice.Service AdminKindBoxReqSvc adminkindboxreqservice.Service AdminReferTimeSvc adminrefertimeservice.Service + NotificationSvc notification.Service } -func initSms(cfg config.Config) *kavenegarotp.Adapter { +func initSmsOtp(cfg config.Config) *kavenegarotp.Adapter { return kavenegarotp.New(smsprovider.New(cfg.KavenegarSmsProvider)) } +func initSmsNotification(cfg config.Config) *kavenegarnotification.Adapter { + return kavenegarnotification.New(smsprovider.New(cfg.KavenegarSmsProvider)) +} func InitAdminService(cfg config.Config, db *mysql.DB) adminservice.Service { return adminservice.New(InitAdminMysql(db), InitAdminAuthService(cfg)) @@ -51,7 +57,7 @@ func InitBenefactorService(cfg config.Config, redisAdapter redis.Adapter, db *my return benefactorservice.New( cfg.BenefactorSvc, redisotp.New(redisAdapter), - initSms(cfg), + initSmsOtp(cfg), InitBenefactorAuthService(cfg), mysqlbenefactor.New(db), ) @@ -86,3 +92,7 @@ func InitAdminReferTimeService(db *mysql.DB) adminrefertimeservice.Service { func InitBenefactorKindBoxService(db *mysql.DB) benefactorkindboxservice.Service { return benefactorkindboxservice.New(mysqlkindbox.New(db)) } + +func InitNotificationService(cfg config.Config, db *mysql.DB) notification.Service { + return notification.New(initSmsNotification(cfg), InitAdminKindBoxReqService(db), InitBenefactorForAdminService(db)) +} diff --git a/main.go b/main.go index da75bd2..049c691 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,9 @@ package main import ( "flag" "fmt" - "git.gocasts.ir/ebhomengo/niki/adapter/redis" + "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar" + kavenegarnotification "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar/notification" "git.gocasts.ir/ebhomengo/niki/config" httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server" "git.gocasts.ir/ebhomengo/niki/internal/initial" @@ -40,6 +41,8 @@ func main() { migrate := parseFlags() cfg := config.C() + sms := kavenegarnotification.New(kavenegar.New(cfg.KavenegarSmsProvider)) + sms.Send("09915436059", "salam") db := initDatabase(cfg, migrate) defer func() { if err := db.CloseStatements(); err != nil { @@ -85,6 +88,7 @@ func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.D AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db), AdminSvc: initial.InitAdminService(cfg, db), AdminReferTimeSvc: initial.InitAdminReferTimeService(db), + NotificationSvc: initial.InitNotificationService(cfg, db), }, initial.AdminAuthorization{ AdminAuthorizationSvc: initial.InitAdminAuthorizationService(db), @@ -101,7 +105,7 @@ func initAndRunServer(cfg config.Config, dependencies *Dependencies) { dependencies.AdminSvc, dependencies.AdminVld, dependencies.AdminAuthSvc, dependencies.AdminKindBoxReqSvc, dependencies.AdminKindBoxReqVld, dependencies.AdminAuthorizationSvc, dependencies.AdminKindBoxSvc, dependencies.AdminKindBoxVld, - dependencies.AgentKindBoxSvc, dependencies.AgentKindBoxVld) + dependencies.AgentKindBoxSvc, dependencies.AgentKindBoxVld, dependencies.NotificationSvc) server.Serve() } diff --git a/param/admin/benefactor/get.go b/param/admin/benefactor/get.go new file mode 100644 index 0000000..67f3e85 --- /dev/null +++ b/param/admin/benefactor/get.go @@ -0,0 +1,10 @@ +package adminbenefactoreparam + +import "git.gocasts.ir/ebhomengo/niki/entity" + +type GetBenefactorByIDRequest struct { + BenefactorID uint +} +type GetBenefactorByIDResponse struct { + entity.Benefactor +} diff --git a/param/notification.go b/param/notification.go new file mode 100644 index 0000000..0aa6d9b --- /dev/null +++ b/param/notification.go @@ -0,0 +1,5 @@ +package param + +type NotificationKindBoxReqAccepted struct { + KindBoxReqID uint +} diff --git a/pkg/sms_msg/message.go b/pkg/sms_msg/message.go new file mode 100644 index 0000000..759f935 --- /dev/null +++ b/pkg/sms_msg/message.go @@ -0,0 +1,5 @@ +package smsmsg + +const ( + SmsMsgKindBoxReqAccepted = "%s عزیز درخواست قلک شما پذیرفته شد" +) diff --git a/repository/mysql/benefactor/exist_benefactor.go b/repository/mysql/benefactor/exist_benefactor.go index d86ddae..46b6e07 100644 --- a/repository/mysql/benefactor/exist_benefactor.go +++ b/repository/mysql/benefactor/exist_benefactor.go @@ -105,3 +105,25 @@ func mapNotNullToBenefactor(data nullableFields, benefactor *entity.Benefactor) benefactor.BirthDate = data.birthdate.Time } } + +func (d *DB) GetByID(ctx context.Context, benefactorID uint) (entity.Benefactor, error) { + const op = "mysqlbenefactor.IsExistBenefactorByID" + + row := d.conn.Conn().QueryRowContext(ctx, `select * from benefactors where id = ?`, benefactorID) + + bnf, err := scanBenefactor(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 bnf, nil + } + + // TODO - log unexpected error for better observability + return bnf, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + + return bnf, nil +} diff --git a/service/admin/benefactor/get.go b/service/admin/benefactor/get.go index 18e4bca..76bfd98 100644 --- a/service/admin/benefactor/get.go +++ b/service/admin/benefactor/get.go @@ -2,11 +2,21 @@ package benefactor import ( "context" - param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" + params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) +func (s Service) GetByID(ctx context.Context, req params.GetBenefactorByIDRequest) (params.GetBenefactorByIDResponse, error) { + const op = "benefactorservice.Get" + bnf, gErr := s.repo.GetByID(ctx, req.BenefactorID) + if gErr != nil { + return params.GetBenefactorByIDResponse{}, richerror.New(op).WithErr(gErr) + } + return params.GetBenefactorByIDResponse{bnf}, nil +} + func (s Service) BenefactorExistByID(ctx context.Context, req param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) { const op = "adminservice.BenefactorExistByID" diff --git a/service/admin/benefactor/service.go b/service/admin/benefactor/service.go index 7d18ca9..c37a630 100644 --- a/service/admin/benefactor/service.go +++ b/service/admin/benefactor/service.go @@ -8,6 +8,7 @@ import ( ) type Repository interface { + GetByID(ctx context.Context, benefactorID uint) (entity.Benefactor, error) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) } diff --git a/service/notification/kindboxreq_accepted.go b/service/notification/kindboxreq_accepted.go new file mode 100644 index 0000000..8a566f0 --- /dev/null +++ b/service/notification/kindboxreq_accepted.go @@ -0,0 +1,19 @@ +package notification + +import ( + "context" + "fmt" + params "git.gocasts.ir/ebhomengo/niki/param" + bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" + kbparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + smsmsg "git.gocasts.ir/ebhomengo/niki/pkg/sms_msg" +) + +func (s Service) KindBoxReqAccepted(req params.NotificationKindBoxReqAccepted) { + ctx := context.Background() + kb, _ := s.KindBoxReqSvc.Get(ctx, kbparam.GetKindBoxReqRequest{ + KindBoxID: req.KindBoxReqID, + }) + bnf, _ := s.BenefactorSvc.GetByID(ctx, bnfparam.GetBenefactorByIDRequest{BenefactorID: kb.BenefactorID}) + s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReqAccepted, bnf.FirstName)) +} diff --git a/service/notification/service.go b/service/notification/service.go index ca70d94..6df7fe8 100644 --- a/service/notification/service.go +++ b/service/notification/service.go @@ -1,15 +1,33 @@ package notification +import ( + "context" + params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +) + type SmsAdapter interface { Send(phoneNumber string, message string) } -type Service struct { - smsAdapter SmsAdapter +type KindBoxReqSvc interface { + Get(ctx context.Context, request param.GetKindBoxReqRequest) (param.GetKindBoxReqResponse, error) } -func New(smsAdapter SmsAdapter) Service { +type BenefactorSvc interface { + GetByID(ctx context.Context, req params.GetBenefactorByIDRequest) (params.GetBenefactorByIDResponse, error) +} + +type Service struct { + smsAdapter SmsAdapter + KindBoxReqSvc KindBoxReqSvc + BenefactorSvc BenefactorSvc +} + +func New(smsAdapter SmsAdapter, kindBoxReqSvc KindBoxReqSvc, benefactorSvc BenefactorSvc) Service { return Service{ - smsAdapter: smsAdapter, + smsAdapter: smsAdapter, + KindBoxReqSvc: kindBoxReqSvc, + BenefactorSvc: benefactorSvc, } } From f2a9e61352904746e502d02662c1970a12611eaf Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Tue, 30 Jul 2024 17:50:49 +0330 Subject: [PATCH 2/2] feat(niki): implement kind box req accepted notification --- .../sms_provider/kavenegar/notification/send.go | 3 ++- config.yml | 4 ++-- .../http_server/admin/kind_box_req/accept.go | 1 + delivery/http_server/server.go | 2 +- internal/initial/service.go | 9 +++++---- internal/initial/validator.go | 4 ++-- main.go | 9 +++------ service/admin/benefactor/address.go | 2 +- service/admin/benefactor/get.go | 14 ++++++++------ service/admin/benefactor/service.go | 17 ++++++++--------- service/benefactor/benefactor/get.go | 12 ++++++++++++ service/benefactor/benefactor/service.go | 1 + service/notification/kindboxreq_accepted.go | 13 +++++++++++-- service/notification/service.go | 1 + 14 files changed, 58 insertions(+), 34 deletions(-) diff --git a/adapter/sms_provider/kavenegar/notification/send.go b/adapter/sms_provider/kavenegar/notification/send.go index e36c091..3b97a6f 100644 --- a/adapter/sms_provider/kavenegar/notification/send.go +++ b/adapter/sms_provider/kavenegar/notification/send.go @@ -7,7 +7,8 @@ import ( ) func (a *Adapter) Send(phoneNumber, message string) { - const op = "kavenegarnotification.SendNotification" + const op = "kavenegarnotification.Send" + var params *kavenegar.MessageSendParam if _, err := a.adapter.Client().Message.Send(a.adapter.Config().Sender, []string{phoneNumber}, message, params); err != nil { //nolint diff --git a/config.yml b/config.yml index dce7f19..201a498 100644 --- a/config.yml +++ b/config.yml @@ -28,8 +28,8 @@ benefactor_service: length_of_otp_code: 5 kavenegar_sms_provider: - api_key: 68556765576F785033342F774C336A7A5A574C7863497457706656364B6848534E63373661736A676B65553D - sender: "" + api_key: insert_your_api_key + sender: insert_sender_number otp_template_new_user: ebhomeverify otp_template_registered_user: ebhomeverify diff --git a/delivery/http_server/admin/kind_box_req/accept.go b/delivery/http_server/admin/kind_box_req/accept.go index 0208912..ba69733 100644 --- a/delivery/http_server/admin/kind_box_req/accept.go +++ b/delivery/http_server/admin/kind_box_req/accept.go @@ -48,5 +48,6 @@ func (h Handler) Accept(c echo.Context) error { return echo.NewHTTPError(code, msg) } go h.notificationSvc.KindBoxReqAccepted(params.NotificationKindBoxReqAccepted{KindBoxReqID: req.ID}) + return c.JSON(http.StatusOK, resp) } diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 064b379..f673a48 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -2,7 +2,6 @@ package httpserver import ( "fmt" - "git.gocasts.ir/ebhomengo/niki/service/notification" "git.gocasts.ir/ebhomengo/niki/config" adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin" @@ -24,6 +23,7 @@ import ( benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" + "git.gocasts.ir/ebhomengo/niki/service/notification" adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box" adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" diff --git a/internal/initial/service.go b/internal/initial/service.go index eacb7c8..0e3d9be 100644 --- a/internal/initial/service.go +++ b/internal/initial/service.go @@ -41,6 +41,7 @@ type Services struct { func initSmsOtp(cfg config.Config) *kavenegarotp.Adapter { return kavenegarotp.New(smsprovider.New(cfg.KavenegarSmsProvider)) } + func initSmsNotification(cfg config.Config) *kavenegarnotification.Adapter { return kavenegarnotification.New(smsprovider.New(cfg.KavenegarSmsProvider)) } @@ -49,8 +50,8 @@ func InitAdminService(cfg config.Config, db *mysql.DB) adminservice.Service { return adminservice.New(InitAdminMysql(db), InitAdminAuthService(cfg)) } -func InitBenefactorForAdminService(db *mysql.DB) benefactorforadminservice.Service { - return benefactorforadminservice.New(InitAdminMysql(db)) +func InitBenefactorForAdminService(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) benefactorforadminservice.Service { + return benefactorforadminservice.New(InitAdminMysql(db), InitBenefactorService(cfg, redisAdapter, db)) } func InitBenefactorService(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) benefactorservice.Service { @@ -93,6 +94,6 @@ func InitBenefactorKindBoxService(db *mysql.DB) benefactorkindboxservice.Service return benefactorkindboxservice.New(mysqlkindbox.New(db)) } -func InitNotificationService(cfg config.Config, db *mysql.DB) notification.Service { - return notification.New(initSmsNotification(cfg), InitAdminKindBoxReqService(db), InitBenefactorForAdminService(db)) +func InitNotificationService(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) notification.Service { + return notification.New(initSmsNotification(cfg), InitAdminKindBoxReqService(db), InitBenefactorForAdminService(cfg, redisAdapter, db)) } diff --git a/internal/initial/validator.go b/internal/initial/validator.go index c598d5e..363ea30 100644 --- a/internal/initial/validator.go +++ b/internal/initial/validator.go @@ -25,8 +25,8 @@ type Validators struct { AgentKindBoxVld agentkindboxvalidator.Validator } -func InitAdminKindBoxReqValidator(db *mysql.DB, cfg config.Config) adminkindboxreqvalidator.Validator { - return adminkindboxreqvalidator.New(InitBenefactorKindBoxReqDB(db), InitAdminService(cfg, db), InitBenefactorForAdminService(db), InitAdminReferTimeService(db), InitBenefactorAddressService(db)) +func InitAdminKindBoxReqValidator(db *mysql.DB, cfg config.Config, redisAdapter redis.Adapter) adminkindboxreqvalidator.Validator { + return adminkindboxreqvalidator.New(InitBenefactorKindBoxReqDB(db), InitAdminService(cfg, db), InitBenefactorForAdminService(cfg, redisAdapter, db), InitAdminReferTimeService(db), InitBenefactorAddressService(db)) } func InitAdminValidator(db *mysql.DB) adminvalidator.Validator { diff --git a/main.go b/main.go index 049c691..4e1c06f 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,8 @@ package main import ( "flag" "fmt" + "git.gocasts.ir/ebhomengo/niki/adapter/redis" - "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar" - kavenegarnotification "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar/notification" "git.gocasts.ir/ebhomengo/niki/config" httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server" "git.gocasts.ir/ebhomengo/niki/internal/initial" @@ -41,8 +40,6 @@ func main() { migrate := parseFlags() cfg := config.C() - sms := kavenegarnotification.New(kavenegar.New(cfg.KavenegarSmsProvider)) - sms.Send("09915436059", "salam") db := initDatabase(cfg, migrate) defer func() { if err := db.CloseStatements(); err != nil { @@ -73,7 +70,7 @@ func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.D BenefactorKindBoxReqVld: initial.InitBenefactorKindBoxReqValidator(cfg, redisAdapter, db), BenefactorAddressVld: initial.InitBenefactorAddressValidator(cfg, redisAdapter, db), BenefactorKindBoxVld: initial.InitBenefactorKindBoxValidator(cfg, redisAdapter, db), - AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg), + AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg, redisAdapter), AdminVld: initial.InitAdminValidator(db), AdminKindBoxVld: initial.InitAdminKindBoxValidator(db, cfg), AgentKindBoxVld: initial.InitAgentKindBoxValidator(db), @@ -88,7 +85,7 @@ func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.D AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db), AdminSvc: initial.InitAdminService(cfg, db), AdminReferTimeSvc: initial.InitAdminReferTimeService(db), - NotificationSvc: initial.InitNotificationService(cfg, db), + NotificationSvc: initial.InitNotificationService(cfg, redisAdapter, db), }, initial.AdminAuthorization{ AdminAuthorizationSvc: initial.InitAdminAuthorizationService(db), diff --git a/service/admin/benefactor/address.go b/service/admin/benefactor/address.go index 584dbc5..ae426df 100644 --- a/service/admin/benefactor/address.go +++ b/service/admin/benefactor/address.go @@ -1,4 +1,4 @@ -package benefactor +package adminbenefactorservice import ( "context" diff --git a/service/admin/benefactor/get.go b/service/admin/benefactor/get.go index 76bfd98..7a909ec 100644 --- a/service/admin/benefactor/get.go +++ b/service/admin/benefactor/get.go @@ -1,24 +1,26 @@ -package benefactor +package adminbenefactorservice import ( "context" + param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) func (s Service) GetByID(ctx context.Context, req params.GetBenefactorByIDRequest) (params.GetBenefactorByIDResponse, error) { - const op = "benefactorservice.Get" - bnf, gErr := s.repo.GetByID(ctx, req.BenefactorID) + const op = "adminbenefactorservice.GetByID" + + bnf, gErr := s.benefactorService.GetByID(ctx, req) if gErr != nil { return params.GetBenefactorByIDResponse{}, richerror.New(op).WithErr(gErr) } - return params.GetBenefactorByIDResponse{bnf}, nil + + return params.GetBenefactorByIDResponse{Benefactor: bnf.Benefactor}, nil } func (s Service) BenefactorExistByID(ctx context.Context, req param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) { - const op = "adminservice.BenefactorExistByID" + const op = "adminbenefactorservice.BenefactorExistByID" isExisted, err := s.repo.IsExistBenefactorByID(ctx, req.ID) if err != nil { diff --git a/service/admin/benefactor/service.go b/service/admin/benefactor/service.go index c37a630..fac9418 100644 --- a/service/admin/benefactor/service.go +++ b/service/admin/benefactor/service.go @@ -1,26 +1,25 @@ -package benefactor +package adminbenefactorservice import ( "context" "git.gocasts.ir/ebhomengo/niki/entity" - param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" + params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" ) type Repository interface { - GetByID(ctx context.Context, benefactorID uint) (entity.Benefactor, error) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) } -type ForAdminSvc interface { - BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) - AddressExistByID(ctx context.Context, request param.GetAddressByIDRequest) (param.GetAddressByIDResponse, error) +type BenefactorService interface { + GetByID(ctx context.Context, req params.GetBenefactorByIDRequest) (params.GetBenefactorByIDResponse, error) } type Service struct { - repo Repository + repo Repository + benefactorService BenefactorService } -func New(repo Repository) Service { - return Service{repo: repo} +func New(repo Repository, benefactorService BenefactorService) Service { + return Service{repo: repo, benefactorService: benefactorService} } diff --git a/service/benefactor/benefactor/get.go b/service/benefactor/benefactor/get.go index 33df195..a0e2405 100644 --- a/service/benefactor/benefactor/get.go +++ b/service/benefactor/benefactor/get.go @@ -3,6 +3,7 @@ package benefactorservice import ( "context" + params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -17,3 +18,14 @@ func (s Service) BenefactorExistByID(ctx context.Context, req param.BenefactorEx return param.BenefactorExistByIDResponse{Existed: isExisted}, nil } + +func (s Service) GetByID(ctx context.Context, req params.GetBenefactorByIDRequest) (params.GetBenefactorByIDResponse, error) { + const op = "benefactorservice.GetByID" + + bnf, gErr := s.repo.GetByID(ctx, req.BenefactorID) + if gErr != nil { + return params.GetBenefactorByIDResponse{}, richerror.New(op).WithErr(gErr) + } + + return params.GetBenefactorByIDResponse{Benefactor: bnf}, nil +} diff --git a/service/benefactor/benefactor/service.go b/service/benefactor/benefactor/service.go index 7d79838..98d4228 100644 --- a/service/benefactor/benefactor/service.go +++ b/service/benefactor/benefactor/service.go @@ -17,6 +17,7 @@ type Repository interface { IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error) CreateBenefactor(ctx context.Context, benefactor entity.Benefactor) (entity.Benefactor, error) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) + GetByID(ctx context.Context, benefactorID uint) (entity.Benefactor, error) } type AuthGenerator interface { diff --git a/service/notification/kindboxreq_accepted.go b/service/notification/kindboxreq_accepted.go index 8a566f0..8dc8ef7 100644 --- a/service/notification/kindboxreq_accepted.go +++ b/service/notification/kindboxreq_accepted.go @@ -3,6 +3,7 @@ package notification import ( "context" "fmt" + params "git.gocasts.ir/ebhomengo/niki/param" bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" kbparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" @@ -10,10 +11,18 @@ import ( ) func (s Service) KindBoxReqAccepted(req params.NotificationKindBoxReqAccepted) { + const op = "notification.KindBoxReqAccepted" + ctx := context.Background() - kb, _ := s.KindBoxReqSvc.Get(ctx, kbparam.GetKindBoxReqRequest{ + kb, err := s.KindBoxReqSvc.Get(ctx, kbparam.GetKindBoxReqRequest{ KindBoxID: req.KindBoxReqID, }) - bnf, _ := s.BenefactorSvc.GetByID(ctx, bnfparam.GetBenefactorByIDRequest{BenefactorID: kb.BenefactorID}) + if err != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, err)) + } + bnf, gErr := s.BenefactorSvc.GetByID(ctx, bnfparam.GetBenefactorByIDRequest{BenefactorID: kb.BenefactorID}) + if err != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, gErr)) + } s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReqAccepted, bnf.FirstName)) } diff --git a/service/notification/service.go b/service/notification/service.go index 6df7fe8..39b3cc2 100644 --- a/service/notification/service.go +++ b/service/notification/service.go @@ -2,6 +2,7 @@ package notification import ( "context" + params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" )