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