Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api-key/api-key.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Base } from 'src/common/base.entity';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import {
Column,
Entity,
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TagModule } from 'src/tag/tag.module';
// import { CacheModule } from '@nestjs/cache-manager';
import { AuthModule } from 'src/auth/auth.module';
import { UserModule } from 'src/user/user.module';
Expand Down Expand Up @@ -34,6 +35,7 @@ import { LoggerMiddleware } from './logger.middleware';
cache: true,
isGlobal: true,
}),
TagModule,
MailModule,
AuthModule,
UserModule,
Expand Down
2 changes: 1 addition & 1 deletion src/conversations/conversations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Conversation } from 'src/conversations/entities/conversation.entity';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { ConfigService } from '@nestjs/config';
import { MessagesService } from 'src/messages/messages.service';
import { WizardAPIService } from 'src/wizard/api.wizard.service';
Expand Down
2 changes: 1 addition & 1 deletion src/conversations/entities/conversation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PrimaryGeneratedColumn,
} from 'typeorm';
import { Namespace } from 'src/namespaces/entities/namespace.entity';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { Message } from 'src/messages/entities/message.entity';
import { Base } from 'src/common/base.entity';

Expand Down
2 changes: 1 addition & 1 deletion src/groups/entities/group-user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Base } from 'src/common/base.entity';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { Group } from './group.entity';
import {
Entity,
Expand Down
2 changes: 1 addition & 1 deletion src/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Group } from './entities/group.entity';
import { GroupUser } from './entities/group-user.entity';
import { CreateGroupDto } from './dto/create-group.dto';
import { UpdateGroupDto } from './dto/update-group.dto';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { NamespacesService } from 'src/namespaces/namespaces.service';
import { Invitation } from 'src/invitations/entities/invitation.entity';

Expand Down
2 changes: 1 addition & 1 deletion src/messages/entities/message.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
PrimaryGeneratedColumn,
} from 'typeorm';
import { Conversation } from 'src/conversations/entities/conversation.entity';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { Base } from 'src/common/base.entity';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/messages/messages.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
OpenAIMessage,
} from 'src/messages/entities/message.entity';
import { CreateMessageDto } from 'src/messages/dto/create-message.dto';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { ChatDeltaResponse } from '../wizard/dto/chat-response.dto';
import { Task } from 'src/tasks/tasks.entity';
import { WizardTask } from 'src/resources/wizard.task.service';
Expand Down
2 changes: 1 addition & 1 deletion src/namespaces/entities/namespace-member.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Base } from 'src/common/base.entity';
import { Namespace } from 'src/namespaces/entities/namespace.entity';
import { Resource } from 'src/resources/resources.entity';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import {
Entity,
PrimaryGeneratedColumn,
Expand Down
2 changes: 1 addition & 1 deletion src/permissions/entities/user-permission.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Base } from 'src/common/base.entity';
import { Namespace } from 'src/namespaces/entities/namespace.entity';
import { Resource } from 'src/resources/resources.entity';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import {
Entity,
PrimaryGeneratedColumn,
Expand Down
4 changes: 2 additions & 2 deletions src/resources/resources.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ export class ResourcesController {
});
}

