Merge branch 'task-69-feature/project_personal_business' into 'development'

Task 69 feature/project personal business

See merge request !49
parents 3d1e05fc 83b2f829
......@@ -8,11 +8,12 @@ import { Member, MemberRole } from "./models/Member";
import { DateTimeTask } from "./models/DateTimeTask";
function randomIntFromInterval(min:number, max:number) {
return Math.floor(Math.random() * (max - min + 1) + min)
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
let countUsers = 0
let countMembers =0
let countProjects = 0
let countRolesProject=0
const cycleThroughObject=(countKey:number,objectObserve:any):MemberRole=>{
......@@ -35,15 +36,6 @@ const cycleArrayOfMembers=(countIndex:number, members:Member[]):Member=>{
}
const cycleArrayOfProjects=(countIndex:number, projects:Project[]):Project=>{
let project = projects[countIndex]
countIndex++
if (countIndex ===projects.length-1){
countIndex=0
}
return project
}
const loadFixtures = async () => {
myDataSource
.initialize()
......@@ -86,10 +78,10 @@ const loadFixtures = async () => {
for (let i = 0; i < 20; i++) {
let dateOfMonth = randomIntFromInterval(20, 30)
let deadLineDateOfMonth = randomIntFromInterval(1, 15)
let startDateTime = new Date(2022, 11, dateOfMonth, randomIntFromInterval(10, 15), 0, 0);
let dueDateTime = new Date(2022, 11, dateOfMonth, randomIntFromInterval(16, 20), 0, 0);
let deadLine = new Date(2022, 12, deadLineDateOfMonth, 0, 0, 0);
let deadLineDateOfMonth = randomIntFromInterval(1, 10)
let startDateTime = new Date(2022, 10, dateOfMonth, randomIntFromInterval(16, 21), 0, 0);
let dueDateTime = new Date(2022, 10, dateOfMonth, randomIntFromInterval(22, 22), 0, 0);
let deadLine = new Date(2022, 11, deadLineDateOfMonth, 0, 0, 0);
const newDateTimeTask= new DateTimeTask()
newDateTimeTask.dateTimeStart=startDateTime;
newDateTimeTask.dateTimeDue=dueDateTime;
......@@ -121,7 +113,7 @@ const loadFixtures = async () => {
}
const members:Member[]=[]
for (let i = 0; i <3; i++) {
for (let i = 0; i <5; i++) {
const newMember = new Member();
newMember.user = faker.helpers.arrayElement(users);
newMember.roleProject=cycleThroughObject(countRolesProject, MemberRole);
......@@ -134,7 +126,7 @@ const loadFixtures = async () => {
const newProject = new Project();
newProject.title = `Project ${faker.random.words(1)}`;
newProject.color = faker.random.words(1);
newProject.members = [cycleArrayOfMembers(countMembers,members), cycleArrayOfMembers(countMembers,members),cycleArrayOfMembers(countMembers,members)]
newProject.members = faker.helpers.arrayElements(members, 3);
newProject.tasks= faker.helpers.arrayElements(tasks, randomIntFromInterval(2, 19));
await newProject.save();
projects.push(newProject)
......
......@@ -19,13 +19,9 @@ import {
id: string;
title: string;
color: string;
// admin:User;
// workers:User[];
tasks:Task[]|null;
createdAt: Date;
active:boolean;
// dateDue: Date| null;
// department:boolean;
members: Member[];
}
......@@ -47,22 +43,10 @@ import {
@Column({ name: 'active',type: 'boolean', default: true })
active!: boolean;
// @Column({ name: 'dateDue', type: Date, default: null })
// dateDue!: Date| null;
// @Column({ name: 'department', type: Boolean,nullable:true ,default: false})
// department!: boolean;
// @ManyToOne(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true})
// admin!: User;
@OneToMany(() => Task, (task: { project: Project; })=>task.project,{nullable:true})
tasks!:Task[];
@OneToMany(() => Member, (member: { project: Project; })=>member.project)
members!:Member[];
// @ManyToMany(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true,cascade: true, onUpdate:'CASCADE',onDelete: 'CASCADE'})
// @JoinTable()
// workers!: User[];
}
\ No newline at end of file
......@@ -16,7 +16,6 @@ import { Exclude, instanceToPlain } from "class-transformer";
import bcrypt from 'bcrypt';
import {nanoid} from 'nanoid';
import {Task} from './Task';
import {Project} from './Project';
import {Member} from './Member';
const SALT_WORK_FACTOR= 10;
......
......@@ -93,49 +93,47 @@ router.get('/my',auth, async (req: Request, res: Response):Promise<Response>=>{
})
/**check tasks of projects in which current user is involved, search by token*/
/**return tasks & users of projects in which current user is involved, search by token*/
router.get('/related', auth,async (req: Request, res: Response):Promise<Response>=>{
console.log('related')
const user = req.body.user
const rawMembership = await dataSource
const tasks = await dataSource
.createQueryBuilder()
.select("member")
.from(Member, "member")
.leftJoinAndSelect('member.user', 'user' )
.leftJoinAndSelect('member.project', 'project' )
.leftJoinAndSelect('project.tasks', 'task' )
.where("member.userId = :id", { id: user.id })
.select(["task"])
.from(Task,"task")
.leftJoinAndSelect("task.executor","user")
.leftJoinAndSelect("task.dateTimeTasks","dateTimeTask")
.leftJoinAndSelect("task.author","users")
.leftJoinAndSelect("task.project","project")
.leftJoinAndSelect("project.members", "member")
.where("member.userId = :userId", {userId:user.id})
.getMany()
let projectIds:any[] = []
if (rawMembership.length>0){
for (let member of rawMembership){
const projectId = member?.project?.id
if (tasks.length>0){
for (let task of tasks){
const projectId = task?.project?.id
projectIds.push(projectId)
}
}
let tasks :Task[]= []
const projectsIdArray = [...new Set(projectIds)];
const searchByProjectsIdArray:object[] = []
for (let projectId of projectsIdArray){
searchByProjectsIdArray.push({id:projectId})
}
let users:User[]=[]
if (projectIds.length>0){
tasks = await dataSource
.getRepository(Task)
users = await dataSource
.getRepository(User)
.find({
relations:{
author:true,
executor:true,
project:true
members:true,
},
where:{
project:
{id:In(
members:
{project:In(
projectsIdArray
)}
}
})
}
return res.send({tasks})
return res.send({tasks, users})
})
......
......@@ -5,6 +5,8 @@ import { nanoid } from 'nanoid';
import multer from 'multer';
import path from 'path';
import {config} from "../config"
import { Project } from '../models/Project';
import { Member, MemberRole } from '../models/Member';
const router:Router = express.Router();
const dataSource = myDataSource;
......@@ -24,7 +26,7 @@ const storage = multer.diskStorage({
const upload = multer({ storage })
//** return all users of DB */
router.get('/', async (req : Request, res : Response):Promise<object> => {
const users = await dataSource
.getRepository(User)
......@@ -33,7 +35,7 @@ const users = await dataSource
return res.send({users})
})
/**create new user*/
router.post('/', upload.single("avatar"), async (req : Request, res : Response):Promise<object> => {
const {name,surname,password,email, role} = req.body;
const displayName = surname+' '+name[0]+'.'
......@@ -44,15 +46,25 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response):
user.displayName= displayName;
user.email = email;
user.role = role;
user.generateToken()
user.generateToken();
await user.save();
const newProject = new Project();
newProject.title = 'Личные дела';
newProject.color= 'Green';
await newProject.save()
const newMember = new Member();
newMember.user = user;
newMember.project = newProject;
newMember.roleProject = MemberRole.ADMIN;
await newMember.save();
const userToFront:User|null = await dataSource.manager.findOneBy(User, {
email: user.email
})
return res.send({userToFront})
})
/** log in*/
router.post('/sessions/', async (req : Request, res : Response):Promise<object> => {
const {email, password} = req.body;
const user = await dataSource
......@@ -77,7 +89,7 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object>
})
})
/** log out*/
router.delete('/sessions', async(req: Request, res: Response):Promise<void | object> => {
const token = req.get('Authorization');
const successMsg = {message:'success'};
......
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