Skip to content

Commit

Permalink
chore: report default project not getting created in case of error
Browse files Browse the repository at this point in the history
It also improves setup service and cleans workspace creation service
which had too many responsibilities
  • Loading branch information
geclos committed Sep 20, 2024
1 parent c087a2b commit 2824c4a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ export const runHandler = factory.createHandlers(
async (stream) => {
const { projectId, commitUuid } = c.req.param()
const { documentPath, parameters, source } = c.req.valid('json')

const workspace = c.get('workspace')

const { document, commit } = await getData({
workspace,
projectId: Number(projectId!),
commitUuid: commitUuid!,
documentPath: documentPath!,
})

const result = await runDocumentAtCommit({
workspace,
document,
Expand Down
10 changes: 10 additions & 0 deletions apps/web/src/helpers/captureException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as Sentry from '@sentry/nextjs'
import env from '$/env'

export const captureException = (error: Error) => {
if (env.NODE_ENV === 'production') {
Sentry.captureException(error)
} else {
console.error(error)
}
}
43 changes: 30 additions & 13 deletions apps/web/src/services/user/setupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { Result } from '@latitude-data/core/lib/Result'
import Transaction, {
PromisedResult,
} from '@latitude-data/core/lib/Transaction'
import { createApiKey } from '@latitude-data/core/services/apiKeys/create'
import { createMembership } from '@latitude-data/core/services/memberships/create'
import { importDefaultProject } from '@latitude-data/core/services/projects/import'
import { createProviderApiKey } from '@latitude-data/core/services/providerApiKeys/create'
import { createUser } from '@latitude-data/core/services/users/createUser'
import { createWorkspace } from '@latitude-data/core/services/workspaces/create'
import { env } from '@latitude-data/env'
import { captureException } from '$/helpers/captureException'

export default function setupService({
email,
Expand All @@ -27,28 +31,41 @@ export default function setupService({
if (userResult.error) return userResult

const user = userResult.value
const result = await createWorkspace(
const resultWorkspace = await createWorkspace(
{
name: companyName,
user,
},
tx,
)
if (resultWorkspace.error) return resultWorkspace
const workspace = resultWorkspace.value

if (result.error) return result

const workspace = result.value
const resultProviderApiKey = await createProviderApiKey(
{
workspace,
provider: Providers.OpenAI,
name: env.DEFAULT_PROVIDER_ID,
token: env.DEFAULT_PROVIDER_API_KEY,
authorId: user.id,
},
const resultImportingDefaultProject = await importDefaultProject(
{ workspace, user },
tx,
)
if (resultProviderApiKey.error) return resultProviderApiKey
if (resultImportingDefaultProject.error) {
captureException(resultImportingDefaultProject.error)
}

const results = await Promise.all([
createMembership({ confirmedAt: new Date(), user, workspace }, tx),
createApiKey({ workspace }, tx),
createProviderApiKey(
{
workspace,
provider: Providers.OpenAI,
name: env.DEFAULT_PROVIDER_ID,
token: env.DEFAULT_PROVIDER_API_KEY,
authorId: user.id,
},
tx,
),
])

const result = Result.findError(results)
if (result) return result

return Result.ok({
user,
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/events/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Message,
ProviderLog,
User,
Workspace,
} from '../../browser'
import { PartialConfig } from '../../services/ai'
import { createEvaluationResultJob } from './createEvaluationResultJob'
Expand Down Expand Up @@ -91,6 +92,14 @@ export type AIProviderCallCompletedEvent = LatitudeEventGeneric<
}
>

export type WorkspaceCreatedEvent = LatitudeEventGeneric<
'workspaceCreated',
{
workspace: Workspace
user: User
}
>

export type LatitudeEvent =
| MembershipCreatedEvent
| UserCreatedEvent
Expand All @@ -99,6 +108,7 @@ export type LatitudeEvent =
| DocumentRunEvent
| ProviderLogCreatedEvent
| AIProviderCallCompletedEvent
| WorkspaceCreatedEvent

export interface IEventsHandlers {
magicLinkTokenCreated: EventHandler<MagicLinkTokenCreated>[]
Expand All @@ -108,6 +118,7 @@ export interface IEventsHandlers {
documentRun: EventHandler<DocumentRunEvent>[]
providerLogCreated: EventHandler<ProviderLogCreatedEvent>[]
aiProviderCallCompleted: EventHandler<AIProviderCallCompletedEvent>[]
workspaceCreated: EventHandler<WorkspaceCreatedEvent>[]
}

export const EventHandlers: IEventsHandlers = {
Expand All @@ -118,4 +129,5 @@ export const EventHandlers: IEventsHandlers = {
documentRun: [createDocumentLogJob],
providerLogCreated: [],
aiProviderCallCompleted: [],
workspaceCreated: [],
} as const
8 changes: 4 additions & 4 deletions packages/core/src/lib/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export class Result {
return result.ok
}

public static findError<V, E extends Error>(
results: TypedResult<V, E>[],
): TypedResult<V, E> | undefined {
return results.find((r) => !r.ok)
public static findError<E extends Error>(
results: TypedResult<any, E>[],
): ErrorResult<E> | undefined {
return results.find((r) => !r.ok) as ErrorResult<E> | undefined
}
}
1 change: 1 addition & 0 deletions packages/core/src/services/projects/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function importDefaultProject(
await defaultProjectDocumentsScope.getDocumentsFromMergedCommits({
projectId: defaultProject!.id,
})

if (defaultDocuments.error) return defaultDocuments

return Transaction.call<Project>(async (tx) => {
Expand Down
12 changes: 5 additions & 7 deletions packages/core/src/services/workspaces/create.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { User, Workspace } from '../../browser'
import { database } from '../../client'
import { publisher } from '../../events/publisher'
import { Result, Transaction } from '../../lib'
import { workspaces } from '../../schema'
import { createApiKey } from '../apiKeys/create'
import { createMembership } from '../memberships/create'
import { importDefaultProject } from '../projects/import'

export async function createWorkspace(
{
Expand All @@ -23,10 +21,10 @@ export async function createWorkspace(
.returning()
const workspace = insertedWorkspaces[0]!

await createMembership({ confirmedAt: new Date(), user, workspace }, tx)
await createApiKey({ workspace }, tx)

await importDefaultProject({ workspace, user }, tx)
publisher.publish({
type: 'workspaceCreated',
data: { workspace, user },
})

return Result.ok(workspace)
}, db)
Expand Down

0 comments on commit 2824c4a

Please sign in to comment.