#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"; ...@@ -4,6 +4,7 @@ import { User, UserRole } from "./models/User";
import { faker } from '@faker-js/faker'; import { faker } from '@faker-js/faker';
import { Task } from "./models/Task"; import { Task } from "./models/Task";
import { Project } from "./models/Project"; import { Project } from "./models/Project";
import { Member, MemberRole } from "./models/Member";
function randomIntFromInterval(min:number, max:number) { function randomIntFromInterval(min:number, max:number) {
return Math.floor(Math.random() * (max - min + 1) + min) return Math.floor(Math.random() * (max - min + 1) + min)
...@@ -50,7 +51,7 @@ const loadFixtures = async () => { ...@@ -50,7 +51,7 @@ const loadFixtures = async () => {
const newTask = new Task(); const newTask = new Task();
newTask.title = `Buy ${faker.commerce.productName()}`; newTask.title = `Buy ${faker.commerce.productName()}`;
newTask.description = faker.random.words(4); 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.dateTimeDue = faker.date.soon(randomIntFromInterval(1, 15));
newTask.dateTimeStart = faker.date.recent((randomIntFromInterval(0, 8))); newTask.dateTimeStart = faker.date.recent((randomIntFromInterval(0, 8)));
newTask.author = faker.helpers.arrayElement(users); newTask.author = faker.helpers.arrayElement(users);
...@@ -62,7 +63,7 @@ const loadFixtures = async () => { ...@@ -62,7 +63,7 @@ const loadFixtures = async () => {
const newTask = new Task(); const newTask = new Task();
newTask.title = `Buy ${faker.commerce.productName()}`; newTask.title = `Buy ${faker.commerce.productName()}`;
newTask.description = faker.random.words(4); 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.dateTimeDue = null;
newTask.dateTimeStart = null; newTask.dateTimeStart = null;
newTask.author = faker.helpers.arrayElement(users); newTask.author = faker.helpers.arrayElement(users);
...@@ -73,14 +74,24 @@ const loadFixtures = async () => { ...@@ -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[] = [] const projects:Project[] = []
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
const newProject = new Project(); const newProject = new Project();
newProject.title = `Project ${faker.random.words(1)}`; newProject.title = `Project ${faker.random.words(1)}`;
newProject.color = 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.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(); await newProject.save();
projects.push(newProject) projects.push(newProject)
} }
......
...@@ -37,7 +37,7 @@ import { ...@@ -37,7 +37,7 @@ import {
@ManyToOne(() => User, (user: { members: Member[]; }) => user.members,{cascade: true, onUpdate:'CASCADE'}) @ManyToOne(() => User, (user: { members: Member[]; }) => user.members,{cascade: true, onUpdate:'CASCADE'})
user!: User; 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; project!: Project;
@Column({ @Column({
......
...@@ -5,7 +5,7 @@ import { ...@@ -5,7 +5,7 @@ import {
CreateDateColumn, CreateDateColumn,
BaseEntity, BaseEntity,
ManyToOne, ManyToOne,
ManyToMany, OneToOne,
JoinTable JoinTable
} from 'typeorm'; } from 'typeorm';
import {User} from './User'; import {User} from './User';
...@@ -25,7 +25,7 @@ import { ...@@ -25,7 +25,7 @@ import {
priority: priorityType | null; priority: priorityType | null;
author: User; author: User;
project:Project|null; project:Project|null;
executors:User[]; executor:User;
} }
@Entity({ name: 'Task' }) @Entity({ name: 'Task' })
...@@ -64,9 +64,9 @@ import { ...@@ -64,9 +64,9 @@ import {
@ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks,{eager : true}) @ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks,{eager : true})
author!: User; author!: User;
@ManyToMany(() => User,{eager : true, cascade: true, onUpdate:'CASCADE'}) @ManyToOne(() =>User, (user: { tasks: Task[]}) => user.tasks,{eager : true,nullable: true, onUpdate:'CASCADE'})
@JoinTable() @JoinTable()
executors!: User[]; executor!: User;
@ManyToOne(()=>Project,(project:{tasks: Task[]}) => project.tasks,{eager : true,nullable: true,onUpdate:'CASCADE'}) @ManyToOne(()=>Project,(project:{tasks: Task[]}) => project.tasks,{eager : true,nullable: true,onUpdate:'CASCADE'})
project!: Project; project!: Project;
......
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
ManyToMany, ManyToMany,
OneToMany, OneToMany,
JoinTable, JoinTable,
OneToOne,
} from 'typeorm'; } from 'typeorm';
import {IsEmail import {IsEmail
} from "class-validator"; } from "class-validator";
...@@ -82,7 +83,7 @@ export class User extends BaseEntity implements IUser { ...@@ -82,7 +83,7 @@ export class User extends BaseEntity implements IUser {
@OneToMany(() => Task, (task: { user: User }) => task.user) @OneToMany(() => Task, (task: { user: User }) => task.user)
createdTasks!: Task[]; createdTasks!: Task[];
@ManyToMany(() => Task, (task: { users: User[] }) =>task.users) @OneToMany(() => Task, (task: { user: User }) =>task.user)
tasks!: Task[]; tasks!: Task[];
......
...@@ -29,10 +29,10 @@ router.post('/', async (req:Request, res:Response): Promise<Response> => { ...@@ -29,10 +29,10 @@ router.post('/', async (req:Request, res:Response): Promise<Response> => {
project.title = title; project.title = title;
project.color = color; project.color = color;
project.dateDue = dateDue || null; project.dateDue = dateDue || null;
project.department = department; // project.department = department;
project.workers = workers|| null; // project.workers = workers|| null;
project.tasks = tasks || null; project.tasks = tasks || null;
project.admin = user; // project.admin = user;
await project.save() await project.save()
return res.send({project}) return res.send({project})
}) })
......
...@@ -3,6 +3,7 @@ import {Task} from '../models/Task'; ...@@ -3,6 +3,7 @@ import {Task} from '../models/Task';
import {myDataSource} from '../app-data-source'; import {myDataSource} from '../app-data-source';
import { User } from '../models/User'; import { User } from '../models/User';
import { Project } from '../models/Project'; import { Project } from '../models/Project';
import { Member } from '../models/Member';
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
...@@ -28,7 +29,7 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{ ...@@ -28,7 +29,7 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
newTask.title = title; newTask.title = title;
newTask.description = description; newTask.description = description;
newTask.project = project; newTask.project = project;
newTask.executors = executors; // newTask.executors = executors;
newTask.dateTimeDue = dateTimeDue; newTask.dateTimeDue = dateTimeDue;
newTask.dateTimeStart = dateTimeStart; newTask.dateTimeStart = dateTimeStart;
newTask.author= user; newTask.author= user;
...@@ -38,13 +39,23 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{ ...@@ -38,13 +39,23 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
return res.send({newTask}) 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 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 const tasks = await dataSource
.getRepository(Task) .getRepository(Member)
.createQueryBuilder('task') .createQueryBuilder('member')
.innerJoinAndSelect('task.executors', 'user') .innerJoinAndSelect('member.project','project')
.where('user.id = :userId', {userId :userId}) .innerJoinAndSelect('project.tasks','task')
.where('member.user.id = :userId')
.andWhere('member.project.id= task.project.id')
.select('task.*')
.getMany() .getMany()
return res.send({tasks}) return res.send({tasks})
}) })
...@@ -104,7 +115,7 @@ router.put('/',async(req:Request, res:Response)=> { ...@@ -104,7 +115,7 @@ router.put('/',async(req:Request, res:Response)=> {
task.dateTimeDue= dateTimeDue task.dateTimeDue= dateTimeDue
task.dateTimeStart= dateTimeStart task.dateTimeStart= dateTimeStart
task.author=user task.author=user
task.executors=executors // task.executors=executors
task.accomplish= accomplish task.accomplish= accomplish
task.priority= priority task.priority= priority
await task.save() await task.save()
...@@ -121,7 +132,7 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{ ...@@ -121,7 +132,7 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
.getRepository(Task) .getRepository(Task)
.find({ .find({
relations:{ relations:{
executors:true, // executors:true,
author:true, author:true,
project: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