feat(exchange): add exchange rate handling and scheduled updates

This commit is contained in:
Chang lue Tsen
2025-10-15 10:05:19 -04:00
parent 8562734fde
commit adbe9a06d8
10 changed files with 84 additions and 47 deletions
-3
View File
@@ -3,7 +3,6 @@ package handler
import (
"github.com/hibiken/asynq"
"github.com/perfect-panel/server/internal/svc"
countrylogic "github.com/perfect-panel/server/queue/logic/country"
orderLogic "github.com/perfect-panel/server/queue/logic/order"
smslogic "github.com/perfect-panel/server/queue/logic/sms"
"github.com/perfect-panel/server/queue/logic/subscription"
@@ -15,8 +14,6 @@ import (
)
func RegisterHandlers(mux *asynq.ServeMux, serverCtx *svc.ServiceContext) {
// get country task
mux.Handle(types.ForthwithGetCountry, countrylogic.NewGetNodeCountryLogic(serverCtx))
// Send email task
mux.Handle(types.ForthwithSendEmail, emailLogic.NewSendEmailLogic(serverCtx))
// Send sms task
-22
View File
@@ -1,22 +0,0 @@
package countrylogic
import (
"context"
"github.com/hibiken/asynq"
"github.com/perfect-panel/server/internal/svc"
)
type GetNodeCountryLogic struct {
svcCtx *svc.ServiceContext
}
func NewGetNodeCountryLogic(svcCtx *svc.ServiceContext) *GetNodeCountryLogic {
return &GetNodeCountryLogic{
svcCtx: svcCtx,
}
}
func (l *GetNodeCountryLogic) ProcessTask(ctx context.Context, task *asynq.Task) error {
return nil
}
+52
View File
@@ -0,0 +1,52 @@
package task
import (
"context"
"github.com/hibiken/asynq"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/pkg/exchangeRate"
"github.com/perfect-panel/server/pkg/logger"
"github.com/perfect-panel/server/pkg/tool"
)
type RateLogic struct {
svcCtx *svc.ServiceContext
}
func NewRateLogic(svcCtx *svc.ServiceContext) *RateLogic {
return &RateLogic{
svcCtx: svcCtx,
}
}
func (l *RateLogic) ProcessTask(ctx context.Context, _ *asynq.Task) error {
// Retrieve system currency configuration
currency, err := l.svcCtx.SystemModel.GetCurrencyConfig(ctx)
if err != nil {
logger.Errorw("[PurchaseCheckout] GetCurrencyConfig error", logger.Field("error", err.Error()))
return err
}
// Parse currency configuration
configs := struct {
CurrencyUnit string
CurrencySymbol string
AccessKey string
}{}
tool.SystemConfigSliceReflectToStruct(currency, &configs)
// Skip conversion if no exchange rate API key configured
if configs.AccessKey == "" {
logger.Debugf("[RateLogic] skip exchange rate, no access key configured")
return nil
}
// Update exchange rates
result, err := exchangeRate.GetExchangeRete(configs.CurrencyUnit, "CNY", configs.AccessKey, 1)
if err != nil {
logger.Errorw("[RateLogic] GetExchangeRete error", logger.Field("error", err.Error()))
return err
}
l.svcCtx.ExchangeRate = result
logger.WithContext(ctx).Infof("[RateLogic] GetExchangeRete success, result: %+v", result)
return nil
}
-11
View File
@@ -1,11 +0,0 @@
package types
const (
// ForthwithGetCountry forthwith country get
ForthwithGetCountry = "forthwith:country:get"
)
type GetNodeCountry struct {
Protocol string `json:"protocol"`
ServerAddr string `json:"server_addr"`
}
+3
View File
@@ -6,4 +6,7 @@ const (
// ForthwithQuotaTask create quota task immediately
ForthwithQuotaTask = "forthwith:quota:task"
// SchedulerExchangeRate fetch exchange rate task
SchedulerExchangeRate = "scheduler:exchange:rate"
)