Skip to content

Commit

Permalink
feat(GoalHistory): db schema and seed data
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed May 29, 2023
1 parent 90793d8 commit 1630024
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 27 deletions.
25 changes: 25 additions & 0 deletions prisma/migrations/20230522102801_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- CreateTable
CREATE TABLE "GoalHistory" (
"id" TEXT NOT NULL,
"goalId" TEXT NOT NULL,
"subject" TEXT NOT NULL,
"action" TEXT NOT NULL,
"previousValue" TEXT,
"nextValue" TEXT,
"activityId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "GoalHistory_id_idx" ON "GoalHistory"("id");

-- CreateIndex
CREATE INDEX "GoalHistory_goalId_idx" ON "GoalHistory"("goalId");

-- AddForeignKey
ALTER TABLE "GoalHistory" ADD CONSTRAINT "GoalHistory_goalId_fkey" FOREIGN KEY ("goalId") REFERENCES "Goal"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "GoalHistory" ADD CONSTRAINT "GoalHistory_activityId_fkey" FOREIGN KEY ("activityId") REFERENCES "Activity"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
73 changes: 46 additions & 27 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,28 @@ model Ghost {
}

model Activity {
id String @id @default(cuid())
ghost Ghost? @relation(fields: [ghostId], references: [id])
ghostId String? @unique
id String @id @default(cuid())
ghost Ghost? @relation(fields: [ghostId], references: [id])
ghostId String? @unique
user User?
filters Filter[]
comments Comment[]
reactions Reaction[]
projects Project[]
estimates Estimate[]
projectParticipant Project[] @relation("projectParticipants")
goalParticipant Goal[] @relation("goalParticipants")
goalWatchers Goal[] @relation("goalWatchers")
goalStargizers Goal[] @relation("goalStargizers")
projectWatchers Project[] @relation("projectWatchers")
projectStargizers Project[] @relation("projectStargizers")
filterStargizers Filter[] @relation("filterStargizers")
goalOwner Goal[] @relation("goalOwner")
goalIssuer Goal[] @relation("goalIssuer")
settings Settings @relation(fields: [settingsId], references: [id])
settingsId String @unique
projectParticipant Project[] @relation("projectParticipants")
goalParticipant Goal[] @relation("goalParticipants")
goalWatchers Goal[] @relation("goalWatchers")
goalStargizers Goal[] @relation("goalStargizers")
projectWatchers Project[] @relation("projectWatchers")
projectStargizers Project[] @relation("projectStargizers")
filterStargizers Filter[] @relation("filterStargizers")
goalOwner Goal[] @relation("goalOwner")
goalIssuer Goal[] @relation("goalIssuer")
settings Settings @relation(fields: [settingsId], references: [id])
settingsId String @unique
tags Tag[]
goalActions GoalHistory[] @relation("goalActions")
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
Expand Down Expand Up @@ -170,28 +171,29 @@ model Goal {
key Boolean?
personal Boolean?
private Boolean?
archived Boolean? @default(false)
archived Boolean? @default(false)
priority String?
estimate Estimate[]
project Project? @relation("projectGoals", fields: [projectId], references: [id])
project Project? @relation("projectGoals", fields: [projectId], references: [id])
projectId String?
teamId String?
state State? @relation("goalState", fields: [stateId], references: [id])
state State? @relation("goalState", fields: [stateId], references: [id])
stateId String?
activity Activity? @relation("goalIssuer", fields: [activityId], references: [id])
activity Activity? @relation("goalIssuer", fields: [activityId], references: [id])
activityId String?
owner Activity? @relation("goalOwner", fields: [ownerId], references: [id])
owner Activity? @relation("goalOwner", fields: [ownerId], references: [id])
ownerId String?
participants Activity[] @relation("goalParticipants")
watchers Activity[] @relation("goalWatchers")
stargizers Activity[] @relation("goalStargizers")
participants Activity[] @relation("goalParticipants")
watchers Activity[] @relation("goalWatchers")
stargizers Activity[] @relation("goalStargizers")
comments Comment[]
reactions Reaction[]
tags Tag[]
dependsOn Goal[] @relation("dependsOn")
blocks Goal[] @relation("dependsOn")
relatedTo Goal[] @relation("connected")
connected Goal[] @relation("connected")
dependsOn Goal[] @relation("dependsOn")
blocks Goal[] @relation("dependsOn")
relatedTo Goal[] @relation("connected")
connected Goal[] @relation("connected")
history GoalHistory[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
Expand Down Expand Up @@ -306,8 +308,25 @@ model Job {
delay Int?
retry Int?
cron String?
error String?
error String?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}

model GoalHistory {
id String @id @default(cuid())
goal Goal @relation(fields: [goalId], references: [id])
goalId String
subject String
action String
previousValue String?
nextValue String?
activity Activity @relation("goalActions", fields: [activityId], references: [id])
activityId String
createdAt DateTime @default(now())
@@index([id])
@@index([goalId])
}
14 changes: 14 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ seed('Default projects', async () => {
},
});
}

const [goal] = allGoals;

// eslint-disable-next-line no-await-in-loop
await prisma.goalHistory.create({
data: {
goalId: goal.id,
subject: 'state',
action: 'change',
previousValue: 'InProgress',
nextValue: 'Critical',
activityId: sample(allUsers).activityId,
},
});
}
}
});
11 changes: 11 additions & 0 deletions src/schema/goalHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import z from 'zod';

export const goalHistorySchema = z.object({
goalId: z.string(),
action: z.enum(['edit', 'remove', 'add', 'delete', 'archive', 'change']),
subject: z.enum(['title', 'description', 'participants', 'state', 'tags']),
nextValue: z.string(),
previousValue: z.string().optional(),
});

export type GoalHistory = z.infer<typeof goalHistorySchema>;
10 changes: 10 additions & 0 deletions trpc/queries/goals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ export const goalDeepQuery = {
comments: true,
},
},
history: {
include: {
activity: {
include: {
user: true,
ghost: true,
},
},
},
},
} as const;

export const addCalclulatedGoalsFields = (goal: any, activityId: string) => {
Expand Down

0 comments on commit 1630024

Please sign in to comment.