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

83.72% Statements 36/43
73.46% Branches 36/49
100% Functions 5/5
83.72% Lines 36/43

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 119 120 121 122 123 124 125 126 127 128 129 130          2x 2x   2x   2x 5x                                                   2x 5x 5x 5x                     2x 5x       5x 5x 5x 5x             2x 5x 5x   5x 5x 5x   5x 3x     2x 2x           2x             6x   6x 1x     5x 5x 5x 5x     5x     5x       5x                                    
import { opensearch } from "shared-types";
import { getDraftAttachments } from "shared-utils";
 
import type { AttachmentArchiveChangelogItem } from "./package-activity";
 
const DRAFT_ACTIVITY_ID_SUFFIX = "draft-activity";
const DRAFT_UPDATED_ACTIVITY_ID_SUFFIX = "draft-updated-activity";
 
const DRAFT_EVENT_FALLBACK: opensearch.changelog.Document["event"] = "new-medicaid-submission";
 
const isDraftEvent = (event: unknown): event is opensearch.changelog.Document["event"] =>
  [
    "app-k",
    "capitated-amendment",
    "capitated-initial",
    "capitated-renewal",
    "contracting-amendment",
    "contracting-initial",
    "contracting-renewal",
    "legacy-admin-change",
    "legacy-withdraw-rai-request",
    "new-chip-details-submission",
    "new-chip-submission",
    "new-medicaid-submission",
    "respond-to-rai",
    "temporary-extension",
    "toggle-withdraw-rai",
    "upload-subsequent-documents",
    "withdraw-package",
    "withdraw-rai",
    "update-values",
    "update-id",
    "delete",
    "split-spa",
    "NOSO",
  ].includes(event as opensearch.changelog.Document["event"]);
 
const getDraftSavedTimestamp = (submission: opensearch.main.Document) => {
  const savedAt = submission.draft?.savedAt ? Date.parse(submission.draft.savedAt) : NaN;
  Eif (!Number.isNaN(savedAt)) {
    return savedAt;
  }
 
  const changedAt = submission.makoChangedDate ? Date.parse(submission.makoChangedDate) : NaN;
  if (!Number.isNaN(changedAt)) {
    return changedAt;
  }
 
  return Date.now();
};
 
const getTimestamp = (value: string | number | undefined, fallback: number) => {
  Iif (typeof value === "number") {
    return value;
  }
 
  Eif (typeof value === "string") {
    const timestamp = Date.parse(value);
    Eif (!Number.isNaN(timestamp)) {
      return timestamp;
    }
  }
 
  return fallback;
};
 
const isUpdatedByDifferentUser = (submission: opensearch.main.Document) => {
  const draft = submission.draft;
  const createdByName = draft?.createdByName ?? draft?.draftOwnerName ?? submission.submitterName;
  const createdByEmail =
    draft?.createdByEmail ?? draft?.draftOwnerEmail ?? submission.submitterEmail;
  const updatedByName = draft?.updatedByName ?? submission.submitterName;
  const updatedByEmail = draft?.updatedByEmail ?? submission.submitterEmail;
 
  if (!updatedByName) {
    return false;
  }
 
  Eif (updatedByEmail && createdByEmail) {
    return updatedByEmail.toLowerCase() !== createdByEmail.toLowerCase();
  }
 
  return updatedByName !== createdByName;
};
 
export const buildDraftAttachmentChangelog = ({
  packageId,
  submission,
}: {
  packageId: string;
  submission: opensearch.main.Document;
}): AttachmentArchiveChangelogItem[] => {
  const attachments = getDraftAttachments(submission);
 
  if (attachments.length === 0) {
    return [];
  }
 
  const savedTimestamp = getDraftSavedTimestamp(submission);
  const updatedByDifferentUser = isUpdatedByDifferentUser(submission);
  const draft = submission.draft;
  const activityIdSuffix = updatedByDifferentUser
    ? DRAFT_UPDATED_ACTIVITY_ID_SUFFIX
    : DRAFT_ACTIVITY_ID_SUFFIX;
  const submitterName = updatedByDifferentUser
    ? (draft?.updatedByName ?? submission.submitterName)
    : (draft?.createdByName ?? draft?.draftOwnerName ?? submission.submitterName);
  const timestamp = updatedByDifferentUser
    ? getTimestamp(draft?.updatedAt ?? draft?.savedAt, savedTimestamp)
    : getTimestamp(draft?.createdAt ?? draft?.savedAt, savedTimestamp);
 
  return [
    {
      _id: `${packageId}-${activityIdSuffix}`,
      _index: "draft-changelog",
      _score: 1,
      sort: [],
      found: true,
      _source: {
        id: `${packageId}-${activityIdSuffix}`,
        packageId,
        event: isDraftEvent(submission.event) ? submission.event : DRAFT_EVENT_FALLBACK,
        timestamp,
        submitterName,
        attachments,
      },
    },
  ];
};