Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ model Task {

history TaskHistory[]
comments TaskComment[]
children Task[] @relation("TaskHierarchy")

status TaskStatus @relation(fields: [statusId], references: [id], onDelete: Restrict)
statusId String
Expand All @@ -155,7 +156,6 @@ model Task {

parentId String?
parent Task? @relation("TaskHierarchy", fields: [parentId], references: [id], onDelete: SetNull)
children Task[] @relation("TaskHierarchy")

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down
22 changes: 0 additions & 22 deletions apps/backend/src/prisma/seed/seed-constants.ts

This file was deleted.

58 changes: 46 additions & 12 deletions apps/backend/src/prisma/seed/seed.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,58 @@
import { PrismaClient } from '@prisma/client';
import { usersForSeed } from './seed-constants';
import { hashPassword } from '../../modules/auth/utils/hashPassword/hashPassword';
import { seedTeamMembers } from './seedTeamMembers';
import { seedTeam } from './seedTeam';
import { seedUsers } from './seedUsers';
import { seedProjects } from './seedTeamProjects';
import { seedBoards } from './seedBoards';
import { seedBoardStatusesAndColumns } from './seedBoardColumns';
import { seedTasks } from './seedTasks';
import { seedTaskComments } from './seedTaskComments';
import { seedTaskHistory } from './seedTaskHistory';

const prisma = new PrismaClient();

async function up() {
// create users with different roles
for (const user of usersForSeed) {
const hashedPassword = await hashPassword(user.password);
await prisma.user.create({
data: {
...user,
password: hashedPassword,
},
});
const users = await seedUsers(prisma);

const team = await seedTeam(prisma, users.userTest1.id);

const { teamMemberAdmin, teamMember } = await seedTeamMembers(
prisma,
team.id,
users.userTest2.id,
users.userTest3.id
);

const { project1, project2 } = await seedProjects(prisma, team.id);

const boards = await seedBoards(prisma, project1.id, project2.id);

for (const board of boards) {
const statuses = await seedBoardStatusesAndColumns(prisma, board.id);

const tasks = await seedTasks(prisma, board.id, statuses, teamMemberAdmin.id, teamMember.id);

await seedTaskComments(prisma, tasks, teamMember.id);

await seedTaskHistory(prisma, tasks, teamMemberAdmin.id);
}
}

async function down() {
await prisma.$executeRaw`TRUNCATE TABLE "User" RESTART IDENTITY CASCADE`;
await prisma.$executeRawUnsafe(`
TRUNCATE TABLE
"TaskComment",
"TaskHistory",
"Task",
"BoardColumn",
"TaskStatus",
"Board",
"Project",
"TeamMember",
"Team",
"User"
RESTART IDENTITY CASCADE
`);
}

async function main() {
Expand Down
35 changes: 35 additions & 0 deletions apps/backend/src/prisma/seed/seedBoardColumns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { PrismaClient } from '@prisma/client';

export async function seedBoardStatusesAndColumns(prisma: PrismaClient, boardId: string) {
return prisma.$transaction(async (tx) => {
const statuses = [
{ name: 'Todo', position: 1 },
{ name: 'In Progress', position: 2 },
{ name: 'Done', position: 3 },
];

const createdStatuses = [];

for (const s of statuses) {
const status = await tx.taskStatus.create({
data: {
name: s.name,
boardId,
},
});

await tx.boardColumn.create({
data: {
title: s.name,
position: s.position,
boardId,
statusId: status.id,
},
});

createdStatuses.push(status);
}

return createdStatuses;
});
}
33 changes: 33 additions & 0 deletions apps/backend/src/prisma/seed/seedBoards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { PrismaClient } from '@prisma/client';

export async function seedBoards(prisma: PrismaClient, project1Id: string, project2Id: string) {
const board1 = await prisma.board.create({
data: {
name: 'Frontend React',
projectId: project1Id,
},
});

const board2 = await prisma.board.create({
data: {
name: 'Backend Express',
projectId: project1Id,
},
});

const board3 = await prisma.board.create({
data: {
name: 'Frontend React Native',
projectId: project2Id,
},
});

const board4 = await prisma.board.create({
data: {
name: 'Backend NestJS',
projectId: project2Id,
},
});

return [board1, board2, board3, board4];
}
23 changes: 23 additions & 0 deletions apps/backend/src/prisma/seed/seedTaskComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { type PrismaClient } from '@prisma/client';

export async function seedTaskComments(
prisma: PrismaClient,
tasks: { id: string }[],
teamMemberId: string
) {
const comments = [];

for (const task of tasks) {
const comment = await prisma.taskComment.create({
data: {
text: `Comment for task ${task.id}`,
taskId: task.id,
teamMemberId,
},
});

comments.push(comment);
}

return comments;
}
19 changes: 19 additions & 0 deletions apps/backend/src/prisma/seed/seedTaskHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { type PrismaClient } from '@prisma/client';

export async function seedTaskHistory(
prisma: PrismaClient,
tasks: { id: string }[],
teamMemberId: string
) {
for (const task of tasks) {
await prisma.taskHistory.create({
data: {
field: 'title',
oldValue: { status: 'todo' },
newValue: { status: 'To do' },
taskId: task.id,
teamMemberId,
},
});
}
}
64 changes: 64 additions & 0 deletions apps/backend/src/prisma/seed/seedTasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { type PrismaClient, TaskPriority, TaskType } from '@prisma/client';

export async function seedTasks(
prisma: PrismaClient,
boardId: string,
statuses: { id: string }[],
creatorId: string,
assigneeId: string
) {
const tasks = [];
const priorities = Object.values(TaskPriority);
const types = Object.values(TaskType);

for (let i = 1; i <= 10; i++) {
const status = statuses[i % statuses.length];
const randomPriority = priorities[Math.floor(Math.random() * priorities.length)];
const randomType = types[Math.floor(Math.random() * types.length)];

const task = await prisma.task.create({
data: {
title: `Task ${i}`,
description: `Description for task ${i}`,
type: randomType,
priority: randomPriority,
boardId,
statusId: status.id,
createdById: creatorId,
assignedToId: assigneeId,
},
});

tasks.push(task);

const shouldCreateSubtasks = Math.random() < 0.3;

if (shouldCreateSubtasks) {
const subtasksCount = Math.random() < 0.5 ? 1 : 2;

for (let j = 1; j <= subtasksCount; j++) {
const subPriority = priorities[Math.floor(Math.random() * priorities.length)];
const subType = types[Math.floor(Math.random() * types.length)];
const subStatus = statuses[Math.floor(Math.random() * statuses.length)];

const subtask = await prisma.task.create({
data: {
title: `Task ${i}.${j}`,
description: `Subtask ${j} for task ${i}`,
type: subType,
priority: subPriority,
boardId,
statusId: subStatus.id,
createdById: creatorId,
assignedToId: assigneeId,
parentId: task.id,
},
});

tasks.push(subtask);
}
}
}

return tasks;
}
22 changes: 22 additions & 0 deletions apps/backend/src/prisma/seed/seedTeam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type PrismaClient, TeamMemberRole } from '@prisma/client';

export async function seedTeam(prisma: PrismaClient, ownerId: string) {
return prisma.$transaction(async (tx) => {
const team = await tx.team.create({
data: {
name: 'Test Team',
ownerId,
},
});

await tx.teamMember.create({
data: {
userId: ownerId,
teamId: team.id,
role: TeamMemberRole.OWNER,
},
});

return team;
});
}
26 changes: 26 additions & 0 deletions apps/backend/src/prisma/seed/seedTeamMembers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { type PrismaClient, TeamMemberRole } from '@prisma/client';

export async function seedTeamMembers(
prisma: PrismaClient,
teamId: string,
userAdminId: string,
userMemberId: string
) {
const teamMemberAdmin = await prisma.teamMember.create({
data: {
userId: userAdminId,
teamId,
role: TeamMemberRole.ADMIN,
},
});

const teamMember = await prisma.teamMember.create({
data: {
userId: userMemberId,
teamId,
role: TeamMemberRole.MEMBER,
},
});

return { teamMemberAdmin, teamMember };
}
19 changes: 19 additions & 0 deletions apps/backend/src/prisma/seed/seedTeamProjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { PrismaClient } from '@prisma/client';

export async function seedProjects(prisma: PrismaClient, teamId: string) {
const project1 = await prisma.project.create({
data: {
name: 'Desktop App',
teamId,
},
});

const project2 = await prisma.project.create({
data: {
name: 'Mobile App',
teamId,
},
});

return { project1, project2 };
}
Loading