(ctx context.Context, providerId string, keyword string, page, pageSize int, statuses []string)
| 275 | } |
| 276 | |
| 277 | func (i *imlKeyModule) List(ctx context.Context, providerId string, keyword string, page, pageSize int, statuses []string) ([]*ai_key_dto.Item, int64, error) { |
| 278 | _, err := i.aiKeyService.DefaultKey(ctx, providerId) |
| 279 | if err != nil { |
| 280 | if !errors.Is(err, gorm.ErrRecordNotFound) { |
| 281 | return nil, 0, fmt.Errorf("get default key failed: %w", err) |
| 282 | } |
| 283 | info, err := i.providerService.Get(ctx, providerId) |
| 284 | if err != nil { |
| 285 | return nil, 0, fmt.Errorf("provider is unconfigued,id is %s", providerId) |
| 286 | } |
| 287 | err = i.aiKeyService.Create(ctx, &ai_key.Create{ |
| 288 | ID: info.Id, |
| 289 | Name: info.Name, |
| 290 | Config: info.Config, |
| 291 | Provider: info.Id, |
| 292 | Status: ai_key_dto.KeyNormal.Int(), |
| 293 | Priority: 1, |
| 294 | ExpireTime: 0, |
| 295 | UseToken: 0, |
| 296 | Default: true, |
| 297 | }) |
| 298 | if err != nil { |
| 299 | return nil, 0, fmt.Errorf("create default key failed: %w", err) |
| 300 | } |
| 301 | } |
| 302 | w := map[string]interface{}{ |
| 303 | "provider": providerId, |
| 304 | } |
| 305 | hasExpired := true |
| 306 | if len(statuses) > 0 { |
| 307 | hasExpired = false |
| 308 | statusConditions := make([]int, 0, len(statuses)) |
| 309 | for _, s := range statuses { |
| 310 | status := ai_key_dto.KeyStatus(s) |
| 311 | if status == ai_key_dto.KeyExpired { |
| 312 | hasExpired = true |
| 313 | } |
| 314 | statusConditions = append(statusConditions, status.Int()) |
| 315 | } |
| 316 | w["status"] = statusConditions |
| 317 | } |
| 318 | var list []*ai_key.Key |
| 319 | var total int64 |
| 320 | if !hasExpired { |
| 321 | if keyword != "" { |
| 322 | list, err = i.aiKeyService.Search(ctx, keyword, w, "sort asc") |
| 323 | if err != nil { |
| 324 | return nil, 0, err |
| 325 | } |
| 326 | if len(list) == 0 { |
| 327 | return nil, 0, nil |
| 328 | } |
| 329 | uuids := utils.SliceToSlice(list, func(key *ai_key.Key) string { |
| 330 | return key.ID |
| 331 | }) |
| 332 | w["uuid"] = uuids |
| 333 | } |
| 334 | list, total, err = i.aiKeyService.SearchUnExpiredByPage(ctx, w, page, pageSize, "sort asc") |
nothing calls this directly
no test coverage detected