65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package senderservicelogic
|
|
|
|
import (
|
|
"app-cloudep-notification-service/internal/domain/usecase"
|
|
"context"
|
|
|
|
ers "code.30cm.net/digimon/library-go/errors"
|
|
|
|
"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 寄簡訊
|
|
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())
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
|
|
return ¬ification.OKResp{}, nil
|
|
}
|