workspace - wip

This commit is contained in:
Philipinho
2023-08-07 18:16:51 +01:00
parent 021a99e716
commit fe7c3ede01
25 changed files with 370 additions and 9464 deletions
@@ -3,10 +3,13 @@ import {
Column,
CreateDateColumn,
Entity,
OneToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import * as bcrypt from 'bcrypt';
import { Workspace } from '../../workspace/entities/workspace.entity';
import { WorkspaceUser } from '../../workspace/entities/workspace-user.entity';
@Entity('users')
export class User {
@@ -49,6 +52,16 @@ export class User {
@UpdateDateColumn()
updatedAt: Date;
@OneToMany(() => Workspace, (workspace) => workspace.creator, {
createForeignKeyConstraints: false,
})
workspaces: Workspace[];
@OneToMany(() => WorkspaceUser, (workspaceUser) => workspaceUser.user, {
createForeignKeyConstraints: false,
})
workspaceUser: WorkspaceUser[];
toJSON() {
delete this.password;
return this;
+8 -2
View File
@@ -1,14 +1,20 @@
import { Module } from '@nestjs/common';
import { forwardRef, Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { UserRepository } from './repositories/user.repository';
import { AuthModule } from '../auth/auth.module';
import { WorkspaceModule } from '../workspace/workspace.module';
@Module({
imports: [TypeOrmModule.forFeature([User]), AuthModule],
imports: [
TypeOrmModule.forFeature([User]),
forwardRef(() => AuthModule),
WorkspaceModule,
],
controllers: [UserController],
providers: [UserService, UserRepository],
exports: [UserService, UserRepository],
})
export class UserModule {}
+17 -3
View File
@@ -5,10 +5,15 @@ import { User } from './entities/user.entity';
import { UserRepository } from './repositories/user.repository';
import { plainToClass } from 'class-transformer';
import * as bcrypt from 'bcrypt';
import { WorkspaceService } from '../workspace/services/workspace.service';
import { CreateWorkspaceDto } from '../workspace/dto/create-workspace.dto';
@Injectable()
export class UserService {
constructor(private userRepository: UserRepository) {}
constructor(
private userRepository: UserRepository,
private workspaceService: WorkspaceService,
) {}
async create(createUserDto: CreateUserDto): Promise<User> {
const existingUser: User = await this.findByEmail(createUserDto.email);
@@ -16,11 +21,20 @@ export class UserService {
throw new BadRequestException('A user with this email already exists');
}
const user: User = plainToClass(User, createUserDto);
let user: User = plainToClass(User, createUserDto);
user.locale = 'en';
user.lastLoginAt = new Date();
return this.userRepository.save(user);
user = await this.userRepository.save(user);
//TODO: create workspace if it is not a signup to an existing workspace
const workspaceDto: CreateWorkspaceDto = {
name: user.name, // will be better handled
};
await this.workspaceService.create(workspaceDto, user.id);
return user;
}
findById(userId: string) {