Commit 4d9e7864 authored by “Yevgeniy's avatar “Yevgeniy

added Task model and relations

parent 9678e654
import { DataSource } from "typeorm";
import {User} from './models/User';
import {Task} from './models/Task';
export const myDataSource = new DataSource({
type: "postgres",
......@@ -8,8 +9,8 @@ export const myDataSource = new DataSource({
username: "pluser",
password: "pluser",
database: "planner",
// entities: ["./models/*.ts"],
entities: [User],
entities: [User,Task],
logging: true,
synchronize: true,
synchronize: true, // in build switch to false
migrationsRun: false
})
\ No newline at end of file
import {
Column,
Entity,
PrimaryGeneratedColumn,
CreateDateColumn,
BaseEntity,
ManyToOne,
ManyToMany
} from 'typeorm';
import {User} from './User';
type taskFinishType = "open" | "done" |"failed";
interface ITask{
id: string;
title: string;
// description: string;
createdAt: Date| undefined;
// dateTimeStart:Date| null;
// dateTimeDue:Date| null;
assignedTo: User[];
accomplish: taskFinishType;
author: User;
}
@Entity({ name: 'Task' })
export class Task extends BaseEntity implements ITask{
@PrimaryGeneratedColumn('uuid')
id!: string
@Column({ name: 'title', type: 'varchar', length:50,nullable: false })
title!: string
// @Column({ name: 'description', type: 'varchar', length:50,nullable: false })
// description!: string
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt: Date | undefined;
// @CreateDateColumn({ name: 'dateTimeStart', type: Date,nullable: true })
// dateTimeStart!: Date | null;
// @CreateDateColumn({ name: 'dateTimeDue', type: Date,nullable: true })
// dateTimeDue!: Date | null;
@Column({
type: "enum",
enum: ["opened", "done" , "failed"],
default: "opened"
})
accomplish!: taskFinishType
@ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks)
author!: User;
@ManyToMany(() => User, (user: { tasks: Task[]; }) => user.tasks)
assignedTo!: User[];
}
......@@ -6,17 +6,18 @@ import {
BeforeInsert,
BeforeUpdate,
BaseEntity,
ManyToOne
ManyToMany,
OneToMany
} from 'typeorm';
import bcrypt from 'bcrypt';
import {nanoid} from 'nanoid';
import {Task} from './Task';
const SALT_WORK_FACTOR= 10;
// enum UserRole {
// DIRECTOR = "director",
// WORKER = "worker",
// }
type UserRoleType = "worker" | "director";
type userRoleType = "worker" | "director";
interface IUser {
id:string;
......@@ -25,48 +26,55 @@ interface IUser {
email: string;
displayName: string;
password:string;
role: UserRoleType;
createdAt: Date | undefined;
token: string;
role: userRoleType;
createdAt: Date;
createdTasks:Task[]| null;
}
@Entity()
@Entity({ name: 'User' })
export class User extends BaseEntity implements IUser {
@PrimaryGeneratedColumn('uuid')
id!: string
@Column({ name: 'first_name', type: 'varchar', length:20,nullable: false })
@Column({ name: 'name', type: 'varchar', length:20,nullable: false })
name!: string
@Column({ name: 'last_name', type: 'varchar', length:30,nullable: false })
@Column({ name: 'surname', type: 'varchar', length:30,nullable: false })
surname!: string
@Column({ name: 'displayName', type: 'varchar', length:30,nullable: false })
displayName!: string
@Column({ type: 'varchar',length:20, unique: true, nullable: false })
@Column({ name: 'email', type: 'varchar',length:20, unique: true, nullable: false })
email!: string
@Column({ type: 'varchar',length:10, unique: true, nullable: true})
@Column({ name: 'phone', type: 'varchar',length:10, unique: true, nullable: true})
phone?: string
// @CreateDateColumn()
@Column({ name: 'token', type: 'varchar',length:100, unique: true, nullable: false })
token!: string
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt: Date | undefined;
createdAt!: Date;
@Column({
type: "enum",
enum: ["worker", "director"],
default: "worker"
})
role!: UserRoleType
role!: userRoleType
@Column({ type: 'varchar', nullable: false, select:false })
password!: string
@OneToMany(() => Task, (task: { user: User }) => task.user)
createdTasks!: Task[];
@ManyToMany(() => Task, (task: { users: User[] }) =>task.users)
tasksToMake!: Task[];
@BeforeInsert()
protected async beforeInserthashPassword():Promise<void> {
......@@ -80,6 +88,19 @@ export class User extends BaseEntity implements IUser {
this.password = await bcrypt.hash(this.password, salt);
}
public generateToken():void{
this.token = nanoid()
return
}
static async comparePasswords(
candidatePassword: string,
hashedPassword: string
):Promise<boolean> {
return await bcrypt.compare(candidatePassword, hashedPassword);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment