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
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
PORT=9005
PORT=3000

DB_HOST=host.docker.internal
DB_PORT=5432
DB_DATABASE=omnibox
DB_USERNAME=omnibox
DB_PASSWORD=omnibox
DB_LOGGING=true
DB_SYNC=true

JWT_SECRET=your-secret-key
JWT_EXPIRE=72000s

# REDIS_URL=redis://redis:6379
# REDIS_PORT=6379
# REDIS_TTL=3600
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ services:
app:
build: .
ports:
- '8000:9005'
- "8000:${PORT}"
env_file:
- .env
depends_on:
Expand Down
125 changes: 72 additions & 53 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ export class AuthController {
}

@Public()
@Post('register')
async register(@Body('url') url: string, @Body('email') email: string) {
return await this.authService.register(url, email);
@Post('sign-up')
async signUp(@Body('url') url: string, @Body('email') email: string) {
return await this.authService.signUp(url, email);
}

@Public()
@Post('register-confirm')
async registerComFirm(
@Post('sign-up/confirm')
async signUpConfirm(
@Body('token') token: string,
@Body('username') username: string,
@Body('password') password: string,
@Body('password_repeat') password_repeat: string,
) {
return await this.authService.registerConfirm(token, {
return await this.authService.signUpConfirm(token, {
username,
password,
password_repeat,
Expand All @@ -45,7 +45,7 @@ export class AuthController {
}

@Public()
@Post('password-confirm')
@Post('password/confirm')
async resetPassword(
@Body('token') token: string,
@Body('password') password: string,
Expand All @@ -71,8 +71,8 @@ export class AuthController {
});
}

@Post('invite-confirm')
async inviteComFirm(@Body('token') token: string) {
@Post('invite/confirm')
async inviteConfirm(@Body('token') token: string) {
return await this.authService.inviteConfirm(token);
}
}
3 changes: 2 additions & 1 deletion src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
import { LocalStrategy } from 'src/auth/local.strategy';
import { AuthController } from 'src/auth/auth.controller';
import { ConfigService, ConfigModule } from '@nestjs/config';
import { InternalAuthController } from 'src/auth/internal.auth.controller';
import { NamespacesModule } from 'src/namespaces/namespaces.module';

@Module({
exports: [AuthService],
controllers: [AuthController],
controllers: [AuthController, InternalAuthController],
providers: [
AuthService,
JwtStrategy,
Expand Down
23 changes: 20 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { JwtService } from '@nestjs/jwt';
import { MailService } from 'src/mail/mail.service';
import { UserService } from 'src/user/user.service';
import { NamespacesService } from 'src/namespaces/namespaces.service';
import { CreateUserDto } from 'src/user/dto/create-user.dto';
import {
Injectable,
ForbiddenException,
Expand Down Expand Up @@ -48,7 +49,23 @@ export class AuthService {
};
}

async register(url: string, email: string) {
async signUpWithoutConfirm(createUser: CreateUserDto) {
const account = await this.userService.findByEmail(createUser.email);
if (account) {
throw new BadRequestException('Email already registered');
}
const user = await this.userService.create(createUser);
return {
id: user.id,
username: user.username,
access_token: this.jwtService.sign({
sub: user.id,
email: user.email,
}),
};
}

async signUp(url: string, email: string) {
const account = await this.userService.findByEmail(email);
if (account) {
throw new BadRequestException(
Expand All @@ -61,10 +78,10 @@ export class AuthService {
expiresIn: '1h',
},
);
await this.mailService.sendRegisterEmail(email, `${url}?token=${token}`);
await this.mailService.sendSignUpEmail(email, `${url}?token=${token}`);
}

async registerConfirm(
async signUpConfirm(
token: string,
data: {
username: string;
Expand Down
15 changes: 15 additions & 0 deletions src/auth/internal.auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AuthService } from 'src/auth/auth.service';
import { Public } from 'src/auth/decorators/public.decorator';
import { Controller, Body, Post } from '@nestjs/common';
import { CreateUserDto } from 'src/user/dto/create-user.dto';

@Controller('internal/api/v1')
export class InternalAuthController {
constructor(private authService: AuthService) {}

@Public()
@Post('sign-up')
async signUp(@Body() createUserDto: CreateUserDto) {
return await this.authService.signUpWithoutConfirm(createUserDto);
}
}
4 changes: 2 additions & 2 deletions src/mail/mail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { MailerService } from '@nestjs-modules/mailer';
export class MailService {
constructor(private readonly mailerService: MailerService) {}

async sendRegisterEmail(email: string, resetUrl: string): Promise<void> {
async sendSignUpEmail(email: string, resetUrl: string): Promise<void> {
try {
await this.mailerService.sendMail({
to: email,
subject: 'Continue completing account registration',
template: './register',
template: './sign-up',
context: {
resetUrl,
},
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions src/namespaces/namespaces.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ export class Namespace extends Base {

@Column('uuid', { array: true, default: [] })
owner_id: string[];

@Column({ type: 'int', default: 1 })
max_running_tasks: number;
}
6 changes: 6 additions & 0 deletions src/namespaces/namespaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export class NamespacesService {
return namespace;
}

async findByName(name: string) {
return await this.namespaceRepository.findOne({
where: { name },
});
}

async create(userId: string, name: string) {
const newNamespace = this.namespaceRepository.create({
name,
Expand Down
1 change: 0 additions & 1 deletion src/resources/dto/update-resource.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
IsEnum,
IsNumber,
IsArray,
IsObject,
IsString,
Expand Down
24 changes: 24 additions & 0 deletions src/tasks/dto/collect-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
import { SpaceType } from 'src/resources/resources.entity';

export class CollectRequestDto {
@IsString()
@IsNotEmpty()
html: string;

@IsString()
@IsNotEmpty()
url: string;

@IsString()
@IsNotEmpty()
title: string;

@IsString()
@IsNotEmpty()
namespace: string;

@IsEnum(SpaceType)
@IsNotEmpty()
spaceType: SpaceType;
}
17 changes: 17 additions & 0 deletions src/tasks/dto/task-callback.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IsNotEmpty, IsString, IsOptional } from 'class-validator';

export class TaskCallbackDto {
@IsString()
@IsNotEmpty()
id: string;

@IsString()
@IsNotEmpty()
endedAt: string;

@IsOptional()
exception: Record<string, any>;

@IsOptional()
output: Record<string, any>;
}
19 changes: 0 additions & 19 deletions src/tasks/internal-tasks.controller.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/tasks/internal.tasks.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TasksService } from 'src/tasks/tasks.service';
import { Body, Controller, Get, Post, Res } from '@nestjs/common';
import { Response } from 'express';
import { Public } from 'src/auth/decorators/public.decorator';
import { TaskCallbackDto } from './dto/task-callback.dto';

@Controller('internal/api/v1/tasks')
export class InternalTasksController {
constructor(private readonly tasksService: TasksService) {}

@Public()
@Post('/callback')
async handleTaskCallback(
@Body() taskCallback: TaskCallbackDto,
): Promise<Record<string, any>> {
return await this.tasksService.taskDoneCallback(taskCallback);
}

@Public()
@Get('/fetch')
async fetchTask(@Res() res: Response) {
const task = await this.tasksService.fetch();
res.status(task ? 200 : 204).json(task);
return;
}
}
17 changes: 12 additions & 5 deletions src/tasks/tasks.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Task } from 'src/tasks/tasks.entity';
import { TasksService } from 'src/tasks/tasks.service';
import {
Post,
Get,
Body,
Query,
Param,
Delete,
Controller,
Delete,
Get,
Param,
Post,
Query,
Req,
} from '@nestjs/common';
import { CollectRequestDto } from 'src/tasks/dto/collect-request.dto';

@Controller('api/v1/tasks')
export class TasksController {
Expand All @@ -19,6 +21,11 @@ export class TasksController {
return await this.tasksService.create(data);
}

@Post('collect')
async collect(@Req() req, @Body() data: CollectRequestDto) {
return await this.tasksService.collect(req.user, data);
}

@Get()
async listTasks(
@Query('namespace') namespace: string,
Expand Down
8 changes: 2 additions & 6 deletions src/tasks/tasks.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { User } from 'src/user/user.entity';
import { Base } from 'src/common/base.entity';
import { Namespace } from 'src/namespaces/namespaces.entity';
import {
// Index,
Column,
Entity,
ManyToOne,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';

Expand All @@ -23,7 +22,7 @@ export class Task extends Base {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ default: 0 })
@Column({ default: 5 })
priority: number;

@Column({ type: 'text' })
Expand All @@ -50,9 +49,6 @@ export class Task extends Base {
@Column({ name: 'canceled_at', nullable: true })
canceledAt: Date;

@Column({ name: 'concurrency_threshold', default: 1 })
concurrencyThreshold: number;

@ManyToOne(() => User)
@JoinColumn({ name: 'user_id' })
user: User;
Expand Down
9 changes: 7 additions & 2 deletions src/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { Task } from 'src/tasks/tasks.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TasksService } from 'src/tasks/tasks.service';
import { TasksController } from 'src/tasks/tasks.controller';
import { InternalTasksController } from 'src/tasks/internal-tasks.controller';
import { InternalTasksController } from 'src/tasks/internal.tasks.controller';
import { NamespacesModule } from 'src/namespaces/namespaces.module';
import { ResourcesModule } from 'src/resources/resources.module';

@Module({
providers: [TasksService],
imports: [NamespacesModule, TypeOrmModule.forFeature([Task])],
imports: [
NamespacesModule,
TypeOrmModule.forFeature([Task]),
ResourcesModule,
],
controllers: [TasksController, InternalTasksController],
})
export class TasksModule {}
Loading