From 41164f6cee68c2b79e0e20929d526f0ae40553d4 Mon Sep 17 00:00:00 2001 From: Ruhollah Date: Fri, 16 Aug 2024 21:07:37 +0330 Subject: [PATCH] feat(niki): implement notification for benefactor (#119) --- config/config.go | 2 ++ config/constant.go | 3 +- config/default.go | 5 +++- .../benefactor/kind_box/handler.go | 4 +++ .../kind_box/register_emptying_request.go | 5 ++++ .../benefactor/kind_box_req/add.go | 5 ++++ .../benefactor/kind_box_req/handler.go | 4 +++ delivery/http_server/server.go | 4 +-- param/notification.go | 8 +++++ pkg/sms_msg/message.go | 12 ++++---- .../kindbox_registered_emptying_request.go | 30 +++++++++++++++++++ service/notification/kindboxreq_added.go | 30 +++++++++++++++++++ service/notification/service.go | 9 +++++- service/service.go | 2 +- validator/admin/admin/login_test.go | 7 ++--- 15 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 service/notification/kindbox_registered_emptying_request.go create mode 100644 service/notification/kindboxreq_added.go diff --git a/config/config.go b/config/config.go index 9690333..22aec59 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ import ( "git.gocasts.ir/ebhomengo/niki/repository/mysql" authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" + "git.gocasts.ir/ebhomengo/niki/service/notification" ) type HTTPServer struct { @@ -20,4 +21,5 @@ type Config struct { Redis redis.Config `koanf:"redis"` KavenegarSmsProvider smsprovider.Config `koanf:"kavenegar_sms_provider"` BenefactorSvc benefactorservice.Config `koanf:"benefactor_service"` + NotificationSvc notification.Config `koanf:"notification_service"` } diff --git a/config/constant.go b/config/constant.go index f5c1ab5..32990ea 100644 --- a/config/constant.go +++ b/config/constant.go @@ -6,11 +6,12 @@ const ( OtpChars = "0123456789" OtpExpireTime time.Duration = 2 * 60 * 1000 * 1000000 // 2 minutes - JwtSignKey = "jwt_secret" AccessTokenSubject = "ac" RefreshTokenSubject = "rt" AccessTokenExpireDuration = time.Hour * 24 RefreshTokenExpireDuration = time.Hour * 24 * 7 AuthMiddlewareContextKey = "claims" BcryptCost = 3 + + NotificationTimeout = time.Second * 10 ) diff --git a/config/default.go b/config/default.go index c3ff428..60d393e 100644 --- a/config/default.go +++ b/config/default.go @@ -3,6 +3,7 @@ package config import ( authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" + "git.gocasts.ir/ebhomengo/niki/service/notification" ) func Default() Config { @@ -19,11 +20,13 @@ func Default() Config { AccessSubject: AccessTokenSubject, RefreshSubject: RefreshTokenSubject, }, - BenefactorSvc: benefactorservice.Config{ OtpChars: OtpChars, OtpExpireTime: OtpExpireTime, }, + NotificationSvc: notification.Config{ + Timeout: NotificationTimeout, + }, } return cfx diff --git a/delivery/http_server/benefactor/kind_box/handler.go b/delivery/http_server/benefactor/kind_box/handler.go index 3c23e6f..bed758e 100644 --- a/delivery/http_server/benefactor/kind_box/handler.go +++ b/delivery/http_server/benefactor/kind_box/handler.go @@ -3,18 +3,22 @@ package benefactorkindboxhandler import ( authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box" + "git.gocasts.ir/ebhomengo/niki/service/notification" ) type Handler struct { authSvc authservice.Service benefactorKindBoxSvc benefactorkindboxservice.Service + notificationSvc notification.Service } func New(authSvc authservice.Service, benefactorKindBoxSvc benefactorkindboxservice.Service, + notificationSvc notification.Service, ) Handler { return Handler{ authSvc: authSvc, benefactorKindBoxSvc: benefactorKindBoxSvc, + notificationSvc: notificationSvc, } } diff --git a/delivery/http_server/benefactor/kind_box/register_emptying_request.go b/delivery/http_server/benefactor/kind_box/register_emptying_request.go index 5f31701..7ba3867 100644 --- a/delivery/http_server/benefactor/kind_box/register_emptying_request.go +++ b/delivery/http_server/benefactor/kind_box/register_emptying_request.go @@ -3,6 +3,7 @@ package benefactorkindboxhandler import ( "net/http" + params "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" "git.gocasts.ir/ebhomengo/niki/pkg/claim" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" @@ -42,5 +43,9 @@ func (h Handler) RegisterEmptyingRequest(c echo.Context) error { return echo.NewHTTPError(code, msg) } + go h.notificationSvc.KindBoxRegisteredEmptyingRequest(params.NotificationKindBoxRegisteredEmptyingRequest{ + KindBoxID: resp.ID, + }) + return c.JSON(http.StatusNoContent, nil) } diff --git a/delivery/http_server/benefactor/kind_box_req/add.go b/delivery/http_server/benefactor/kind_box_req/add.go index 0ae2e39..7066139 100644 --- a/delivery/http_server/benefactor/kind_box_req/add.go +++ b/delivery/http_server/benefactor/kind_box_req/add.go @@ -3,6 +3,7 @@ package benefactorkindboxreqhandler import ( "net/http" + params "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" "git.gocasts.ir/ebhomengo/niki/pkg/claim" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" @@ -44,5 +45,9 @@ func (h Handler) Add(c echo.Context) error { return echo.NewHTTPError(code, msg) } + go h.notificationSvc.KindBoxReqAdded(params.NotificationKindBoxReqAdded{ + KindBoxReqID: resp.KindBoxReq.ID, + }) + return c.JSON(http.StatusCreated, resp) } diff --git a/delivery/http_server/benefactor/kind_box_req/handler.go b/delivery/http_server/benefactor/kind_box_req/handler.go index 61a2e78..3ab4bc7 100644 --- a/delivery/http_server/benefactor/kind_box_req/handler.go +++ b/delivery/http_server/benefactor/kind_box_req/handler.go @@ -3,18 +3,22 @@ package benefactorkindboxreqhandler import ( authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" + "git.gocasts.ir/ebhomengo/niki/service/notification" ) type Handler struct { authSvc authservice.Service benefactorKindBoxReqSvc benefactorkindboxreqservice.Service + notificationSvc notification.Service } func New(authSvc authservice.Service, benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, + notificationSvc notification.Service, ) Handler { return Handler{ authSvc: authSvc, benefactorKindBoxReqSvc: benefactorKindBoxReqSvc, + notificationSvc: notificationSvc, } } diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index ed72e71..ff00ed0 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -47,9 +47,9 @@ func New( adminAgentHandler: adminagenthandler.New(svc.AdminAuthSvc, svc.AdminAgentSvc, svc.AdminAuthorizeSvc), agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc), benefactorHandler: benefactorhandler.New(svc.BenefactorAuthSvc, svc.BenefactorSvc), - benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(svc.BenefactorAuthSvc, svc.BenefactorKindBoxReqSvc), + benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(svc.BenefactorAuthSvc, svc.BenefactorKindBoxReqSvc, svc.NotificationSvc), benefactorAddressHandler: benefactoraddresshandler.New(svc.BenefactorAuthSvc, svc.BenefactorAddressSvc), - benefactorKindBoxHandler: benefactorkindboxhandler.New(svc.BenefactorAuthSvc, svc.BenefactorKindBoxSvc), + benefactorKindBoxHandler: benefactorkindboxhandler.New(svc.BenefactorAuthSvc, svc.BenefactorKindBoxSvc, svc.NotificationSvc), } } diff --git a/param/notification.go b/param/notification.go index bf8a32d..1a9d32d 100644 --- a/param/notification.go +++ b/param/notification.go @@ -1,5 +1,9 @@ package param +type NotificationKindBoxReqAdded struct { + KindBoxReqID uint +} + type NotificationKindBoxReqAccepted struct { KindBoxReqID uint } @@ -13,6 +17,10 @@ type NotificationKindBoxReqAssigned struct { KindBoxReqID uint } +type NotificationKindBoxRegisteredEmptyingRequest struct { + KindBoxID uint +} + type NotificationKindBoxAssigned struct { ReceiverAgentID uint } diff --git a/pkg/sms_msg/message.go b/pkg/sms_msg/message.go index 03156c7..908b437 100644 --- a/pkg/sms_msg/message.go +++ b/pkg/sms_msg/message.go @@ -1,9 +1,11 @@ package smsmsg const ( - SmsMsgKindBoxReqAccepted = "%s عزیز، درخواست قلک شما پذیرفته شد" - SmsMsgKindBoxReqRejected = "%s عزیز، درخواست قلک شما به دلیل %s پذیرفته نشد" - SmsMsgKindBoxReqAssigned = "%s عزیز، درخواست قلک شماره %d جهت تحویل به نیکوکار به شما اختصاص داده شد" - SmsMsgKindBoxAssigned = "%s عزیز، قلکی جهت تخلیه به شما اختصاص داده شد" - SmsMsgKindBoxEnumerated = "%s عزیز، قلک شما با شماره سریال %s و مبلغ %d تومان شمارش شد. از مشارکت شما سپاسگزاریم." + SmsMsgKindBoxReqAdded = "%s عزیز، درخواست قلک شما ثبت شد" + SmsMsgKindBoxReqAccepted = "%s عزیز، درخواست قلک شما پذیرفته شد" + SmsMsgKindBoxReqRejected = "%s عزیز، درخواست قلک شما به دلیل %s پذیرفته نشد" + SmsMsgKindBoxReqAssigned = "%s عزیز، درخواست قلک شماره %d جهت تحویل به نیکوکار به شما اختصاص داده شد" + SmsMsgKindBoxRegistedEmptyingRequest = "%s عزیز، درخواست خالی کردن قلک شما ثبت شد" + SmsMsgKindBoxAssigned = "%s عزیز، قلکی جهت تخلیه به شما اختصاص داده شد" + SmsMsgKindBoxEnumerated = "%s عزیز، قلک شما با شماره سریال %s و مبلغ %d تومان شمارش شد. از مشارکت شما سپاسگزاریم." ) diff --git a/service/notification/kindbox_registered_emptying_request.go b/service/notification/kindbox_registered_emptying_request.go new file mode 100644 index 0000000..cf1c35d --- /dev/null +++ b/service/notification/kindbox_registered_emptying_request.go @@ -0,0 +1,30 @@ +package notification + +import ( + "context" + "fmt" + + params "git.gocasts.ir/ebhomengo/niki/param" + bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" + kbp "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + smsmsg "git.gocasts.ir/ebhomengo/niki/pkg/sms_msg" +) + +func (s Service) KindBoxRegisteredEmptyingRequest(req params.NotificationKindBoxRegisteredEmptyingRequest) { + const op = "notification.KindBoxRegisteredEmptyingRequest" + + ctx, cancel := context.WithTimeout(context.Background(), s.cfg.Timeout) + defer cancel() + + kb, err := s.KindBoxSvc.Get(ctx, kbp.KindBoxGetRequest{ + KindBoxID: req.KindBoxID, + }) + if err != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, err)) + } + bnf, gErr := s.BenefactorSvc.GetByID(ctx, bnfparam.GetBenefactorByIDRequest{BenefactorID: kb.BenefactorID}) + if gErr != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, gErr)) + } + s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxRegistedEmptyingRequest, bnf.FirstName)) +} diff --git a/service/notification/kindboxreq_added.go b/service/notification/kindboxreq_added.go new file mode 100644 index 0000000..6b50551 --- /dev/null +++ b/service/notification/kindboxreq_added.go @@ -0,0 +1,30 @@ +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) KindBoxReqAdded(req params.NotificationKindBoxReqAdded) { + const op = "notification.KindBoxReqAdded" + + ctx, cancel := context.WithTimeout(context.Background(), s.cfg.Timeout) + defer cancel() + + kb, err := s.KindBoxReqSvc.Get(ctx, kbparam.GetKindBoxReqRequest{ + KindBoxID: req.KindBoxReqID, + }) + if err != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, err)) + } + bnf, gErr := s.BenefactorSvc.GetByID(ctx, bnfparam.GetBenefactorByIDRequest{BenefactorID: kb.BenefactorID}) + if gErr != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, gErr)) + } + s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReqAdded, bnf.FirstName)) +} diff --git a/service/notification/service.go b/service/notification/service.go index 8e1cf3f..85cd3bb 100644 --- a/service/notification/service.go +++ b/service/notification/service.go @@ -2,6 +2,7 @@ package notification import ( "context" + "time" smscontract "git.gocasts.ir/ebhomengo/niki/contract/sms" param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" @@ -10,6 +11,10 @@ import ( adminkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" ) +type Config struct { + Timeout time.Duration `koanf:"timeout"` +} + type KindBoxReqSvc interface { Get(ctx context.Context, request adminkindboxreqparam.GetKindBoxReqRequest) (adminkindboxreqparam.GetKindBoxReqResponse, error) } @@ -27,6 +32,7 @@ type KindBoxSvc interface { } type Service struct { + cfg Config smsAdapter smscontract.SmsAdapter KindBoxReqSvc KindBoxReqSvc BenefactorSvc BenefactorSvc @@ -34,10 +40,11 @@ type Service struct { KindBoxSvc KindBoxSvc } -func New(smsAdapter smscontract.SmsAdapter, kindBoxReqSvc KindBoxReqSvc, benefactorSvc BenefactorSvc, +func New(cfg Config, smsAdapter smscontract.SmsAdapter, kindBoxReqSvc KindBoxReqSvc, benefactorSvc BenefactorSvc, adminSvc AdminSvc, kindBoxSvc KindBoxSvc, ) Service { return Service{ + cfg: cfg, smsAdapter: smsAdapter, KindBoxReqSvc: kindBoxReqSvc, BenefactorSvc: benefactorSvc, diff --git a/service/service.go b/service/service.go index 6b273d3..97821ca 100644 --- a/service/service.go +++ b/service/service.go @@ -98,7 +98,7 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont BenefactorKindBoxVld = benefactorkindboxvalidator.New(kindBoxRepo, BenefactorSvc, BenefactorAddressSvc, BenefactorReferTimeSvc) BenefactorKindBoxSvc = benefactorkindboxservice.New(kindBoxRepo, BenefactorKindBoxVld) ) - NotificationSvc := notification.New(smsAdapter, AdminKindBoxReqSvc, AdminBenefactorSvc, AdminSvc, AdminKindBoxSvc) + NotificationSvc := notification.New(cfg.NotificationSvc, smsAdapter, AdminKindBoxReqSvc, AdminBenefactorSvc, AdminSvc, AdminKindBoxSvc) return &Service{ AdminAuthSvc: AdminAuthSvc, diff --git a/validator/admin/admin/login_test.go b/validator/admin/admin/login_test.go index b488023..6c08607 100644 --- a/validator/admin/admin/login_test.go +++ b/validator/admin/admin/login_test.go @@ -3,10 +3,10 @@ package adminvalidator import ( "context" "errors" - adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" - errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" "testing" + adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" "github.com/stretchr/testify/assert" ) @@ -18,7 +18,6 @@ func TestValidateLoginWithPhoneNumberRequest(t *testing.T) { validPassword := "validpassword" t.Run("Valid request", func(t *testing.T) { - req := adminserviceparam.LoginWithPhoneNumberRequest{ PhoneNumber: validPhoneNumber, Password: validPassword, @@ -32,7 +31,6 @@ func TestValidateLoginWithPhoneNumberRequest(t *testing.T) { }) t.Run("Empty phone number", func(t *testing.T) { - req := adminserviceparam.LoginWithPhoneNumberRequest{ PhoneNumber: "", Password: validPassword, @@ -45,7 +43,6 @@ func TestValidateLoginWithPhoneNumberRequest(t *testing.T) { }) t.Run("Empty password", func(t *testing.T) { - req := adminserviceparam.LoginWithPhoneNumberRequest{ PhoneNumber: validPhoneNumber, Password: "",