feat(t230): use OCR for ERP session login

This commit is contained in:
QiuSW
2026-07-29 10:48:06 +08:00
parent 8c84b81d48
commit f887bc4882
30 changed files with 512 additions and 205 deletions
@@ -30,6 +30,10 @@ type FreightService struct {
timeout time.Duration
}
type FreightSourcePreflight interface {
EnsureAuthenticated(context.Context) error
}
type CreateFreightSyncCommand struct {
CreatorSubject string
ActorUserID string
@@ -101,6 +105,9 @@ func (service *FreightService) CreateOrderSync(
fields,
)
}
if err := service.ensureSource(ctx); err != nil {
return CreateFreightSyncResult{}, err
}
runID, err := service.ids.NewID()
if err != nil {
return CreateFreightSyncResult{}, wrapRepositoryError(err)
@@ -166,6 +173,9 @@ func (service *FreightService) CreateDateSync(
fields,
)
}
if err := service.ensureSource(ctx); err != nil {
return CreateFreightSyncResult{}, err
}
now := service.clock.Now().UTC()
location, err := time.LoadLocation("Asia/Shanghai")
@@ -278,6 +288,24 @@ func (service *FreightService) CreateDateSync(
return CreateFreightSyncResult{Run: run, Replayed: !created}, nil
}
func (service *FreightService) ensureSource(ctx context.Context) error {
preflight, ok := service.source.(FreightSourcePreflight)
if !ok {
return nil
}
if err := preflight.EnsureAuthenticated(ctx); err != nil {
result := newError(
ErrorKindUnavailable,
freightSourceErrorCode(err),
"freight source session is unavailable",
err,
)
result.Retryable = true
return result
}
return nil
}
func (service *FreightService) execute(run domain.FreightSyncRun) {
ctx, cancel := context.WithTimeout(context.Background(), service.timeout)
defer cancel()
@@ -679,6 +707,8 @@ func freightSourceErrorCode(err error) string {
return "ERP_FREIGHT_NOT_FOUND"
case errors.Is(err, domain.ErrFreightSourceProtocol):
return "ERP_RESPONSE_INVALID"
case errors.Is(err, domain.ErrFreightSourceOCRInvalid):
return "OCR_SERVICE_INVALID"
default:
return "ERP_UNAVAILABLE"
}