Skip to content

Commit

Permalink
feat(backend): add internal projects support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Dec 25, 2024
1 parent 2feb7c3 commit 9b53fb6
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 5 deletions.
31 changes: 31 additions & 0 deletions apps/backend/src/migrations/0025-add-internal-projects-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Kysely } from 'kysely';

export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('projects_files')
.addColumn('message_id', 'uuid', col => col.references('messages.id').onDelete('restrict'))
.execute();

await db.schema
.createIndex('projects_files_message_id_index')
.on('projects_files')
.column('message_id')
.execute();

await db.schema
.alterTable('projects')
.addColumn('internal', 'boolean', col => col.notNull().defaultTo(false))
.execute();
}

export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('projects')
.dropColumn('internal')
.execute();

await db.schema
.alterTable('projects_files')
.dropColumn('message_id')
.execute();
}
2 changes: 2 additions & 0 deletions apps/backend/src/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as dropUniqueNameFromS3Assets from './0021-drop-unique-name-from-s3-ass
import * as addIdToProjectFilesTable from './0022-add-id-to-project-files-table';
import * as addProjectsEmbeddingsTable from './0023-add-projects-embeddings-table';
import * as dropUnusedImagesTable from './0024-drop-unused-images-table';
import * as addInternalProjectsFields from './0025-add-internal-projects-fields';

export const DB_MIGRATIONS = {
'0000-add-users-tables': addUsersTables,
Expand Down Expand Up @@ -50,4 +51,5 @@ export const DB_MIGRATIONS = {
'0022-add-id-to-project-files-table': addIdToProjectFilesTable,
'0023-add-projects-embeddings-table': addProjectsEmbeddingsTable,
'0024-drop-unused-images-table': dropUnusedImagesTable,
'0025-add-internal-projects-fields': addInternalProjectsFields,
};
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export class ProjectsFilesRepo extends createDatabaseRepo('projects_files') {
s3_resource_created_at: s3ResourceCreatedAt,
s3_resource_updated_at: s3ResourceUpdatedAt,

message_id: messageId,

...item
}): ProjectFileTableRowWithRelations => ({
...camelcaseKeys(item),
Expand All @@ -114,11 +116,15 @@ export class ProjectsFilesRepo extends createDatabaseRepo('projects_files') {
name: bucketName,
},
},

project: {
id: projectId,
name: projectName,
},
message: messageId
? {
id: messageId,
}
: null,
})),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
NormalizeSelectTableRow,
TableId,
TableRowWithIdName,
TableRowWithUuid,
TableUuid,
TableWithDefaultColumns,
} from '../database';
import type { S3ResourcesTableRowWithRelations } from '../s3';
Expand All @@ -14,16 +16,18 @@ export type ProjectsFilesTable =
& {
project_id: ColumnType<TableId, TableId, never>;
s3_resource_id: ColumnType<TableId, TableId, never>;
message_id: ColumnType<TableUuid | null, TableUuid | null, null>;
};

export type ProjectFileTableRow = NormalizeSelectTableRow<ProjectsFilesTable>;

export type ProjectFileTableInsertRow = NormalizeInsertTableRow<ProjectsFilesTable>;

export type ProjectFileTableRowWithRelations =
& Omit<ProjectFileTableRow, 'projectId' | 's3ResourceId'>
& Omit<ProjectFileTableRow, 'projectId' | 's3ResourceId' | 'messageId'>
& {
resource: S3ResourcesTableRowWithRelations;
project: TableRowWithIdName;
message: TableRowWithUuid | null;
description: string | null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const ProjectsAbstractEsIndexRepo = createElasticsearchIndexRepo({
...createBaseAutocompleteFieldMappings(),
...createArchivedRecordMappings(),
organization: createIdNameObjectMapping(),
internal: {
type: 'keyword',
},
description: {
type: 'text',
analyzer: 'folded_lowercase_analyzer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import {
ProjectsEsIndexRepo,
} from './projects-es-index.repo';

type InternalSearchProjectsInputT = SdKSearchProjectsInputT & {
excludeInternal?: boolean;
};

@injectable()
export class ProjectsEsSearchRepo {
constructor(
Expand All @@ -31,7 +35,7 @@ export class ProjectsEsSearchRepo {
TE.map(ProjectsEsSearchRepo.mapOutputHit),
);

search = (dto: SdKSearchProjectsInputT) =>
search = (dto: InternalSearchProjectsInputT) =>
pipe(
this.indexRepo.search(
ProjectsEsSearchRepo.createEsRequestSearchBody(dto).toJSON(),
Expand All @@ -53,11 +57,12 @@ export class ProjectsEsSearchRepo {

private static createEsRequestSearchFilters = (
{
excludeInternal = true,
phrase,
ids,
organizationIds,
archived,
}: SdKSearchProjectsInputT,
}: InternalSearchProjectsInputT,
): esb.Query =>
esb.boolQuery().must(
rejectFalsyItems([
Expand All @@ -73,6 +78,7 @@ export class ProjectsEsSearchRepo {
.minimumShouldMatch(1)
),
!isNil(archived) && esb.termQuery('archived', archived),
excludeInternal && esb.termQuery('internal', false),
]),
);

Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ export class ProjectsService implements WithAuthFirewall<ProjectsFirewall> {

search = this.esSearchRepo.search;

create = ({ organization, ...values }: SdkCreateProjectInputT) => pipe(
create = ({ internal, organization, ...values }: SdkCreateProjectInputT & { internal?: boolean; }) => pipe(
this.repo.create({
value: {
...values,
internal: !!internal,
organizationId: organization.id,
},
}),
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/modules/projects/projects.tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type ProjectsTable =
organization_id: ColumnType<TableId, TableId, never>;
name: string;
description: string | null;
internal: boolean;
};

export type ProjectTableRow = NormalizeSelectTableRow<ProjectsTable>;
Expand Down

0 comments on commit 9b53fb6

Please sign in to comment.