All files / lib/lambda rebuildAttachmentArchives.ts

0% Statements 0/10
0% Branches 0/4
0% Functions 0/2
0% Lines 0/10

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                                                                             
import { SQSHandler } from "aws-lambda";
import { opensearch } from "shared-types";
 
import { AttachmentArchiveRebuildMessage } from "../attachment-archive/types";
import { getPackageChangelog } from "../libs/api/package";
import { rebuildPackageAttachmentArchives } from "./attachmentArchive-lib";
 
function parseRecordBody(body: string): AttachmentArchiveRebuildMessage {
  const parsed = JSON.parse(body) as Partial<AttachmentArchiveRebuildMessage>;
  if (!parsed.packageId || !parsed.source) {
    throw new Error("Attachment archive rebuild message must include packageId and source");
  }
 
  return parsed as AttachmentArchiveRebuildMessage;
}
 
export const handler: SQSHandler = async (event) => {
  for (const record of event.Records) {
    const message = parseRecordBody(record.body);
    const changelog = (await getPackageChangelog(message.packageId)).hits
      .hits as opensearch.changelog.ItemResult[];
 
    const result = await rebuildPackageAttachmentArchives({
      packageId: message.packageId,
      changelog,
    });
 
    console.info(
      JSON.stringify({
        event: "attachment_archive_rebuild_processed",
        packageId: message.packageId,
        latestTimestamp: message.latestTimestamp,
        source: message.source,
        result,
      }),
    );
  }
};