Skip to content

Commit

Permalink
update list properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ebisbe committed Jul 8, 2024
1 parent 06ff960 commit 07792a2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/functions/list/__tests__/update.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from "@jest/globals";

import { List } from "../../../models/table";
import { handler } from "../update";

describe("UPDATE List", () => {
it("requires path parameter ID", async () => {
const response = await handler({
headers: { "Content-Type": "application/json" },
body: null,
});

expect(response).toStrictEqual({
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: '{"issues":[{"code":"invalid_type","expected":"object","received":"null","path":[],"message":"Expected object, received null"}],"name":"ZodError"}',
});
});

it("returns found list by ID", async () => {
const list = await List.create({
name: "<NAME>",
description: "This is a test list",
});

const response = await handler({
headers: { "Content-Type": "application/json" },
pathParameters: { id: list.id },
body: JSON.stringify({ name: "Name v2", description: "Description v2" }),
});

expect(response.statusCode).toBe(200);

const body = JSON.parse(response.body);
expect(body.name).toBe("Name v2");
expect(body.description).toBe("Description v2");
});
});
37 changes: 36 additions & 1 deletion src/functions/list/update.ts
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
export const handler = () => {};
import middy from "@middy/core";
import httpErrorHandler from "@middy/http-error-handler";
import httpJsonBodyParser from "@middy/http-json-body-parser";
import { APIGatewayProxyEventV2, Handler } from "aws-lambda";
import { z } from "zod";

import { List } from "../../models/table";
import { ValidationError } from "../../utils/validationError";

const updateHandler: Handler<APIGatewayProxyEventV2> = async (event) => {
const listSchema = z.object({
name: z.string(),
description: z.string(),
}) satisfies z.ZodType<List>;

const list = listSchema.safeParse(event.body);
if (!list.success) {
throw new ValidationError(400, list.error);
}

const updateListRaw = {
id: event.pathParameters?.id,
...list.data,
};
const updatedList = await List.update(updateListRaw);

return {
statusCode: 200,
body: JSON.stringify(updatedList),
};
};

export const handler = middy()
.use(httpErrorHandler({ logger: false }))
.use(httpJsonBodyParser())
.handler(updateHandler);

0 comments on commit 07792a2

Please sign in to comment.