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

added Task model and relations

parent 9678e654
import { DataSource } from "typeorm"; import { DataSource } from "typeorm";
import {User} from './models/User'; import {User} from './models/User';
import {Task} from './models/Task';
export const myDataSource = new DataSource({ export const myDataSource = new DataSource({
type: "postgres", type: "postgres",
...@@ -8,8 +9,8 @@ export const myDataSource = new DataSource({ ...@@ -8,8 +9,8 @@ export const myDataSource = new DataSource({
username: "pluser", username: "pluser",
password: "pluser", password: "pluser",
database: "planner", database: "planner",
// entities: ["./models/*.ts"], entities: [User,Task],
entities: [User],
logging: true, 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 { ...@@ -6,17 +6,18 @@ import {
BeforeInsert, BeforeInsert,
BeforeUpdate, BeforeUpdate,
BaseEntity, BaseEntity,
ManyToOne ManyToMany,
OneToMany
} from 'typeorm'; } from 'typeorm';
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
import {nanoid} from 'nanoid';
import {Task} from './Task';
const SALT_WORK_FACTOR= 10; const SALT_WORK_FACTOR= 10;
// enum UserRole {
// DIRECTOR = "director", type userRoleType = "worker" | "director";
// WORKER = "worker",
// }
type UserRoleType = "worker" | "director";
interface IUser { interface IUser {
id:string; id:string;
...@@ -25,48 +26,55 @@ interface IUser { ...@@ -25,48 +26,55 @@ interface IUser {
email: string; email: string;
displayName: string; displayName: string;
password:string; password:string;
role: UserRoleType; token: string;
createdAt: Date | undefined; role: userRoleType;
createdAt: Date;
createdTasks:Task[]| null;
} }
@Entity() @Entity({ name: 'User' })
export class User extends BaseEntity implements IUser { export class User extends BaseEntity implements IUser {
@PrimaryGeneratedColumn('uuid') @PrimaryGeneratedColumn('uuid')
id!: string id!: string
@Column({ name: 'first_name', type: 'varchar', length:20,nullable: false }) @Column({ name: 'name', type: 'varchar', length:20,nullable: false })
name!: string name!: string
@Column({ name: 'last_name', type: 'varchar', length:30,nullable: false }) @Column({ name: 'surname', type: 'varchar', length:30,nullable: false })
surname!: string surname!: string
@Column({ name: 'displayName', type: 'varchar', length:30,nullable: false }) @Column({ name: 'displayName', type: 'varchar', length:30,nullable: false })
displayName!: string displayName!: string
@Column({ type: 'varchar',length:20, unique: true, nullable: false }) @Column({ name: 'email', type: 'varchar',length:20, unique: true, nullable: false })
email!: string email!: string
@Column({ type: 'varchar',length:10, unique: true, nullable: true}) @Column({ name: 'phone', type: 'varchar',length:10, unique: true, nullable: true})
phone?: string phone?: string
@Column({ name: 'token', type: 'varchar',length:100, unique: true, nullable: false })
// @CreateDateColumn() token!: string
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() }) @CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt: Date | undefined; createdAt!: Date;
@Column({ @Column({
type: "enum", type: "enum",
enum: ["worker", "director"], enum: ["worker", "director"],
default: "worker" default: "worker"
}) })
role!: UserRoleType role!: userRoleType
@Column({ type: 'varchar', nullable: false, select:false }) @Column({ type: 'varchar', nullable: false, select:false })
password!: string password!: string
@OneToMany(() => Task, (task: { user: User }) => task.user)
createdTasks!: Task[];
@ManyToMany(() => Task, (task: { users: User[] }) =>task.users)
tasksToMake!: Task[];
@BeforeInsert() @BeforeInsert()
protected async beforeInserthashPassword():Promise<void> { protected async beforeInserthashPassword():Promise<void> {
...@@ -80,6 +88,19 @@ export class User extends BaseEntity implements IUser { ...@@ -80,6 +88,19 @@ export class User extends BaseEntity implements IUser {
this.password = await bcrypt.hash(this.password, salt); 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