From 7289702dec09378dd02b04121adfcff90907a230 Mon Sep 17 00:00:00 2001 From: James Rodewig Date: Tue, 13 Aug 2024 10:32:47 -0400 Subject: [PATCH] [DOCS-3223] Consistently use Integer for Product price --- __tests__/products.test.ts | 30 ++++++++++++------------- docs/index.html | 42 ++++++++++++++++------------------- openapi.yml | 45 ++++++++++++++++++-------------------- schema/collections.fsl | 7 +++--- src/middleware/products.ts | 8 +++---- 5 files changed, 63 insertions(+), 69 deletions(-) diff --git a/__tests__/products.test.ts b/__tests__/products.test.ts index fd4e5c4..4877409 100644 --- a/__tests__/products.test.ts +++ b/__tests__/products.test.ts @@ -122,14 +122,14 @@ describe("Products", () => { const missingRes = await req(app).post(`/products`).send(rest); expect(missingRes.status).toEqual(400); expect(missingRes.body.message).toEqual( - "Price must be a number greater than 0." + "Price must be an integer greater than 0." ); const invalidRes = await req(app) .post(`/products`) .send({ ...rest, price: "foo" }); expect(invalidRes.status).toEqual(400); expect(invalidRes.body.message).toEqual( - "Price must be a number greater than 0." + "Price must be an integer greater than 0." ); }); @@ -154,14 +154,14 @@ describe("Products", () => { const missingRes = await req(app).post(`/products`).send(rest); expect(missingRes.status).toEqual(400); expect(missingRes.body.message).toEqual( - "Stock must be a number greater than or equal to 0." + "Stock must be an integer greater than or equal to 0." ); const invalidRes = await req(app) .post(`/products`) .send({ ...rest, stock: -1 }); expect(invalidRes.status).toEqual(400); expect(invalidRes.body.message).toEqual( - "Stock must be a number greater than or equal to 0." + "Stock must be an integer greater than or equal to 0." ); }); @@ -204,25 +204,25 @@ describe("Products", () => { describe("PATCH /products/:name", () => { it("updates a product", async () => { // Create a new product. - const product = mockProduct({ price: 10.99, category: "electronics" }); + const product = mockProduct({ price: 10_99, category: "electronics" }); const createRes = await req(app).post(`/products`).send(product); productsToCleanup.push(createRes.body); // Check that the product was created successfully. expect(createRes.status).toEqual(201); - expect(createRes.body.price).toEqual(10.99); + expect(createRes.body.price).toEqual(10_99); // Update the product. const updateRes = await req(app) .patch(`/products/${createRes.body.id}`) - .send({ price: 19.99 }); + .send({ price: 19_99 }); // Check that the product was updated successfully. expect(updateRes.status).toEqual(200); - expect(updateRes.body.price).toEqual(19.99); + expect(updateRes.body.price).toEqual(19_99); expect(updateRes.body.name).toEqual(product.name); expect(Object.keys(updateRes.body).sort()).toEqual(productFields.sort()); }); it("returns a 404 if the product does not exist", async () => { - const res = await req(app).patch(`/products/1234`).send({ price: 19.99 }); + const res = await req(app).patch(`/products/1234`).send({ price: 19_99 }); expect(res.status).toEqual(404); expect(res.body.message).toEqual("No product with id '1234' exists."); }); @@ -250,14 +250,14 @@ describe("Products", () => { .send({ price: "not a number" }); expect(priceAsString.status).toEqual(400); expect(priceAsString.body.message).toEqual( - "Price must be a number greater than 0 or be omitted." + "Price must be an integer greater than 0 or be omitted." ); const negativePrice = await req(app) .patch("/products/does-not-matter") .send({ price: -1 }); expect(negativePrice.status).toEqual(400); expect(negativePrice.body.message).toEqual( - "Price must be a number greater than 0 or be omitted." + "Price must be an integer greater than 0 or be omitted." ); }); @@ -267,14 +267,14 @@ describe("Products", () => { .send({ stock: "not a number" }); expect(stockAsString.status).toEqual(400); expect(stockAsString.body.message).toEqual( - "Stock must be a number greater than or equal to 0 or be omitted." + "Stock must be an integer greater than or equal to 0 or be omitted." ); const negativeStock = await req(app) .patch("/products/does-not-matter") .send({ stock: -1 }); expect(negativeStock.status).toEqual(400); expect(negativeStock.body.message).toEqual( - "Stock must be a number greater than or equal to 0 or be omitted." + "Stock must be an integer greater than or equal to 0 or be omitted." ); }); @@ -319,8 +319,8 @@ describe("Products", () => { describe("GET /products/by-price", () => { it("gets products within a price range", async () => { - const minPrice = 10; - const maxPrice = 50; + const minPrice = 10_00; + const maxPrice = 50_00; const res = await req(app).get( `/products/by-price?minPrice=${minPrice}&maxPrice=${maxPrice}` ); diff --git a/docs/index.html b/docs/index.html index b7e23cd..1aaf241 100644 --- a/docs/index.html +++ b/docs/index.html @@ -489,18 +489,18 @@ ensure name values are unique across products.

description
required
string

Description of the product.

-
price
required
number
price
required
integer

Price of the product in USD cents.

Internally, the Product collection uses a check constraint to -ensure price is a positive number.

+ensure price is an integer greater than zero.

stock
required
integer

Quantity of the product available in stock.

Internally, the Product collection uses a check constraint to -ensure stock is a positive integer.

+ensure stock is greater than or equal to zero.

category
required
string
description
required
string

Description of the product.

-
price
required
number
price
required
integer

Price of the product in USD cents.

Internally, the Product collection uses a check constraint to -ensure price is a positive number.

+ensure price is an integer greater than zero.

stock
required
integer

Quantity of the product available in stock.

Internally, the Product collection uses a check constraint to -ensure stock is a positive integer.

+ensure stock is greater than or equal to zero.

category
required
string

Internal server error

Request samples

Content type
application/json
{
  • "name": "The Old Man and the Sea",
  • "price": 899,
  • "description": "A book by Ernest Hemingway",
  • "stock": 10,
  • "category": "books"
}

Response samples

Content type
application/json
{
  • "id": "123",
  • "name": "The Old Man and the Sea",
  • "price": 899,
  • "stock": 10,
  • "description": "A book by Ernest Hemingway",
  • "category": {
    }
}

Get a page of products by price.

query Parameters
minPrice
number

Minimum price to filter products by.

-
maxPrice
number

Maximum price to filter products by.

+
http://localhost:8000/products/{id}

Request samples

Content type
application/json
{
  • "name": "The Old Man and the Sea",
  • "price": 899,
  • "description": "A book by Ernest Hemingway",
  • "stock": 10,
  • "category": "books"
}

Response samples

Content type
application/json
{
  • "id": "123",
  • "name": "The Old Man and the Sea",
  • "price": 899,
  • "stock": 10,
  • "description": "A book by Ernest Hemingway",
  • "category": {
    }
}

Get a page of products by price.

query Parameters
minPrice
integer

Minimum price, in USD cents, to filter products by.

+
maxPrice
integer

Maximum price, in USD cents, to filter products by.

nextToken
string

Accepts a pagination token. The endpoint's responses @@ -647,14 +647,10 @@ " class="sc-euGpHm sc-exayXG fwfkcU jYGAQp">

ID of the customer.

Request Body schema: application/json
required
productName
required
string

Name of an existing product to add to the order.

-
quantity
required
integer

Quantity of the product to add to the order.

-

Must be a non-negative integer. The quantity must be less than the product's stock.

-

Internally, the OrderItem collection uses a check constraint to -ensure quantity is a positive integer.

+
quantity
required
integer

Quantity of the product to add to the order. Must be less than the product's stock. +Internally, the OrderItem collection uses a check constraint to ensure quantity is greater than or equal to zero.

Responses

Request samples

curl -v 'http://localhost:8000/customers/111/orders?pageSize=1'

Response samples

Content type
application/json
{
  • "results": [
    ],
  • "nextToken": "hdW..."
}