Skip to content

Commit

Permalink
fix(PartnershipProjects): disallow works with current project as part…
Browse files Browse the repository at this point in the history
…nership
  • Loading branch information
LamaEats committed Aug 19, 2024
1 parent 778a0c7 commit 2ea3bc2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/components/GoalSidebar/GoalSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export const GoalSidebar: FC<GoalSidebarProps> = ({ goal, onGoalTransfer, onGoal
{nullable(goal._isEditable, () => (
<GoalParentDropdown
mode="single"
filter={goal.projectId != null ? [goal.projectId] : []}
placement="bottom-start"
placeholder={tr('Type project title')}
onChange={({ id }) => addPartnerProject(id)}
Expand Down Expand Up @@ -300,6 +301,7 @@ export const GoalSidebar: FC<GoalSidebarProps> = ({ goal, onGoalTransfer, onGoal
{nullable(goal._isEditable, () => (
<IssueMeta>
<GoalParentDropdown
filter={goal.projectId != null ? [goal.projectId] : []}
mode="single"
placement="bottom-start"
placeholder={tr('Type project title')}
Expand Down
7 changes: 6 additions & 1 deletion src/utils/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,12 @@ export const updateLinkedGoalsByProject = async (projectId: string, activityId:
data: { deleted: true },
});

await Promise.all([archiveGoals, createHistoryRecords, updateCriterias]);
const removePartnershipGoals = ctx.$executeRaw`
DELETE FROM "_partnershipProjects"
WHERE "B" = ${projectId};
`;

await Promise.all([archiveGoals, createHistoryRecords, updateCriterias, removePartnershipGoals]);

// get all goals which current project's goals as criteria
const res = await ctx.goalAchieveCriteria.findMany({
Expand Down
23 changes: 23 additions & 0 deletions trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ export const goal = router({

if (!actualGoal || !actualProject || actualProject.personal) return null;

if (actualGoal.projectId === actualProject.id) {
throw new TRPCError({
code: 'PRECONDITION_FAILED',
message: tr('Cannot transfer goal to selected project'),
});
}

const { activityId, role } = ctx.session.user;

try {
Expand Down Expand Up @@ -824,6 +831,7 @@ export const goal = router({
participants: { include: { user: true, ghost: true } },
watchers: { include: { user: true, ghost: true } },
activity: { include: { user: true, ghost: true } },
partnershipProjects: true,
owner: { include: { user: true, ghost: true } },
state: true,
project: true,
Expand All @@ -849,6 +857,9 @@ export const goal = router({
nextValue: archived ? 'move to archive' : 'move from archive',
},
},
partnershipProjects: {
disconnect: actualGoal.partnershipProjects.map(({ id }) => ({ id })),
},
},
include: {
state: true,
Expand Down Expand Up @@ -1531,6 +1542,18 @@ export const goal = router({
.input(togglePartnerProjectSchema)
.use(goalEditAccessMiddleware)
.mutation(async ({ input, ctx }) => {
const currentGoal = await prisma.goal.findFirst({
where: { id: input.id },
include: { partnershipProjects: true },
});

if (currentGoal?.partnershipProjects.find(({ id }) => id === input.projectId)) {
throw new TRPCError({
code: 'PRECONDITION_FAILED',
message: tr('Cannot link goal to selected project'),
});
}

try {
const updatedGoal = await prisma.goal.update({
where: { id: input.id },
Expand Down
4 changes: 3 additions & 1 deletion trpc/router/router.i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"Cannot create goal. Project now is archived": "Cannot create goal. Project {title} ({id}) now is archived",
"These bindings is already exist": "These bindings is already exist",
"No filter with id": "No filter with id `{id}`"
"No filter with id": "No filter with id `{id}`",
"Cannot link goal to selected project": "Cannot link goal to selected project",
"Cannot transfer goal to selected project": "Cannot transfer goal to selected project"
}
4 changes: 3 additions & 1 deletion trpc/router/router.i18n/ru.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"Cannot create goal. Project now is archived": "Невозможно создать цель. Проект {title} ({id}) заархивирован",
"These bindings is already exist": "Такая связь уже сущевует",
"No filter with id": "Нет фильтра с id `{id}`"
"No filter with id": "Нет фильтра с id `{id}`",
"Cannot link goal to selected project": "Невозможно связать текущую цель с выбранным проектом",
"Cannot transfer goal to selected project": "Невозможно перенести цель в выбранный проект"
}

0 comments on commit 2ea3bc2

Please sign in to comment.