# All files / src/trpc/routers page.ts

3.03% Statements 1/33
0% Branches 0/6
11.11% Functions 1/9
3.03% Lines 1/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118

    11x

     /**
     * Page Router — CRUD, tree operations
     */

    import { TRPCError } from '@trpc/server';
    import { z } from 'zod';
    import type { ProcedureBuilder } from '../trpc.js';
    import { requirePageRepository } from './_helpers.js';

    export function pageProcedures(publicProcedure: ProcedureBuilder) {
      return {
        getRootPage: publicProcedure
          .input(z.object({ initiativeId: z.string().min(1) }))
          .query(async ({ ctx, input }) => {
            const repo = requirePageRepository(ctx);
            return repo.getOrCreateRootPage(input.initiativeId);
          }),

        getPage: publicProcedure
          .input(z.object({ id: z.string().min(1) }))
          .query(async ({ ctx, input }) => {
            const repo = requirePageRepository(ctx);
            const page = await repo.findById(input.id);
            if (!page) {
              throw new TRPCError({
                code: 'NOT_FOUND',
                message: `Page '${input.id}' not found`,
              });
            }
            return page;
          }),

        getPageUpdatedAtMap: publicProcedure
          .input(z.object({ ids: z.array(z.string().min(1)) }))
          .query(async ({ ctx, input }) => {
            const repo = requirePageRepository(ctx);
            const foundPages = await repo.findByIds(input.ids);
            const map: Record<string, string> = {};
            for (const p of foundPages) {
              map[p.id] = p.updatedAt instanceof Date ? p.updatedAt.toISOString() : String(p.updatedAt);
            }
            return map;
          }),

        listPages: publicProcedure
          .input(z.object({ initiativeId: z.string().min(1) }))
          .query(async ({ ctx, input }) => {
            const repo = requirePageRepository(ctx);
            return repo.findByInitiativeId(input.initiativeId);
          }),

        listChildPages: publicProcedure
          .input(z.object({ parentPageId: z.string().min(1) }))
          .query(async ({ ctx, input }) => {
            const repo = requirePageRepository(ctx);
            return repo.findByParentPageId(input.parentPageId);
          }),

        createPage: publicProcedure
          .input(z.object({
            initiativeId: z.string().min(1),
            parentPageId: z.string().min(1).nullable(),
            title: z.string().min(1),
          }))
          .mutation(async ({ ctx, input }) => {
            const repo = requirePageRepository(ctx);
            const page = await repo.create({
              initiativeId: input.initiativeId,
              parentPageId: input.parentPageId,
              title: input.title,
              content: null,
              sortOrder: 0,
            });
            ctx.eventBus.emit({
              type: 'page:created',
              timestamp: new Date(),
              payload: { pageId: page.id, initiativeId: input.initiativeId, title: input.title },
            });
            return page;
          }),

        updatePage: publicProcedure
          .input(z.object({
            id: z.string().min(1),
            title: z.string().min(1).optional(),
            content: z.string().nullable().optional(),
            sortOrder: z.number().int().optional(),
          }))
          .mutation(async ({ ctx, input }) => {
            const repo = requirePageRepository(ctx);
            const { id, ...data } = input;
            const page = await repo.update(id, data);
            ctx.eventBus.emit({
              type: 'page:updated',
              timestamp: new Date(),
              payload: { pageId: id, initiativeId: page.initiativeId, title: input.title },
            });
            return page;
          }),

        deletePage: publicProcedure
          .input(z.object({ id: z.string().min(1) }))
          .mutation(async ({ ctx, input }) => {
            const repo = requirePageRepository(ctx);
            const page = await repo.findById(input.id);
            await repo.delete(input.id);
            if (page) {
              ctx.eventBus.emit({
                type: 'page:deleted',
                timestamp: new Date(),
                payload: { pageId: input.id, initiativeId: page.initiativeId },
              });
            }
            return { success: true };
          }),
      };
    }