All files / lib/lambda/submit/submissionPayloads respond-to-rai.ts

78.94% Statements 15/19
60% Branches 6/10
100% Functions 1/1
83.33% Lines 15/18

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                  3x 3x   3x 3x 1x       2x         2x       2x 2x 2x 2x 2x   2x       2x               2x    
import { type APIGatewayEvent } from "aws-lambda";
import { itemExists } from "libs/api/package";
import * as os from "libs/opensearch-lib";
import { getDomain, getOsNamespace } from "libs/utils";
import { events } from "shared-types/events";
 
import { getAuthDetails, isAuthorized, lookupUserAttributes } from "../../../libs/api/auth/user";
import { getUserByEmail } from "../../user-management/userManagementService";
 
export const respondToRai = async (event: APIGatewayEvent) => {
  Iif (!event.body) return;
 
  const parsedResult = events["respond-to-rai"].baseSchema.safeParse(JSON.parse(event.body));
  if (!parsedResult.success) {
    throw parsedResult.error;
  }
 
  // This is the backend check for auth
  Iif (!(await isAuthorized(event, parsedResult.data.id.slice(0, 2)))) {
    throw "Unauthorized";
  }
 
  // This is the backend check for the item already existing
  Iif (!(await itemExists({ id: parsedResult.data.id }))) {
    throw "Item Doesn't Exist";
  }
 
  const item = await os.getItem(getDomain(), getOsNamespace("main"), parsedResult.data.id);
  const authDetails = getAuthDetails(event);
  const userAttr = await lookupUserAttributes(authDetails.userId, authDetails.poolId);
  const submitterEmail = userAttr.email;
  const user = await getUserByEmail(submitterEmail);
 
  Iif (!user) {
    throw new Error("User does not exist in User OpenSearch Index");
  }
 
  const transformedData = events["respond-to-rai"].schema.parse({
    ...parsedResult.data,
    authority: item?._source?.authority,
    submitterName: user.fullName,
    submitterEmail: user.email,
    timestamp: Date.now(),
  });
 
  return transformedData;
};