forked from ebhomengo/niki
1
0
Fork 0

feat(niki): implement kind box req accepted notification

This commit is contained in:
Erfan Mohammadi 2024-07-30 17:50:49 +03:30
parent feee5c43ff
commit f2a9e61352
14 changed files with 58 additions and 34 deletions

View File

@ -7,7 +7,8 @@ import (
) )
func (a *Adapter) Send(phoneNumber, message string) { func (a *Adapter) Send(phoneNumber, message string) {
const op = "kavenegarnotification.SendNotification" const op = "kavenegarnotification.Send"
var params *kavenegar.MessageSendParam var params *kavenegar.MessageSendParam
if _, err := a.adapter.Client().Message.Send(a.adapter.Config().Sender, []string{phoneNumber}, message, params); err != nil { if _, err := a.adapter.Client().Message.Send(a.adapter.Config().Sender, []string{phoneNumber}, message, params); err != nil {
//nolint //nolint

View File

@ -28,8 +28,8 @@ benefactor_service:
length_of_otp_code: 5 length_of_otp_code: 5
kavenegar_sms_provider: kavenegar_sms_provider:
api_key: 68556765576F785033342F774C336A7A5A574C7863497457706656364B6848534E63373661736A676B65553D api_key: insert_your_api_key
sender: "" sender: insert_sender_number
otp_template_new_user: ebhomeverify otp_template_new_user: ebhomeverify
otp_template_registered_user: ebhomeverify otp_template_registered_user: ebhomeverify

View File

@ -48,5 +48,6 @@ func (h Handler) Accept(c echo.Context) error {
return echo.NewHTTPError(code, msg) return echo.NewHTTPError(code, msg)
} }
go h.notificationSvc.KindBoxReqAccepted(params.NotificationKindBoxReqAccepted{KindBoxReqID: req.ID}) go h.notificationSvc.KindBoxReqAccepted(params.NotificationKindBoxReqAccepted{KindBoxReqID: req.ID})
return c.JSON(http.StatusOK, resp) return c.JSON(http.StatusOK, resp)
} }

View File

@ -2,7 +2,6 @@ package httpserver
import ( import (
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/service/notification"
"git.gocasts.ir/ebhomengo/niki/config" "git.gocasts.ir/ebhomengo/niki/config"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin" 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" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box" benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box"
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" 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" adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box" adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"

View File

@ -41,6 +41,7 @@ type Services struct {
func initSmsOtp(cfg config.Config) *kavenegarotp.Adapter { func initSmsOtp(cfg config.Config) *kavenegarotp.Adapter {
return kavenegarotp.New(smsprovider.New(cfg.KavenegarSmsProvider)) return kavenegarotp.New(smsprovider.New(cfg.KavenegarSmsProvider))
} }
func initSmsNotification(cfg config.Config) *kavenegarnotification.Adapter { func initSmsNotification(cfg config.Config) *kavenegarnotification.Adapter {
return kavenegarnotification.New(smsprovider.New(cfg.KavenegarSmsProvider)) 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)) return adminservice.New(InitAdminMysql(db), InitAdminAuthService(cfg))
} }
func InitBenefactorForAdminService(db *mysql.DB) benefactorforadminservice.Service { func InitBenefactorForAdminService(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) benefactorforadminservice.Service {
return benefactorforadminservice.New(InitAdminMysql(db)) return benefactorforadminservice.New(InitAdminMysql(db), InitBenefactorService(cfg, redisAdapter, db))
} }
func InitBenefactorService(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) benefactorservice.Service { 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)) return benefactorkindboxservice.New(mysqlkindbox.New(db))
} }
func InitNotificationService(cfg config.Config, db *mysql.DB) notification.Service { func InitNotificationService(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) notification.Service {
return notification.New(initSmsNotification(cfg), InitAdminKindBoxReqService(db), InitBenefactorForAdminService(db)) return notification.New(initSmsNotification(cfg), InitAdminKindBoxReqService(db), InitBenefactorForAdminService(cfg, redisAdapter, db))
} }

View File

