All files / lib/lambda/update submitNOSO.ts

96.77% Statements 30/31
75% Branches 15/20
100% Functions 3/3
100% Lines 30/30

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                                                                      1x 2x 2x 2x     1x 2x 2x 1x     1x 1x 1x   1x                                         1x           1x 6x 1x           5x 5x       3x   3x   3x   1x   1x 1x             2x 2x   3x 3x 2x           1x            
import { APIGatewayEvent } from "aws-lambda";
import { produceMessage } from "libs/api/kafka";
import { getPackage } from "libs/api/package";
import { response } from "libs/handler-lib";
import { getStatus } from "shared-types";
import { ItemResult } from "shared-types/opensearch/main";
import { z } from "zod";
 
import { submitNOSOAdminSchema } from "./adminChangeSchemas";
 
/** @typedef {object} json
 * @property {object} body
 * @property {string} body.id
 * @property {string} body.authority
 * @property {string} body.status
 * @property {string} body.submitterEmail
 * @property {string} body.submitterName
 * @property {string} body.adminChangeType
 * @property {string} body.mockEvent
 * @property {string} body.changeMade
 * @property {string} body.changeReason
 */
interface submitMessageType {
  id: string;
  authority: string;
  status: string;
  submitterEmail: string;
  submitterName: string;
  submissionDate: string;
  proposedDate: string;
  adminChangeType: string;
  stateStatus: string;
  cmsStatus: string;
}
 
const convertStringToTimestamp = (date: string) => {
  const formatedDate = new Date(date).getTime();
  Iif (isNaN(formatedDate)) throw new Error("Not a valid time");
  return formatedDate;
};
 
const sendSubmitMessage = async (item: submitMessageType) => {
  const topicName = process.env.topicName as string;
  if (!topicName) {
    throw new Error("Topic name is not defined");
  }
 
  const currentTime = Date.now();
  const formatedSubmittedDate = convertStringToTimestamp(item.submissionDate);
  const formatedProposedDate = convertStringToTimestamp(item.proposedDate);
 
  await produceMessage(
    topicName,
    item.id,
    JSON.stringify({
      ...item,
      packageId: item.id,
      origin: "SEATool",
      isAdminChange: true,
      adminChangeType: "NOSO",
      description: null,
      event: "NOSO",
      state: item.id.substring(0, 2),
      submissionDate: formatedSubmittedDate,
      proposedDate: formatedProposedDate,
      makoChangedDate: currentTime,
      changedDate: currentTime,
      statusDate: currentTime,
      timestamp: currentTime,
    }),
  );
 
  return response({
    statusCode: 200,
    body: { message: `${item.id} has been submitted.` },
  });
};
 
export const handler = async (event: APIGatewayEvent) => {
  if (!event.body) {
    return response({
      statusCode: 400,
      body: { message: "Event body required" },
    });
  }
 
  try {
    const item = submitNOSOAdminSchema.parse(
      typeof event.body === "string" ? JSON.parse(event.body) : event.body,
    );
 
    let status: string = item.status;
    // check if it already exists in onemac - should exist in SEATool
    const currentPackage: ItemResult | undefined = await getPackage(item.id);
 
    if (currentPackage && currentPackage.found == true) {
      // we should default to the current status in SEATool, and use entered status as a backup
      status = currentPackage._source?.seatoolStatus ?? item.status;
      // if it exists and has origin OneMAC we shouldn't override it
      Eif (currentPackage._source.origin === "OneMAC") {
        return response({
          statusCode: 400,
          body: { message: `Package with id: ${item.id} already exists.` },
        });
      }
    }
 
    const { stateStatus, cmsStatus } = getStatus(status);
    return await sendSubmitMessage({ ...item, stateStatus, cmsStatus });
  } catch (err) {
    console.error("Error has occured submitting package:", err);
    if (err instanceof z.ZodError) {
      return response({
        statusCode: 400,
        body: { message: err.errors },
      });
    }
 
    return response({
      statusCode: 500,
      body: { message: err.message || "Internal Server Error" },
    });
  }
};