#68 refactor of code routers/tasks&projects

parent 6802b4ab
import express, { NextFunction, Request, Response, Router } from "express";
import { myDataSource } from "./app-data-source";
import { User } from "./models/User";
const router:Router = express.Router();
const dataSource = myDataSource;
export const auth = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<any, Record<string, any>>>=>{
const token = req.get('Authorization');
if(!token) return res.status(401).send({Message:'token not exists'})
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'})
req.body={...req.body,user:user}
next()
};
\ No newline at end of file
......@@ -35,8 +35,6 @@ interface IUser {
token: string;
createdAt: Date;
createdTasks:Task[];
// workerInProjects:Project[];
// adminInProjects:Project[];
members: Member[];
}
......
......@@ -5,6 +5,7 @@ import { User } from '../models/User';
import { Member, MemberRole } from '../models/Member';
import { userInfo } from 'os';
import { Task } from '../models/Task';
import { auth } from '../helpers';
const router:Router = express.Router();
const dataSource = myDataSource;
......@@ -14,18 +15,9 @@ router.get('/',async (req:Request, res:Response): Promise<Response>=> {
const projects:Project[] = await dataSource.manager.find(Project)
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'})
/**get projects were user is member, by user token */
router.get('/my',auth, async (req:Request, res:Response): Promise<Response>=> {
const user = req.body.user
const rawProjects = await dataSource
.createQueryBuilder()
.from(Project, "project")
......@@ -34,9 +26,6 @@ router.get('/my',async (req:Request, res:Response): Promise<Response>=> {
.loadRelationCountAndMap('project.tasks', 'project.tasks')
.leftJoinAndSelect('member.user', 'user' )
.where('member.userId = :userId',{userId:user.id})
// .where("project.id = :id", { id: req.params.id })
// .select(["*","members"])
// .select("*")
.getMany()
const projectIds = []
if (rawProjects.length>0){
......@@ -45,7 +34,6 @@ router.get('/my',async (req:Request, res:Response): Promise<Response>=> {
)
}
}
console.log('projectIds', projectIds)
const projects = await dataSource
.createQueryBuilder()
.from(Project, "project")
......@@ -55,22 +43,14 @@ router.get('/my',async (req:Request, res:Response): Promise<Response>=> {
.leftJoinAndSelect('member.user', 'user')
.where('project.id IN(:...projectIds)', {projectIds})
.getMany()
// const projects:Project[] = await dataSource.manager.find(Project)
return res.send({projects})
})
router.post('/', async (req:Request, res:Response): Promise<Response> => {
/**create new project */
router.post('/',auth, 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');
const {title,color}= 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'})
const {user, title,color}= req.body;
const member:Member = new Member;
member.user= user;
member.roleProject= MemberRole.ADMIN;
......@@ -82,7 +62,7 @@ router.post('/', async (req:Request, res:Response): Promise<Response> => {
await project.save()
return res.send({project})
})
/**get project with all FK & tasks with all FK, by project ID */
router.get("/:id",async (req:Request, res:Response): Promise<Response> => {
const project = await dataSource
.createQueryBuilder()
......@@ -110,7 +90,7 @@ router.get("/:id",async (req:Request, res:Response): Promise<Response> => {
return res.send({project, tasks})
})
/** Delete project by project ID*/
router.delete('/:projectId',async (req: Request, res: Response):Promise<Response>=>{
const projectId = req.params.projectId;
await myDataSource
......@@ -123,6 +103,7 @@ router.delete('/:projectId',async (req: Request, res: Response):Promise<Response
})
/** Get projects were user is admin, by user ID*/
router.get('/user/:userId', async (req : Request, res : Response): Promise<Response>=>{
const userId:string = req.params.userId
......@@ -157,11 +138,8 @@ router.post('/add-user/', async (req: Request, res: Response):Promise<Response>=
/** Remove user from specific project */
router.post('/remove-user', async (req: Request, res: Response):Promise<Response>=> {
console.log('in delete user')
const token = req.get('Authorization');
console.log('token ', token)
const {userId, projectId} = req.body;
console.log('req.body', req.body )
const adminOfProject = await dataSource
.createQueryBuilder()
.select("user")
......@@ -175,9 +153,7 @@ router.post('/remove-user', async (req: Request, res: Response):Promise<Response
if (!adminOfProject){
return res.send({message:'User is not authorized'})
}
// return res.send({adminOfProject})
console.log('adminOfProject', adminOfProject)
try{
await dataSource
.createQueryBuilder()
......
import express,{Router, Request, Response} from 'express';
import express,{Router, Request, Response,NextFunction } from 'express';
import {Task} from '../models/Task';
import {myDataSource} from '../app-data-source';
import { User } from '../models/User';
import { Member } from '../models/Member';
import { In } from 'typeorm';
import { DateTimeTask } from '../models/DateTimeTask';
import { auth } from '../helpers';
const router:Router = express.Router();
const dataSource = myDataSource;
/**get all tasks */
router.get('/', async(req:Request, res:Response):Promise<Response> => {
const tasks = await dataSource
......@@ -21,17 +25,9 @@ router.get('/', async(req:Request, res:Response):Promise<Response> => {
})
/**create new task */
router.post('/', async(req:Request, res:Response):Promise<Response>=>{
const token = req.get('Authorization');
router.post('/', auth, async(req:Request, res:Response):Promise<Response>=>{
const {user,title,description,project,dateTimeTaskArray, dateTimeDeadLine,priority} = req.body;
const newTask = new Task();
const {title,description,project,dateTimeTaskArray, dateTimeDeadLine,accomplish,priority} = 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'})
const dateTimeArray=[];
if(dateTimeTaskArray && dateTimeTaskArray.length>0){
for (let dateTimeStartDue of dateTimeTaskArray){
......@@ -41,16 +37,15 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
await newDateTimeTask.save();
dateTimeArray.push(newDateTimeTask);
}
newTask.dateTimeTasks=dateTimeArray;
}
newTask.title = title;
newTask.description = description;
newTask.project = project;
newTask.dateTimeTasks=dateTimeArray;
newTask.dateTimeDeadLine=dateTimeDeadLine;
newTask.author= user;
newTask.accomplish = accomplish;
newTask.priority = priority;
await newTask.save();
await newTask.save();
return res.send({newTask});
})
......@@ -81,15 +76,9 @@ router.get('/user/:userId', async (req: Request, res: Response):Promise<Response
})
/**check tasks of current user where he is author or executor, search by id*/
router.get('/my', async (req: Request, res: Response):Promise<Response>=>{
router.get('/my',auth, 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 user = req.body.user
const tasks = await dataSource
.getRepository(Task)
.find({
......@@ -114,16 +103,8 @@ router.get('/my', async (req: Request, res: Response):Promise<Response>=>{
})
/**check tasks of projects in which current user is involved, search by token*/
router.get('/related', 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'})
router.get('/related', auth,async (req: Request, res: Response):Promise<Response>=>{
const user = req.body.user
const rawMembership = await dataSource
.createQueryBuilder()
.select("member")
......@@ -182,16 +163,9 @@ router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>
/**modification of task by task id */
router.put('/',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 {id,title,description,project,dateTimeTaskArray,executor,accomplish,dateTimeDeadLine,priority} = req.body;
router.put('/',auth,async(req:Request, res:Response)=> {
const {user,id,title,description,project,dateTimeTaskArray,executor,accomplish,dateTimeDeadLine,priority} = req.body;
const task = await dataSource
.createQueryBuilder()
.select("task")
......@@ -208,11 +182,11 @@ router.put('/',async(req:Request, res:Response)=> {
await newDateTimeTask.save();
dateTimeArray.push(newDateTimeTask);
}
task.dateTimeTasks=dateTimeArray;
}
task.title= title
task.description= description
task.project= project
task.dateTimeTasks=dateTimeArray;
task.dateTimeDeadLine=dateTimeDeadLine;
task.author=user
task.executor=executor
......@@ -222,6 +196,14 @@ router.put('/',async(req:Request, res:Response)=> {
res.send({message:'update task successfully'})
})
/**create copy of task */
router.post('make-copy', async (req: Request, res: Response)=>{
})
/** search all tasks on given projects: projectId[] */
router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
let projectArray :string[]= req.body;
......
......@@ -50,7 +50,7 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response):
email: user.email
})
return res.send({userToFront})
})
})
router.post('/sessions/', async (req : Request, res : Response):Promise<object> => {
......
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