Commit 1c5e08c3 authored by Ibadullina Inabat's avatar Ibadullina Inabat

Merge branch 'development' of…

Merge branch 'development' of ssh://git.attractor-school.com:30022/apollo64/crm-team-one into task-31-feature/connect_registration_login_with_back
parents 9b487731 bd90d3e8
This diff is collapsed.
......@@ -7,18 +7,21 @@
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon",
"start": "npm run build && node build/server.js",
"lint": "eslint . --ext .ts"
"lint": "eslint . --ext .ts",
"fixtures": "ts-node ./src/fixtures.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@faker-js/faker": "^7.6.0",
"@types/node": "^18.11.8",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"eslint": "^8.26.0",
"nodemon": "^2.0.20",
"ts-node": "^10.9.1",
"typeorm-fixtures-cli": "^3.0.1",
"typescript": "^4.8.4"
},
"dependencies": {
......@@ -26,6 +29,7 @@
"@types/cors": "^2.8.12",
"@types/express": "^4.17.14",
"bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"cors": "^2.8.5",
"express": "^4.18.2",
......
import { myDataSource } from "./app-data-source";
import { User, UserRole } from "./models/User";
import { faker } from '@faker-js/faker';
import { Task } from "./models/Task";
import { Project } from "./models/Project";
function randomIntFromInterval(min:number, max:number) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
const loadFixtures = async () => {
myDataSource
.initialize()
.then(async () => {
const repositoryTask = myDataSource.getRepository(Task);
await repositoryTask.delete({});
const repositoryUser = myDataSource.getRepository(User);
await repositoryUser.delete({});
const repositoryProject = myDataSource.getRepository(Project);
await repositoryProject.delete({});
console.log('========================== ' + '\n' + 'Data Source has been cleared!' +'\n' + '==========================')
const userRoles = [{role: UserRole.DIRECTOR}, {role: UserRole.SUPERUSER}, {role: UserRole.USER}, {role: UserRole.USER}];
const users = []
for (let i = 0; i < 4; i++) {
const name = faker.name.firstName()
const surname = faker.name.lastName()
const displayName = name + ' ' + surname[0] + '.'
const user = new User()
user.name = name;
user.surname = surname;
user.password = '12345qwert';
user.displayName= displayName;
user.phone = faker.phone.number('+77#########')
user.email = faker.internet.email();
user.role = userRoles[i].role;
user.generateToken()
await user.save();
users.push(user)
}
const tasks = []
type taskFinishType = "opened" | "done" |"failed";
type priorityType = "A" | "B" |"C";
const priorities:priorityType[] = ["A", "B" , "C"]
const accomplish:taskFinishType[] = ["opened", "done" , "failed"]
for (let i = 0; i < 15; i++) {
if (i <= 10) {
const newTask = new Task();
newTask.title = `Buy ${faker.commerce.productName()}`;
newTask.description = faker.random.words(4);
newTask.executors = faker.helpers.arrayElements(users, randomIntFromInterval(0, 3));
newTask.dateTimeDue = faker.date.soon(randomIntFromInterval(1, 15));
newTask.dateTimeStart = faker.date.recent((randomIntFromInterval(0, 8)));
newTask.author = faker.helpers.arrayElement(users);
newTask.accomplish = faker.helpers.arrayElement(accomplish);
newTask.priority = faker.helpers.arrayElement(priorities);
await newTask.save();
tasks.push(newTask)
} else {
const newTask = new Task();
newTask.title = `Buy ${faker.commerce.productName()}`;
newTask.description = faker.random.words(4);
newTask.executors = faker.helpers.arrayElements(users, randomIntFromInterval(0, 3));
newTask.dateTimeDue = null;
newTask.dateTimeStart = null;
newTask.author = faker.helpers.arrayElement(users);
newTask.accomplish = accomplish[0];
newTask.priority = faker.helpers.arrayElement(priorities);
await newTask.save();
tasks.push(newTask)
}
}
console.log('========================== ' + '\n' + 'Fixtures done!' +'\n' + '==========================')
})
.catch((err) => {
console.error("Error during Data Source initialization:", err)
})
};
loadFixtures()
\ No newline at end of file
......@@ -11,7 +11,8 @@ import {
import {User} from './User';
import {Project} from './Project';
type taskFinishType = "open" | "done" |"failed";
type taskFinishType = "opened" | "done" |"failed";
type priorityType = "A" | "B" |"C";
interface ITask{
id: string;
......@@ -21,6 +22,7 @@ import {
dateTimeStart:Date| null;
dateTimeDue:Date| null;
accomplish: taskFinishType;
priority: priorityType;
author: User;
project:Project|null;
executors:User[]
......@@ -40,6 +42,7 @@ import {
dateTimeStart!: Date | null;
@Column({ name: 'dateTimeDue', type: Date,nullable: true })
dateTimeDue!: Date | null;
@Column({
type: "enum",
enum: ["opened", "done" , "failed"],
......@@ -48,6 +51,15 @@ import {
accomplish!: taskFinishType
@Column({
type: "enum",
enum: ["A", "B" , "C"],
default: "C"
})
priority!: priorityType
@ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks,{eager : true})
author!: User;
......
......@@ -7,11 +7,11 @@ import {
BaseEntity,
ManyToMany,
OneToMany,
JoinTable
JoinTable,
} from 'typeorm';
import {IsEmail
} from "class-validator";
import { Exclude, instanceToPlain } from "class-transformer";
import bcrypt from 'bcrypt';
import {nanoid} from 'nanoid';
import {Task} from './Task';
......@@ -20,17 +20,17 @@ import {Project} from './Project';
const SALT_WORK_FACTOR= 10;
type userRoleType = "worker" | "director";
export enum UserRole {USER="user" ,DIRECTOR= "director",SUPERUSER="superuser"}
interface IUser {
id:string;
role: UserRole;
name: string;
surname: string;
email: string;
displayName: string;
password:string;
token: string;
role: userRoleType;
createdAt: Date;
createdTasks:Task[];
workerInProjects:Project[];
......@@ -41,39 +41,40 @@ interface IUser {
@Entity({ name: 'User' })
export class User extends BaseEntity implements IUser {
@PrimaryGeneratedColumn('uuid')
id!: string
@Column({ name: 'name', type: 'varchar', length:20,nullable: false })
name!: string
@PrimaryGeneratedColumn("uuid")
id!: string;
@Column({ name: 'name', type: 'varchar', length:30,nullable: false })
name!: string;
@Column({ name: 'surname', type: 'varchar', length:30,nullable: false })
surname!: string
surname!: string;
@Column({ name: 'displayName', type: 'varchar', length:30,nullable: false })
displayName!: string
@Column({ name: 'displayName', type: 'varchar', length:35,nullable: false })
displayName!: string;
@Column({ name: 'email', type: 'varchar',length:20, unique: true, nullable: false })
@Column({ name: 'email', type: 'varchar',length:40, unique: true, nullable: false })
@IsEmail()
email!: string
email!: string;
@Column({ name: 'phone', type: 'varchar',length:10, unique: true, nullable: true})
phone?: string
@Column({ name: 'phone', type: 'varchar',length:15, unique: true, nullable: true})
phone?: string;
@Column({ name: 'token', type: 'varchar',length:100, unique: true, nullable: false })
token!: string
token!: string;
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt!: Date;
@Column({
type: "enum",
enum: ["worker", "director"],
default: "worker"
enum: UserRole,
default: UserRole.USER
})
role!: userRoleType
role!: UserRole
@Column({ type: 'varchar', nullable: false, select:false })
password!: string
@Column({ type: 'varchar', nullable: false, select:true })
@Exclude({ toPlainOnly: true })
password!: string;
@OneToMany(() => Task, (task: { user: User }) => task.user)
......@@ -106,20 +107,16 @@ export class User extends BaseEntity implements IUser {
public async checkPassword(
candidatePassword: string,
):Promise<boolean> {
console.log("Checking password", candidatePassword,'this.password', this.password)
return await bcrypt.compare(candidatePassword, this.password);
}
toJSON() {
return instanceToPlain(this);
}
}
......
......@@ -17,7 +17,7 @@ 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 {title,description,project,executors,dateTimeDue,dateTimeStart,accomplish,priority} = req.body;
const user = await dataSource
.createQueryBuilder()
.select("user")
......@@ -32,11 +32,13 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
newTask.dateTimeDue = dateTimeDue;
newTask.dateTimeStart = dateTimeStart;
newTask.author= user;
newTask.accomplish = accomplish;
newTask.priority = priority;
await newTask.save();
return res.send({newTask})
})
router.get('/userId/:userId', async (req: Request, res: Response)=>{
router.get('/userId/:userId', async (req: Request, res: Response):Promise<Response>=>{
const userId = req.params.userId;
const tasks = await dataSource
.getRepository(Task)
......@@ -47,8 +49,7 @@ router.get('/userId/:userId', async (req: Request, res: Response)=>{
return res.send({tasks})
})
router.get('/my', async (req: Request, res: Response)=>{
router.get('/my', async (req: Request, res: Response):Promise<Response>=>{
const token = req.get('Authorization');
const user = await dataSource
.createQueryBuilder()
......@@ -65,3 +66,55 @@ router.get('/my', async (req: Request, res: Response)=>{
.getMany()
return res.send({tasks})
})
router.delete('/:taskId',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 taskId = req.params.taskId;
await myDataSource
.createQueryBuilder()
.delete()
.from(Task)
.where("id = :id", { id: taskId })
.execute()
return res.send({message: 'Task deleted successfully'})
})
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,executors,dateTimeDue,dateTimeStart,accomplish,priority} = req.body;
await dataSource
.createQueryBuilder()
.update(Task)
.set({
title: title,
description: description,
project: project,
executors: executors,
dateTimeDue: dateTimeDue,
dateTimeStart: dateTimeStart,
author:user,
accomplish: accomplish,
priority: priority
})
.where("id = :id", { id: id })
.execute()
res.send({message:'update task successfully'})
// res.send({task})
})
......@@ -17,7 +17,7 @@ return res.send({users})
router.post('/', async (req : Request, res : Response):Promise<object> => {
const {name,surname,password,email} = req.body;
const {name,surname,password,email, role} = req.body;
const displayName = surname+' '+name[0]+'.'
const user = new User();
user.name = name;
......@@ -25,6 +25,7 @@ router.post('/', async (req : Request, res : Response):Promise<object> => {
user.password = password;
user.displayName= displayName;
user.email = email;
user.role = role;
user.generateToken()
await user.save();
const userToFront:User|null = await dataSource.manager.findOneBy(User, {
......@@ -42,9 +43,9 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object>
.select("user")
.from(User, "user")
.where("user.email = :email", { email: email })
.addSelect('password')
.getOne()
if(!user) return res.status(404).send({Message:'user not found'})
const isMatch:boolean = await user.checkPassword(password);
console.log("123")
if (!isMatch) return res.status(400).send({
......@@ -69,7 +70,6 @@ router.delete('/sessions', async(req: Request, res: Response):Promise<void | obj
token: token
})
if(!user) return res.send({successMsg});
console.log('token: ' + token)
user.token = nanoid();
await user.save();
......
import { Grid } from "@mui/material";
const CalendarRow = ({children}) => {
return <>
<Grid
container
align='center'
sx={{borderBottom: '1px solid black', borderRight: '1px solid black', borderLeft: '1px solid black'}}
>
{children}
</Grid>
</>
};
export default CalendarRow;
import { Grid } from "@mui/material";
const CalendarSmallCell = ({children, xs}) => {
return <>
<Grid align='center' item xs={xs} sx={{borderRight: '1px solid black'}}>
{children}
</Grid>
</>
};
export default CalendarSmallCell;
\ No newline at end of file
import { Grid } from "@mui/material";
const CalendarStandartCell = ({children, xs, onClick}) => {
return <>
<Grid
item xs={xs}
sx={{borderRight: '1px solid black'}}
onClick={onClick}>
{children}
</Grid>
</>
};
export default CalendarStandartCell;
\ No newline at end of file
import { Grid, TextField } from "@mui/material";
import React, { useEffect, useState } from "react";
const CalendarTask = ({year, month, tasks, day, hours, setCurrentTask, onChange, hourFormat}) => {
const getTaskInDayCell = (tasks, day, hours) => {
const hour = parseInt(hours.split(':')[0])
let hourDiffEnd
let hourDiffStart
if (hourFormat) {
hourDiffEnd = hour + 1
} else {
hourDiffEnd = hour + 2
}
if (hourFormat) {
hourDiffStart = hour - 1
} else {
hourDiffStart = hour - 2
}
const tasksCell = tasks.filter(task=> {
if (year === task.infoForCell.startYear) {
if (month + 1 === task.infoForCell.startMonth) {
if (day.dayNumber === task.infoForCell.startDay) {
if (((task.infoForCell.endHour <= hour || task.infoForCell.startHour <= hour) && (task.infoForCell.endHour > hour))
|| (task.infoForCell.startHour >= hour && task.infoForCell.endHour < hourDiffEnd)
|| (task.infoForCell.endMinute <= 59 && task.infoForCell.endHour === hour)) {
return task
}
}
}
}
})
return tasksCell
}
const tasksCell = getTaskInDayCell(tasks, day, hours)
return (<>
{tasksCell.length ? tasksCell.map((task, i)=>
{
return (
<Grid key={task.id} sx={{backgroundColor: 'lightgreen'}}>
<TextField
id={task.title}
variant="standard"
value={task.title}
name='title'
onClick={(e)=>{e.stopPropagation(); setCurrentTask(task)}}
onChange={onChange}>
</TextField>
</Grid>)}
) : null }
</>)
};
export default CalendarTask;
\ No newline at end of file
import { Container } from '@mui/material';
import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import MonthCalendarBody from '../../components/MonthCalendarBody/MonthCalendarBody';
import MonthCalendarHeader from '../../components/MonthCalendarHeader/MonthCalendarHeader';
import { addTask, fetchTasks} from '../../store/actions/tasksActions';
function MonthCalendar() {
const dispatch = useDispatch();
const { tasks } = useSelector(state => state.tasks);
const [hourFormat, setHourFormat] = useState(false);
const [month, setMonth] = useState('')
const [year, setYear] = useState('')
const [worker, setWorker] = useState('');
const [calendarType, setCalendarType] = useState('Месяц');
const [currentTask, setCurrentTask] = useState({})
useEffect(()=>{
setMonth(new Date().getMonth())
setYear(new Date().getFullYear())
},[])
dispatch(fetchTasks())
}, [dispatch])
const onChangeWorkerHandler = (event) => {
setWorker(event.target.value);
};
......@@ -46,6 +54,40 @@ function MonthCalendar() {
})
}
function dateToISOLikeButLocal(date) {
const offsetMs = date.getTimezoneOffset() * 60 * 1000;
const msLocal = date.getTime() - offsetMs;
const dateLocal = new Date(msLocal);
const iso = dateLocal.toISOString();
return iso;
}
const createTaskInCellHandler = (dayNumber, dayHour) => {
const hour = parseInt(dayHour.split(':')[0])
let hourDue
if (hourFormat) {
hourDue = hour + 0
} else {
hourDue = hour + 1
}
const newTask = {
title:"Задача",
description:"описание",
dateTimeStart: dateToISOLikeButLocal(new Date(year, month, dayNumber, hour, 0)),
dateTimeDue: dateToISOLikeButLocal(new Date(year, month, dayNumber, hourDue, 59)),
}
console.log(newTask)
dispatch(addTask(newTask))
setCurrentTask((newTask))
}
const onChangeCellTaskTitle = (e) => {
e.stopPropagation()
const value = e.target.value;
const name = e.target.name;
setCurrentTask({ ...currentTask, [name]: value })
}
return (
<>
<Container>
......@@ -63,6 +105,12 @@ function MonthCalendar() {
<MonthCalendarBody
month={month}
year={year}
tasks={tasks}
createTaskInCellHandler={createTaskInCellHandler}
onChangeCellTaskTitle={onChangeCellTaskTitle}
setCurrentTask={setCurrentTask}
hourFormat={hourFormat}
setHourFormat={setHourFormat}
/>
</Container>
</>
......
......@@ -5,6 +5,7 @@ import App from './App';
import { configureStore } from '@reduxjs/toolkit';
import { Provider } from 'react-redux';
import usersReducer from './store/reducers/usersReducer';
import tasksReducer from './store/reducers/tasksReducer';
import axios from 'axios';
const localStorageMiddleware = ({getState}) => (next) => (action) => {
......@@ -31,6 +32,7 @@ axios.interceptors.request.use(config=>{
const store = configureStore({
reducer: {
users: usersReducer,
tasks: tasksReducer
},
preloadedState: loadFromLocalStorage(),
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(localStorageMiddleware)
......
export const FETCH_TASKS_REQUEST = "FETCH_TASKS_REQUEST";
export const FETCH_TASKS_SUCCESS = "FETCH_TASKS_SUCCESS";
export const FETCH_TASKS_FAILURE = "FETCH_TASKS_FAILURE";
export const ADD_NEW_TASK_REQUEST = "ADD_NEW_TASK_REQUEST";
export const ADD_NEW_TASK_SUCCESS = "ADD_NEW_TASK_SUCCESS";
export const ADD_NEW_TASK_FAILURE = "ADD_NEW_TASK_FAILURE";
\ No newline at end of file
import { ADD_NEW_TASK_FAILURE, ADD_NEW_TASK_REQUEST, ADD_NEW_TASK_SUCCESS, FETCH_TASKS_FAILURE, FETCH_TASKS_REQUEST, FETCH_TASKS_SUCCESS} from "../actionTypes/tasksTypes";
import axios from '../../axiosPlanner'
const fetchTasksRequest = () => {
return {type: FETCH_TASKS_REQUEST}
};
const fetchTasksSuccess = (tasks) => {
return {type: FETCH_TASKS_SUCCESS, tasks}
};
const fetchTasksFailure = (error) => {
return {type: FETCH_TASKS_FAILURE, error}
};
export const fetchTasks = () => {
return async (dispatch) => {
dispatch(fetchTasksRequest());
try {
const response = await axios.get("/tasks");
dispatch(fetchTasksSuccess(response.data.tasks))
} catch (error) {
dispatch(fetchTasksFailure(error.response.data));
}
}
}
const addTaskRequest = () => {
return {type: ADD_NEW_TASK_REQUEST}
};
const addTaskSuccess = () => {
return {type: ADD_NEW_TASK_SUCCESS}
};
const addTaskFailure = (error) => {
return {type: ADD_NEW_TASK_FAILURE, error}
};
export const addTask = (task) => {
return async (dispatch, getState) => {
dispatch(addTaskRequest());
const token = getState().users?.user?.token;
try {
await axios.post("/tasks", task, {
headers: {
'Authorization': 'yjBjcPCQwytwrYo9rRuiK'
}
});
dispatch(addTaskSuccess())
dispatch(fetchTasks())
} catch (error) {
dispatch(addTaskFailure(error.response.data));
}
}
}
\ No newline at end of file
import { FETCH_TASKS_FAILURE, FETCH_TASKS_REQUEST, FETCH_TASKS_SUCCESS} from "../actionTypes/tasksTypes";
const initialState = {
tasks: [],
loading: false,
error: null
};
const tasksReduсer = (state = initialState, action) => {
switch(action.type) {
case FETCH_TASKS_REQUEST:
return {...state, loading: true};
case FETCH_TASKS_SUCCESS:
const newArr = []
action.tasks.forEach((task)=>{
if (task.dateTimeStart && task.dateTimeDue) {
if (new Date(task.dateTimeDue).getTime() - new Date(task.dateTimeStart).getTime() < (4 * 3600000)) {
const dateStart = task.dateTimeStart.split('T')[0]
const timeStart = task.dateTimeStart.split('T')[1]
const timeEnd = task.dateTimeDue.split('T')[1]
const dayStart = parseInt(dateStart.split('-')[2])
const monthStartNumber = parseInt(dateStart.split('-')[1])
const yearStartNumber = parseInt(dateStart.split('-')[0])
const timeStartHour = parseInt(timeStart.split(':')[0])
const timeEndHour = parseInt(timeEnd.split(':')[0])
const timeStartMinute = parseInt(timeStart.split(':')[1])
const timeEndMinute = parseInt(timeEnd.split(':')[1])
newArr.push({...task, infoForCell: {
startDay: dayStart,
startHour: timeStartHour,
startMonth: monthStartNumber,
startYear: yearStartNumber,
startMinute: timeStartMinute,
endHour: timeEndHour,
endMinute: timeEndMinute,
}
} )
}
}
})
return {...state, loading: false, tasks: newArr};
case FETCH_TASKS_FAILURE:
return {...state, loading: false, error: action.error};
default:
return state;
}
};
export default tasksReduсer;
\ No newline at end of file
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