app-cloudep-notification-se.../internal/logic/senderservice/send_sms_logic.go

65 lines
1.6 KiB
Go
Raw Normal View History

2024-08-20 06:35:14 +00:00
package senderservicelogic
import (
"app-cloudep-notification-service/internal/domain/usecase"
2024-08-20 06:35:14 +00:00
"context"
2024-08-20 15:53:20 +00:00
ers "code.30cm.net/digimon/library-go/errors"
2024-08-20 06:35:14 +00:00
"app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type SendSmsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
return &SendSmsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
type sendSMSReq struct {
// RecipientAddress 收件者
RecipientAddress string `validate:"required,phone"`
// Body 內容
Body string `validate:"required"`
// RecipientName 收件者信名
RecipientName string `validate:"required"`
}
// SendSms 寄簡訊
2024-08-20 06:35:14 +00:00
func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKResp, error) {
if err := l.svcCtx.Validate.ValidateAll(&sendSMSReq{
RecipientName: in.GetTo(),
Body: in.GetBody(),
RecipientAddress: in.GetTo(),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
}
2024-08-20 15:53:20 +00:00
// TODO 以後可以做換線
err := l.svcCtx.SMSSender.SendSMS(l.ctx, usecase.SMSReq{
RecipientAddress: in.GetTo(),
RecipientName: in.GetRecipientName(),
Body: in.GetBody(),
})
if err != nil {
logx.WithCallerSkip(1).WithFields(
logx.Field("func", "SMSSender.SendSMS"),
logx.Field("in", in),
logx.Field("err", err),
).Error(err.Error())
return nil, ers.ArkInternal("SMSSender.SendSMS failed to send sms")
}
2024-08-20 06:35:14 +00:00
return &notification.OKResp{}, nil
}