Merge branch 'task-58-enhance/model_Member' into 'development'

Task 58 enhance/model member

See merge request !38
parents cf98dee7 50f4e782
......@@ -2,6 +2,7 @@ import { DataSource } from "typeorm";
import {User} from './models/User';
import {Task} from './models/Task';
import { Project } from "./models/Project";
import { Member } from "./models/Member";
export const myDataSource = new DataSource({
type: "postgres",
......@@ -10,7 +11,7 @@ export const myDataSource = new DataSource({
username: "pluser",
password: "pluser",
database: "planner",
entities: [User,Task,Project],
entities: [User,Task,Project,Member],
logging: true,
synchronize: true, // in build switch to false
migrationsRun: false
......
......@@ -4,6 +4,7 @@ import { User, UserRole } from "./models/User";
import { faker } from '@faker-js/faker';
import { Task } from "./models/Task";
import { Project } from "./models/Project";
import { Member, MemberRole } from "./models/Member";
function randomIntFromInterval(min:number, max:number) {
return Math.floor(Math.random() * (max - min + 1) + min)
......@@ -13,6 +14,8 @@ const loadFixtures = async () => {
myDataSource
.initialize()
.then(async () => {
const repositoryMember = myDataSource.getRepository(Member);
await repositoryMember.delete({})
const repositoryTask = myDataSource.getRepository(Task);
await repositoryTask.delete({});
const repositoryProject = myDataSource.getRepository(Project);
......@@ -50,9 +53,11 @@ const loadFixtures = async () => {
const newTask = new Task();
newTask.title = `Buy ${faker.commerce.productName()}`;
newTask.description = faker.random.words(4);
newTask.executors = faker.helpers.arrayElements(users, randomIntFromInterval(0, 3));
newTask.executor = faker.helpers.arrayElement(users);
newTask.dateTimeDue = faker.date.soon(randomIntFromInterval(1, 15));
newTask.dateTimeStart = faker.date.recent((randomIntFromInterval(0, 8)));
newTask.dateTimeDeadLine = faker.date.soon(randomIntFromInterval(1, 15));
newTask.dateTimeFactDeadLine = faker.date.soon(randomIntFromInterval(1, 15));
newTask.author = faker.helpers.arrayElement(users);
newTask.accomplish = faker.helpers.arrayElement(accomplish);
newTask.priority = faker.helpers.arrayElement(priorities);
......@@ -62,9 +67,9 @@ const loadFixtures = async () => {
const newTask = new Task();
newTask.title = `Buy ${faker.commerce.productName()}`;
newTask.description = faker.random.words(4);
newTask.executors = faker.helpers.arrayElements(users, randomIntFromInterval(0, 3));
newTask.dateTimeDue = null;
newTask.dateTimeStart = null;
newTask.executor = faker.helpers.arrayElement(users);
// newTask.dateTimeDue = null;
// newTask.dateTimeStart = null;
newTask.author = faker.helpers.arrayElement(users);
newTask.accomplish = accomplish[0];
newTask.priority = faker.helpers.arrayElement(priorities);
......@@ -73,14 +78,24 @@ const loadFixtures = async () => {
}
}
const members:Member[]=[]
for (let i = 0; i < 5; i++) {
const newMember = new Member();
newMember.user = faker.helpers.arrayElement(users);
newMember.roleProject=faker.helpers.objectValue(MemberRole);
await newMember.save();
members.push(newMember)
}
const projects:Project[] = []
for (let i = 0; i < 5; i++) {
const newProject = new Project();
newProject.title = `Project ${faker.random.words(1)}`;
newProject.color = faker.random.words(1);
newProject.admin = faker.helpers.arrayElement(users);
newProject.members = faker.helpers.arrayElements(members,randomIntFromInterval(1, 3))
// newProject.admin = faker.helpers.arrayElement(users);
newProject.tasks= faker.helpers.arrayElements(tasks, randomIntFromInterval(1, 3));
newProject.workers = faker.helpers.arrayElements(users, randomIntFromInterval(1, 3));
// newProject.workers = faker.helpers.arrayElements(users, randomIntFromInterval(1, 3));
await newProject.save();
projects.push(newProject)
}
......
import {
Column,
Entity,
PrimaryGeneratedColumn,
CreateDateColumn,
BaseEntity,
ManyToOne,
ManyToMany,
OneToMany,
JoinTable
} from 'typeorm';
import {User} from './User';
import {Project} from './Project';
import {Task} from './Task';
export enum MemberRole {USER="user" ,WATCHER= "watcher",ADMIN="admin"}
interface IMember{
id: string;
user: User;
project: Project;
createdAt: Date;
roleProject: MemberRole;
}
@Entity({name:"Member"})
export class Member extends BaseEntity implements IMember{
@PrimaryGeneratedColumn("uuid")
id!: string;
@CreateDateColumn({ name: 'createdAt', type: Date, default: new Date() })
createdAt!: Date;
@ManyToOne(() => User, (user: { members: Member[]; }) => user.members,{cascade: true, onUpdate:'CASCADE',eager:true})
user!: User;
@ManyToOne(() => Project, (project: { members: Member[]; }) => project.members,{cascade: true, onUpdate:'CASCADE',nullable:true})
project!: Project;
@Column({
type: "enum",
enum: MemberRole,
default: MemberRole.USER
})
roleProject!: MemberRole
}
\ No newline at end of file
......@@ -9,8 +9,9 @@ import {
OneToMany,
JoinTable
} from 'typeorm';
import {User} from './User';
// import {User} from './User';
import {Task} from './Task';
import {Member} from './Member';
......@@ -18,12 +19,14 @@ import {
id: string;
title: string;
color: string;
admin:User;
workers:User[];
// admin:User;
// workers:User[];
tasks:Task[]|null;
createdAt: Date;
dateDue: Date| null;
department:boolean;
active:boolean;
// dateDue: Date| null;
// department:boolean;
members: Member[];
}
@Entity({ name: 'Project' })
......@@ -39,21 +42,27 @@ import {
createdAt!: Date;
@Column({ name: 'color', type: 'varchar', length:100,nullable: true })
color!: string
color!: string;
@Column({ name: 'dateDue', type: Date, default: null })
dateDue!: Date| null;
@Column({ name: 'active',type: 'boolean', default: true })
active!: boolean;
@Column({ name: 'department', type: Boolean,nullable:true ,default: false})
department!: boolean;
// @Column({ name: 'dateDue', type: Date, default: null })
// dateDue!: Date| null;
@ManyToOne(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true})
admin!: User;
// @Column({ name: 'department', type: Boolean,nullable:true ,default: false})
// department!: boolean;
// @ManyToOne(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true})
// admin!: User;
@OneToMany(() => Task, (task: { project: Project; })=>task.project)
tasks!:Task[]|null;
@OneToMany(() => Task, (task: { project: Project; })=>task.project,{nullable:true})
tasks!:Task[];
@OneToMany(() => Member, (member: { project: Project; })=>member.project)
members!:Member[];
@ManyToMany(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true,cascade: true, onUpdate:'CASCADE',onDelete: 'CASCADE'})
@JoinTable()
workers!: User[];
// @ManyToMany(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true,cascade: true, onUpdate:'CASCADE',onDelete: 'CASCADE'})
// @JoinTable()
// workers!: User[];
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ import {
CreateDateColumn,
BaseEntity,
ManyToOne,
ManyToMany,
OneToOne,
JoinTable
} from 'typeorm';
import {User} from './User';
......@@ -21,11 +21,13 @@ import {
createdAt: Date;
dateTimeStart:Date| null;
dateTimeDue:Date| null;
dateTimeDeadLine: Date| null;
dateTimeFactDeadLine: Date| null;
accomplish: taskFinishType;
priority: priorityType | null;
author: User;
project:Project|null;
executors:User[];
executor:User;
}
@Entity({ name: 'Task' })
......@@ -39,9 +41,13 @@ import {
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt!: Date;
@Column({ name: 'dateTimeStart', type: Date,nullable: true })
dateTimeStart!: Date | null;
dateTimeStart!: Date ;
@Column({ name: 'dateTimeDue', type: Date,nullable: true })
dateTimeDue!: Date | null;
dateTimeDue!: Date ;
@Column({ name: 'dateTimeDeadLine', type: Date,nullable: true })
dateTimeDeadLine!: Date;
@Column({ name: 'dateTimeFactDeadLine', type: Date,nullable: true })
dateTimeFactDeadLine!: Date;
@Column({
type: "enum",
......@@ -64,9 +70,9 @@ import {
@ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks,{eager : true})
author!: User;
@ManyToMany(() => User,{eager : true, cascade: true, onUpdate:'CASCADE'})
@ManyToOne(() =>User, (user: { tasks: Task[]}) => user.tasks,{eager : true,nullable: true, onUpdate:'CASCADE'})
@JoinTable()
executors!: User[];
executor!: User;
@ManyToOne(()=>Project,(project:{tasks: Task[]}) => project.tasks,{eager : true,nullable: true,onUpdate:'CASCADE'})
project!: Project;
......
......@@ -8,6 +8,7 @@ import {
ManyToMany,
OneToMany,
JoinTable,
OneToOne,
} from 'typeorm';
import {IsEmail
} from "class-validator";
......@@ -16,6 +17,7 @@ import bcrypt from 'bcrypt';
import {nanoid} from 'nanoid';
import {Task} from './Task';
import {Project} from './Project';
import {Member} from './Member';
const SALT_WORK_FACTOR= 10;
......@@ -33,8 +35,9 @@ interface IUser {
token: string;
createdAt: Date;
createdTasks:Task[];
workerInProjects:Project[];
adminInProjects:Project[];
// workerInProjects:Project[];
// adminInProjects:Project[];
membership: Member[];
}
......@@ -80,16 +83,16 @@ export class User extends BaseEntity implements IUser {
@OneToMany(() => Task, (task: { user: User }) => task.user)
createdTasks!: Task[];
@ManyToMany(() => Task, (task: { users: User[] }) =>task.users)
@OneToMany(() => Task, (task: { user: User }) =>task.user)
tasks!: Task[];
@OneToMany(() => Project, (project: { user: User }) => project.user)
adminInProjects!: Project[];
@OneToMany(() => Member, (member: { user: User }) => member.user)
membership!: Member[];
@ManyToMany(() => Project,(project: { user: User }) => project.user)
@JoinTable()
workerInProjects!: Project[];
// @ManyToMany(() => Project,(project: { user: User }) => project.user)
// @JoinTable()
// workerInProjects!: Project[];
@BeforeInsert()
......
......@@ -2,6 +2,10 @@ import express,{Router, Request, Response} from 'express';
import {Project} from '../models/Project';
import {myDataSource} from '../app-data-source';
import { User } from '../models/User';
import { Member, MemberRole } from '../models/Member';
import { userInfo } from 'os';
import { Task } from '../models/Task';
import { getRepository } from 'typeorm';
const router:Router = express.Router();
const dataSource = myDataSource;
......@@ -12,27 +16,40 @@ router.get('/',async (req:Request, res:Response): Promise<Response>=> {
return res.send({projects})
})
router.get('/my',async (req:Request, res:Response): Promise<Response>=> {
const token = req.get('Authorization');
const user = await dataSource
.createQueryBuilder()
.select("user")
.from(User, "user")
.where("user.token = :token", { token: token })
.getOne();
if(!user) return res.status(404).send({Message:'user not found'})
const projects:Project[] = await dataSource.manager.find(Project)
return res.send({projects})
})
router.post('/', async (req:Request, res:Response): Promise<Response> => {
if (!req.body) return res.status(400).send({Message:'problem in incoming req.body'})
const token = req.get('Authorization');
console.log("token:" + token)
const {title, dateDue,color,department,workers,tasks}= req.body;
const {title,color}= req.body;
const user = await dataSource
.createQueryBuilder()
.select("user")
.from(User, "user")
.where("user.token = :token", { token: token })
.getOne()
.getOne();
if(!user) return res.status(404).send({Message:'user not found'})
const member:Member = new Member;
member.user= user;
member.roleProject= MemberRole.ADMIN;
await member.save();
const project : Project= new Project()
project.title = title;
project.color = color;
project.dateDue = dateDue || null;
project.department = department;
project.workers = workers|| null;
project.tasks = tasks || null;
project.admin = user;
project.members=[member]
await project.save()
return res.send({project})
})
......@@ -42,14 +59,30 @@ router.get("/:id",async (req:Request, res:Response): Promise<Response> => {
.createQueryBuilder()
.select("project")
.from(Project, "project")
.leftJoinAndSelect('project.admin', 'user')
.leftJoinAndSelect('project.members', 'member')
.leftJoinAndSelect('member.user', 'user' )
.where("project.id = :id", { id: req.params.id })
.getOne()
if (!project) return res.status(404).send({Message:'no info on this project'})
return res.send({project})
const tasks = await dataSource.getRepository('Task')
.find(
{
relations:{
executor:true,
author:true,
},
where:{
project:{
id:req.params.id
}
}
}
)
return res.send({project, tasks})
})
router.get('/userId/:userId', async (req : Request, res : Response): Promise<Response>=>{
router.get('/user/:userId', async (req : Request, res : Response): Promise<Response>=>{
const userId:string = req.params.userId
const userProjects = await dataSource
.createQueryBuilder()
......
......@@ -2,7 +2,6 @@ import express,{Router, Request, Response} from 'express';
import {Task} from '../models/Task';
import {myDataSource} from '../app-data-source';
import { User } from '../models/User';
import { Project } from '../models/Project';
const router:Router = express.Router();
const dataSource = myDataSource;
......@@ -10,14 +9,18 @@ const dataSource = myDataSource;
router.get('/', async(req:Request, res:Response):Promise<Response> => {
const tasks = await dataSource
.getRepository(Task)
.find()
.find({
relations:{
executor:true,
author:true,
}})
return res.send({tasks})
})
router.post('/', async(req:Request, res:Response):Promise<Response>=>{
const token = req.get('Authorization');
const newTask = new Task();
const {title,description,project,executors,dateTimeDue,dateTimeStart,accomplish,priority} = req.body;
const {title,description,project,dateTimeDue,dateTimeStart,accomplish,priority} = req.body;
const user = await dataSource
.createQueryBuilder()
.select("user")
......@@ -28,7 +31,6 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
newTask.title = title;
newTask.description = description;
newTask.project = project;
newTask.executors = executors;
newTask.dateTimeDue = dateTimeDue;
newTask.dateTimeStart = dateTimeStart;
newTask.author= user;
......@@ -38,14 +40,28 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
return res.send({newTask})
})
router.get('/:userId', async (req: Request, res: Response):Promise<Response>=>{
router.get('/user/:userId', async (req: Request, res: Response):Promise<Response>=>{
const userId = req.params.userId;
const tasks = await dataSource
.getRepository(Task)
.createQueryBuilder('task')
.innerJoinAndSelect('task.executors', 'user')
.where('user.id = :userId', {userId :userId})
.getMany()
.find({
relations:{
executor:true,
author:true,
},
where:[
{
executor:{
id:userId
}
},
{
author:{
id:userId
}
},
]
})
return res.send({tasks})
})
......@@ -60,10 +76,24 @@ router.get('/my', async (req: Request, res: Response):Promise<Response>=>{
if(!user) return res.status(404).send({Message:'user not found'})
const tasks = await dataSource
.getRepository(Task)
.createQueryBuilder('task')
.innerJoinAndSelect('task.executors', 'user')
.where('user.id = :userId', {userId :user.id})
.getMany()
.find({
relations:{
executor:true,
author:true,
},
where:[
{
executor:{
id:user.id
}
},
{
author:{
id:user.id
}
},
]
})
return res.send({tasks})
})
......@@ -104,7 +134,7 @@ router.put('/',async(req:Request, res:Response)=> {
task.dateTimeDue= dateTimeDue
task.dateTimeStart= dateTimeStart
task.author=user
task.executors=executors
// task.executors=executors
task.accomplish= accomplish
task.priority= priority
await task.save()
......@@ -121,7 +151,7 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
.getRepository(Task)
.find({
relations:{
executors:true,
// executors:true,
author:true,
project:true
}
......@@ -141,24 +171,4 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
// router.post('/projects',async (req: Request, res: Response):Promise<Response>=>{
// let projectArray :string[]= req.body;
// console.log('projectArray ', projectArray)
// if (projectArray.length===0) {
// const tasks = await
// dataSource
// .getRepository(Task)
// .createQueryBuilder('task')
// // .innerJoinAndSelect('task.executors', 'user')
// .innerJoinAndSelect('task.executors AND task.author', 'user')
// .where('task.project IS NULL')
// .getMany()
// return res.send({tasks})
// }
// ///ssome code
// return res.send({message:"some other staff"})
// })
export default router;
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