(ctx context.Context, providerId string, id string, enable bool)
| 364 | } |
| 365 | |
| 366 | func (i *imlKeyModule) UpdateKeyStatus(ctx context.Context, providerId string, id string, enable bool) error { |
| 367 | _, err := i.providerService.Get(ctx, providerId) |
| 368 | if err != nil { |
| 369 | return fmt.Errorf("provider not found: %w", err) |
| 370 | } |
| 371 | info, err := i.aiKeyService.Get(ctx, id) |
| 372 | if err != nil { |
| 373 | return fmt.Errorf("key not found: %w", err) |
| 374 | } |
| 375 | return i.transaction.Transaction(ctx, func(ctx context.Context) error { |
| 376 | if !enable { |
| 377 | status := ai_key_dto.KeyDisable.Int() |
| 378 | err = i.aiKeyService.Save(ctx, id, &ai_key.Edit{ |
| 379 | Status: &status, |
| 380 | }) |
| 381 | if err != nil { |
| 382 | return err |
| 383 | } |
| 384 | releases := []*gateway.DynamicRelease{{ |
| 385 | BasicItem: &gateway.BasicItem{ |
| 386 | ID: fmt.Sprintf("%s-%s", providerId, id), |
| 387 | Resource: "ai-key", |
| 388 | }, |
| 389 | Attr: nil, |
| 390 | }} |
| 391 | return i.syncGateway(ctx, cluster.DefaultClusterID, releases, false) |
| 392 | } |
| 393 | if info.Status == ai_key_dto.KeyDisable.Int() || info.Status == ai_key_dto.KeyExceed.Int() { |
| 394 | // 超额 或 停用状态,可启用 |
| 395 | if info.ExpireTime > 0 && time.Unix(int64(info.ExpireTime), 0).Before(time.Now()) { |
| 396 | // 如果过期时间未更改,且已过期,则设置为过期状态 |
| 397 | status := ai_key_dto.KeyExpired.Int() |
| 398 | return i.aiKeyService.Save(ctx, id, &ai_key.Edit{ |
| 399 | Status: &status, |
| 400 | }) |
| 401 | } |
| 402 | status := ai_key_dto.KeyNormal.Int() |
| 403 | err = i.aiKeyService.Save(ctx, id, &ai_key.Edit{ |
| 404 | Status: &status, |
| 405 | }) |
| 406 | if err != nil { |
| 407 | return err |
| 408 | } |
| 409 | info, err = i.aiKeyService.Get(ctx, id) |
| 410 | if err != nil { |
| 411 | return err |
| 412 | } |
| 413 | releases := []*gateway.DynamicRelease{newKey(info)} |
| 414 | return i.syncGateway(ctx, cluster.DefaultClusterID, releases, true) |
| 415 | } |
| 416 | return nil |
| 417 | }) |
| 418 | } |
| 419 | |
| 420 | func (i *imlKeyModule) Sort(ctx context.Context, providerId string, input *ai_key_dto.Sort) error { |
| 421 | _, err := i.providerService.Get(ctx, providerId) |
nothing calls this directly
no test coverage detected