refactor(log): consolidate logging models and update related logic for improved clarity and functionality
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package emailLogic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
|
||||
@@ -31,8 +34,7 @@ func (l *SendEmailLogic) ProcessTask(ctx context.Context, task *asynq.Task) erro
|
||||
)
|
||||
return nil
|
||||
}
|
||||
messageLog := log.MessageLog{
|
||||
Type: log.Email.String(),
|
||||
messageLog := log.Message{
|
||||
Platform: l.svcCtx.Config.Email.Platform,
|
||||
To: payload.Email,
|
||||
Subject: payload.Subject,
|
||||
@@ -43,18 +45,108 @@ func (l *SendEmailLogic) ProcessTask(ctx context.Context, task *asynq.Task) erro
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] NewSender failed", logger.Field("error", err.Error()))
|
||||
return nil
|
||||
}
|
||||
err = sender.Send([]string{payload.Email}, payload.Subject, payload.Content)
|
||||
var content string
|
||||
switch payload.Type {
|
||||
case types.EmailTypeVerify:
|
||||
tpl, _ := template.New("verify").Parse(l.svcCtx.Config.Email.VerifyEmailTemplate)
|
||||
var result bytes.Buffer
|
||||
err = tpl.Execute(&result, payload.Content)
|
||||
if err != nil {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Execute template failed",
|
||||
logger.Field("error", err.Error()),
|
||||
logger.Field("template", l.svcCtx.Config.Email.VerifyEmailTemplate),
|
||||
logger.Field("data", payload.Content),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
content = result.String()
|
||||
case types.EmailTypeMaintenance:
|
||||
tpl, _ := template.New("maintenance").Parse(l.svcCtx.Config.Email.MaintenanceEmailTemplate)
|
||||
var result bytes.Buffer
|
||||
err = tpl.Execute(&result, payload.Content)
|
||||
if err != nil {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Execute template failed",
|
||||
logger.Field("error", err.Error()),
|
||||
logger.Field("template", l.svcCtx.Config.Email.MaintenanceEmailTemplate),
|
||||
logger.Field("data", payload.Content),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
content = result.String()
|
||||
case types.EmailTypeExpiration:
|
||||
tpl, _ := template.New("expiration").Parse(l.svcCtx.Config.Email.ExpirationEmailTemplate)
|
||||
var result bytes.Buffer
|
||||
err = tpl.Execute(&result, payload.Content)
|
||||
if err != nil {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Execute template failed",
|
||||
logger.Field("error", err.Error()),
|
||||
logger.Field("template", l.svcCtx.Config.Email.ExpirationEmailTemplate),
|
||||
logger.Field("data", payload.Content),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
content = result.String()
|
||||
case types.EmailTypeTrafficExceed:
|
||||
tpl, _ := template.New("traffic_exceed").Parse(l.svcCtx.Config.Email.TrafficExceedEmailTemplate)
|
||||
var result bytes.Buffer
|
||||
err = tpl.Execute(&result, payload.Content)
|
||||
if err != nil {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Execute template failed",
|
||||
logger.Field("error", err.Error()),
|
||||
logger.Field("template", l.svcCtx.Config.Email.TrafficExceedEmailTemplate),
|
||||
logger.Field("data", payload.Content),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
content = result.String()
|
||||
case types.EmailTypeCustom:
|
||||
if payload.Content == nil {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Custom email content is empty",
|
||||
logger.Field("payload", payload),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
if tpl, ok := payload.Content["content"].(string); !ok {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Custom email content is not a string",
|
||||
logger.Field("payload", payload),
|
||||
)
|
||||
return nil
|
||||
} else {
|
||||
content = tpl
|
||||
}
|
||||
default:
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Unsupported email type",
|
||||
logger.Field("type", payload.Type),
|
||||
logger.Field("payload", payload),
|
||||
)
|
||||
}
|
||||
|
||||
err = sender.Send([]string{payload.Email}, payload.Subject, content)
|
||||
if err != nil {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Send email failed", logger.Field("error", err.Error()))
|
||||
return nil
|
||||
}
|
||||
messageLog.Status = 1
|
||||
if err = l.svcCtx.LogModel.InsertMessageLog(ctx, &messageLog); err != nil {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] InsertMessageLog failed",
|
||||
emailLog, err := messageLog.Marshal()
|
||||
if err != nil {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Marshal message log failed",
|
||||
logger.Field("error", err.Error()),
|
||||
logger.Field("messageLog", messageLog),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = l.svcCtx.LogModel.Insert(ctx, &log.SystemLog{
|
||||
Type: log.TypeEmailMessage.Uint8(),
|
||||
Date: time.Now().Format("2006-01-02"),
|
||||
ObjectID: 0,
|
||||
Content: string(emailLog),
|
||||
}); err != nil {
|
||||
logger.WithContext(ctx).Error("[SendEmailLogic] Insert email log failed",
|
||||
logger.Field("error", err.Error()),
|
||||
logger.Field("emailLog", string(emailLog)),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
logger.WithContext(ctx).Info("[SendEmailLogic] Send email", logger.Field("email", payload.Email), logger.Field("content", payload.Content))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/internal/model/log"
|
||||
"github.com/perfect-panel/server/pkg/constant"
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
|
||||
@@ -375,12 +376,28 @@ func (l *ActivateOrderLogic) handleCommission(ctx context.Context, userInfo *use
|
||||
return err
|
||||
}
|
||||
|
||||
commissionLog := &user.CommissionLog{
|
||||
UserId: referer.Id,
|
||||
OrderNo: orderInfo.OrderNo,
|
||||
Amount: amount,
|
||||
var commissionType uint8
|
||||
switch orderInfo.Type {
|
||||
case OrderTypeSubscribe:
|
||||
commissionType = log.CommissionTypePurchase
|
||||
case OrderTypeRenewal:
|
||||
commissionType = log.CommissionTypeRenewal
|
||||
}
|
||||
return l.svc.UserModel.InsertCommissionLog(ctx, commissionLog, tx)
|
||||
|
||||
commissionLog := &log.Commission{
|
||||
Type: commissionType,
|
||||
Amount: amount,
|
||||
OrderNo: orderInfo.OrderNo,
|
||||
CreatedAt: orderInfo.CreatedAt.UnixMilli(),
|
||||
}
|
||||
|
||||
content, _ := commissionLog.Marshal()
|
||||
return tx.Model(&log.SystemLog{}).Create(&log.SystemLog{
|
||||
Type: log.TypeCommission.Uint8(),
|
||||
Date: time.Now().Format("2006-01-02"),
|
||||
ObjectID: referer.Id,
|
||||
Content: string(content),
|
||||
}).Error
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -594,14 +611,20 @@ func (l *ActivateOrderLogic) Recharge(ctx context.Context, orderInfo *order.Orde
|
||||
return err
|
||||
}
|
||||
|
||||
balanceLog := &user.BalanceLog{
|
||||
UserId: orderInfo.UserId,
|
||||
balanceLog := &log.Balance{
|
||||
Amount: orderInfo.Price,
|
||||
Type: CommissionTypeRecharge,
|
||||
OrderId: orderInfo.Id,
|
||||
Balance: userInfo.Balance,
|
||||
}
|
||||
return l.svc.UserModel.InsertBalanceLog(ctx, balanceLog, tx)
|
||||
content, _ := balanceLog.Marshal()
|
||||
|
||||
return tx.Model(&log.Balance{}).Create(&log.SystemLog{
|
||||
Type: log.TypeBalance.Uint8(),
|
||||
Date: time.Now().Format("2006-01-02"),
|
||||
ObjectID: userInfo.Id,
|
||||
Content: string(content),
|
||||
}).Error
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
|
||||
@@ -43,17 +44,16 @@ func (l *SendSmsLogic) ProcessTask(ctx context.Context, task *asynq.Task) error
|
||||
logger.WithContext(ctx).Error("[SendSmsLogic] New send sms client failed", logger.Field("error", err.Error()), logger.Field("payload", payload))
|
||||
return err
|
||||
}
|
||||
createSms := &log.MessageLog{
|
||||
Type: log.Mobile.String(),
|
||||
createSms := &log.Message{
|
||||
Platform: l.svcCtx.Config.Mobile.Platform,
|
||||
To: fmt.Sprintf("+%s%s", payload.TelephoneArea, payload.Telephone),
|
||||
Subject: constant.ParseVerifyType(payload.Type).String(),
|
||||
Content: "",
|
||||
Content: map[string]interface{}{
|
||||
"content": client.GetSendCodeContent(payload.Content),
|
||||
},
|
||||
}
|
||||
err = client.SendCode(payload.TelephoneArea, payload.Telephone, payload.Content)
|
||||
|
||||
createSms.Content = client.GetSendCodeContent(payload.Content)
|
||||
|
||||
if err != nil {
|
||||
logger.WithContext(ctx).Error("[SendSmsLogic] Send sms failed", logger.Field("error", err.Error()), logger.Field("payload", payload))
|
||||
if l.svcCtx.Config.Model != constant.DevMode {
|
||||
@@ -64,7 +64,14 @@ func (l *SendSmsLogic) ProcessTask(ctx context.Context, task *asynq.Task) error
|
||||
}
|
||||
createSms.Status = 1
|
||||
logger.WithContext(ctx).Info("[SendSmsLogic] Send sms", logger.Field("telephone", payload.Telephone), logger.Field("content", createSms.Content))
|
||||
err = l.svcCtx.LogModel.InsertMessageLog(ctx, createSms)
|
||||
|
||||
content, _ := createSms.Marshal()
|
||||
err = l.svcCtx.LogModel.Insert(ctx, &log.SystemLog{
|
||||
Type: log.TypeMobileMessage.Uint8(),
|
||||
Date: time.Now().Format("2006-01-02"),
|
||||
ObjectID: 0,
|
||||
Content: string(content),
|
||||
})
|
||||
if err != nil {
|
||||
logger.WithContext(ctx).Error("[SendSmsLogic] Send sms failed", logger.Field("error", err.Error()), logger.Field("payload", payload))
|
||||
return nil
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package subscription
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
queue "github.com/perfect-panel/server/queue/types"
|
||||
@@ -129,24 +127,14 @@ func (l *CheckSubscriptionLogic) sendExpiredNotify(ctx context.Context, subs []i
|
||||
continue
|
||||
}
|
||||
var taskPayload queue.SendEmailPayload
|
||||
taskPayload.Type = queue.EmailTypeExpiration
|
||||
taskPayload.Email = method.AuthIdentifier
|
||||
taskPayload.Subject = "Subscription Expired"
|
||||
tpl, err := template.New("Expired").Parse(l.svc.Config.Email.ExpirationEmailTemplate)
|
||||
if err != nil {
|
||||
logger.Errorw("[CheckSubscription] Parse template failed", logger.Field("error", err.Error()))
|
||||
continue
|
||||
}
|
||||
var result bytes.Buffer
|
||||
err = tpl.Execute(&result, map[string]interface{}{
|
||||
taskPayload.Content = map[string]interface{}{
|
||||
"SiteLogo": l.svc.Config.Site.SiteLogo,
|
||||
"SiteName": l.svc.Config.Site.SiteName,
|
||||
"ExpireDate": sub.ExpireTime.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
if err != nil {
|
||||
logger.Errorw("[CheckSubscription] Execute template failed", logger.Field("error", err.Error()))
|
||||
continue
|
||||
}
|
||||
taskPayload.Content = result.String()
|
||||
payloadBuy, err := json.Marshal(taskPayload)
|
||||
if err != nil {
|
||||
logger.Errorw("[CheckSubscription] Marshal payload failed", logger.Field("error", err.Error()))
|
||||
@@ -179,23 +167,13 @@ func (l *CheckSubscriptionLogic) sendTrafficNotify(ctx context.Context, subs []i
|
||||
continue
|
||||
}
|
||||
var taskPayload queue.SendEmailPayload
|
||||
taskPayload.Type = queue.EmailTypeTrafficExceed
|
||||
taskPayload.Email = method.AuthIdentifier
|
||||
taskPayload.Subject = "Subscription Traffic Exceed"
|
||||
tpl, err := template.New("Traffic").Parse(l.svc.Config.Email.TrafficExceedEmailTemplate)
|
||||
if err != nil {
|
||||
logger.Errorw("[CheckSubscription] Parse template failed", logger.Field("error", err.Error()))
|
||||
continue
|
||||
}
|
||||
var result bytes.Buffer
|
||||
err = tpl.Execute(&result, map[string]interface{}{
|
||||
taskPayload.Content = map[string]interface{}{
|
||||
"SiteLogo": l.svc.Config.Site.SiteLogo,
|
||||
"SiteName": l.svc.Config.Site.SiteName,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Errorw("[CheckSubscription] Execute template failed", logger.Field("error", err.Error()))
|
||||
continue
|
||||
}
|
||||
taskPayload.Content = result.String()
|
||||
payloadBuy, err := json.Marshal(taskPayload)
|
||||
if err != nil {
|
||||
logger.Errorw("[CheckSubscription] Marshal payload failed", logger.Field("error", err.Error()))
|
||||
|
||||
+12
-4
@@ -7,11 +7,19 @@ const (
|
||||
ScheduledBatchSendEmail = "scheduled:email:batch"
|
||||
)
|
||||
|
||||
const (
|
||||
EmailTypeVerify = "verify"
|
||||
EmailTypeMaintenance = "maintenance"
|
||||
EmailTypeExpiration = "expiration"
|
||||
EmailTypeTrafficExceed = "traffic_exceed"
|
||||
EmailTypeCustom = "custom"
|
||||
)
|
||||
|
||||
type (
|
||||
SendEmailPayload struct {
|
||||
Type string `json:"type"`
|
||||
Email string `json:"to"`
|
||||
Subject string `json:"subject"`
|
||||
Content string `json:"content"`
|
||||
Type string `json:"type"`
|
||||
Email string `json:"to"`
|
||||
Subject string `json:"subject"`
|
||||
Content map[string]interface{} `json:"content"`
|
||||
}
|
||||
)
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
package types
|
||||
|
||||
const (
|
||||
// ForthwithSendEmail forthwith send email
|
||||
// ForthwithSendSms forthwith send email
|
||||
ForthwithSendSms = "forthwith:sms:send"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user