Commit 0d4aeafc authored by Нелли Ибрагимова's avatar Нелли Ибрагимова

Merge branch 'development' of…

Merge branch 'development' of ssh://git.attractor-school.com:30022/apollo64/crm-team-one into task-75-modals-styles
parents 75b6939d ef9b5054
...@@ -8,11 +8,12 @@ import { Member, MemberRole } from "./models/Member"; ...@@ -8,11 +8,12 @@ import { Member, MemberRole } from "./models/Member";
import { DateTimeTask } from "./models/DateTimeTask"; import { DateTimeTask } from "./models/DateTimeTask";
function randomIntFromInterval(min:number, max:number) { 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 countMembers =0
let countProjects = 0
let countRolesProject=0 let countRolesProject=0
const cycleThroughObject=(countKey:number,objectObserve:any):MemberRole=>{ const cycleThroughObject=(countKey:number,objectObserve:any):MemberRole=>{
...@@ -35,15 +36,6 @@ const cycleArrayOfMembers=(countIndex:number, members:Member[]):Member=>{ ...@@ -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 () => { const loadFixtures = async () => {
myDataSource myDataSource
.initialize() .initialize()
...@@ -86,16 +78,16 @@ const loadFixtures = async () => { ...@@ -86,16 +78,16 @@ const loadFixtures = async () => {
for (let i = 0; i < 20; i++) { for (let i = 0; i < 20; i++) {
let dateOfMonth = randomIntFromInterval(20, 30) let dateOfMonth = randomIntFromInterval(20, 30)
let deadLineDateOfMonth = randomIntFromInterval(1, 15) let deadLineDateOfMonth = randomIntFromInterval(1, 10)
let startDateTime = new Date(2022, 11, dateOfMonth, randomIntFromInterval(10, 15), 0, 0); let startDateTime = new Date(2022, 10, dateOfMonth, randomIntFromInterval(16, 21), 0, 0);
let dueDateTime = new Date(2022, 11, dateOfMonth, randomIntFromInterval(16, 20), 0, 0); let dueDateTime = new Date(2022, 10, dateOfMonth, randomIntFromInterval(22, 22), 0, 0);
let deadLine = new Date(2022, 12, deadLineDateOfMonth, 0, 0, 0); let deadLine = new Date(2022, 11, deadLineDateOfMonth, 0, 0, 0);
const newDateTimeTask= new DateTimeTask() const newDateTimeTask= new DateTimeTask()
newDateTimeTask.dateTimeStart=startDateTime; newDateTimeTask.dateTimeStart=startDateTime;
newDateTimeTask.dateTimeDue=dueDateTime; newDateTimeTask.dateTimeDue=dueDateTime;
await newDateTimeTask.save() await newDateTimeTask.save()
if (i <= 15) { if (i <= 15) {
const newTask = new Task(); const newTask = new Task();
newTask.title = `Buy ${faker.commerce.productName()}`; newTask.title = `Buy ${faker.commerce.productName()}`;
newTask.description = faker.random.words(4); newTask.description = faker.random.words(4);
newTask.author = faker.helpers.arrayElement(users); newTask.author = faker.helpers.arrayElement(users);
...@@ -121,7 +113,7 @@ const loadFixtures = async () => { ...@@ -121,7 +113,7 @@ const loadFixtures = async () => {
} }
const members:Member[]=[] const members:Member[]=[]
for (let i = 0; i <3; i++) { for (let i = 0; i <5; i++) {
const newMember = new Member(); const newMember = new Member();
newMember.user = faker.helpers.arrayElement(users); newMember.user = faker.helpers.arrayElement(users);
newMember.roleProject=cycleThroughObject(countRolesProject, MemberRole); newMember.roleProject=cycleThroughObject(countRolesProject, MemberRole);
...@@ -134,7 +126,7 @@ const loadFixtures = async () => { ...@@ -134,7 +126,7 @@ const loadFixtures = async () => {
const newProject = new Project(); const newProject = new Project();
newProject.title = `Project ${faker.random.words(1)}`; newProject.title = `Project ${faker.random.words(1)}`;
newProject.color = 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)); newProject.tasks= faker.helpers.arrayElements(tasks, randomIntFromInterval(2, 19));
await newProject.save(); await newProject.save();
projects.push(newProject) projects.push(newProject)
......
...@@ -19,13 +19,9 @@ import { ...@@ -19,13 +19,9 @@ import {
id: string; id: string;
title: string; title: string;
color: string; color: string;
// admin:User;
// workers:User[];
tasks:Task[]|null; tasks:Task[]|null;
createdAt: Date; createdAt: Date;
active:boolean; active:boolean;
// dateDue: Date| null;
// department:boolean;
members: Member[]; members: Member[];
} }
...@@ -47,22 +43,10 @@ import { ...@@ -47,22 +43,10 @@ import {
@Column({ name: 'active',type: 'boolean', default: true }) @Column({ name: 'active',type: 'boolean', default: true })
active!: boolean; 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}) @OneToMany(() => Task, (task: { project: Project; })=>task.project,{nullable:true})
tasks!:Task[]; tasks!:Task[];
@OneToMany(() => Member, (member: { project: Project; })=>member.project) @OneToMany(() => Member, (member: { project: Project; })=>member.project)
members!:Member[]; 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"; ...@@ -16,7 +16,6 @@ import { Exclude, instanceToPlain } from "class-transformer";
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
import {nanoid} from 'nanoid'; import {nanoid} from 'nanoid';
import {Task} from './Task'; import {Task} from './Task';
import {Project} from './Project';
import {Member} from './Member'; import {Member} from './Member';
const SALT_WORK_FACTOR= 10; const SALT_WORK_FACTOR= 10;
......
...@@ -31,7 +31,7 @@ router.get('/my',auth, async (req:Request, res:Response): Promise<Response>=> { ...@@ -31,7 +31,7 @@ router.get('/my',auth, async (req:Request, res:Response): Promise<Response>=> {
if (rawProjects.length>0){ if (rawProjects.length>0){
for(let project of rawProjects){ for(let project of rawProjects){
projectIds.push(project.id projectIds.push(project.id
) )
} }
} }
const projects = await dataSource const projects = await dataSource
......
...@@ -93,49 +93,47 @@ router.get('/my',auth, async (req: Request, res: Response):Promise<Response>=>{ ...@@ -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>=>{ router.get('/related', auth,async (req: Request, res: Response):Promise<Response>=>{
console.log('related')
const user = req.body.user const user = req.body.user
const rawMembership = await dataSource const tasks = await dataSource
.createQueryBuilder() .createQueryBuilder()
.select("member") .select(["task"])
.from(Member, "member") .from(Task,"task")
.leftJoinAndSelect('member.user', 'user' ) .leftJoinAndSelect("task.executor","user")
.leftJoinAndSelect('member.project', 'project' ) .leftJoinAndSelect("task.dateTimeTasks","dateTimeTask")
.leftJoinAndSelect('project.tasks', 'task' ) .leftJoinAndSelect("task.author","users")
.where("member.userId = :id", { id: user.id }) .leftJoinAndSelect("task.project","project")
.leftJoinAndSelect("project.members", "member")
.where("member.userId = :userId", {userId:user.id})
.getMany() .getMany()
let projectIds:any[] = [] let projectIds:any[] = []
if (rawMembership.length>0){ if (tasks.length>0){
for (let member of rawMembership){ for (let task of tasks){
const projectId = member?.project?.id const projectId = task?.project?.id
projectIds.push(projectId) projectIds.push(projectId)
} }
} }
let tasks :Task[]= []
const projectsIdArray = [...new Set(projectIds)]; const projectsIdArray = [...new Set(projectIds)];
const searchByProjectsIdArray:object[] = [] let users:User[]=[]
for (let projectId of projectsIdArray){
searchByProjectsIdArray.push({id:projectId})
}
if (projectIds.length>0){ if (projectIds.length>0){
tasks = await dataSource users = await dataSource
.getRepository(Task) .getRepository(User)
.find({ .find({
relations:{ relations:{
author:true, members:true,
executor:true,
project:true
}, },
where:{ where:{
project: members:
{id:In( {project:In(
projectsIdArray projectsIdArray
)} )}
} }
}) })
} }
return res.send({tasks}) return res.send({tasks, users})
}) })
......
...@@ -5,6 +5,8 @@ import { nanoid } from 'nanoid'; ...@@ -5,6 +5,8 @@ import { nanoid } from 'nanoid';
import multer from 'multer'; import multer from 'multer';
import path from 'path'; import path from 'path';
import {config} from "../config" import {config} from "../config"
import { Project } from '../models/Project';
import { Member, MemberRole } from '../models/Member';
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
...@@ -24,16 +26,16 @@ const storage = multer.diskStorage({ ...@@ -24,16 +26,16 @@ const storage = multer.diskStorage({
const upload = multer({ storage }) const upload = multer({ storage })
//** return all users of DB */
router.get('/', async (req : Request, res : Response):Promise<object> => { router.get('/', async (req : Request, res : Response):Promise<object> => {
const users = await dataSource const users = await dataSource
.getRepository(User) .getRepository(User)
.createQueryBuilder("user") .createQueryBuilder("user")
.getMany() .getMany()
return res.send({users}) return res.send({users})
}) })
/**create new user*/
router.post('/', upload.single("avatar"), async (req : Request, res : Response):Promise<object> => { router.post('/', upload.single("avatar"), async (req : Request, res : Response):Promise<object> => {
const {name,surname,password,email, role} = req.body; const {name,surname,password,email, role} = req.body;
const displayName = surname+' '+name[0]+'.' const displayName = surname+' '+name[0]+'.'
...@@ -44,15 +46,25 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response): ...@@ -44,15 +46,25 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response):
user.displayName= displayName; user.displayName= displayName;
user.email = email; user.email = email;
user.role = role; user.role = role;
user.generateToken() user.generateToken();
await user.save(); 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, { const userToFront:User|null = await dataSource.manager.findOneBy(User, {
email: user.email email: user.email
}) })
return res.send({userToFront}) return res.send({userToFront})
}) })
/** log in*/
router.post('/sessions/', async (req : Request, res : Response):Promise<object> => { router.post('/sessions/', async (req : Request, res : Response):Promise<object> => {
const {email, password} = req.body; const {email, password} = req.body;
const user = await dataSource const user = await dataSource
...@@ -77,7 +89,7 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object> ...@@ -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> => { router.delete('/sessions', async(req: Request, res: Response):Promise<void | object> => {
const token = req.get('Authorization'); const token = req.get('Authorization');
const successMsg = {message:'success'}; 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