()
| 5505 | // ------------------------------------------------------------------ |
| 5506 | |
| 5507 | const makeAdmin = (): ExecutorAdmin => { |
| 5508 | const platformCore = makeCoreDb( |
| 5509 | makeFumaClient( |
| 5510 | withQueryContext(rootDbUntyped, { |
| 5511 | ...ownerContext, |
| 5512 | reach: "tenant", |
| 5513 | } satisfies ExecutorOwnerPolicyContext), |
| 5514 | ), |
| 5515 | ); |
| 5516 | |
| 5517 | const rowToAdminSubject = (row: CoreRow<"subject">): AdminSubject => ({ |
| 5518 | externalId: row.external_id, |
| 5519 | createdAt: row.created_at instanceof Date ? row.created_at : new Date(row.created_at), |
| 5520 | // bigint on drivers that return one, and a blob on SQLite — hence the |
| 5521 | // ORM read rather than raw SQL (see `subject-registry.ts`). |
| 5522 | lastSeenAt: row.last_seen_at == null ? null : Number(row.last_seen_at), |
| 5523 | status: row.status ?? null, |
| 5524 | }); |
| 5525 | |
| 5526 | const rowToAdminConnection = (row: ConnectionRow): AdminConnection => { |
| 5527 | const owner = row.owner as Owner; |
| 5528 | return { |
| 5529 | owner, |
| 5530 | // Org rows carry the empty-string sentinel, not a principal. |
| 5531 | subject: owner === "org" ? null : row.subject, |
| 5532 | integration: IntegrationSlug.make(row.integration), |
| 5533 | name: ConnectionName.make(row.name), |
| 5534 | oauthScope: row.oauth_scope == null ? null : String(row.oauth_scope), |
| 5535 | lastHealth: Option.getOrNull(decodeLastHealth(row.last_health)), |
| 5536 | }; |
| 5537 | }; |
| 5538 | |
| 5539 | const listSubjects = ( |
| 5540 | options?: AdminListSubjectsOptions, |
| 5541 | ): Effect.Effect<readonly AdminSubject[], StorageFailure> => { |
| 5542 | // Always both, always integers — see `normalizeAdminPaging`. Passing |
| 5543 | // them through independently produced a bare OFFSET, which SQLite |
| 5544 | // rejects outright. |
| 5545 | const { limit, offset } = normalizeAdminPaging(options); |
| 5546 | return platformCore |
| 5547 | .findMany("subject", { |
| 5548 | // Oldest first, ties broken on the unique key so the order is |
| 5549 | // total and paging can't repeat or skip a row. |
| 5550 | orderBy: [ |
| 5551 | ["created_at", "asc"], |
| 5552 | ["external_id", "asc"], |
| 5553 | ], |
| 5554 | limit, |
| 5555 | offset, |
| 5556 | }) |
| 5557 | .pipe(Effect.map((rows) => rows.map(rowToAdminSubject))); |
| 5558 | }; |
| 5559 | |
| 5560 | // Keyed on `(tenant, external_id)` — the table's unique index. No |
| 5561 | // `tenant` clause here: the tenant policy adds it to every read, the |
| 5562 | // same way `touchSubject` relies on it. |
| 5563 | const getSubject = (externalId: string): Effect.Effect<AdminSubject | null, StorageFailure> => |
| 5564 | platformCore |
no test coverage detected