users, candidate, recruiter
users
, то я автоматически создаю candidate
или recruiter
в зависимости какую роль пользователь на фронте выбрал.const credentials = {
username: "One",
photoURL: "http://image-way.jpg"
email: "one@gmail.com",
role: "recruiter"
}
user = await this.userService.create(credentials);
const userCreationData = {
fullName: credentials.username,
profilePhoto: credentials.photoURL,
};
this.userService.createUserByRole(user, userCreationData);
const jwtAccessToken = this.generateAccessToken(user);
response = {
accessToken: jwtAccessToken,
redirectURL: `/sign-up/${user.role}`,
};
export class CreateUserByRoleDto {
fullName: string;
profilePhoto: string;
}
async createUserByRole(
userData: User,
createUserByRoleDto?: CreateUserByRoleDto,
): Promise<void> {
const user = await this.userRepository.findOne({ email: userData.email });
if (!user) throw new NotFoundException('User not found');
let createdUser: Recruiter | Candidate;
if (user.role === 'recruiter') {
createdUser = this.recruiterRepository.create();
createdUser = Object.assign(createdUser, createUserByRoleDto, {
user, // вот это создает мне userId в таблице recruiter
});
} else {
createdUser = this.candidateRepository.create();
createdUser = Object.assign(createdUser, createUserByRoleDto, {
user, // вот это создает мне userId в таблице candidate
});
}
await createdUser.save();
}
import {
BaseEntity,
Entity,
PrimaryGeneratedColumn,
Column,
Unique,
} from 'typeorm';
import { hash } from 'bcrypt';
@Entity()
@Unique(['email'])
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
role: string;
@Column()
email: string;
@Column()
salt: string;
@Column()
password: string;
@Column({ default: false })
agreement: boolean;
@Column({ default: '' })
photoURL: string;
@Column({ default: '' })
username: string;
async validatePassword(password: string): Promise<boolean> {
const hashedPassword = await hash(password, this.salt);
return hashedPassword === this.password;
}
}
import { User } from 'src/users/user.entity';
import {
BaseEntity,
Column,
Entity,
JoinColumn,
OneToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
@Entity()
export class Candidate extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@OneToOne(
type => User,
(user: User) => user.id,
)
@JoinColumn()
user: number; // Вот тут вся магия
@Column({ default: '' })
primaryCategory: string;
@Column({ default: '' })
experience: string;
@Column({ default: 0 })
salary: number;
@Column({ default: '' })
location: string;
@Column({ default: '' })
fullName: string;
/** It should be related with another table which get files from aws s3 */
@Column({ default: '' })
profilePhoto: string;
}
users
и в candidate
и в recruiter
(photoURL, username )?Делать ли мне OneToOne связь с другими похожими полями