ea586e3ba2
Co-authored-by: multica-agent <github@multica.ai>
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package promo
|
|
|
|
import (
|
|
"context"
|
|
|
|
promoModel "github.com/perfect-panel/server/internal/model/promo"
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"github.com/perfect-panel/server/internal/types"
|
|
"github.com/perfect-panel/server/pkg/logger"
|
|
"github.com/perfect-panel/server/pkg/xerr"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type CreatePromoRuleLogic struct {
|
|
logger.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// Create promo rule
|
|
func NewCreatePromoRuleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePromoRuleLogic {
|
|
return &CreatePromoRuleLogic{
|
|
Logger: logger.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *CreatePromoRuleLogic) CreatePromoRule(req *types.CreatePromoRuleRequest) (*types.PromoRule, error) {
|
|
if err := validateRulePayload(req.Type, req.Params, req.Priority, req.StartTime, req.EndTime); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
start, end := ruleTimes(req.StartTime, req.EndTime)
|
|
data := &promoModel.Rule{
|
|
Name: req.Name,
|
|
Type: req.Type,
|
|
Params: string(req.Params),
|
|
Priority: req.Priority,
|
|
Enabled: *req.Enabled,
|
|
StartTime: start,
|
|
EndTime: end,
|
|
}
|
|
if err := l.svcCtx.PromoModel.InsertRule(l.ctx, data); err != nil {
|
|
l.Errorw("[CreatePromoRule] Database Error", logger.Field("error", err.Error()))
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "create promo rule failed: %v", err.Error())
|
|
}
|
|
clearRuleCache(l.ctx, l.svcCtx)
|
|
resp := toRuleResponse(data)
|
|
return &resp, nil
|
|
}
|