mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-18 02:31:11 +10:00
Refactoring
* Refactor workspace membership system * Create setup endpoint * Use Passport.js * Several updates and fixes
This commit is contained in:
@ -3,92 +3,31 @@ import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { User } from './entities/user.entity';
|
||||
import { UserRepository } from './repositories/user.repository';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { WorkspaceService } from '../workspace/services/workspace.service';
|
||||
import { DataSource, EntityManager } from 'typeorm';
|
||||
import { transactionWrapper } from '../../helpers/db.helper';
|
||||
import { CreateWorkspaceDto } from '../workspace/dto/create-workspace.dto';
|
||||
import { Workspace } from '../workspace/entities/workspace.entity';
|
||||
|
||||
export type UserWithWorkspace = {
|
||||
user: User;
|
||||
workspace: Workspace;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
private userRepository: UserRepository,
|
||||
private workspaceService: WorkspaceService,
|
||||
private dataSource: DataSource,
|
||||
) {}
|
||||
async create(
|
||||
createUserDto: CreateUserDto,
|
||||
manager?: EntityManager,
|
||||
): Promise<User> {
|
||||
let user: User;
|
||||
|
||||
const existingUser: User = await this.findByEmail(createUserDto.email);
|
||||
|
||||
if (existingUser) {
|
||||
throw new BadRequestException('A user with this email already exists');
|
||||
}
|
||||
|
||||
await transactionWrapper(
|
||||
async (manager: EntityManager) => {
|
||||
user = plainToInstance(User, createUserDto);
|
||||
user.locale = 'en';
|
||||
user.lastLoginAt = new Date();
|
||||
user.name = createUserDto.email.split('@')[0];
|
||||
|
||||
user = await manager.save(User, user);
|
||||
|
||||
const createWorkspaceDto: CreateWorkspaceDto = {
|
||||
name: 'My Workspace',
|
||||
};
|
||||
|
||||
await this.workspaceService.createOrJoinWorkspace(
|
||||
user.id,
|
||||
createWorkspaceDto,
|
||||
manager,
|
||||
);
|
||||
},
|
||||
this.dataSource,
|
||||
manager,
|
||||
);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async getUserInstance(userId: string): Promise<UserWithWorkspace> {
|
||||
const user: User = await this.findById(userId);
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
let workspace;
|
||||
|
||||
try {
|
||||
workspace = await this.workspaceService.getUserCurrentWorkspace(userId);
|
||||
} catch (error) {
|
||||
//console.log(error);
|
||||
}
|
||||
|
||||
return { user, workspace };
|
||||
}
|
||||
constructor(private userRepository: UserRepository) {}
|
||||
|
||||
async findById(userId: string) {
|
||||
return this.userRepository.findById(userId);
|
||||
}
|
||||
|
||||
async findByEmail(email: string) {
|
||||
return this.userRepository.findByEmail(email);
|
||||
async getUserInstance(userId: string): Promise<any> {
|
||||
const user: User = await this.userRepository.findOne({
|
||||
relations: ['workspace'],
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async update(userId: string, updateUserDto: UpdateUserDto) {
|
||||
@ -101,6 +40,7 @@ export class UserService {
|
||||
user.name = updateUserDto.name;
|
||||
}
|
||||
|
||||
// todo need workspace scoping
|
||||
if (updateUserDto.email && user.email != updateUserDto.email) {
|
||||
if (await this.userRepository.findByEmail(updateUserDto.email)) {
|
||||
throw new BadRequestException('A user with this email already exists');
|
||||
@ -114,11 +54,4 @@ export class UserService {
|
||||
|
||||
return this.userRepository.save(user);
|
||||
}
|
||||
|
||||
async compareHash(
|
||||
plainPassword: string,
|
||||
passwordHash: string,
|
||||
): Promise<boolean> {
|
||||
return await bcrypt.compare(plainPassword, passwordHash);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user