Skip to content

Commit

Permalink
fix(ProjectSettingsPage): forbidden to set a project as a parent if i…
Browse files Browse the repository at this point in the history
…t is in a child
  • Loading branch information
IgorGoryany committed Aug 19, 2024
1 parent a274c93 commit a57e399
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/components/ProjectSettingsPage/ProjectSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const ProjectSettingsPage = ({ user, ssrTime, params: { id } }: ExternalP
const project = trpc.project.getById.useQuery({ id });

const { updateProject, deleteProject, transferOwnership } = useProjectResource(id);
const { data: childrenIds = [] } = trpc.v2.project.childrenIds.useQuery({ in: [{ id }] });

const {
handleSubmit,
Expand Down Expand Up @@ -259,7 +260,7 @@ export const ProjectSettingsPage = ({ user, ssrTime, params: { id } }: ExternalP
mode="single"
placement="bottom-start"
onChange={onProjectAdd}
filter={[id]}
filter={[id, ...childrenIds.map(({ id }) => id)]}
value={value}
renderTrigger={(props) => (
<IconPlusCircleOutline
Expand Down
1 change: 1 addition & 0 deletions src/pages/projects/[id]/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const getServerSideProps = declareSsrProps(
async ({ ssrHelpers, params: { id } }) => {
try {
const project = await ssrHelpers.project.getById.fetch({ id });
await ssrHelpers.v2.project.childrenIds.fetch({ in: [{ id }] });

if (!project) {
throw new TRPCError({ code: 'NOT_FOUND' });
Expand Down
4 changes: 4 additions & 0 deletions src/schema/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export const projectDeepInfoSchema = queryWithFiltersSchema.extend({
id: z.string(),
});

export const projectsChildrenIdsSchema = z.object({
in: z.array(z.object({ id: z.string() })),
});

export const projectSuggestionsSchema = z.object({
query: z.string(),
take: z.number().optional(),
Expand Down
13 changes: 11 additions & 2 deletions trpc/router/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,17 @@ export const project = router({
.input(projectUpdateSchema)
.use(projectEditAccessMiddleware)
.mutation(async ({ input: { id, parent, accessUsers, ...data }, ctx }) => {
const project = await prisma.project.findUnique({
where: { id },
const project = await prisma.project.findFirst({
where: {
id,
AND: {
children: {
none: {
id: { in: parent?.map(({ id }) => id) },
},
},
},
},
include: {
teams: true,
parent: true,
Expand Down
7 changes: 6 additions & 1 deletion trpc/router/projectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { sql } from 'kysely';
import { jsonBuildObject } from 'kysely/helpers/postgres';

import { router, protectedProcedure } from '../trpcBackend';
import { projectSuggestionsSchema, userProjectsSchema } from '../../src/schema/project';
import { projectsChildrenIdsSchema, projectSuggestionsSchema, userProjectsSchema } from '../../src/schema/project';
import {
getProjectsByIds,
getStarredProjectsIds,
getProjectSuggestions,
getUserProjectsQuery,
getUserProjectsWithGoals,
getWholeGoalCountByProjectIds,
getChildrenProjectsId,
} from '../queries/projectV2';
import { queryWithFiltersSchema } from '../../src/schema/common';
import {
Expand Down Expand Up @@ -141,6 +142,10 @@ export const project = router({
}
}),

childrenIds: protectedProcedure.input(projectsChildrenIdsSchema).query(async ({ input }) => {
return getChildrenProjectsId(input).execute();
}),

userProjectsWithGoals: protectedProcedure
.input(
z.object({
Expand Down

0 comments on commit a57e399

Please sign in to comment.