Skip to content

Commit

Permalink
feat(Goal): change project with scope id
Browse files Browse the repository at this point in the history
  • Loading branch information
awinogradov committed May 23, 2023
1 parent 2825ded commit eb81fdd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
7 changes: 7 additions & 0 deletions src/schema/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,10 @@ export const goalUpdateSchema = z.object({
});

export type GoalUpdate = z.infer<typeof goalUpdateSchema>;

export const goalChangeProjectSchema = z.object({
id: z.string(),
projectId: z.string(),
});

export type GoalChangeProject = z.infer<typeof goalChangeProjectSchema>;
16 changes: 15 additions & 1 deletion src/utils/createGoalInDb.ts → src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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,
},
});
};
27 changes: 6 additions & 21 deletions trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }) => {
Expand Down Expand Up @@ -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}>`,
Expand All @@ -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 },
Expand All @@ -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,
Expand Down

0 comments on commit eb81fdd

Please sign in to comment.