@Get(':resourceId/search')
@Get('search')
async search(
@Req() req,
@Param('namespaceId') namespaceId: string,
@Param('resourceId') resourceId: string,
@Query('resourceId') resourceId: string,
@Query('name') name: string,
) {
return await this.resourcesService.search({
Expand Down
2 changes: 1 addition & 1 deletion src/resources/resources.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { Base } from 'src/common/base.entity';
import generateId from 'src/utils/generate-id';
import {
Expand Down
20 changes: 11 additions & 9 deletions src/resources/resources.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
NotFoundException,
} from '@nestjs/common';
import { Task } from 'src/tasks/tasks.entity';
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { MinioService } from 'src/resources/minio/minio.service';
import { WizardTask } from 'src/resources/wizard.task.service';
import { SpaceType } from 'src/namespaces/entities/namespace.entity';
Expand Down Expand Up @@ -195,20 +195,22 @@ export class ResourcesService {
}

async search({ namespaceId, resourceId, name, userId }) {
// Self and child exclusions
const resourceChildren = await this.getAllSubResources(
namespaceId,
resourceId,
'',
true,
);
const where: any = {
user: { id: userId },
// Cannot move to root directory
parentId: Not(IsNull()),
namespace: { id: namespaceId },
id: Not(In(resourceChildren.map((children) => children.id))),
};
// Self and child exclusions
if (resourceId) {
const resourceChildren = await this.getAllSubResources(
namespaceId,
resourceId,
'',
true,
);
where.id = Not(In(resourceChildren.map((children) => children.id)));
}
if (name) {
where.name = Like(`%${name}%`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/resources/wizard-task/index.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { Resource } from 'src/resources/resources.entity';
import { Repository } from 'typeorm';
import { Task } from 'src/tasks/tasks.entity';
Expand Down
2 changes: 1 addition & 1 deletion src/resources/wizard-task/reader.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { Resource } from 'src/resources/resources.entity';
import { Repository } from 'typeorm';
import { Task } from 'src/tasks/tasks.entity';
Expand Down
7 changes: 7 additions & 0 deletions src/tag/dto/create-tag.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsString, MaxLength } from 'class-validator';

export class CreateTagDto {
@IsString()
@MaxLength(20)
name: string;
}
39 changes: 39 additions & 0 deletions src/tag/tag.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { TagService } from 'src/tag/tag.service';
import { CreateTagDto } from 'src/tag/dto/create-tag.dto';
import { Get, Body, Param, Query, Post, Controller } from '@nestjs/common';

@Controller('api/v1/namespaces/:namespaceId/tag')
export class TagController {
constructor(private tagService: TagService) {}

@Post()
async create(
@Param('namespaceId') namespaceId: string,
@Body() createTagDto: CreateTagDto,
) {
return await this.tagService.create(namespaceId, createTagDto);
}

@Get()
async findAll(
@Param('namespaceId') namespaceId: string,
@Query('name') name: string,
) {
return await this.tagService.findAll(namespaceId, name);
}

@Get('tags-by-ids')
async tagsByIds(
@Param('namespaceId') namespaceId: string,
@Query('id') id: string,
) {
if (!id) {
return [];
}
const ids = id.split(',');
if (ids.length <= 0) {
return [];
}
return await this.tagService.tagsByIds(namespaceId, ids);
}
}
34 changes: 34 additions & 0 deletions src/tag/tag.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Base } from 'src/common/base.entity';
import generateId from 'src/utils/generate-id';
import { Namespace } from 'src/namespaces/entities/namespace.entity';
import {
Column,
Entity,
ManyToOne,
JoinColumn,
BeforeInsert,
PrimaryColumn,
} from 'typeorm';

@Entity('tag')
export class Tag extends Base {
@PrimaryColumn('varchar', {
length: 6,
})
id: string;

@BeforeInsert()
generateId?() {
this.id = generateId(6);
}

@Column({
type: 'varchar',
length: 20,
})
name: string;

@ManyToOne(() => Namespace)
@JoinColumn({ name: 'namespace_id' })
namespace: Namespace;
}
13 changes: 13 additions & 0 deletions src/tag/tag.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { Tag } from 'src/tag/tag.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TagService } from 'src/tag/tag.service';
import { TagController } from 'src/tag/tag.controller';

@Module({
exports: [TagService],
providers: [TagService],
controllers: [TagController],
imports: [TypeOrmModule.forFeature([Tag])],
})
export class TagModule {}
56 changes: 56 additions & 0 deletions src/tag/tag.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Tag } from 'src/tag/tag.entity';
import { Repository, In, Like } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { CreateTagDto } from 'src/tag/dto/create-tag.dto';

@Injectable()
export class TagService {
constructor(
@InjectRepository(Tag)
private tagRepository: Repository<Tag>,
) {}

async create(namespaceId: string, createTagDto: CreateTagDto) {
const oldTag = await this.findByName(namespaceId, createTagDto.name);
if (oldTag) {
return oldTag;
}
const newTag = this.tagRepository.create({
namespace: { id: namespaceId },
name: createTagDto.name,
});
return await this.tagRepository.save(newTag);
}

async findByName(namespaceId: string, name: string) {
return await this.tagRepository.findOneBy({
name,
namespace: { id: namespaceId },
});
}

async tagsByIds(namespaceId: string, ids: Array<string>) {
if (ids.length <= 0) {
return [];
}
return await this.tagRepository.find({
where: { namespace: { id: namespaceId }, id: In(ids) },
});
}

async findAll(namespaceId: string, name: string) {
const where: any = {
namespace: { id: namespaceId },
};
if (name) {
where.name = Like(`%${name}%`);
}
return await this.tagRepository.find({
where,
skip: 0,
take: 10,
order: { updatedAt: 'DESC' },
});
}
}
2 changes: 1 addition & 1 deletion src/tasks/tasks.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from 'src/user/user.entity';
import { User } from 'src/user/entities/user.entity';
import { Base } from 'src/common/base.entity';
import {
Column,
Expand Down
10 changes: 10 additions & 0 deletions src/user/dto/create-user-option.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsString, MaxLength } from 'class-validator';

export class CreateUserOptionDto {
@IsString()
@MaxLength(20)
name: string;

@IsString()
value: string;
}
19 changes: 19 additions & 0 deletions src/user/entities/user-option.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Base } from 'src/common/base.entity';
import { User } from 'src/user/entities/user.entity';
import { Entity, Column, ManyToOne, JoinColumn, PrimaryColumn } from 'typeorm';

@Entity('user_option')
export class UserOption extends Base {
@PrimaryColumn({
type: 'varchar',
length: 20,
})
name: string;

@Column({ type: 'text' })
value: string;

@ManyToOne(() => User, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'user_id' })
user: User;
}
File renamed without changes.
24 changes: 24 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { UserService } from 'src/user/user.service';
import { UpdateUserDto } from 'src/user/dto/update-user.dto';
import { CreateUserOptionDto } from 'src/user/dto/create-user-option.dto';
import {
Req,
Get,
Body,
Patch,
Query,
Param,
Delete,
Post,
Controller,
ParseIntPipe,
} from '@nestjs/common';
Expand Down Expand Up @@ -50,4 +53,25 @@ export class UserController {
async remove(@Param('id') id: string) {
return await this.userService.remove(id);
}

@Post('option')
async createOption(@Req() req, @Body() createOptionDto: CreateUserOptionDto) {
const option = await this.userService.getOption(
req.user.id,
createOptionDto.name,
);
if (option && option.name) {
return await this.userService.updateOption(
req.user.id,
option.name,
createOptionDto.value,
);
}
return await this.userService.createOption(req.user.id, createOptionDto);
}

@Get('option/:name')
async getOption(@Req() req, @Param('name') name: string) {
return await this.userService.getOption(req.user.id, name);
}
}
Loading