Skip to content

Commit 8265ee9

Browse files
committed
Dev
DEV
1 parent 52996da commit 8265ee9

12 files changed

+173
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import ProjectEntity from '@entities/project/project.entity';
22
import TaskEntity from '@entities/task/task.entity';
3-
import {
4-
Column,
5-
Entity,
6-
ManyToOne,
7-
OneToMany,
8-
PrimaryGeneratedColumn,
9-
} from 'typeorm';
3+
import { Entity, OneToMany, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
104

115
@Entity()
126
export default class GanttEntity {
@@ -16,6 +10,6 @@ export default class GanttEntity {
1610
@OneToMany(() => TaskEntity, (taskEntity) => taskEntity.gantt)
1711
tasks: TaskEntity[];
1812

19-
@ManyToOne(() => ProjectEntity, (projectEntity) => projectEntity.gantt)
13+
@OneToOne(() => ProjectEntity, (projectEntity) => projectEntity.gantt)
2014
project: ProjectEntity;
2115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import GanttEntity from '@entities/gantt/gant.entity';
2+
import { IGetGanttDATA } from '@entities/gantt/gantt.interface';
3+
import { Injectable } from '@nestjs/common';
4+
import { InjectRepository } from '@nestjs/typeorm';
5+
import { RecordNotFoundError } from 'src/shared/errors';
6+
import { Repository } from 'typeorm';
7+
8+
@Injectable()
9+
export default class GanttDAO {
10+
constructor(
11+
@InjectRepository(GanttEntity)
12+
private ganttRepository: Repository<GanttEntity>,
13+
) {}
14+
15+
public async getGanttWithTasks(args: IGetGanttDATA) {
16+
const gantWithTaks = await this.ganttRepository.findOne({
17+
where: args,
18+
relations: { tasks: true },
19+
});
20+
21+
if (gantWithTaks === null)
22+
throw new RecordNotFoundError('Gantt record not found');
23+
24+
return gantWithTaks;
25+
}
26+
27+
public async deleteGantt(args: IGetGanttDATA) {
28+
await this.getGanttWithTasks(args);
29+
await this.ganttRepository.delete(args);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from '@nestjs/common';
2+
import GanttDAO from '@entities/gantt/gantt.dao';
3+
import { TypeOrmModule } from '@nestjs/typeorm';
4+
import GanttEntity from '@entities/gantt/gant.entity';
5+
6+
@Module({
7+
imports: [TypeOrmModule.forFeature([GanttEntity])],
8+
providers: [GanttDAO],
9+
exports: [GanttDAO],
10+
})
11+
export default class GanttEntityModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import ProjectEntity from '@entities/project/project.entity';
2+
3+
export type IGetGanttDATA =
4+
| {
5+
ganttID: number;
6+
}
7+
| {
8+
project: {
9+
projectID: number;
10+
};
11+
}
12+
| {
13+
ganttID: number;
14+
project: {
15+
projectID: number;
16+
};
17+
};

packages/api/src/database/entity/project/project.entity.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default class ProjectEntity {
4848
)
4949
teamMembers: TeamMemberEntity[];
5050

51-
@OneToMany(() => GanttEntity, (ganttEntity) => ganttEntity.project)
51+
@OneToOne(() => GanttEntity, (ganttEntity) => ganttEntity.project)
52+
@JoinColumn({ name: 'ganttFID', referencedColumnName: 'ganttID' })
5253
gantt: GanttEntity;
5354
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from '@nestjs/common';
2+
import { TypeOrmModule } from '@nestjs/typeorm';
3+
import TaskDAO from '@entities/task/task.dao';
4+
import TaskEntity from '@entities/task/task.entity';
5+
6+
@Module({
7+
imports: [TypeOrmModule.forFeature([TaskEntity])],
8+
providers: [TaskDAO],
9+
exports: [TaskDAO],
10+
})
11+
export default class TaskEntityModule {}

packages/api/src/database/entity/task/task.entity.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import GanttEntity from '@entities/gantt/gant.entity';
2-
import { PredecessorType } from './../../../../../shared/interfaces/project/task.interface';
2+
import { PredecessorType } from 'shared/interfaces';
33
import {
44
Entity,
55
PrimaryGeneratedColumn,

packages/api/src/database/entity/task/task.interface.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import GanttEntity from '@entities/gantt/gant.entity';
21
import TaskEntity from '@entities/task/task.entity';
3-
import { PredecessorType } from 'shared/interfaces/project/task.interface';
2+
3+
type TaskKeys = keyof TaskEntity;
4+
5+
export interface ITask extends Pick<TaskEntity, TaskKeys> {}
46

57
export type IGetTaskDATA =
68
| {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Controller } from '@nestjs/common';
2+
import GanttFeatureService from './gantt.feature.service';
3+
import { ITasChangeDTO } from './gantt.feature.interface';
4+
5+
@Controller('')
6+
export default class GanttFeatureController {
7+
constructor(private ganttFeatureService: GanttFeatureService) {}
8+
9+
public async applyChanges(data: ITasChangeDTO[]) {
10+
return await this.ganttFeatureService.applyChanges(data);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ITask } from './../../database/entity/task/task.interface';
2+
3+
type ChangeState = 'Unchanged' | 'Deleted' | 'Updated' | 'Created';
4+
5+
export interface ITasChangeDTO
6+
extends Omit<ITask, 'taskID' | 'gantt' | 'createdAt' | 'updatedAt'> {
7+
taskID?: number;
8+
ganttID?: number;
9+
state: ChangeState;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from '@nestjs/common';
2+
import GanttFeatureService from './gantt.feature.service';
3+
import GanttFeatureController from './gantt.feature.controller';
4+
import DatabaseModule from 'src/database/database.module';
5+
6+
@Module({
7+
imports: [DatabaseModule],
8+
providers: [GanttFeatureService],
9+
controllers: [GanttFeatureController],
10+
})
11+
export default class GanttFeatureModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Injectable } from '@nestjs/common';
2+
import GanttDAO from '@entities/gantt/gantt.dao';
3+
import TaskDAO from '@entities/task/task.dao';
4+
import { ITasChangeDTO } from './gantt.feature.interface';
5+
6+
@Injectable()
7+
export default class GanttFeatureService {
8+
constructor(
9+
private ganttDAO: GanttDAO,
10+
private taskDAO: TaskDAO,
11+
) {}
12+
13+
public async applyChanges(data: ITasChangeDTO[]) {
14+
const updatedTasks: any[] = [];
15+
const deletedTasks: any[] = [];
16+
const createdTasks: any[] = [];
17+
18+
for (const task of data) {
19+
if (task.state === 'Unchanged') continue;
20+
21+
switch (task.state) {
22+
case 'Created':
23+
const { taskID, ...rest } = task;
24+
createdTasks.push(rest);
25+
break;
26+
27+
case 'Deleted':
28+
if (task.taskID === undefined)
29+
throw new Error("Data missing 'taskID' attribute");
30+
31+
const gantt = task.ganttID
32+
? { gantt: { ganttID: task.ganttID } }
33+
: {};
34+
35+
deletedTasks.push({
36+
taskID: task.taskID,
37+
gantt,
38+
});
39+
break;
40+
41+
case 'Updated':
42+
if (task.taskID === undefined)
43+
throw new Error("Data missing 'taskID' attribute");
44+
45+
updatedTasks.push(task);
46+
47+
default:
48+
throw new Error('Unknown error');
49+
}
50+
}
51+
52+
const [createdTaskEntities, updatedTaskEntites, deletedTaskEntites] =
53+
await Promise.all([
54+
this.taskDAO.bulkCreateTaskRecords(createdTasks),
55+
this.taskDAO.bulkUpdateTaskRecords(updatedTasks),
56+
this.taskDAO.bulkDeleteTaskRecords(deletedTasks),
57+
]).then((res) => res);
58+
59+
return { createdTaskEntities, updatedTaskEntites, deletedTaskEntites };
60+
}
61+
}

0 commit comments

Comments
 (0)