Skip to content

Commit

Permalink
add zod to event body
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffanyvu committed Dec 11, 2024
1 parent 46cb421 commit 38633c1
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions lib/lambda/update/updatePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { APIGatewayEvent } from "aws-lambda";
import { getPackage } from "libs/api/package";
import { produceMessage } from "libs/api/kafka";
import { ItemResult } from "shared-types/opensearch/main";
import { z } from "zod";

type ActionType = "update-id" | "update-values" | "delete";
// type ActionType = "update-id" | "update-values" | "delete";

const sendDeleteMessage = async (topicName: string, packageId: string) => {
await produceMessage(
Expand Down Expand Up @@ -49,7 +50,7 @@ const sendUpdateIdMessage = async (
const { _id, _index, _source } = currentPackage;
//eslint-disable-next-line
const { id, changeMade, ...remainingFields } = _source;
console.log({ ..._source }, "ORIGINAL SPREAD");

await sendDeleteMessage(topicName, currentPackage._id);
await produceMessage(
topicName,
Expand All @@ -64,6 +65,14 @@ const sendUpdateIdMessage = async (
);
};

const updatePackageEventBodySchema = z.object({
packageId: z.string(),
action: z.enum(["update-values", "update-id", "delete"]),
updatedId: z.string().optional(),
updatedFields: z.record(z.unknown()).optional(),
changeReason: z.string().optional(),
});

export const handler = async (event: APIGatewayEvent) => {
const topicName = process.env.topicName as string;

Expand All @@ -78,15 +87,17 @@ export const handler = async (event: APIGatewayEvent) => {
});
}
try {
// TODO: allow user to input title of the accordion
const { packageId, action, updatedId, updatedFields, changeReason } =
typeof event.body === "string"
? JSON.parse(event.body)
: (event.body as {
packageId: string;
action: ActionType;
updatedFields: object;
});
const parseEventBody = (body: unknown) => {
return updatePackageEventBodySchema.parse(typeof body === "string" ? JSON.parse(body) : body);
};

const {
packageId,
action,
updatedId,
updatedFields = {},
changeReason,
} = parseEventBody(event.body);

if (!packageId || !action) {
return response({
Expand Down Expand Up @@ -125,7 +136,6 @@ export const handler = async (event: APIGatewayEvent) => {
}

await sendUpdateIdMessage(topicName, packageResult, updatedId);
// delete/hide old record and create new one with new id but same values
}

if (action === "update-values") {
Expand Down

0 comments on commit 38633c1

Please sign in to comment.