diff --git a/src/schema/goal.ts b/src/schema/goal.ts index 970f74613..b53f0c888 100644 --- a/src/schema/goal.ts +++ b/src/schema/goal.ts @@ -165,3 +165,10 @@ export const goalUpdateSchema = z.object({ }); export type GoalUpdate = z.infer; + +export const goalChangeProjectSchema = z.object({ + id: z.string(), + projectId: z.string(), +}); + +export type GoalChangeProject = z.infer; diff --git a/src/utils/createGoalInDb.ts b/src/utils/db.ts similarity index 79% rename from src/utils/createGoalInDb.ts rename to src/utils/db.ts index 286027a13..af14e29f1 100644 --- a/src/utils/createGoalInDb.ts +++ b/src/utils/db.ts @@ -15,7 +15,7 @@ import { prisma } from './prisma'; * @param input goal FormData * @returns new goal id */ -export const createGoalInDb = async (activityId: string, input: GoalCommon) => { +export const createGoal = async (activityId: string, input: GoalCommon) => { const id = nanoid(); await prisma.$executeRaw` @@ -60,3 +60,17 @@ export const createGoalInDb = async (activityId: string, input: GoalCommon) => { }, }); }; + +export const changeGoalProject = async (id: string, newProjectId: string) => { + await prisma.$executeRaw` + UPDATE "Goal" + SET "projectId" = ${newProjectId}, "scopeId" = (SELECT max("scopeId") + 1 FROM "Goal" WHERE "projectId" = ${newProjectId}) + WHERE "id" = ${id}; + `; + + return prisma.goal.findUnique({ + where: { + id, + }, + }); +}; diff --git a/trpc/router/goal.ts b/trpc/router/goal.ts index 80bda4cdb..82ba738b2 100644 --- a/trpc/router/goal.ts +++ b/trpc/router/goal.ts @@ -5,6 +5,7 @@ import { prisma } from '../../src/utils/prisma'; import { protectedProcedure, router } from '../trpcBackend'; import { addCalclulatedGoalsFields, calcGoalsMeta, goalDeepQuery, goalsFilter } from '../queries/goals'; import { + goalChangeProjectSchema, goalCommonSchema, goalUpdateSchema, toogleGoalArchiveSchema, @@ -13,7 +14,7 @@ import { } from '../../src/schema/goal'; import { ToggleSubscriptionSchema } from '../../src/schema/common'; import { connectionMap } from '../queries/connections'; -import { createGoalInDb } from '../../src/utils/createGoalInDb'; +import { createGoal, changeGoalProject } from '../../src/utils/db'; export const goal = router({ suggestions: protectedProcedure.input(z.string()).query(async ({ input }) => { @@ -247,7 +248,7 @@ export const goal = router({ const { activityId } = ctx.session.user; try { - return createGoalInDb(activityId, input); + return createGoal(activityId, input); // await mailServer.sendMail({ // from: `"Fred Foo 👻" <${process.env.MAIL_USER}>`, @@ -260,6 +261,9 @@ export const goal = router({ throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: String(error.message), cause: error }); } }), + changeProject: protectedProcedure.input(goalChangeProjectSchema).mutation(async ({ input }) => { + return changeGoalProject(input.id, input.projectId); + }), update: protectedProcedure.input(goalUpdateSchema).mutation(async ({ ctx, input }) => { const actualGoal = await prisma.goal.findUnique({ where: { id: input.id }, @@ -282,28 +286,9 @@ export const goal = router({ } try { - if (input.parent?.id && actualGoal.projectId !== input.parent.id) { - const project = await prisma.project.findUnique({ - where: { id: input.parent.id }, - }); - - if (!project) return null; - - // FIXME: https://github.com/taskany-inc/issues/issues/627 - const pre = `${project.id}-`; - const lastGoal = await prisma.goal.findFirst({ - where: { id: { contains: pre } }, - orderBy: { createdAt: 'desc' }, - }); - const numId = lastGoal ? Number(lastGoal?.id?.replace(pre, '')) + 1 : 1; - - input.id = `${pre}${numId}`; - } - return prisma.goal.update({ where: { id: actualGoal.id }, data: { - id: input.id, ownerId: input.owner?.id, projectId: input.parent?.id, title: input.title,