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 21, 2024
1 parent b9693c5 commit a45b237
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 26 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
25 changes: 25 additions & 0 deletions packages/core/src/events/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {
ChainCallResponse,
Commit,
LogSources,
MagicLinkToken,
Membership,
Message,
Project,
ProviderLog,
User,
Workspace,
} from '../../browser'
import { PartialConfig } from '../../services/ai'
import { createEvaluationResultJob } from './createEvaluationResultJob'
Expand Down Expand Up @@ -91,6 +94,22 @@ export type AIProviderCallCompletedEvent = LatitudeEventGeneric<
}
>

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

export type ProjectCreated = LatitudeEventGeneric<
'projectCreated',
{
project: Project
commit: Commit
}
>

export type LatitudeEvent =
| MembershipCreatedEvent
| UserCreatedEvent
Expand All @@ -99,6 +118,8 @@ export type LatitudeEvent =
| DocumentRunEvent
| ProviderLogCreatedEvent
| AIProviderCallCompletedEvent
| WorkspaceCreatedEvent
| ProjectCreated

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

export const EventHandlers: IEventsHandlers = {
Expand All @@ -118,4 +141,6 @@ export const EventHandlers: IEventsHandlers = {
documentRun: [createDocumentLogJob],
providerLogCreated: [],
aiProviderCallCompleted: [],
workspaceCreated: [],
projectCreated: [],
} 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
}
}
6 changes: 6 additions & 0 deletions packages/core/src/services/projects/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Commit, Project, User, Workspace } from '../../browser'
import { database } from '../../client'
import { publisher } from '../../events/publisher'
import { Result, Transaction } from '../../lib'
import { projects } from '../../schema'
import { createCommit } from '../commits/create'
Expand Down Expand Up @@ -38,6 +39,11 @@ export async function createProject(
})
if (result.error) return result

publisher.publishLater({
type: 'projectCreated',
data: { project, commit: result.value },
})

return Result.ok({ project, commit: result.value })
}, db)
}
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 @@ -34,6 +34,7 @@ export async function importDefaultProject(
await defaultProjectDocumentsScope.getDocumentsFromMergedCommits({
projectId: defaultProject!.id,
})

if (defaultDocuments.error) return defaultDocuments

return Transaction.call<Project>(async (tx) => {
Expand Down
11 changes: 5 additions & 6 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,9 +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.publishLater({
type: 'workspaceCreated',
data: { workspace, user },
})

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

0 comments on commit a45b237

Please sign in to comment.