forked from ebhomengo/niki
1
0
Fork 0

feat(niki): implement admin sms notifications

This commit is contained in:
Erfan Mohammadi 2024-08-07 01:11:09 +03:30
parent 705adda09b
commit a906601d12
15 changed files with 172 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package adminkindboxhandler
import ( import (
"net/http" "net/http"
params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -40,5 +41,9 @@ func (h Handler) AssignReceiverAgent(c echo.Context) error {
return echo.NewHTTPError(code, msg) return echo.NewHTTPError(code, msg)
} }
go h.notificationSvc.KindBoxAssigned(params.NotificationKindBoxAssigned{
ReceiverAgentID: req.ReceiverAgentID,
})
return c.JSON(http.StatusNoContent, nil) return c.JSON(http.StatusNoContent, nil)
} }

View File

@ -3,6 +3,7 @@ package adminkindboxhandler
import ( import (
"net/http" "net/http"
params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -44,5 +45,9 @@ func (h Handler) Enumerate(c echo.Context) error {
return echo.NewHTTPError(code, msg) return echo.NewHTTPError(code, msg)
} }
go h.notificationSvc.KindBoxEnumerated(params.NotificationKindBoxEnumerated{
KindBoxID: req.KindBoxID,
})
return c.JSON(http.StatusNoContent, resp) return c.JSON(http.StatusNoContent, resp)
} }

View File

@ -4,21 +4,25 @@ import (
adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization" adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization"
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth" authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
"git.gocasts.ir/ebhomengo/niki/service/notification"
) )
type Handler struct { type Handler struct {
authSvc authservice.Service authSvc authservice.Service
adminKindBoxSvc adminkindboxservice.Service adminKindBoxSvc adminkindboxservice.Service
adminAuthorizeSvc adminauthorizationservice.Service adminAuthorizeSvc adminauthorizationservice.Service
notificationSvc notification.Service
} }
func New(authSvc authservice.Service, func New(authSvc authservice.Service,
adminKindBoxSvc adminkindboxservice.Service, adminKindBoxSvc adminkindboxservice.Service,
adminAuthorizeSvc adminauthorizationservice.Service, adminAuthorizeSvc adminauthorizationservice.Service,
notificationSvc notification.Service,
) Handler { ) Handler {
return Handler{ return Handler{
authSvc: authSvc, authSvc: authSvc,
adminKindBoxSvc: adminKindBoxSvc, adminKindBoxSvc: adminKindBoxSvc,
adminAuthorizeSvc: adminAuthorizeSvc, adminAuthorizeSvc: adminAuthorizeSvc,
notificationSvc: notificationSvc,
} }
} }

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_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) return echo.NewHTTPError(code, msg)
} }
go h.notificationSvc.KindBoxReqAssigned(params.NotificationKindBoxReqAssigned{
KindBoxReqID: req.KindBoxReqID,
})
return c.JSON(http.StatusOK, resp) return c.JSON(http.StatusOK, resp)
} }

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_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) return echo.NewHTTPError(code, msg)
} }
go h.notificationSvc.KindBoxReqRejected(params.NotificationKindBoxReqRejected{
KindBoxReqID: req.ID,
Description: req.Description,
})
return c.JSON(http.StatusOK, resp) return c.JSON(http.StatusOK, resp)
} }

View File

@ -43,7 +43,7 @@ func New(
config: cfg, config: cfg,
adminHandler: adminhandler.New(svc.AdminAuthSvc, svc.AdminSvc, svc.AdminAuthorizeSvc), adminHandler: adminhandler.New(svc.AdminAuthSvc, svc.AdminSvc, svc.AdminAuthorizeSvc),
adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), 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), adminAgentHandler: adminagenthandler.New(svc.AdminAuthSvc, svc.AdminAgentSvc, svc.AdminAuthorizeSvc),
agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc), agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc),
benefactorHandler: benefactorhandler.New(svc.BenefactorAuthSvc, svc.BenefactorSvc), benefactorHandler: benefactorhandler.New(svc.BenefactorAuthSvc, svc.BenefactorSvc),

View File

