Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/backend/src/db/queries/workout-plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ function mapAdjustmentRow(r: Record<string, unknown>): PlanAdjustment {
/**
* Returns the single current plan for a user along with its latest version,
* or null if the user has no plan yet.
*
* Response shape matches the frontend CurrentPlanResponse: { plan, latestVersion }.
*/
export async function getCurrentPlan(
pool: pg.Pool,
userId: string,
): Promise<(WorkoutPlan & { latestVersion: PlanVersion }) | null> {
): Promise<{ plan: WorkoutPlan; latestVersion: PlanVersion } | null> {
// Get the most recent plan for the user
const { rows: planRows } = await pool.query(
`SELECT ${PLAN_COLUMNS} FROM workout_plans WHERE user_id = $1
Expand All @@ -149,7 +151,7 @@ export async function getCurrentPlan(
if (versionRows.length === 0) return null;

const latestVersion = mapVersionRow(versionRows[0] as Record<string, unknown>);
return { ...plan, latestVersion };
return { plan, latestVersion };
}

/** Returns a plan by ID, or null if not found. */
Expand Down
18 changes: 10 additions & 8 deletions packages/backend/src/routes/__tests__/workout-plans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ describe('GET /api/workout-plans/current', () => {
it('when user has a plan → 200 with plan + latestVersion', async () => {
const { getCurrentPlan } = await import('../../db/queries/workout-plans.js');
vi.mocked(getCurrentPlan).mockResolvedValueOnce({
id: 'plan-uuid',
userId: 'user-uuid',
name: 'My Plan',
splitType: 'Custom',
activeVersionId: 'version-uuid',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
plan: {
id: 'plan-uuid',
userId: 'user-uuid',
name: 'My Plan',
splitType: 'Custom',
activeVersionId: 'version-uuid',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
latestVersion: {
id: 'version-uuid',
planId: 'plan-uuid',
Expand All @@ -204,7 +206,7 @@ describe('GET /api/workout-plans/current', () => {
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(body.data.id).toBe('plan-uuid');
expect(body.data.plan.id).toBe('plan-uuid');
expect(body.data.latestVersion).toBeDefined();
await app.close();
});
Expand Down
Loading