Skip to content

Commit

Permalink
feat(ProjectListItem): average project score
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Jul 20, 2023
1 parent 1103386 commit 54cd60b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 11 deletions.
2 changes: 2 additions & 0 deletions prisma/migrations/20230718153020_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Project" ADD COLUMN "averageScore" INTEGER;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ model Project {
tags Tag[]
watchers Activity[] @relation("projectWatchers")
stargizers Activity[] @relation("projectStargizers")
averageScore Int?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
Expand Down
1 change: 1 addition & 0 deletions src/components/DashboardPage/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export const DashboardPage = ({ user, ssrTime }: ExternalPageProps) => {
participants={group.project?.participants}
starred={group.project?._isStarred}
watching={group.project?._isWatching}
averageScore={group.project?.averageScore}
/>
</NextLink>
</ProjectListContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const ExploreProjectsPage = ({ user, ssrTime }: ExternalPageProps) => {
starred={p._isStarred}
watching={p._isWatching}
participants={p.participants}
averageScore={p.averageScore}
/>
</NextLink>
)),
Expand Down
1 change: 1 addition & 0 deletions src/components/ExporeProjectsPage/ExporeProjectsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const ExploreProjectsPage = ({ user, ssrTime }: ExternalPageProps) => {
starred={p._isStarred}
watching={p._isWatching}
participants={p.participants}
averageScore={p.averageScore}
/>
</NextLink>
)),
Expand Down
11 changes: 8 additions & 3 deletions src/components/ProjectListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, ReactNode, forwardRef } from 'react';
import { EyeIcon, StarFilledIcon, Text, nullable } from '@taskany/bricks';
import { CircleProgressBar, EyeIcon, StarFilledIcon, Text, nullable } from '@taskany/bricks';
import styled from 'styled-components';

import { ActivityByIdReturnType } from '../../trpc/inferredTypes';
Expand All @@ -18,20 +18,24 @@ interface ProjectListItemProps {
watching?: boolean;
className?: string;
disabled?: boolean;
averageScore: number | null;
}

const StyledTitleCell = styled(TableCell)`
justify-content: space-between;
`;

export const ProjectListContainer: FC<{ children: ReactNode; offset?: number }> = ({ children, offset = 0 }) => (
<Table columns={5} offset={offset}>
<Table columns={6} offset={offset}>
{children}
</Table>
);

export const ProjectListItem = forwardRef<HTMLDivElement, ProjectListItemProps>(
({ as = 'div', children, title, owner, participants, starred, watching, className, ...props }, ref) => (
(
{ as = 'div', children, title, owner, participants, starred, watching, averageScore, className, ...props },
ref,
) => (
<TableRow as={as} className={className} ref={ref} {...props}>
<StyledTitleCell>
<Text size="l" weight="bold">
Expand All @@ -47,6 +51,7 @@ export const ProjectListItem = forwardRef<HTMLDivElement, ProjectListItemProps>(
</TableCell>

<TableCell>{nullable(participants, (p) => (p.length ? <UserGroup users={p} /> : null))}</TableCell>
<TableCell>{averageScore != null ? <CircleProgressBar value={averageScore} size="m" /> : null}</TableCell>

<TableCell>
{nullable(starred, () => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export const ProjectListItemCollapsable: React.FC<ProjectListItemCollapsableProp
starred={project._isStarred}
watching={project._isWatching}
disabled={!onClickEnabled}
averageScore={project.averageScore}
>
<StyledProjectListItemActionsContainer>
<StyledProjectListItemAction forceVisibility={!collapsedGoals}>
Expand Down
38 changes: 30 additions & 8 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { nanoid } from 'nanoid';
import { z } from 'zod';
import { GoalHistory, Comment, Activity, User, Goal } from '@prisma/client';
import { TRPCError } from '@trpc/server';

import { GoalCommon, GoalUpdate, dependencyKind } from '../schema/goal';
import { addCalclulatedGoalsFields, calcAchievedWeight } from '../../trpc/queries/goals';
import { HistoryRecordWithActivity, HistoryRecordMeta, HistoryRecordSubject, HistoryAction } from '../types/history';
import { HistoryRecordWithActivity, HistoryRecordSubject, HistoryAction } from '../types/history';

import { prisma } from './prisma';
import { castToMetaDto, subjectToEnumValue } from './goalHistory';
import { subjectToEnumValue } from './goalHistory';

export const findOrCreateEstimate = async (
estimate: GoalCommon['estimate'] | GoalUpdate['estimate'],
Expand Down Expand Up @@ -316,10 +317,31 @@ export const updateGoalWithCalculatedWeight = async (goalId: string) => {
},
});

await prisma.goal.update({
where: { id: goalId },
data: {
completedCriteriaWeight: criteriaList.length ? calcAchievedWeight(criteriaList) : null,
},
});
try {
await prisma.$transaction(async (ctx) => {
// update goal score by criteria list
const updatedGoal = await ctx.goal.update({
where: { id: goalId },
data: {
completedCriteriaWeight: criteriaList.length ? calcAchievedWeight(criteriaList) : null,
},
});

if (!updatedGoal) {
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR' });
}

// update project score after goal score update
if (updatedGoal.projectId) {
await ctx.$executeRaw`
UPDATE "Project"
SET "averageScore" = (SELECT AVG("completedCriteriaWeight") FROM "Goal"
WHERE "projectId" = ${updatedGoal.projectId})
WHERE "id" = ${updatedGoal.projectId};
`;
}
});
} catch (error: any) {
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', cause: error, message: error?.message });
}
};

0 comments on commit 54cd60b

Please sign in to comment.