#67 added adjust fixtures to new model

parent 55c98e79
......@@ -5,11 +5,44 @@ import { faker } from '@faker-js/faker';
import { priorityType, Task, taskFinishType } from "./models/Task";
import { Project } from "./models/Project";
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)
}
let countUsers = 0
let countMembers =0
let countProjects = 0
let countRolesProject=0
const cycleThroughObject=(countKey:number,objectObserve:any):MemberRole=>{
let arrayOfKeys = Object.keys(objectObserve)
let keyOfObject = arrayOfKeys[countKey]
let valueOfKey = objectObserve[keyOfObject]
countKey++
if(countKey===arrayOfKeys.length-1){
countKey=0
}
return valueOfKey
}
const cycleArrayOfMembers=(countIndex:number, members:Member[]):Member=>{
let member = members[countIndex]
countIndex++
if (countIndex ===members.length-1){
countIndex=0
}
return 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
......@@ -19,25 +52,27 @@ const loadFixtures = async () => {
await repositoryMember.delete({})
const repositoryTask = myDataSource.getRepository(Task);
await repositoryTask.delete({});
const repositoryDateTimeTask = myDataSource.getRepository(DateTimeTask);
await repositoryDateTimeTask.delete({})
const repositoryProject = myDataSource.getRepository(Project);
await repositoryProject.delete({})
const repositoryUser = myDataSource.getRepository(User);
await repositoryUser.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 userRoles = [{role: UserRole.SUPERUSER}, {role: UserRole.USER}, {role: UserRole.USER},{role: UserRole.USER},];
const users = []
for (let i = 0; i < 4; i++) {
for (let i = 0; i < 3; 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.password = '123';
user.displayName= displayName;
user.phone = faker.phone.number('+77#########')
user.email = faker.internet.email();
user.email = 'a@a.a'+i;
user.role = userRoles[i].role;
user.generateToken()
await user.save();
......@@ -46,18 +81,28 @@ const loadFixtures = async () => {
const tasks:Task[] = []
const priorities:priorityType[] = ["A", "B" , "C"]
const accomplish:taskFinishType[] = ["opened","in-progress", "done" , "failed"]
const accomplish:taskFinishType[] = ["opened", "done" , "failed"]
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);
const newDateTimeTask= new DateTimeTask()
newDateTimeTask.dateTimeStart=startDateTime;
newDateTimeTask.dateTimeDue=dueDateTime;
await newDateTimeTask.save()
if (i <= 15) {
const newTask = new Task();
newTask.title = `Buy ${faker.commerce.productName()}`;
newTask.description = faker.random.words(4);
newTask.executor = faker.helpers.arrayElement(users);
newTask.dateTimeDue = faker.date.soon(randomIntFromInterval(1, 15));
newTask.dateTimeStart = faker.date.recent((randomIntFromInterval(0, 8)));
newTask.dateTimeDeadLine = faker.date.soon(randomIntFromInterval(1, 15));
newTask.dateTimeFactDeadLine = faker.date.soon(randomIntFromInterval(1, 15));
newTask.author = faker.helpers.arrayElement(users);
newTask.executor = faker.helpers.arrayElement(users);
newTask.dateTimeTasks=[newDateTimeTask]
newTask.dateTimeDeadLine =deadLine;
newTask.dateTimeFactDeadLine = deadLine;
newTask.accomplish = faker.helpers.arrayElement(accomplish);
newTask.priority = faker.helpers.arrayElement(priorities);
await newTask.save();
......@@ -76,36 +121,33 @@ const loadFixtures = async () => {
}
const members:Member[]=[]
for (let i = 0; i < 5; i++) {
for (let i = 0; i <3; i++) {
const newMember = new Member();
newMember.user = faker.helpers.arrayElement(users);
newMember.roleProject=faker.helpers.objectValue(MemberRole);
newMember.roleProject=cycleThroughObject(countRolesProject, MemberRole);
await newMember.save();
members.push(newMember)
}
const projects:Project[] = []
for (let i = 0; i < 5; i++) {
for (let i = 0; i < 3; i++) {
const newProject = new Project();
newProject.title = `Project ${faker.random.words(1)}`;
newProject.color = faker.random.words(1);
newProject.members = faker.helpers.arrayElements(members,randomIntFromInterval(1, 3))
newProject.tasks= faker.helpers.arrayElements(tasks, randomIntFromInterval(1, 3));
newProject.members = [cycleArrayOfMembers(countMembers,members), cycleArrayOfMembers(countMembers,members),cycleArrayOfMembers(countMembers,members)]
newProject.tasks= faker.helpers.arrayElements(tasks, randomIntFromInterval(2, 19));
await newProject.save();
projects.push(newProject)
}
console.log('========================== ' + '\n' + 'Fixtures done!' +'\n' + '==========================')
})
.catch((err) => {
console.error("Error during Data Source initialization:", err)
console.error("Error during Data Source initialization:", err)
})
};
......
......@@ -13,7 +13,7 @@ import {
import {Project} from './Project';
import { DateTimeTask } from './DateTimeTask';
export type taskFinishType = "opened"| "in-progress" | "done" |"failed";
export type taskFinishType = "opened"| "progress" | "done" |"failed";
export type priorityType = "A" | "B" |"C";
interface ITask{
......@@ -22,8 +22,6 @@ import { DateTimeTask } from './DateTimeTask';
description: string;
note: string;
createdAt: Date;
dateTimeStart:Date| null;
dateTimeDue:Date| null;
dateTimeDeadLine: Date| null;
dateTimeFactDeadLine: Date| null;
accomplish: taskFinishType;
......@@ -47,10 +45,6 @@ import { DateTimeTask } from './DateTimeTask';
note!: string
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt!: Date;
@Column({ name: 'dateTimeStart', type: Date,nullable: true })
dateTimeStart!: Date ;
@Column({ name: 'dateTimeDue', type: Date,nullable: true })
dateTimeDue!: Date ;
@Column({ name: 'dateTimeDeadLine', type: Date,nullable: true })
dateTimeDeadLine!: Date;
@Column({ name: 'dateTimeFactDeadLine', type: Date,nullable: true })
......
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