Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

1.0 KB
Raw Blame
import { TodoItems } from '../../../src/db/entities/TodoItems.entity';
import { inContext, type FactoryOrm } from './context';
import { createRow } from './rows';
import type { EntityData, EntityDTO } from '@mikro-orm/core';

export type TodoItemRow = EntityDTO<TodoItems>;

/** An open to-do on the trip, appended after the trip's last one. */
export async function makeTodoItem(
  orm: FactoryOrm,
  tripId: number,
  overrides: EntityData<TodoItems> = {},
): Promise<TodoItemRow> {
  const sortOrder =
    overrides.sort_order ??
    (await inContext(orm, async (em) => {
      const last = await em.findOne(
        TodoItems,
        { trip: tripId },
        { orderBy: { sort_order: 'desc' }, disableIdentityMap: true },
      );
      return last?.sort_order == null ? 0 : last.sort_order + 1;
    }));
  return createRow(
    orm,
    TodoItems,
    {
      trip: tripId,
      name: 'Test Todo',
      checked: 0,
      category: null,
      sort_order: sortOrder,
    },
    { ...overrides, sort_order: sortOrder },
  );
}