Merge branch 'task-95-enhance/crud_rights_author_executor' into 'development'

Task 95 enhance/crud rights author executor

See merge request !73
parents d4298065 5b9d18b9
...@@ -45,6 +45,7 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next ...@@ -45,6 +45,7 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next
relations:{ relations:{
executor:true, executor:true,
author:true, author:true,
dateTimeTasks: true,
}, },
where:[ where:[
{ {
...@@ -61,6 +62,7 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next ...@@ -61,6 +62,7 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next
}, },
]}) ]})
if (!task) return res.status(404).send({message:'task with possible user involved is not found'}) if (!task) return res.status(404).send({message:'task with possible user involved is not found'})
req.body={...req.body, task:task}
if(task?.executor?.token === token) { if(task?.executor?.token === token) {
req.body={...req.body,executorStatus:true} req.body={...req.body,executorStatus:true}
} }
...@@ -83,23 +85,30 @@ export const authAuthorOrExecutorOfDateTimeTask = async(req: Request,res: Respon ...@@ -83,23 +85,30 @@ export const authAuthorOrExecutorOfDateTimeTask = async(req: Request,res: Respon
} else if (req.params?.dateTimeTaskId){ } else if (req.params?.dateTimeTaskId){
dateTimeTaskId = req.params.dateTimeTaskId dateTimeTaskId = req.params.dateTimeTaskId
} else return res.send({vessage:"there are no dateTimeTaskId found"}) } else return res.send({vessage:"there are no dateTimeTaskId found"})
const task = await dataSource const task =await dataSource
.createQueryBuilder() .getRepository(Task)
.select(["task"]) .findOne({
.from(Task,"task") relations:
.leftJoinAndSelect("task.executor","user") {
.leftJoinAndSelect("task.dateTimeTasks","dateTimeTask") dateTimeTasks: true,
.leftJoinAndSelect("task.author","users") executor:true,
.where("dateTimeTask.id = :dateTimeTaskId", {dateTimeTaskId}) author:true,
.getOne() },
where:{
dateTimeTasks:
{
id:dateTimeTaskId
}
}})
if (!task) return res.status(404).send({message:'task with possible user involved is not found'}) if (!task) return res.status(404).send({message:'task with possible user involved is not found'})
req.body={...req.body, task:task}
if(task?.executor?.token === token) { if(task?.executor?.token === token) {
req.body={...req.body,executorStatus:true} req.body={...req.body,executorStatus:true}
} }
if (task?.author?.token === token ) { if (task?.author?.token === token ) {
req.body={...req.body,authorStatus:true} req.body={...req.body,authorStatus:true}
} }
if (req.body.authorStatus ===false && req.body.executorStatus===false) return res.status(403).send({Message:'user is not uathorized'})
next() next()
} }
...@@ -146,6 +155,25 @@ export const taskFinderById = async (taskId:string):Promise<null | Task>=>{ ...@@ -146,6 +155,25 @@ export const taskFinderById = async (taskId:string):Promise<null | Task>=>{
return task return task
} }
/** find task by dateTimeTaskId */
export const taskFinderByDateTimeTaskId = async (dateTimeTaskId:string):Promise<null | Task>=>{
const task =await dataSource
.getRepository(Task)
.findOne({
relations:
{
dateTimeTasks: true
},
where:{
dateTimeTasks:
{
id:dateTimeTaskId
}
}})
return task
}
/**member finder by userId and projectId, return one task */ /**member finder by userId and projectId, return one task */
export const memberFinderById = async (userId:string, projectId:string)=>{ export const memberFinderById = async (userId:string, projectId:string)=>{
...@@ -170,8 +198,6 @@ export const memberFinderById = async (userId:string, projectId:string)=>{ ...@@ -170,8 +198,6 @@ export const memberFinderById = async (userId:string, projectId:string)=>{
} }
export let transporter = nodemailer.createTransport({ export let transporter = nodemailer.createTransport({
service:'Yandex', service:'Yandex',
// host: "smtp.yandex.ru", // host: "smtp.yandex.ru",
...@@ -184,5 +210,6 @@ export let transporter = nodemailer.createTransport({ ...@@ -184,5 +210,6 @@ export let transporter = nodemailer.createTransport({
}) })
export const FRONTEND_URL = 'localhost:3000';
export const FRONTEND_URL = 'localhost:3000'
\ No newline at end of file
import express,{Router, Request, Response,NextFunction } from 'express'; import express,{Router, Request, Response } 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 { Member } from '../models/Member';
import { In } from 'typeorm';
import { DateTimeTask } from '../models/DateTimeTask'; import { DateTimeTask } from '../models/DateTimeTask';
import { auth, authAuthorOrExecutorOfDateTimeTask, authAuthorOrExecutorOfTask } from '../helpers'; import { auth, authAuthorOrExecutorOfDateTimeTask, authAuthorOrExecutorOfTask,taskFinderById } from '../helpers';
import { info } from 'console';
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
/**task finder by id, return one task */
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
}
/** make copy of task in calendar view */ /** make copy of task in calendar view */
router.post("/make-copy", async(req:Request, res:Response):Promise<Response>=>{ router.post("/make-copy",authAuthorOrExecutorOfTask, async(req:Request, res:Response):Promise<Response>=>{
const {taskId, dateTimeDue, dateTimeStart} = req.body const {taskId, dateTimeDue, dateTimeStart} = req.body;
const task = await taskFinderById(taskId);
if(!task) return res.status(401).send({message:'task with possible user involved is not found'})
const newDateTimeTask = new DateTimeTask(); const newDateTimeTask = new DateTimeTask();
newDateTimeTask.dateTimeStart = dateTimeStart newDateTimeTask.dateTimeStart = dateTimeStart;
newDateTimeTask.dateTimeDue = dateTimeDue newDateTimeTask.dateTimeDue = dateTimeDue;
newDateTimeTask.task = taskId newDateTimeTask.task = taskId;
await newDateTimeTask.save() await newDateTimeTask.save();
const task = taskFinderById(taskId)
return res.send({task}) return res.send({task})
} ) } )
...@@ -43,7 +25,7 @@ router.post("/make-copy", async(req:Request, res:Response):Promise<Response>=>{ ...@@ -43,7 +25,7 @@ router.post("/make-copy", async(req:Request, res:Response):Promise<Response>=>{
/** change date time of copy of task in calendar view */ /** change date time of copy of task in calendar view */
router.put("/change-copy/:dateTimeTaskId", authAuthorOrExecutorOfTask, async(req:Request, res: Response):Promise<Response>=>{ router.put("/change-copy/:dateTimeTaskId", authAuthorOrExecutorOfTask, async(req:Request, res: Response):Promise<Response>=>{
const {dateTimeTaskId} = req.params const {dateTimeTaskId} = req.params
const {executorStatus, taskId, dateTimeStart, dateTimeDue, description, title, priority} = req.body const {executorStatus,authorStatus, task, dateTimeStart, dateTimeDue, description, title, priority} = req.body
const dateTimeTask = await dataSource const dateTimeTask = await dataSource
.createQueryBuilder() .createQueryBuilder()
.select('dateTimeTask') .select('dateTimeTask')
...@@ -51,11 +33,40 @@ router.put("/change-copy/:dateTimeTaskId", authAuthorOrExecutorOfTask, async(req ...@@ -51,11 +33,40 @@ router.put("/change-copy/:dateTimeTaskId", authAuthorOrExecutorOfTask, async(req
.where("dateTimeTask.id = :dateTimeTaskId",{dateTimeTaskId}) .where("dateTimeTask.id = :dateTimeTaskId",{dateTimeTaskId})
.getOne() .getOne()
if(!dateTimeTask) return res.send({message:"such dateTimeTask does not exists"}) if(!dateTimeTask) return res.send({message:"such dateTimeTask does not exists"})
let allDateTimeTasks = task.dateTimeTasks
if (allDateTimeTasks.length===1) {
if(executorStatus && !authorStatus) {
if(task.dateTimeDeadLine>dateTimeStart &&
task.dateTimeDeadLine>dateTimeDue)
{
dateTimeTask.dateTimeStart=dateTimeStart
dateTimeTask.dateTimeDue=dateTimeDue
await dateTimeTask.save()
return res.send({message:"copyTask changed succesfully"})
}
}else if(authorStatus){
dateTimeTask.dateTimeStart=dateTimeStart
dateTimeTask.dateTimeDue=dateTimeDue
await dateTimeTask.save()
return res.send({message:"copyTask changed succesfully"})
}
} else if (allDateTimeTasks.length>1) {
let dateTimeTasks = task.dateTimeTasks
dateTimeTasks.sort(function(a:DateTimeTask, b:DateTimeTask) {
return (a.dateTimeDue > b.dateTimeDue) ? -1 : ((a.dateTimeDue < b.dateTimeDue) ? 1 : 0);
});
let latestDueDateCopyTask = dateTimeTasks[0]
if (latestDueDateCopyTask.id ===dateTimeTaskId)
if (authorStatus) {
dateTimeTask.dateTimeStart=dateTimeStart
dateTimeTask.dateTimeDue=dateTimeDue
await dateTimeTask.save()
return res.send({message:"copyTask changed succesfully"})
}
}
dateTimeTask.dateTimeStart=dateTimeStart dateTimeTask.dateTimeStart=dateTimeStart
dateTimeTask.dateTimeDue=dateTimeDue dateTimeTask.dateTimeDue=dateTimeDue
await dateTimeTask.save() await dateTimeTask.save()
const task = await taskFinderById(taskId)
if (!task) return res.status(404).send({Message:'task not found'})
task.title = title; task.title = title;
task.description = description; task.description = description;
task.priority = priority; task.priority = priority;
...@@ -65,17 +76,44 @@ router.put("/change-copy/:dateTimeTaskId", authAuthorOrExecutorOfTask, async(req ...@@ -65,17 +76,44 @@ router.put("/change-copy/:dateTimeTaskId", authAuthorOrExecutorOfTask, async(req
/**delete copyTask by dateTimeTaskId */ /**delete copyTask by dateTimeTaskId */
router.delete('/:dateTimeTaskId',authAuthorOrExecutorOfDateTimeTask, async(req:Request, res:Response):Promise<Response|void>=>{ router.delete('/:dateTimeTaskId',authAuthorOrExecutorOfDateTimeTask, async(req:Request, res:Response):Promise<Response|void>=>{
const {executorStatus} = req.body const {authorStatus, executorStatus, task} = req.body
if(executorStatus){
const {dateTimeTaskId} = req.params const {dateTimeTaskId} = req.params
if (!task) return res.status(404).send({message:'task with possible copytask involved is not found'})
let dateTimeTasks = task?.dateTimeTasks
if (authorStatus || executorStatus) {
if (dateTimeTasks.length>1) {
dateTimeTasks.sort(function(a:DateTimeTask, b:DateTimeTask) {
return (a.dateTimeDue > b.dateTimeDue) ? -1 : ((a.dateTimeDue < b.dateTimeDue) ? 1 : 0);
});
const latestDueDateCopyTask = dateTimeTasks[0]
await myDataSource await myDataSource
.createQueryBuilder() .createQueryBuilder()
.delete() .delete()
.from(DateTimeTask) .from(DateTimeTask)
.where("id = :dateTimeTaskId", { dateTimeTaskId }) .where("id = :dateTimeTaskId", { dateTimeTaskId })
.execute() .execute()
if (latestDueDateCopyTask.id === dateTimeTaskId) {
task.dateTimeDeadLine= dateTimeTasks[1].dateTimeDue
await task.save()
} else {
task.dateTimeDeadLine= dateTimeTasks[0].dateTimeDue
}
return res.send({message:"copyTask delete succesfully"}) return res.send({message:"copyTask delete succesfully"})
}
} }
if (dateTimeTasks.length ===1 ) {
if ( authorStatus) {
Task.remove(task)
return res.send({message:"task delete succesfully"})
} else{
return res.send({message:"not uathorized to delete task"})
}
}
} }
) )
......
...@@ -25,6 +25,7 @@ const SwitchUserModal= ({open, handleClose})=> { ...@@ -25,6 +25,7 @@ const SwitchUserModal= ({open, handleClose})=> {
const navigate= useNavigate(); const navigate= useNavigate();
const submitFormHandler=(e, currentUser)=>{ const submitFormHandler=(e, currentUser)=>{
dispatch(logoutUser(navigate)) dispatch(logoutUser(navigate))
e.preventDefault();
e.preventDefault(); e.preventDefault();
dispatch(loginUser({ dispatch(loginUser({
"email": currentUser.email, "email": currentUser.email,
......
...@@ -14,15 +14,15 @@ const usersReducer = (state = initialState, action) => { ...@@ -14,15 +14,15 @@ const usersReducer = (state = initialState, action) => {
case REGISTER_USER_REQUEST: case REGISTER_USER_REQUEST:
return {...state, loading: true}; return {...state, loading: true};
case REGISTER_USER_SUCCESS: case REGISTER_USER_SUCCESS:
console.log("register.user " + action) console.log("register.user " , action)
return {...state, loading: false}; return {...state, loading: false};
case REGISTER_USER_FAILURE: case REGISTER_USER_FAILURE:
return {...state, loading: false, registerError: action.error}; return {...state, loading: false, registerError: action.error};
case LOGIN_USER_SUCCESS: case LOGIN_USER_SUCCESS:
console.log("action.user " + action) console.log("action.user " , action)
return {...state, user: action.user}; return {...state, user: action.user};
case LOGIN_USER_FAILURE: case LOGIN_USER_FAILURE:
console.log("action.error" + action.error) console.log("action.error" , action.error)
return {...state, loginError: action.error}; return {...state, loginError: action.error};
case LOGOUT_USER_SUCCESS: case LOGOUT_USER_SUCCESS:
return {...state, user: null}; return {...state, user: null};
......
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