All files / lib/lambda item.ts

100% Statements 17/17
88.46% Branches 23/26
100% Functions 1/1
100% Lines 17/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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                        1x                           1x                   6x   6x 2x 2x   2x         2x                         4x   4x 1x           3x 2x 2x   2x         2x                         1x          
import {
  getDraftPackage,
  isActiveDraftPackage,
  isActiveMainNonDraftPackage,
} from "libs/api/package";
import { response } from "libs/handler-lib";
import { APIGatewayEvent } from "shared-types";
import { isCmsUser, isHelpDeskUser } from "shared-utils";
import { z } from "zod";
 
import {
  authenticatedMiddy,
  canViewPackage,
  ContextWithAuthenticatedUser,
  ContextWithPackage,
  fetchAppkChildren,
  fetchChangelog,
  fetchPackage,
} from "./middleware";
 
const itemEventSchema = z
  .object({
    body: z
      .object({
        id: z.string().trim().min(1),
        includeDraft: z.boolean().optional(),
        preferDraft: z.boolean().optional(),
      })
      .strict(),
  })
  .passthrough();
 
export type ItemEvent = APIGatewayEvent & z.infer<typeof itemEventSchema>;
 
export const handler = authenticatedMiddy({
  opensearch: true,
  setToContext: true,
  eventSchema: itemEventSchema,
})
  .use(fetchPackage({ allowNotFound: true, rethrowErrors: true, setToContext: true }))
  .use(canViewPackage())
  .use(fetchAppkChildren({ setToContext: true }))
  .use(fetchChangelog({ setToContext: true }))
  .handler(async (event: ItemEvent, context: ContextWithPackage & ContextWithAuthenticatedUser) => {
    const { packageResult, authenticatedUser } = context;
 
    if (event.body?.includeDraft === true && event.body?.preferDraft === true) {
      const draftPackageResult = await getDraftPackage(event.body.id.toUpperCase());
      const isActiveDraft = isActiveDraftPackage(draftPackageResult);
 
      Eif (
        draftPackageResult &&
        isActiveDraft &&
        (!authenticatedUser || !isCmsUser(authenticatedUser) || isHelpDeskUser(authenticatedUser))
      ) {
        return response({
          statusCode: 200,
          body: {
            ...draftPackageResult,
            _source: {
              ...draftPackageResult._source,
              changelog: draftPackageResult._source?.changelog ?? [],
            },
          },
        });
      }
    }
 
    const isActiveMainNonDraft = isActiveMainNonDraftPackage(packageResult);
 
    if (isActiveMainNonDraft) {
      return response({
        statusCode: 200,
        body: packageResult,
      });
    }
 
    if (event.body?.includeDraft === true) {
      const draftPackageResult = await getDraftPackage(event.body.id.toUpperCase());
      const isActiveDraft = isActiveDraftPackage(draftPackageResult);
 
      Eif (
        draftPackageResult &&
        isActiveDraft &&
        (!authenticatedUser || !isCmsUser(authenticatedUser) || isHelpDeskUser(authenticatedUser))
      ) {
        return response({
          statusCode: 200,
          body: {
            ...draftPackageResult,
            _source: {
              ...draftPackageResult._source,
              changelog: draftPackageResult._source?.changelog ?? [],
            },
          },
        });
      }
    }
 
    return response({
      statusCode: 404,
      body: { message: "No record found for the given id" },
    });
  });