All files / lib/lambda getAttachmentArchive.ts

93.33% Statements 14/15
75% Branches 9/12
100% Functions 3/3
93.33% Lines 14/15

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                            1x                   6x                           1x                 3x     3x         3x             2x 1x 8x 8x 2x     6x     1x             2x          
import { APIGatewayEvent } from "shared-types";
import { opensearch } from "shared-types";
import { z } from "zod";
 
import { sendAttachmentArchiveRebuildRequest } from "../attachment-archive/rebuild-queue";
import { getRequestedAttachmentArchiveStatus } from "./attachmentArchive-lib";
import {
  authenticatedMiddy,
  canViewPackage,
  ContextWithPackage,
  fetchChangelog,
  fetchPackage,
} from "./middleware";
 
const getAttachmentArchiveEventSchema = z
  .object({
    body: z
      .object({
        id: z.string(),
        scope: z.enum(["all", "section"]),
        sectionId: z.string().optional(),
      })
      .strict()
      .superRefine((value, ctx) => {
        Iif (value.scope === "section" && !value.sectionId) {
          ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: "sectionId is required when scope is 'section'",
            path: ["sectionId"],
          });
        }
      }),
  })
  .passthrough();
 
export type GetAttachmentArchiveEvent = APIGatewayEvent &
  z.infer<typeof getAttachmentArchiveEventSchema>;
 
export const handler = authenticatedMiddy({
  opensearch: true,
  setToContext: true,
  eventSchema: getAttachmentArchiveEventSchema,
})
  .use(fetchPackage({ setToContext: true }))
  .use(canViewPackage())
  .use(fetchChangelog({ setToContext: true }))
  .handler(async (event: GetAttachmentArchiveEvent, context: ContextWithPackage) => {
    const body = event.body;
    const changelog =
      (
        context.packageResult?._source as
          | { changelog?: opensearch.changelog.ItemResult[] }
          | undefined
      )?.changelog || [];
 
    const result = await getRequestedAttachmentArchiveStatus({
      packageId: body.id,
      scope: body.scope,
      sectionId: body.sectionId,
      changelog,
    });
 
    if (result.needsRebuild) {
      const latestTimestamp = changelog.reduce<number | undefined>((latest, item) => {
        const timestamp = item._source?.timestamp;
        if (typeof timestamp !== "number") {
          return latest;
        }
 
        return latest === undefined ? timestamp : Math.max(latest, timestamp);
      }, undefined);
 
      await sendAttachmentArchiveRebuildRequest({
        packageId: body.id,
        latestTimestamp,
        source: "request",
      });
    }
 
    return {
      statusCode: 200,
      body: result.response,
    };
  });