47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import type { PrismaClient } from '@prisma/client';
|
|
|
|
export async function getCalendar(
|
|
prisma: PrismaClient,
|
|
userId: string,
|
|
month?: string,
|
|
) {
|
|
const now = new Date();
|
|
let year: number;
|
|
let mon: number;
|
|
if (month) {
|
|
[year, mon] = month.split('-').map(Number);
|
|
} else {
|
|
year = now.getFullYear();
|
|
mon = now.getMonth() + 1;
|
|
}
|
|
const start = new Date(year, mon - 1, 1);
|
|
const end = new Date(year, mon, 1);
|
|
|
|
const days = await prisma.planDay.findMany({
|
|
where: {
|
|
week: { plan: { userId, status: 'ACTIVE' } },
|
|
date: { gte: start, lt: end },
|
|
},
|
|
include: {
|
|
exercises: {
|
|
include: { exercise: { select: { name: true, primaryMuscle: true } } },
|
|
},
|
|
},
|
|
orderBy: { date: 'asc' },
|
|
});
|
|
|
|
return {
|
|
month: `${year}-${String(mon).padStart(2, '0')}`,
|
|
days: days.map((d) => ({
|
|
id: d.id,
|
|
date: d.date!.toISOString(),
|
|
status: d.status,
|
|
dayOfWeek: d.dayOfWeek,
|
|
exercises: d.exercises.map((e) => ({
|
|
name: e.exercise.name,
|
|
muscle: e.exercise.primaryMuscle,
|
|
})),
|
|
})),
|
|
};
|
|
}
|