#68 refacor create and change of task

parent 2f75a546
import express, { NextFunction, Request, Response, Router } from "express";
import { myDataSource } from "./app-data-source";
import { Task } from "./models/Task";
import { User } from "./models/User";
const router:Router = express.Router();
const dataSource = myDataSource;
/** Check if user with given token exists , return user */
......@@ -25,8 +25,8 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next
const token = req.get('Authorization');
const {taskId} = req.body
if(!token) return res.status(401).send({Message:'token not exists'})
req.body={...req.body,executor:false}
req.body={...req.body,author:false}
req.body={...req.body,executorStatus:false}
req.body={...req.body,authorStatus:false}
const executor = await dataSource
.createQueryBuilder()
......@@ -37,7 +37,7 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next
.getOne();
console.log('executor', executor)
if (executor) {
req.body={...req.body,executor:executor}
req.body={...req.body,executorStatus:true}
}
const author = await dataSource
.createQueryBuilder()
......@@ -48,8 +48,25 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next
.getOne();
console.log('author', author)
if (author) {
req.body={...req.body,author:author}
req.body={...req.body,authorStatus:true}
}
if(!author && !executor)return res.status(401).send({Message:'user is not authorized'})
next()
};
/**task finder by id, return one task */
export const taskFinderById = async (taskId:string):Promise<null | Task>=>{
const task = await dataSource
.getRepository(Task)
.findOne({
relations:{
executor:true,
author:true,
dateTimeTasks:true
},
where:{
id:taskId
}
})
return task
}
\ No newline at end of file
......@@ -20,6 +20,7 @@ import { DateTimeTask } from './DateTimeTask';
id: string;
title: string;
description: string;
note: string;
createdAt: Date;
dateTimeStart:Date| null;
dateTimeDue:Date| null;
......@@ -27,6 +28,7 @@ import { DateTimeTask } from './DateTimeTask';
dateTimeFactDeadLine: Date| null;
accomplish: taskFinishType;
priority: priorityType | null;
archive:boolean,
author: User;
project:Project|null;
dateTimeTasks:DateTimeTask[]|null;
......@@ -41,6 +43,8 @@ import { DateTimeTask } from './DateTimeTask';
title!: string
@Column({ name: 'description', type: 'varchar', length:50,nullable: true })
description!: string
@Column({ name: 'note', type: 'varchar', length:100,nullable: true })
note!: string
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt!: Date;
@Column({ name: 'dateTimeStart', type: Date,nullable: true })
......@@ -51,6 +55,8 @@ import { DateTimeTask } from './DateTimeTask';
dateTimeDeadLine!: Date;
@Column({ name: 'dateTimeFactDeadLine', type: Date,nullable: true })
dateTimeFactDeadLine!: Date;
@Column({ name: 'archive', type: 'varchar', length:50,nullable: false, default:false })
archive!: boolean
@Column({
type: "enum",
......
......@@ -29,8 +29,8 @@ const taskFinderById = async (taskId:string):Promise<null | Task>=>{
/** make copy of task in calendar view */
router.post("/make-copy",authAuthorOrExecutorOfTask, async(req:Request, res:Response):Promise<Response>=>{
const {executor,taskId,start, due} = req.body
if (executor){
const {executorStatus,taskId,start, due} = req.body
if (executorStatus){
const newDateTimeTask = new DateTimeTask();
newDateTimeTask.dateTimeStart = start
newDateTimeTask.dateTimeDue = due
......@@ -44,8 +44,8 @@ router.post("/make-copy",authAuthorOrExecutorOfTask, async(req:Request, res:Resp
/** change date time of copy of task in calendar view */
router.put("change-copy", authAuthorOrExecutorOfTask, async(req:Request, res: Response):Promise<Response>=>{
const {executor,dateTimeTaskId,taskId, start, due} = req.body
if (executor){
const {executorStatus,dateTimeTaskId,taskId, start, due} = req.body
if (executorStatus){
const dateTimeTask = await dataSource
.createQueryBuilder()
.select('dateTikeTask')
......
......@@ -5,7 +5,7 @@ import { User } from '../models/User';
import { Member } from '../models/Member';
import { In } from 'typeorm';
import { DateTimeTask } from '../models/DateTimeTask';
import { auth } from '../helpers';
import { auth, authAuthorOrExecutorOfTask, taskFinderById } from '../helpers';
const router:Router = express.Router();
const dataSource = myDataSource;
......@@ -25,24 +25,14 @@ router.get('/', async(req:Request, res:Response):Promise<Response> => {
/**create new task */
router.post('/', auth, async(req:Request, res:Response):Promise<Response>=>{
const {user,title,description,project,dateTimeTaskArray, dateTimeDeadLine,priority} = req.body;
const {user,title,description,project,executor, dateTimeDeadLine,priority} = req.body;
const newTask = new Task();
const dateTimeArray=[];
if(dateTimeTaskArray && dateTimeTaskArray.length>0){
for (let dateTimeStartDue of dateTimeTaskArray){
const newDateTimeTask = new DateTimeTask();
newDateTimeTask.dateTimeStart = dateTimeStartDue.dateTimeStart;
newDateTimeTask.dateTimeDue = dateTimeStartDue.dateTimeDue;
await newDateTimeTask.save();
dateTimeArray.push(newDateTimeTask);
}
newTask.dateTimeTasks=dateTimeArray;
}
newTask.title = title;
newTask.description = description;
newTask.project = project;
newTask.dateTimeDeadLine=dateTimeDeadLine;
newTask.author= user;
newTask.executor= executor;
newTask.priority = priority;
await newTask.save();
return res.send({newTask});
......@@ -78,7 +68,6 @@ 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',auth, async (req: Request, res: Response):Promise<Response>=>{
const token = req.get('Authorization');
const user = req.body.user
const tasks = await dataSource
.getRepository(Task)
......@@ -165,34 +154,40 @@ router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>
/**change of task by task id */
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
router.put('/',authAuthorOrExecutorOfTask,async(req:Request, res:Response)=> {
const {authorStatus,executorStatus,taskId,title,description,note, archive,project,dateTimeTaskId,start,due,executor,accomplish,dateTimeDeadLine, dateTimeFactDeadLine,priority} = req.body;
const task = await taskFinderById(taskId)
if (!task) return res.status(404).send({Message:'task not found'})
let dateTimeTask = null;
if (dateTimeTaskId) {
const dateTimeTaskData = await dataSource
.createQueryBuilder()
.select("task")
.from(Task, "task")
.where("task.id = :id", { id })
.select("dateTimeTask")
.from(DateTimeTask, "dateTimeTask")
.where("dateTimeTask,id=:dateTimeTaskId",{dateTimeTaskId})
.getOne()
if (!task) return res.status(404).send({Message:'task not found'})
const dateTimeArray=[];
if(dateTimeTaskArray && dateTimeTaskArray.length>0){
for (let dateTimeStartDue of dateTimeTaskArray){
const newDateTimeTask = new DateTimeTask();
newDateTimeTask.dateTimeStart = dateTimeStartDue.dateTimeStart;
newDateTimeTask.dateTimeDue = dateTimeStartDue.dateTimeDue;
await newDateTimeTask.save();
dateTimeArray.push(newDateTimeTask);
}
task.dateTimeTasks=dateTimeArray;
if (!dateTimeTask) return res.status(404).send({Message:'dateTimeTask not found'})
dateTimeTask = dateTimeTaskData
}
task.title= title
if (authorStatus){
task.title = title
task.description= description
task.project= project
task.archive=archive
task.dateTimeDeadLine=dateTimeDeadLine;
task.author=user
task.project=project
task.executor=executor
task.accomplish= accomplish
task.priority= priority
}
if(executorStatus && dateTimeTask!==null){
dateTimeTask.dateTimeStart = start
dateTimeTask.dateTimeDue = due
await dateTimeTask.save()
task.note = note
task.dateTimeFactDeadLine= dateTimeFactDeadLine
}
task.accomplish= accomplish
await task.save()
res.send({message:'update task successfully'})
})
......
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