@ -25,8 +25,8 @@ type Validators struct {
AgentKindBoxVld agentkindboxvalidator.Validator AgentKindBoxVld agentkindboxvalidator.Validator
} }
func InitAdminKindBoxReqValidator(db *mysql.DB, cfg config.Config) adminkindboxreqvalidator.Validator { func InitAdminKindBoxReqValidator(db *mysql.DB, cfg config.Config, redisAdapter redis.Adapter) adminkindboxreqvalidator.Validator {
return adminkindboxreqvalidator.New(InitBenefactorKindBoxReqDB(db), InitAdminService(cfg, db), InitBenefactorForAdminService(db), InitAdminReferTimeService(db), InitBenefactorAddressService(db)) return adminkindboxreqvalidator.New(InitBenefactorKindBoxReqDB(db), InitAdminService(cfg, db), InitBenefactorForAdminService(cfg, redisAdapter, db), InitAdminReferTimeService(db), InitBenefactorAddressService(db))
} }
func InitAdminValidator(db *mysql.DB) adminvalidator.Validator { func InitAdminValidator(db *mysql.DB) adminvalidator.Validator {

View File

@ -3,9 +3,8 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/adapter/redis" "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" "git.gocasts.ir/ebhomengo/niki/config"
httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server" httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server"
"git.gocasts.ir/ebhomengo/niki/internal/initial" "git.gocasts.ir/ebhomengo/niki/internal/initial"
@ -41,8 +40,6 @@ func main() {
migrate := parseFlags() migrate := parseFlags()
cfg := config.C() cfg := config.C()
sms := kavenegarnotification.New(kavenegar.New(cfg.KavenegarSmsProvider))
sms.Send("09915436059", "salam")
db := initDatabase(cfg, migrate) db := initDatabase(cfg, migrate)
defer func() { defer func() {
if err := db.CloseStatements(); err != nil { 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), BenefactorKindBoxReqVld: initial.InitBenefactorKindBoxReqValidator(cfg, redisAdapter, db),
BenefactorAddressVld: initial.InitBenefactorAddressValidator(cfg, redisAdapter, db), BenefactorAddressVld: initial.InitBenefactorAddressValidator(cfg, redisAdapter, db),
BenefactorKindBoxVld: initial.InitBenefactorKindBoxValidator(cfg, redisAdapter, db), BenefactorKindBoxVld: initial.InitBenefactorKindBoxValidator(cfg, redisAdapter, db),
AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg), AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg, redisAdapter),
AdminVld: initial.InitAdminValidator(db), AdminVld: initial.InitAdminValidator(db),
AdminKindBoxVld: initial.InitAdminKindBoxValidator(db, cfg), AdminKindBoxVld: initial.InitAdminKindBoxValidator(db, cfg),
AgentKindBoxVld: initial.InitAgentKindBoxValidator(db), AgentKindBoxVld: initial.InitAgentKindBoxValidator(db),
@ -88,7 +85,7 @@ func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.D
AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db), AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db),
AdminSvc: initial.InitAdminService(cfg, db), AdminSvc: initial.InitAdminService(cfg, db),
AdminReferTimeSvc: initial.InitAdminReferTimeService(db), AdminReferTimeSvc: initial.InitAdminReferTimeService(db),
NotificationSvc: initial.InitNotificationService(cfg, db), NotificationSvc: initial.InitNotificationService(cfg, redisAdapter, db),
}, },
initial.AdminAuthorization{ initial.AdminAuthorization{
AdminAuthorizationSvc: initial.InitAdminAuthorizationService(db), AdminAuthorizationSvc: initial.InitAdminAuthorizationService(db),

View File

@ -1,4 +1,4 @@
package benefactor package adminbenefactorservice
import ( import (
"context" "context"

View File

@ -1,24 +1,26 @@
package benefactor package adminbenefactorservice
import ( import (
"context" "context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" param "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (s Service) GetByID(ctx context.Context, req params.GetBenefactorByIDRequest) (params.GetBenefactorByIDResponse, error) { func (s Service) GetByID(ctx context.Context, req params.GetBenefactorByIDRequest) (params.GetBenefactorByIDResponse, error) {
const op = "benefactorservice.Get" const op = "adminbenefactorservice.GetByID"
bnf, gErr := s.repo.GetByID(ctx, req.BenefactorID)
bnf, gErr := s.benefactorService.GetByID(ctx, req)
if gErr != nil { if gErr != nil {
return params.GetBenefactorByIDResponse{}, richerror.New(op).WithErr(gErr) 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) { 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) isExisted, err := s.repo.IsExistBenefactorByID(ctx, req.ID)
if err != nil { if err != nil {

View File

@ -1,26 +1,25 @@
package benefactor package adminbenefactorservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "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 { type Repository interface {
GetByID(ctx context.Context, benefactorID uint) (entity.Benefactor, error)
IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error)
GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error)
} }
type ForAdminSvc interface { type BenefactorService interface {
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) GetByID(ctx context.Context, req params.GetBenefactorByIDRequest) (params.GetBenefactorByIDResponse, error)
AddressExistByID(ctx context.Context, request param.GetAddressByIDRequest) (param.GetAddressByIDResponse, error)
} }
type Service struct { type Service struct {
repo Repository repo Repository
benefactorService BenefactorService
} }
func New(repo Repository) Service { func New(repo Repository, benefactorService BenefactorService) Service {
return Service{repo: repo} return Service{repo: repo, benefactorService: benefactorService}
} }

View File

@ -3,6 +3,7 @@ package benefactorservice
import ( import (
"context" "context"
params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" 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 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
}

View File

@ -17,6 +17,7 @@ type Repository interface {
IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error) IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error)
CreateBenefactor(ctx context.Context, benefactor entity.Benefactor) (entity.Benefactor, error) CreateBenefactor(ctx context.Context, benefactor entity.Benefactor) (entity.Benefactor, error)
IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error)
GetByID(ctx context.Context, benefactorID uint) (entity.Benefactor, error)
} }
type AuthGenerator interface { type AuthGenerator interface {

View File

@ -3,6 +3,7 @@ package notification
import ( import (
"context" "context"
"fmt" "fmt"
params "git.gocasts.ir/ebhomengo/niki/param" params "git.gocasts.ir/ebhomengo/niki/param"
bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
kbparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" kbparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
@ -10,10 +11,18 @@ import (
) )
func (s Service) KindBoxReqAccepted(req params.NotificationKindBoxReqAccepted) { func (s Service) KindBoxReqAccepted(req params.NotificationKindBoxReqAccepted) {
const op = "notification.KindBoxReqAccepted"
ctx := context.Background() ctx := context.Background()
kb, _ := s.KindBoxReqSvc.Get(ctx, kbparam.GetKindBoxReqRequest{ kb, err := s.KindBoxReqSvc.Get(ctx, kbparam.GetKindBoxReqRequest{
KindBoxID: req.KindBoxReqID, 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)) s.smsAdapter.Send(bnf.PhoneNumber, fmt.Sprintf(smsmsg.SmsMsgKindBoxReqAccepted, bnf.FirstName))
} }

View File

@ -2,6 +2,7 @@ package notification
import ( import (
"context" "context"
params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" params "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
) )