@ -3,3 +3,20 @@ package param
type NotificationKindBoxReqAccepted struct { type NotificationKindBoxReqAccepted struct {
KindBoxReqID uint KindBoxReqID uint
} }
type NotificationKindBoxReqRejected struct {
KindBoxReqID uint
Description string
}
type NotificationKindBoxReqAssigned struct {
KindBoxReqID uint
}
type NotificationKindBoxAssigned struct {
ReceiverAgentID uint
}
type NotificationKindBoxEnumerated struct {
KindBoxID uint
}

View File

@ -1,5 +1,9 @@
package smsmsg package smsmsg
const ( const (
SmsMsgKindBoxReqAccepted = "%s عزیز درخواست قلک شما پذیرفته شد" SmsMsgKindBoxReqAccepted = "%s عزیز، درخواست قلک شما پذیرفته شد"
SmsMsgKindBoxReqRejected = "%s عزیز، درخواست قلک شما به دلیل %s پذیرفته نشد"
SmsMsgKindBoxReqAssigned = "%s عزیز، درخواست قلک شماره %d جهت تحویل به نیکوکار به شما اختصاص داده شد"
SmsMsgKindBoxAssigned = "%s عزیز، قلکی جهت تخلیه به شما اختصاص داده شد"
SmsMsgKindBoxEnumerated = "%s عزیز، قلک شما با شماره سریال %s و مبلغ %d تومان شمارش شد. از مشارکت شما سپاسگزاریم."
) )

View File

@ -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))
}

View File

@ -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))
}

View File

@ -21,7 +21,7 @@ func (s Service) KindBoxReqAccepted(req params.NotificationKindBoxReqAccepted) {
fmt.Println(fmt.Errorf("error(%s):%w", op, err)) fmt.Println(fmt.Errorf("error(%s):%w", op, err))
} }
bnf, gErr := s.BenefactorSvc.GetByID(ctx, bnfparam.GetBenefactorByIDRequest{BenefactorID: kb.BenefactorID}) 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)) fmt.Println(fmt.Errorf("error(%s):%w", op, gErr))
} }
s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReqAccepted, bnf.FirstName)) s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReqAccepted, bnf.FirstName))

View File

@ -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))
}

View File

@ -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))
}

View File

@ -4,7 +4,9 @@ import (
"context" "context"
smscontract "git.gocasts.ir/ebhomengo/niki/contract/sms" 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" 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" 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) 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 { type Service struct {
smsAdapter smscontract.SmsAdapter smsAdapter smscontract.SmsAdapter
KindBoxReqSvc KindBoxReqSvc KindBoxReqSvc KindBoxReqSvc
BenefactorSvc BenefactorSvc 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{ return Service{
smsAdapter: smsAdapter, smsAdapter: smsAdapter,
KindBoxReqSvc: kindBoxReqSvc, KindBoxReqSvc: kindBoxReqSvc,
BenefactorSvc: benefactorSvc, BenefactorSvc: benefactorSvc,
AdminSvc: adminSvc,
KindBoxSvc: kindBoxSvc,
} }
} }

View File

@ -98,7 +98,7 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont
BenefactorKindBoxVld = benefactorkindboxvalidator.New(kindBoxRepo, BenefactorSvc, BenefactorAddressSvc, BenefactorReferTimeSvc) BenefactorKindBoxVld = benefactorkindboxvalidator.New(kindBoxRepo, BenefactorSvc, BenefactorAddressSvc, BenefactorReferTimeSvc)
BenefactorKindBoxSvc = benefactorkindboxservice.New(kindBoxRepo, BenefactorKindBoxVld) BenefactorKindBoxSvc = benefactorkindboxservice.New(kindBoxRepo, BenefactorKindBoxVld)
) )
NotificationSvc := notification.New(smsAdapter, AdminKindBoxReqSvc, AdminBenefactorSvc) NotificationSvc := notification.New(smsAdapter, AdminKindBoxReqSvc, AdminBenefactorSvc, AdminSvc, AdminKindBoxSvc)
return &Service{ return &Service{
AdminAuthSvc: AdminAuthSvc, AdminAuthSvc: AdminAuthSvc,