#58 added fixtures members and fix relations in models, remove related fields in routers

parent ac604ae7
......@@ -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)
......@@ -50,7 +51,7 @@ 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.author = faker.helpers.arrayElement(users);
......@@ -62,7 +63,7 @@ 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 = null;
newTask.dateTimeStart = null;
newTask.author = faker.helpers.arrayElement(users);
......@@ -73,14 +74,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.role=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)
}
......
......@@ -37,7 +37,7 @@ import {
@ManyToOne(() => User, (user: { members: Member[]; }) => user.members,{cascade: true, onUpdate:'CASCADE'})
user!: User;
@ManyToOne(() => Project, (project: { members: Member[]; }) => project.members,{cascade: true, onUpdate:'CASCADE'})
@ManyToOne(() => Project, (project: { members: Member[]; }) => project.members,{cascade: true, onUpdate:'CASCADE',nullable:true})
project!: Project;
@Column({
......
......@@ -5,7 +5,7 @@ import {
CreateDateColumn,
BaseEntity,
ManyToOne,
ManyToMany,
OneToOne,
JoinTable
} from 'typeorm';
import {User} from './User';
......@@ -25,7 +25,7 @@ import {
priority: priorityType | null;
author: User;
project:Project|null;
executors:User[];
executor:User;
}
@Entity({ name: 'Task' })
......@@ -64,9 +64,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";
......@@ -82,7 +83,7 @@ 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[];
......
......@@ -29,10 +29,10 @@ router.post('/', async (req:Request, res:Response): Promise<Response> => {
project.title = title;
project.color = color;
project.dateDue = dateDue || null;
project.department = department;
project.workers = workers|| null;
// project.department = department;
// project.workers = workers|| null;
project.tasks = tasks || null;
project.admin = user;
// project.admin = user;
await project.save()
return res.send({project})
})
......
......@@ -3,6 +3,7 @@ import {Task} from '../models/Task';
import {myDataSource} from '../app-data-source';
import { User } from '../models/User';
import { Project } from '../models/Project';
import { Member } from '../models/Member';
const router:Router = express.Router();
const dataSource = myDataSource;
......@@ -28,7 +29,7 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
newTask.title = title;
newTask.description = description;
newTask.project = project;
newTask.executors = executors;
// newTask.executors = executors;
newTask.dateTimeDue = dateTimeDue;
newTask.dateTimeStart = dateTimeStart;
newTask.author= user;
......@@ -38,13 +39,23 @@ 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()
// return res.send({tasks})
const tasks = await dataSource
.getRepository(Task)
.createQueryBuilder('task')
.innerJoinAndSelect('task.executors', 'user')
.where('user.id = :userId', {userId :userId})
.getRepository(Member)
.createQueryBuilder('member')
.innerJoinAndSelect('member.project','project')
.innerJoinAndSelect('project.tasks','task')
.where('member.user.id = :userId')
.andWhere('member.project.id= task.project.id')
.select('task.*')
.getMany()
return res.send({tasks})
})
......@@ -104,7 +115,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 +132,7 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
.getRepository(Task)
.find({
relations:{
executors:true,
// executors:true,
author:true,
project:true
}
......
This diff is collapsed.
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