Skip to content

Commit

Permalink
make changes to mesocycle exercise split days as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyAsh5114 committed Jul 6, 2024
1 parent c8061c7 commit 7792f98
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions src/lib/trpc/routes/workouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ export const workouts = t.router({
endedAt: new Date(),
userBodyweight: input.workoutData.userBodyweight
};
if (input.workoutData.workoutOfMesocycle)

const { workoutOfMesocycle } = input.workoutData;
if (workoutOfMesocycle)
workout.workoutOfMesocycle = {
create: {
mesocycleId: input.workoutData.workoutOfMesocycle.mesocycle.id,
splitDayName: input.workoutData.workoutOfMesocycle.splitDayName
mesocycleId: workoutOfMesocycle.mesocycle.id,
splitDayName: workoutOfMesocycle.splitDayName
}
};
const workoutExercises: Prisma.WorkoutExerciseUncheckedCreateInput[] =
Expand Down Expand Up @@ -179,15 +181,55 @@ export const workouts = t.router({
})
);

// TODO: update mesocycleExerciseSplitDays... oh god

const transactionQueries = [
prisma.workout.create({ data: workout }),
prisma.workoutExercise.createMany({ data: workoutExercises }),
prisma.workoutExerciseSet.createMany({ data: workoutExercisesSets }),
prisma.workoutExerciseMiniSet.createMany({ data: workoutExercisesMiniSets })
];

if (workoutOfMesocycle && workoutOfMesocycle.workoutStatus === undefined) {
const todaysSplitDay = await prisma.mesocycleExerciseSplitDay.findFirst({
where: {
mesocycleId: workoutOfMesocycle.mesocycle.id,
dayIndex: workoutOfMesocycle.dayNumber - 1,
name: workoutOfMesocycle.splitDayName,
mesocycle: { userId: ctx.userId }
},
select: { id: true }
});
if (!todaysSplitDay)
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Related mesocycle exercise split day not found'
});
transactionQueries.push(
...[
prisma.mesocycleExerciseTemplate.deleteMany({
where: {
mesocycleExerciseSplitDay: {
name: workoutOfMesocycle.splitDayName,
mesocycle: {
id: workoutOfMesocycle.mesocycle.id,
userId: ctx.userId
}
}
}
}),
prisma.mesocycleExerciseTemplate.createMany({
data: workoutExercises.map((ex, exerciseIdx) => {
const { workoutId, ...exercise } = ex;
return {
...exercise,
mesocycleExerciseSplitDayId: todaysSplitDay.id,
sets: input.workoutExercisesSets[exerciseIdx].length
};
})
})
]
);
}

await prisma.$transaction(transactionQueries);
return { message: 'Workout created successfully' };
}),
Expand Down

0 comments on commit 7792f98

Please sign in to comment.