All files / lib/attachment-archive package-activity.ts

0% Statements 0/18
0% Branches 0/4
0% Functions 0/11
0% Lines 0/17

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                                                                                                                                                                               
import { opensearch } from "shared-types";
import { getPackageActivityLabelSlug } from "shared-utils";
 
import { buildSectionArchiveFolderName, getSectionArchiveRootFolderName } from "./archive-manifest";
import { AttachmentArchiveSectionInfo, AttachmentArchiveSourceAttachment } from "./types";
 
export interface AttachmentArchiveSectionDescriptor extends AttachmentArchiveSectionInfo {
  packageId: string;
  attachments: AttachmentArchiveSourceAttachment[];
}
 
export function getArchiveEligibleChangelogEntries(
  changelog: opensearch.changelog.ItemResult[],
): opensearch.changelog.Document[] {
  return changelog
    .map((item) => item._source)
    .filter((document): document is opensearch.changelog.Document => Boolean(document))
    .filter((document) => !document.isAdminChange);
}
 
function getSectionAttachments(
  entry: opensearch.changelog.Document,
): AttachmentArchiveSourceAttachment[] {
  return (entry.attachments || []).map((attachment) => ({
    bucket: attachment.bucket,
    filename: attachment.filename,
    key: attachment.key,
    title: attachment.title,
    uploadDate: attachment.uploadDate,
  }));
}
 
export function buildAttachmentArchiveSections({
  packageId,
  changelog,
}: {
  packageId: string;
  changelog: opensearch.changelog.ItemResult[];
}): AttachmentArchiveSectionDescriptor[] {
  return getArchiveEligibleChangelogEntries(changelog).reduce<AttachmentArchiveSectionDescriptor[]>(
    (sections, entry, index) => {
      const attachments = getSectionAttachments(entry);
      if (attachments.length === 0) {
        return sections;
      }
 
      const sectionNumber = index + 1;
      const sectionLabel = getPackageActivityLabelSlug(entry.event);
      const sectionFolderName = buildSectionArchiveFolderName({ sectionNumber, sectionLabel });
 
      sections.push({
        packageId,
        attachments,
        sectionId: entry.id,
        sectionNumber,
        sectionLabel,
        sectionFolderName,
        rootFolderName: getSectionArchiveRootFolderName({
          packageId,
          sectionNumber,
          sectionLabel,
        }),
      });
 
      return sections;
    },
    [],
  );
}
 
export function getAttachmentArchiveSectionById({
  packageId,
  changelog,
  sectionId,
}: {
  packageId: string;
  changelog: opensearch.changelog.ItemResult[];
  sectionId: string;
}): AttachmentArchiveSectionDescriptor | undefined {
  return buildAttachmentArchiveSections({ packageId, changelog }).find(
    (section) => section.sectionId === sectionId,
  );
}
 
export function hasArchiveableAttachments(changelog: opensearch.changelog.ItemResult[]): boolean {
  return buildAttachmentArchiveSections({ packageId: "_", changelog }).length > 0;
}