From 4ef5c0ed66551ce6a3f0ef1ba95c7afcfa8f940b Mon Sep 17 00:00:00 2001 From: Iman Mirazimi Date: Mon, 16 Sep 2024 18:26:05 +0330 Subject: [PATCH] feat(niki): sms notification for agent --- .../http_server/agent/kind_box/handler.go | 4 +++ delivery/http_server/agent/kind_box/return.go | 5 ++++ .../http_server/agent/kind_box_req/deliver.go | 5 ++++ .../http_server/agent/kind_box_req/handler.go | 4 +++ delivery/http_server/server.go | 4 +-- param/notification.go | 8 ++++++ pkg/sms_msg/message.go | 2 ++ service/notification/kindbox_returned.go | 27 +++++++++++++++++++ service/notification/kindboxreq_delivered.go | 27 +++++++++++++++++++ 9 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 service/notification/kindbox_returned.go create mode 100644 service/notification/kindboxreq_delivered.go diff --git a/delivery/http_server/agent/kind_box/handler.go b/delivery/http_server/agent/kind_box/handler.go index be5d185..39c962b 100644 --- a/delivery/http_server/agent/kind_box/handler.go +++ b/delivery/http_server/agent/kind_box/handler.go @@ -4,21 +4,25 @@ import ( adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization" agentkindboxservice "git.gocasts.ir/ebhomengo/niki/service/agent/kind_box" authservice "git.gocasts.ir/ebhomengo/niki/service/auth" + "git.gocasts.ir/ebhomengo/niki/service/notification" ) type Handler struct { authSvc authservice.Service agentKindBoxSvc agentkindboxservice.Service adminAuthorizeSvc adminauthorizationservice.Service + notificationSvc notification.Service } func New(authSvc authservice.Service, agentKindBoxSvc agentkindboxservice.Service, adminAuthorizeSvc adminauthorizationservice.Service, + notificationSvc notification.Service, ) Handler { return Handler{ authSvc: authSvc, agentKindBoxSvc: agentKindBoxSvc, adminAuthorizeSvc: adminAuthorizeSvc, + notificationSvc: notificationSvc, } } diff --git a/delivery/http_server/agent/kind_box/return.go b/delivery/http_server/agent/kind_box/return.go index 950b0f2..890be1c 100644 --- a/delivery/http_server/agent/kind_box/return.go +++ b/delivery/http_server/agent/kind_box/return.go @@ -3,6 +3,7 @@ package agentkindboxhandler import ( "net/http" + params "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" "git.gocasts.ir/ebhomengo/niki/pkg/claim" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" @@ -46,5 +47,9 @@ func (h Handler) Return(c echo.Context) error { return echo.NewHTTPError(code, msg) } + go h.notificationSvc.KindBoxReturned(params.NotificationKindBoxReturned{ + KindBoxID: req.KindBoxID, + }) + return c.NoContent(http.StatusNoContent) } diff --git a/delivery/http_server/agent/kind_box_req/deliver.go b/delivery/http_server/agent/kind_box_req/deliver.go index 23fcad2..2b1fc16 100644 --- a/delivery/http_server/agent/kind_box_req/deliver.go +++ b/delivery/http_server/agent/kind_box_req/deliver.go @@ -2,6 +2,7 @@ package agentkindboxreqhandler import ( "context" + params "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" "net/http" @@ -52,5 +53,9 @@ func (h Handler) Deliver(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, errmsg.ErrorMsgSomethingWentWrong) } + go h.notificationSvc.KindBoxReqDelivered(params.NotificationKindBoxReqDelivered{ + KindBoxReqID: req.KindBoxReqID, + }) + return c.JSON(http.StatusOK, resp) } diff --git a/delivery/http_server/agent/kind_box_req/handler.go b/delivery/http_server/agent/kind_box_req/handler.go index 357cfa0..5ae299e 100644 --- a/delivery/http_server/agent/kind_box_req/handler.go +++ b/delivery/http_server/agent/kind_box_req/handler.go @@ -4,21 +4,25 @@ import ( adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization" agentkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/agent/kind_box_req" authservice "git.gocasts.ir/ebhomengo/niki/service/auth" + "git.gocasts.ir/ebhomengo/niki/service/notification" ) type Handler struct { authSvc authservice.Service agentKindBoxReqSvc agentkindboxreqservice.Service adminAuthorizeSvc adminauthorizationservice.Service + notificationSvc notification.Service } func New(authSvc authservice.Service, agentKindBoxReqSvc agentkindboxreqservice.Service, adminAuthorizeSvc adminauthorizationservice.Service, + notificationSvc notification.Service, ) Handler { return Handler{ authSvc: authSvc, agentKindBoxReqSvc: agentKindBoxReqSvc, adminAuthorizeSvc: adminAuthorizeSvc, + notificationSvc: notificationSvc, } } diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 8eb4a15..7464e14 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -47,8 +47,8 @@ func New( adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), adminAgentHandler: adminagenthandler.New(svc.AdminAuthSvc, svc.AdminAgentSvc, svc.AdminAuthorizeSvc), - agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc), - agentKindBoxReqHandler: agentkindboxreqhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxReqSvc, svc.AdminAuthorizeSvc), + agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), + agentKindBoxReqHandler: agentkindboxreqhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), benefactorHandler: benefactorhandler.New(svc.BenefactorAuthSvc, svc.BenefactorSvc), benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(svc.BenefactorAuthSvc, svc.BenefactorKindBoxReqSvc, svc.NotificationSvc), benefactorAddressHandler: benefactoraddresshandler.New(svc.BenefactorAuthSvc, svc.BenefactorAddressSvc), diff --git a/param/notification.go b/param/notification.go index 1a9d32d..3c1c54f 100644 --- a/param/notification.go +++ b/param/notification.go @@ -17,6 +17,10 @@ type NotificationKindBoxReqAssigned struct { KindBoxReqID uint } +type NotificationKindBoxReqDelivered struct { + KindBoxReqID uint +} + type NotificationKindBoxRegisteredEmptyingRequest struct { KindBoxID uint } @@ -28,3 +32,7 @@ type NotificationKindBoxAssigned struct { type NotificationKindBoxEnumerated struct { KindBoxID uint } + +type NotificationKindBoxReturned struct { + KindBoxID uint +} diff --git a/pkg/sms_msg/message.go b/pkg/sms_msg/message.go index 908b437..6e6b9c2 100644 --- a/pkg/sms_msg/message.go +++ b/pkg/sms_msg/message.go @@ -5,7 +5,9 @@ const ( SmsMsgKindBoxReqAccepted = "%s عزیز، درخواست قلک شما پذیرفته شد" SmsMsgKindBoxReqRejected = "%s عزیز، درخواست قلک شما به دلیل %s پذیرفته نشد" SmsMsgKindBoxReqAssigned = "%s عزیز، درخواست قلک شماره %d جهت تحویل به نیکوکار به شما اختصاص داده شد" + SmsMsgKindBoxReqDelivered = "%s عزیز, درخواست قلک شما تحویل داده شد" SmsMsgKindBoxRegistedEmptyingRequest = "%s عزیز، درخواست خالی کردن قلک شما ثبت شد" SmsMsgKindBoxAssigned = "%s عزیز، قلکی جهت تخلیه به شما اختصاص داده شد" SmsMsgKindBoxEnumerated = "%s عزیز، قلک شما با شماره سریال %s و مبلغ %d تومان شمارش شد. از مشارکت شما سپاسگزاریم." + SmsMsgKindBoxReturned = "%s عزیز, قلک با شماره سریال %s از شما تحویل گرفته شد" ) diff --git a/service/notification/kindbox_returned.go b/service/notification/kindbox_returned.go new file mode 100644 index 0000000..b17c8da --- /dev/null +++ b/service/notification/kindbox_returned.go @@ -0,0 +1,27 @@ +package notification + +import ( + "context" + "fmt" + "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) KindBoxReturned(req param.NotificationKindBoxReturned) { + const op = "notification.KindBoxReturned" + + ctx := context.Background() + 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.Data.BenefactorID}) + if gErr != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, gErr)) + } + s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReturned, bnf.FirstName, kb.Data.SerialNumber)) +} diff --git a/service/notification/kindboxreq_delivered.go b/service/notification/kindboxreq_delivered.go new file mode 100644 index 0000000..84ade45 --- /dev/null +++ b/service/notification/kindboxreq_delivered.go @@ -0,0 +1,27 @@ +package notification + +import ( + "context" + "fmt" + "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) KindBoxReqDelivered(req param.NotificationKindBoxReqDelivered) { + const op = "notification.KindBoxReqDelivered" + + ctx := context.Background() + 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.Data.BenefactorID}) + if gErr != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, gErr)) + } + s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReqDelivered, bnf.FirstName)) +}