(params: {
draft: { workspaceId: string; displayName: string; description: string | null }
accountId: string
providerId: string
userId: string
now: Date
})
| 13 | * Creates a new credential from a pending draft (normal OAuth connect flow). |
| 14 | */ |
| 15 | export async function handleCreateCredentialFromDraft(params: { |
| 16 | draft: { workspaceId: string; displayName: string; description: string | null } |
| 17 | accountId: string |
| 18 | providerId: string |
| 19 | userId: string |
| 20 | now: Date |
| 21 | }) { |
| 22 | const { draft, accountId, providerId, userId, now } = params |
| 23 | const credentialId = generateId() |
| 24 | |
| 25 | try { |
| 26 | await db.insert(schema.credential).values({ |
| 27 | id: credentialId, |
| 28 | workspaceId: draft.workspaceId, |
| 29 | type: 'oauth', |
| 30 | displayName: draft.displayName, |
| 31 | description: draft.description ?? null, |
| 32 | providerId, |
| 33 | accountId, |
| 34 | createdBy: userId, |
| 35 | createdAt: now, |
| 36 | updatedAt: now, |
| 37 | }) |
| 38 | |
| 39 | await db.insert(schema.credentialMember).values({ |
| 40 | id: generateId(), |
| 41 | credentialId, |
| 42 | userId, |
| 43 | role: 'admin', |
| 44 | status: 'active', |
| 45 | joinedAt: now, |
| 46 | invitedBy: userId, |
| 47 | createdAt: now, |
| 48 | updatedAt: now, |
| 49 | }) |
| 50 | |
| 51 | logger.info('Created credential from draft', { |
| 52 | credentialId, |
| 53 | displayName: draft.displayName, |
| 54 | providerId, |
| 55 | accountId, |
| 56 | }) |
| 57 | |
| 58 | await clearDeadFlag(accountId) |
| 59 | |
| 60 | recordAudit({ |
| 61 | workspaceId: draft.workspaceId, |
| 62 | actorId: userId, |
| 63 | action: AuditAction.CREDENTIAL_CREATED, |
| 64 | resourceType: AuditResourceType.CREDENTIAL, |
| 65 | resourceId: credentialId, |
| 66 | resourceName: draft.displayName, |
| 67 | description: `Created OAuth credential "${draft.displayName}"`, |
| 68 | metadata: { providerId, accountId }, |
| 69 | }) |
| 70 | |
| 71 | captureServerEvent( |
| 72 | userId, |
no test coverage detected