-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from DashHub-ai/feature/add-files-to-organiza…
…tion-context Add files to organization scope project
- Loading branch information
Showing
31 changed files
with
425 additions
and
136 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
apps/backend/src/migrations/0046-add-projects-to-organizations-ai-settings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type { Kysely } from 'kysely'; | ||
|
||
export async function up(db: Kysely<any>): Promise<void> { | ||
await db.schema | ||
.alterTable('organizations_ai_settings') | ||
.addColumn('project_id', 'integer', col => col.references('projects.id').onDelete('restrict')) | ||
.execute(); | ||
|
||
const organizations = await db | ||
.selectFrom('organizations') | ||
.select(['id']) | ||
.execute(); | ||
|
||
const rootUser = await db | ||
.selectFrom('users') | ||
.select(['id']) | ||
.where('role', '=', 'root') | ||
.executeTakeFirst(); | ||
|
||
if (rootUser) { | ||
for (const [index, organization] of organizations.entries()) { | ||
const project = await db | ||
.insertInto('projects') | ||
.values({ | ||
name: `Migrated Organization Project - ${Date.now()}-${index}`, | ||
organization_id: organization.id, | ||
internal: true, | ||
creator_user_id: rootUser.id, | ||
}) | ||
.returning(['id']) | ||
.executeTakeFirstOrThrow(); | ||
|
||
await db | ||
.insertInto('projects_summaries') | ||
.values({ | ||
project_id: project.id, | ||
}) | ||
.execute(); | ||
|
||
await db | ||
.insertInto('organizations_ai_settings') | ||
.values({ | ||
organization_id: organization.id, | ||
project_id: project.id, | ||
chat_context: null, | ||
}) | ||
.onConflict(oc => oc | ||
.column('organization_id') | ||
.doUpdateSet(eb => ({ | ||
project_id: eb.ref('excluded.project_id'), | ||
})), | ||
) | ||
.execute(); | ||
} | ||
} | ||
|
||
await db.schema | ||
.alterTable('organizations_ai_settings') | ||
.alterColumn('project_id', col => col.setNotNull()) | ||
.execute(); | ||
} | ||
|
||
export async function down(db: Kysely<any>): Promise<void> { | ||
const projects = await db | ||
.selectFrom('organizations_ai_settings') | ||
.select(['project_id']) | ||
.where('project_id', 'is not', null) | ||
.execute(); | ||
|
||
await db.schema | ||
.alterTable('organizations_ai_settings') | ||
.dropColumn('project_id') | ||
.execute(); | ||
|
||
if (projects.length > 0) { | ||
await db | ||
.deleteFrom('projects_summaries') | ||
.where('project_id', 'in', projects.map(app => app.project_id)) | ||
.execute(); | ||
|
||
await db | ||
.deleteFrom('projects') | ||
.where('id', 'in', projects.map(app => app.project_id)) | ||
.execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './organizations-ai-settings.repo'; | ||
export * from './organizations-ai-settings.service'; | ||
export * from './organizations-ai-settings.tables'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 36 additions & 2 deletions
38
apps/backend/src/modules/organizations-ai-settings/organizations-ai-settings.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,46 @@ | ||
import { taskEither as TE } from 'fp-ts'; | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import { inject, injectable } from 'tsyringe'; | ||
|
||
import { SdkUpsertOrganizationAISettingsInputT } from '@llm/sdk'; | ||
|
||
import { TableRowWithId } from '../database'; | ||
import { ProjectsService } from '../projects'; | ||
import { OrganizationsAISettingsRepo } from './organizations-ai-settings.repo'; | ||
|
||
@injectable() | ||
export class OrganizationsAISettingsService { | ||
constructor( | ||
@inject(OrganizationsAISettingsRepo) private readonly organizationsAISettingsRepo: OrganizationsAISettingsRepo, | ||
@inject(OrganizationsAISettingsRepo) private readonly repo: OrganizationsAISettingsRepo, | ||
@inject(ProjectsService) private readonly projectsService: ProjectsService, | ||
) {} | ||
|
||
getChatContextByOrganizationIdOrNil = this.organizationsAISettingsRepo.getChatContextByOrganizationIdOrNil; | ||
getProjectIdByOrganizationIdOrNil = this.repo.getProjectIdByOrganizationIdOrNil; | ||
|
||
getChatContextByOrganizationIdOrNil = this.repo.getChatContextByOrganizationIdOrNil; | ||
|
||
upsert = ({ chatContext, organization }: SdkUpsertOrganizationAISettingsInputT & { organization: TableRowWithId; }) => pipe( | ||
TE.Do, | ||
TE.bind('project', () => pipe( | ||
this.repo.getProjectIdByOrganizationIdOrNil({ | ||
organizationId: organization.id, | ||
}), | ||
TE.chainW((projectId) => { | ||
if (projectId) { | ||
return TE.right({ id: projectId }); | ||
} | ||
|
||
return this.projectsService.createInternal({ | ||
organization, | ||
}); | ||
}), | ||
)), | ||
TE.chainW(({ project }) => this.repo.upsert({ | ||
value: { | ||
projectId: project.id, | ||
organizationId: organization.id, | ||
chatContext, | ||
}, | ||
})), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.