diff --git a/delivery/http_server/admin/kind_box/assign_receiver_agent.go b/delivery/http_server/admin/kind_box/assign_receiver_agent.go index de6f27d..0a601c8 100644 --- a/delivery/http_server/admin/kind_box/assign_receiver_agent.go +++ b/delivery/http_server/admin/kind_box/assign_receiver_agent.go @@ -3,6 +3,7 @@ package adminkindboxhandler import ( "net/http" + params "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" "github.com/labstack/echo/v4" @@ -40,5 +41,9 @@ func (h Handler) AssignReceiverAgent(c echo.Context) error { return echo.NewHTTPError(code, msg) } + go h.notificationSvc.KindBoxAssigned(params.NotificationKindBoxAssigned{ + ReceiverAgentID: req.ReceiverAgentID, + }) + return c.JSON(http.StatusNoContent, nil) } diff --git a/delivery/http_server/admin/kind_box/enumerate.go b/delivery/http_server/admin/kind_box/enumerate.go index 7431697..495430c 100644 --- a/delivery/http_server/admin/kind_box/enumerate.go +++ b/delivery/http_server/admin/kind_box/enumerate.go @@ -3,6 +3,7 @@ package adminkindboxhandler import ( "net/http" + params "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" "github.com/labstack/echo/v4" @@ -44,5 +45,9 @@ func (h Handler) Enumerate(c echo.Context) error { return echo.NewHTTPError(code, msg) } + go h.notificationSvc.KindBoxEnumerated(params.NotificationKindBoxEnumerated{ + KindBoxID: req.KindBoxID, + }) + return c.JSON(http.StatusNoContent, resp) } diff --git a/delivery/http_server/admin/kind_box/handler.go b/delivery/http_server/admin/kind_box/handler.go index 9529b04..95e3f86 100644 --- a/delivery/http_server/admin/kind_box/handler.go +++ b/delivery/http_server/admin/kind_box/handler.go @@ -4,21 +4,25 @@ import ( adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization" adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" authservice "git.gocasts.ir/ebhomengo/niki/service/auth" + "git.gocasts.ir/ebhomengo/niki/service/notification" ) type Handler struct { authSvc authservice.Service adminKindBoxSvc adminkindboxservice.Service adminAuthorizeSvc adminauthorizationservice.Service + notificationSvc notification.Service } func New(authSvc authservice.Service, adminKindBoxSvc adminkindboxservice.Service, adminAuthorizeSvc adminauthorizationservice.Service, + notificationSvc notification.Service, ) Handler { return Handler{ authSvc: authSvc, adminKindBoxSvc: adminKindBoxSvc, adminAuthorizeSvc: adminAuthorizeSvc, + notificationSvc: notificationSvc, } } 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 index 56002c8..45162b3 100644 --- a/delivery/http_server/admin/kind_box_req/assign_sender_agent.go +++ b/delivery/http_server/admin/kind_box_req/assign_sender_agent.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" @@ -50,5 +51,9 @@ func (h Handler) AssignSenderAgent(c echo.Context) error { return echo.NewHTTPError(code, msg) } + go h.notificationSvc.KindBoxReqAssigned(params.NotificationKindBoxReqAssigned{ + KindBoxReqID: req.KindBoxReqID, + }) + return c.JSON(http.StatusOK, resp) } diff --git a/delivery/http_server/admin/kind_box_req/reject.go b/delivery/http_server/admin/kind_box_req/reject.go index 46ff3e3..2444cb2 100644 --- a/delivery/http_server/admin/kind_box_req/reject.go +++ b/delivery/http_server/admin/kind_box_req/reject.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" @@ -45,5 +46,10 @@ func (h Handler) Reject(c echo.Context) error { return echo.NewHTTPError(code, msg) } + go h.notificationSvc.KindBoxReqRejected(params.NotificationKindBoxReqRejected{ + KindBoxReqID: req.ID, + Description: req.Description, + }) + return c.JSON(http.StatusOK, resp) } diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 3f76c12..ed72e71 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -43,7 +43,7 @@ func New( config: cfg, adminHandler: adminhandler.New(svc.AdminAuthSvc, svc.AdminSvc, svc.AdminAuthorizeSvc), adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), - adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminAuthorizeSvc), + 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), benefactorHandler: benefactorhandler.New(svc.BenefactorAuthSvc, svc.BenefactorSvc), diff --git a/param/notification.go b/param/notification.go index 0aa6d9b..bf8a32d 100644 --- a/param/notification.go +++ b/param/notification.go @@ -3,3 +3,20 @@ package param type NotificationKindBoxReqAccepted struct { KindBoxReqID uint } + +type NotificationKindBoxReqRejected struct { + KindBoxReqID uint + Description string +} + +type NotificationKindBoxReqAssigned struct { + KindBoxReqID uint +} + +type NotificationKindBoxAssigned struct { + ReceiverAgentID uint +} + +type NotificationKindBoxEnumerated struct { + KindBoxID uint +} diff --git a/pkg/sms_msg/message.go b/pkg/sms_msg/message.go index 759f935..03156c7 100644 --- a/pkg/sms_msg/message.go +++ b/pkg/sms_msg/message.go @@ -1,5 +1,9 @@ package smsmsg const ( - SmsMsgKindBoxReqAccepted = "%s عزیز درخواست قلک شما پذیرفته شد" + SmsMsgKindBoxReqAccepted = "%s عزیز، درخواست قلک شما پذیرفته شد" + SmsMsgKindBoxReqRejected = "%s عزیز، درخواست قلک شما به دلیل %s پذیرفته نشد" + SmsMsgKindBoxReqAssigned = "%s عزیز، درخواست قلک شماره %d جهت تحویل به نیکوکار به شما اختصاص داده شد" + SmsMsgKindBoxAssigned = "%s عزیز، قلکی جهت تخلیه به شما اختصاص داده شد" + SmsMsgKindBoxEnumerated = "%s عزیز، قلک شما با شماره سریال %s و مبلغ %d تومان شمارش شد. از مشارکت شما سپاسگزاریم." ) diff --git a/service/notification/kindbox_assigned.go b/service/notification/kindbox_assigned.go new file mode 100644 index 0000000..9aaedde --- /dev/null +++ b/service/notification/kindbox_assigned.go @@ -0,0 +1,21 @@ +package notification + +import ( + "context" + "fmt" + + params "git.gocasts.ir/ebhomengo/niki/param" + adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" + smsmsg "git.gocasts.ir/ebhomengo/niki/pkg/sms_msg" +) + +func (s Service) KindBoxAssigned(req params.NotificationKindBoxAssigned) { + const op = "notification.KindBoxAssigned" + + ctx := context.Background() + bnf, gErr := s.AdminSvc.AdminExistByID(ctx, adminserviceparam.AdminExistByIDRequest{AdminID: req.ReceiverAgentID}) + if gErr != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, gErr)) + } + s.smsAdapter.Send(bnf.Admin.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxAssigned, bnf.Admin.FirstName)) +} diff --git a/service/notification/kindbox_enumerated.go b/service/notification/kindbox_enumerated.go new file mode 100644 index 0000000..08b7ab4 --- /dev/null +++ b/service/notification/kindbox_enumerated.go @@ -0,0 +1,28 @@ +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) KindBoxEnumerated(req params.NotificationKindBoxEnumerated) { + const op = "notification.KindBoxEnumerated" + + 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.BenefactorID}) + if gErr != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, gErr)) + } + s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxEnumerated, bnf.FirstName, kb.SerialNumber, kb.Amount)) +} diff --git a/service/notification/kindboxreq_accepted.go b/service/notification/kindboxreq_accepted.go index 8dc8ef7..f5e9c67 100644 --- a/service/notification/kindboxreq_accepted.go +++ b/service/notification/kindboxreq_accepted.go @@ -21,7 +21,7 @@ func (s Service) KindBoxReqAccepted(req params.NotificationKindBoxReqAccepted) { fmt.Println(fmt.Errorf("error(%s):%w", op, err)) } bnf, gErr := s.BenefactorSvc.GetByID(ctx, bnfparam.GetBenefactorByIDRequest{BenefactorID: kb.BenefactorID}) - if err != nil { + if gErr != 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/kindboxreq_assigned.go b/service/notification/kindboxreq_assigned.go new file mode 100644 index 0000000..9ef236f --- /dev/null +++ b/service/notification/kindboxreq_assigned.go @@ -0,0 +1,28 @@ +package notification + +import ( + "context" + "fmt" + + params "git.gocasts.ir/ebhomengo/niki/param" + adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" + kbparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + smsmsg "git.gocasts.ir/ebhomengo/niki/pkg/sms_msg" +) + +func (s Service) KindBoxReqAssigned(req params.NotificationKindBoxReqAssigned) { + const op = "notification.KindBoxReqAssigned" + + 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.AdminSvc.AdminExistByID(ctx, adminserviceparam.AdminExistByIDRequest{AdminID: kb.SenderAgentID}) + if gErr != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, gErr)) + } + s.smsAdapter.Send(bnf.Admin.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReqAssigned, bnf.Admin.FirstName, kb.ID)) +} diff --git a/service/notification/kindboxreq_rejected.go b/service/notification/kindboxreq_rejected.go new file mode 100644 index 0000000..ab40998 --- /dev/null +++ b/service/notification/kindboxreq_rejected.go @@ -0,0 +1,28 @@ +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) KindBoxReqRejected(req params.NotificationKindBoxReqRejected) { + const op = "notification.KindBoxReqRejected" + + 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.BenefactorID}) + if gErr != nil { + fmt.Println(fmt.Errorf("error(%s):%w", op, gErr)) + } + s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReqRejected, bnf.FirstName, req.Description)) +} diff --git a/service/notification/service.go b/service/notification/service.go index 90db595..8e1cf3f 100644 --- a/service/notification/service.go +++ b/service/notification/service.go @@ -4,7 +4,9 @@ import ( "context" smscontract "git.gocasts.ir/ebhomengo/niki/contract/sms" + param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminbenefactorparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" + kbp "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" adminkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" ) @@ -16,16 +18,30 @@ type BenefactorSvc interface { GetByID(ctx context.Context, req adminbenefactorparam.GetBenefactorByIDRequest) (adminbenefactorparam.GetBenefactorByIDResponse, error) } +type AdminSvc interface { + AdminExistByID(ctx context.Context, req param.AdminExistByIDRequest) (param.AdminExistByIDResponse, error) +} + +type KindBoxSvc interface { + Get(ctx context.Context, req kbp.KindBoxGetRequest) (kbp.KindBoxGetResponse, error) +} + type Service struct { smsAdapter smscontract.SmsAdapter KindBoxReqSvc KindBoxReqSvc BenefactorSvc BenefactorSvc + AdminSvc AdminSvc + KindBoxSvc KindBoxSvc } -func New(smsAdapter smscontract.SmsAdapter, kindBoxReqSvc KindBoxReqSvc, benefactorSvc BenefactorSvc) Service { +func New(smsAdapter smscontract.SmsAdapter, kindBoxReqSvc KindBoxReqSvc, benefactorSvc BenefactorSvc, + adminSvc AdminSvc, kindBoxSvc KindBoxSvc, +) Service { return Service{ smsAdapter: smsAdapter, KindBoxReqSvc: kindBoxReqSvc, BenefactorSvc: benefactorSvc, + AdminSvc: adminSvc, + KindBoxSvc: kindBoxSvc, } } diff --git a/service/service.go b/service/service.go index e13fffc..6b273d3 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) + NotificationSvc := notification.New(smsAdapter, AdminKindBoxReqSvc, AdminBenefactorSvc, AdminSvc, AdminKindBoxSvc) return &Service{ AdminAuthSvc: AdminAuthSvc,