Skip to content

Commit

Permalink
fix: fix priority sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisVorop committed Jul 15, 2023
1 parent 168e1b0 commit 6c56f00
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 33 deletions.
11 changes: 11 additions & 0 deletions prisma/migrations/20230714171654_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- Made the column `priority` on table `Goal` required. This step will fail if there are existing NULL values in that column.
*/
-- DropIndex
DROP INDEX "Flow_title_key";

-- AlterTable
ALTER TABLE "Goal" ALTER COLUMN "priority" SET NOT NULL;
24 changes: 24 additions & 0 deletions prisma/migrations/20230714181336_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Warnings:
- A unique constraint covering the columns `[title]` on the table `Flow` will be added. If there are existing duplicate values, this will fail.
- Added the required column `priorityId` to the `Goal` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Goal" ADD COLUMN "priorityId" INTEGER NOT NULL;

-- CreateTable
CREATE TABLE "Priority" (
"id" SERIAL NOT NULL,
"title" TEXT NOT NULL,
"value" INTEGER NOT NULL,

CONSTRAINT "Priority_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Flow_title_key" ON "Flow"("title");

-- AddForeignKey
ALTER TABLE "Goal" ADD CONSTRAINT "Goal_priorityId_fkey" FOREIGN KEY ("priorityId") REFERENCES "Priority"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
15 changes: 12 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ model EstimateToGoal {
@@index([estimateId])
}

model Priority {
id Int @id @default(autoincrement())
title String
value Int
Goal Goal[]
}

model Goal {
id String @id @default(cuid())
scopeId Int @default(1)
Expand All @@ -194,7 +201,9 @@ model Goal {
personal Boolean?
private Boolean?
archived Boolean? @default(false)
priority String?
priority String
priorityExact Priority @relation(fields: [priorityId], references: [id])
priorityId Int
estimate EstimateToGoal[]
project Project? @relation("projectGoals", fields: [projectId], references: [id])
projectId String?
Expand All @@ -219,8 +228,8 @@ model Goal {
goalAchiveCriteria GoalAchieveCriteria[] @relation("GoalCriterion")
goalAsCriteria GoalAchieveCriteria? @relation("GoalAsCriteria")
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@unique([projectId, scopeId])
@@index([ownerId])
Expand Down
88 changes: 60 additions & 28 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,34 @@ import { keyPredictor } from '../src/utils/keyPredictor';

const adminEmail = process.env.ADMIN_EMAIL || 'tony@taskany.org';
const adminPassword = process.env.ADMIN_PASSWORD || 'taskany';
const priorities = ['Highest', 'High', 'Medium', 'Low'];
// const priorities = ['Highest', 'High', 'Medium', 'Low'];
const prioritiesExact = [
{
id: 4,
title: 'Highest',
value: 4,
},
{
id: 3,
title: 'High',
value: 3,
},
{
id: 2,
title: 'Medium',
value: 2,
},
{
id: 1,
title: 'Low',
value: 1,
},
];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sample = (arr: any[]) => arr[Math.floor(Math.random() * arr.length)];
function sample<T>(arr: T[]) {
return arr[Math.floor(Math.random() * arr.length)];
}

async function seed(title: string, cb: () => void) {
console.log('SEED:', title, '...');
Expand All @@ -23,6 +48,7 @@ assert(adminPassword, "Admin's password isn't provided. Check your environment v

let allUsers: User[];
let tags: Tag[];
// let priorityExact: Priority[];

const init = (async () => {
allUsers = await Promise.all(
Expand Down Expand Up @@ -123,13 +149,13 @@ const init = (async () => {
prisma.tag.create({
data: {
title: 'frontend',
activityId: sample(allUsers).activityId,
activityId: sample(allUsers).activityId ?? '',
},
}),
prisma.tag.create({
data: {
title: 'backend',
activityId: sample(allUsers).activityId,
activityId: sample(allUsers).activityId ?? '',
},
}),
]);
Expand Down Expand Up @@ -169,6 +195,8 @@ seed('Default projects', async () => {

await init;

await Promise.all(prioritiesExact.map((priority) => prisma.priority.create({ data: priority })));

const allProjects = await Promise.all(
[
['Frontend', sample(allUsers).activityId],
Expand All @@ -181,14 +209,14 @@ seed('Default projects', async () => {
['Finance department', sample(allUsers).activityId],
['Social promotion team', sample(allUsers).activityId],
['Cyber security', sample(allUsers).activityId],
].map(([title, activityId]: string[]) =>
].map(([title, activityId]) =>
prisma.project.create({
data: {
id: keyPredictor(title),
title,
id: keyPredictor(title ?? ''),
title: title ?? '',
flowId: f.id,
description: faker.lorem.sentence(5),
activityId,
activityId: activityId ?? '',
},
}),
),
Expand All @@ -209,29 +237,33 @@ seed('Default projects', async () => {
[faker.lorem.words(2), faker.lorem.sentence(5), sample(allUsers).activityId],
[faker.lorem.words(2), faker.lorem.sentence(5), sample(allUsers).activityId],
// eslint-disable-next-line no-loop-func
].map(([title, description, activityId]: string[], index) =>
prisma.goal.create({
].map(([title, description, activityId], index) => {
const priority = sample(prioritiesExact);

return prisma.goal.create({
data: {
scopeId: index + 1,
title,
description,
title: title ?? '',
description: description ?? '',
projectId: project.id,
activityId,
ownerId: activityId,
priority: sample(priorities),
priority: priority.title,
priorityId: priority.id,
// priorityExact: priority,
participants: {
connect:
Math.random() > 0.5
? [
{
id: sample(allUsers)?.activityId as string,
id: sample(allUsers)?.activityId ?? '',
},
{
id: sample(allUsers)?.activityId as string,
id: sample(allUsers)?.activityId ?? '',
},
]
: {
id: sample(allUsers)?.activityId as string,
id: sample(allUsers)?.activityId ?? '',
},
},
stateId: sample(f.states)?.id,
Expand Down Expand Up @@ -261,9 +293,9 @@ seed('Default projects', async () => {
[faker.lorem.sentence(5), sample(allUsers).activityId],
[faker.lorem.sentence(5), sample(allUsers).activityId],
[faker.lorem.sentence(5), sample(allUsers).activityId],
].map(([description, activityId]: string[]) => ({
description,
activityId,
].map(([description, activityId]) => ({
description: description ?? '',
activityId: activityId ?? '',
})),
},
},
Expand All @@ -273,8 +305,8 @@ seed('Default projects', async () => {
blocks: true,
relatedTo: true,
},
}),
),
});
}),
);
for (const goal of allGoals) {
// eslint-disable-next-line no-await-in-loop
Expand All @@ -301,16 +333,16 @@ seed('Default projects', async () => {
history: {
createMany: {
data: [
[faker.lorem.sentence(2), goal.title, sample(allUsers).activityId, 'title'],
[faker.lorem.sentence(2), goal.title, sample(allUsers).activityId ?? '', 'title'],
[
faker.lorem.sentence(5),
goal.description,
sample(allUsers).activityId,
sample(allUsers).activityId ?? '',
'description',
],
[sample(allUsers).activityId, null, goal.activityId, 'participants'],
[null, sample(allGoals).id, sample(allUsers).activityId, 'dependencies'],
[sample(allGoals).id, null, sample(allUsers).activityId, 'dependencies'],
[sample(allUsers).activityId ?? '', null, goal.activityId ?? '', 'participants'],
[null, sample(allGoals).id, sample(allUsers).activityId ?? '', 'dependencies'],
[sample(allGoals).id, null, sample(allUsers).activityId ?? '', 'dependencies'],
].map(([previousValue, nextValue, activityId, subject]) => {
let action = 'add';

Expand All @@ -321,11 +353,11 @@ seed('Default projects', async () => {
}

return {
subject,
subject: subject ?? '',
previousValue,
nextValue,
action,
activityId,
activityId: activityId ?? '',
};
}),
},
Expand Down
2 changes: 1 addition & 1 deletion trpc/queries/goals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const goalsFilter = (

if (data.sort?.priority) {
orderBy.push({
priority: data.sort.priority,
priorityId: data.sort.priority,
});
}

Expand Down
2 changes: 1 addition & 1 deletion trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ export const goal = router({
title: input.title,
description: input.description,
stateId: input.state?.id,
priority: input.priority,
priority: input.priority ?? '',
estimate: correctEstimate?.id
? {
create: {
Expand Down

0 comments on commit 6c56f00

Please sign in to comment.