Merge branch 'task-25-feature-fix/fix_routers_users_tasks_projects' into 'development'

Task 25 feature fix/fix routers users tasks projects

See merge request !9
parents b2be2b7b 493dcab0
...@@ -6,7 +6,8 @@ import { ...@@ -6,7 +6,8 @@ import {
BaseEntity, BaseEntity,
ManyToOne, ManyToOne,
ManyToMany, ManyToMany,
OneToMany OneToMany,
JoinTable
} from 'typeorm'; } from 'typeorm';
import {User} from './User'; import {User} from './User';
import {Task} from './Task'; import {Task} from './Task';
...@@ -59,14 +60,15 @@ import { ...@@ -59,14 +60,15 @@ import {
dateDue!: Date| null; dateDue!: Date| null;
@Column({ name: 'department', type: Boolean,nullable:true ,default: false}) @Column({ name: 'department', type: Boolean,nullable:true ,default: false})
department!: boolean | false; department!: boolean;
@ManyToOne(() => User, (user: { projects: Project[]; }) => user.projects) @ManyToOne(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true})
admin!: User; admin!: User;
@ManyToMany(() => User, (user: { projects: Project[]; }) => user.projects) @OneToMany(() => Task, (task: { project: Project; })=>task.project,{eager : true})
workers!: User[];
@OneToMany(() => Task, (task: { project: Project; })=>task.project)
tasks!:Task[]|null; tasks!:Task[]|null;
@ManyToMany(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true})
@JoinTable()
workers!: User[];
} }
\ No newline at end of file
...@@ -5,7 +5,8 @@ import { ...@@ -5,7 +5,8 @@ import {
CreateDateColumn, CreateDateColumn,
BaseEntity, BaseEntity,
ManyToOne, ManyToOne,
ManyToMany ManyToMany,
JoinTable
} from 'typeorm'; } from 'typeorm';
import {User} from './User'; import {User} from './User';
import {Project} from './Project'; import {Project} from './Project';
...@@ -17,12 +18,12 @@ import { ...@@ -17,12 +18,12 @@ import {
title: string; title: string;
description: string; description: string;
createdAt: Date; createdAt: Date;
// dateTimeStart:Date| null; dateTimeStart:Date| null;
// dateTimeDue:Date| null; dateTimeDue:Date| null;
assignedTo: User[];
accomplish: taskFinishType; accomplish: taskFinishType;
author: User; author: User;
project:Project|null; project:Project|null;
executors:User[]
} }
@Entity({ name: 'Task' }) @Entity({ name: 'Task' })
...@@ -35,10 +36,10 @@ import { ...@@ -35,10 +36,10 @@ import {
description!: string description!: string
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() }) @CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt!: Date; createdAt!: Date;
// @CreateDateColumn({ name: 'dateTimeStart', type: Date,nullable: true }) @Column({ name: 'dateTimeStart', type: Date,nullable: true })
// dateTimeStart!: Date | null; dateTimeStart!: Date | null;
// @CreateDateColumn({ name: 'dateTimeDue', type: Date,nullable: true }) @Column({ name: 'dateTimeDue', type: Date,nullable: true })
// dateTimeDue!: Date | null; dateTimeDue!: Date | null;
@Column({ @Column({
type: "enum", type: "enum",
enum: ["opened", "done" , "failed"], enum: ["opened", "done" , "failed"],
...@@ -47,12 +48,16 @@ import { ...@@ -47,12 +48,16 @@ import {
accomplish!: taskFinishType accomplish!: taskFinishType
@ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks) @ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks,{eager : true})
author!: User; author!: User;
@ManyToMany(() => User, (user: { tasks: Task[]; }) => user.tasks) @ManyToMany(() => User,{eager : true})
assignedTo!: User[]; @JoinTable()
executors!: User[];
@ManyToOne(()=>Project,(project:{tasks: Task[]}) => project.tasks) @ManyToOne(()=>Project,(project:{tasks: Task[]}) => project.tasks)
project!: Project | null; project!: Project | null;
} }
...@@ -6,7 +6,8 @@ import { ...@@ -6,7 +6,8 @@ import {
BeforeInsert, BeforeInsert,
BaseEntity, BaseEntity,
ManyToMany, ManyToMany,
OneToMany OneToMany,
JoinTable
} from 'typeorm'; } from 'typeorm';
import {IsEmail import {IsEmail
...@@ -31,7 +32,9 @@ interface IUser { ...@@ -31,7 +32,9 @@ interface IUser {
token: string; token: string;
role: userRoleType; role: userRoleType;
createdAt: Date; createdAt: Date;
createdTasks:Task[]| null; createdTasks:Task[];
workerInProjects:Project[];
adminInProjects:Project[];
} }
...@@ -77,13 +80,14 @@ export class User extends BaseEntity implements IUser { ...@@ -77,13 +80,14 @@ export class User extends BaseEntity implements IUser {
createdTasks!: Task[]; createdTasks!: Task[];
@ManyToMany(() => Task, (task: { users: User[] }) =>task.users) @ManyToMany(() => Task, (task: { users: User[] }) =>task.users)
tasksToMake!: Task[]; tasks!: Task[];
@OneToMany(() => Project, (project: { user: User }) => project.user) @OneToMany(() => Project, (project: { user: User }) => project.user)
adminInProjects!: Project[]; adminInProjects!: Project[];
@ManyToMany(() => Project, (project: { users: User[] }) =>project.users) @ManyToMany(() => Project,(project: { user: User }) => project.user)
@JoinTable()
workerInProjects!: Project[]; workerInProjects!: Project[];
......
...@@ -12,22 +12,16 @@ router.get('/',async (req:Request, res:Response): Promise<Response>=> { ...@@ -12,22 +12,16 @@ router.get('/',async (req:Request, res:Response): Promise<Response>=> {
return res.send({projects}) return res.send({projects})
}) })
router.get("/:project_id",async (req:Request, res:Response): Promise<Response> => {
const project : Project|null= await dataSource.manager.findOneBy(Project, {
id: req.params.project_id
})
if (!project) return res.status(404).send({Message:'no info in the base'})
return res.send({project})
})
router.post('/', async (req:Request, res:Response): Promise<Response> => { router.post('/', async (req:Request, res:Response): Promise<Response> => {
if (!req.body) return res.status(400).send({Message:'problem in incoming req.body'}) if (!req.body) return res.status(400).send({Message:'problem in incoming req.body'})
const {title, dateDue,color,department, userId,workers,tasks}= req.body; const token = req.get('Authorization');
const {title, dateDue,color,department,workers,tasks}= req.body;
const user = await dataSource const user = await dataSource
.createQueryBuilder() .createQueryBuilder()
.select("user") .select("user")
.from(User, "user") .from(User, "user")
.where("user.id = :id", { id: userId }) .where("user.token = :token", { token: token })
.getOne() .getOne()
if(!user) return res.status(404).send({Message:'user not found'}) if(!user) return res.status(404).send({Message:'user not found'})
const project : Project= new Project() const project : Project= new Project()
...@@ -42,22 +36,34 @@ router.post('/', async (req:Request, res:Response): Promise<Response> => { ...@@ -42,22 +36,34 @@ router.post('/', async (req:Request, res:Response): Promise<Response> => {
return res.send({project}) return res.send({project})
}) })
router.get("/id/:id",async (req:Request, res:Response): Promise<Response> => {
const project = await dataSource
.createQueryBuilder()
.select("project")
.from(Project, "project")
.leftJoinAndSelect('project.admin', '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})
})
// router.get('/:userId', async (req : Request, res : Response): Promise<Response>=>{ router.get('/userId/:userId', async (req : Request, res : Response): Promise<Response>=>{
// const userId:string = req.params.userId const userId:string = req.params.userId
// const user = await dataSource const user = await dataSource
// .createQueryBuilder() .createQueryBuilder()
// .select("user") .select("user")
// .from(User, "user") .from(User, "user")
// .where("user.id = :id", { id: userId }) .where("user.id = :id", { id: userId })
// .getOne() .getOne()
// console.log('user ', user) console.log('user ', user)
// const userProjects = await dataSource const userProjects = await dataSource
// .createQueryBuilder() .createQueryBuilder()
// .select('project') .select('project')
// .from(Project, "project") .from(Project, "project")
// .where("Project_adminId = :id", { id: userId }) .where("project.admin = :id", { id: userId })
// return res.send({userProjects}) .getMany();
// }) return res.send({userProjects})
})
export default router; export default router;
import express,{Router, Request, Response} from 'express'; import express,{Router, Request, Response} from 'express';
// import {User} from '../models/User';
// import {Project} from '../models/Project';
import {Task} from '../models/Task'; import {Task} from '../models/Task';
import {myDataSource} from '../app-data-source'; import {myDataSource} from '../app-data-source';
// import { nanoid } from 'nanoid'; import { User } from '../models/User';
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
router.get('/', async(req:Request, res:Response):Promise<Response> => { router.get('/', async(req:Request, res:Response):Promise<Response> => {
const tasks = await dataSource.manager.find(Task) const tasks = await dataSource
.getRepository(Task)
.find()
return res.send({tasks}) return res.send({tasks})
}) })
export default router; export default router;
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} = req.body;
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'})
newTask.title = title;
newTask.description = description;
newTask.project = project;
newTask.executors = executors;
newTask.dateTimeDue = dateTimeDue;
newTask.dateTimeStart = dateTimeStart;
newTask.author= user;
await newTask.save();
return res.send({newTask})
})
router.get('/userId/:userId', async (req: Request, res: 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})
})
router.get('/my', async (req: Request, res: 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 tasks = await dataSource
.getRepository(Task)
.createQueryBuilder('task')
.innerJoinAndSelect('task.executors', 'user')
.where('user.id = :userId', {userId :user.id})
.getMany()
return res.send({tasks})
})
\ No newline at end of file
...@@ -7,7 +7,10 @@ const router:Router = express.Router(); ...@@ -7,7 +7,10 @@ const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
router.get('/', async (req : Request, res : Response):Promise<object> => { router.get('/', async (req : Request, res : Response):Promise<object> => {
const users = await dataSource.manager.find(User) const users = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.getMany()
return res.send({users}) return res.send({users})
}) })
......
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