All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m35s
52 lines
2.3 KiB
Go
52 lines
2.3 KiB
Go
package wecom
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"github.com/bytedance/sonic"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/infrastructure/messaging/outbox"
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
"github.com/break/junhong_cmp_fiber/pkg/queue"
|
|
)
|
|
|
|
type UnknownRecoveryConsumer struct {
|
|
db *gorm.DB
|
|
queue *queue.Client
|
|
}
|
|
|
|
func NewUnknownRecoveryConsumer(db *gorm.DB, client *queue.Client) *UnknownRecoveryConsumer {
|
|
return &UnknownRecoveryConsumer{db: db, queue: client}
|
|
}
|
|
|
|
func (c *UnknownRecoveryConsumer) Consume(ctx context.Context, envelope outbox.DeliveryEnvelope) error {
|
|
if c == nil || c.db == nil || c.queue == nil {
|
|
return errors.New(errors.CodeServiceUnavailable, "审批未知恢复消费者未配置")
|
|
}
|
|
var payload struct {
|
|
InstanceID uint `json:"instance_id"`
|
|
}
|
|
if err := sonic.Unmarshal(envelope.Payload, &payload); err != nil || payload.InstanceID == 0 {
|
|
return errors.New(errors.CodeInvalidParam, "审批未知恢复事件载荷无效")
|
|
}
|
|
id := strconv.FormatUint(uint64(payload.InstanceID), 10)
|
|
var instance model.ApprovalInstance
|
|
if err := c.db.WithContext(ctx).Where("id = ?", payload.InstanceID).First(&instance).Error; err != nil {
|
|
return errors.New(errors.CodeConflict, "审批未知恢复实例不存在")
|
|
}
|
|
if envelope.EventType != constants.OutboxEventTypeApprovalUnknownRecoveryRequested || envelope.AggregateType != "approval" || envelope.AggregateID != id || envelope.ResourceType != instance.BusinessType || envelope.ResourceID != strconv.FormatUint(uint64(instance.BusinessID), 10) {
|
|
return errors.New(errors.CodeInvalidParam, "审批未知恢复事件身份无效")
|
|
}
|
|
var approvalContext model.WeComApprovalContext
|
|
if err := c.db.WithContext(ctx).Where("approval_instance_id = ? AND submission_status = ? AND sp_no = ''", payload.InstanceID, constants.WeComSubmissionStatusUnknown).First(&approvalContext).Error; err != nil || instance.Status != constants.ApprovalStatusSubmissionUnknown {
|
|
return errors.New(errors.CodeConflict, "审批未知恢复上下文不一致")
|
|
}
|
|
return c.queue.EnqueueTask(ctx, constants.TaskTypeWeComApprovalRecovery, map[string]any{"instance_id": payload.InstanceID})
|
|
}
|
|
|
|
var _ outbox.EventConsumer = (*UnknownRecoveryConsumer)(nil)
|