All files / lib/lambda/submit/submissionPayloads new-chip-submission.ts

87.5% Statements 14/16
75% Branches 6/8
100% Functions 1/1
93.33% Lines 14/15

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          3x 3x   3x 3x 1x       2x 1x       1x       1x 1x 1x 1x   1x             1x    
import { events } from "shared-types/events";
import { isAuthorized, getAuthDetails, lookupUserAttributes } from "../../../libs/api/auth/user";
import { type APIGatewayEvent } from "aws-lambda";
import { itemExists } from "libs/api/package";
 
export const newChipSubmission = async (event: APIGatewayEvent) => {
  Iif (!event.body) return;
 
  const parsedResult = events["new-chip-submission"].baseSchema.safeParse(JSON.parse(event.body));
  if (!parsedResult.success) {
    throw parsedResult.error;
  }
 
  // This is the backend check for auth
  if (!(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 Already Exists";
  }
 
  const authDetails = getAuthDetails(event);
  const userAttr = await lookupUserAttributes(authDetails.userId, authDetails.poolId);
  const submitterEmail = userAttr.email;
  const submitterName = `${userAttr.given_name} ${userAttr.family_name}`;
 
  const transformedData = events["new-chip-submission"].schema.parse({
    ...parsedResult.data,
    submitterName,
    submitterEmail,
    timestamp: Date.now(),
  });
 
  return transformedData;
};