Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#set($items = $ctx.prev.result.items)
#foreach($item in $items)
#set($stock = 0)
#if($item.stock)
#set($stock = $item.stock)
#end
#set($price = 0)
#if($item.price)
#set($price = $item.price)
#end
#set($total = $price * $stock)
$util.qr($item.put("totalValue", $total))
#end
$util.toJson($ctx.prev.result)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Add 10% discount to all products
#foreach($item in $ctx.result.items)
#if($item.price)
#set($discount = $item.price * 0.10)
#set($discountedPrice = $item.price - $discount)
$util.qr($item.put("discountedPrice", $discountedPrice))
$util.qr($item.put("savings", $discount))
#end
#end

$util.toJson($ctx.result)
12 changes: 12 additions & 0 deletions amplify-migration-apps/product-catalog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,18 @@ On the AppSync AWS Console, locate the ID of Gen1 API, it will be named `product
+ export async function handler(event) {
```

```diff
- const crypto = require('@aws-crypto/sha256-js');
- const { defaultProvider } = require('@aws-sdk/credential-provider-node');
- const { SignatureV4 } = require('@aws-sdk/signature-v4');
- const { HttpRequest } = require('@aws-sdk/protocol-http');
- const Sha256 = crypto.Sha256;
+ import { Sha256 } from '@aws-crypto/sha256-js';
+ import { defaultProvider } from '@aws-sdk/credential-provider-node';
+ import { SignatureV4 } from '@aws-sdk/signature-v4';
+ import { HttpRequest } from '@aws-sdk/protocol-http';
```

**Edit in `./src/main.tsx`:**

```diff
Expand Down
4 changes: 3 additions & 1 deletion amplify-migration-apps/product-catalog/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ cp -f lowstockproducts.js ./amplify/backend/function/lowstockproducts/src/index.
cp -f lowstockproducts.package.json ./amplify/backend/function/lowstockproducts/src/package.json
cp -f onimageuploaded.js ./amplify/backend/function/${s3_trigger_function_name}/src/index.js
cp -f onimageuploaded.package.json ./amplify/backend/function/${s3_trigger_function_name}/src/package.json
cp -f custom-roles.json ./amplify/backend/api/productcatalog/custom-roles.json
cp -f custom-roles.json ./amplify/backend/api/productcatalog/custom-roles.json
cp -f Query.listProducts.res.vtl ./amplify/backend/api/productcatalog/resolvers/Query.listProducts.res.vtl
cp -f Query.listProducts.postDataLoad.1.res.vtl ./amplify/backend/api/productcatalog/resolvers/Query.listProducts.postDataLoad.1.res.vtl
18 changes: 6 additions & 12 deletions amplify-migration-apps/product-catalog/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ enum UserRole {
VIEWER
}

type User @model @auth(rules: [
{ allow: private, provider: iam },
{ allow: owner, ownerField: "id" }
]) {
type User @model @auth(rules: [{ allow: private, provider: iam }, { allow: owner, ownerField: "id" }]) {
id: ID!
email: String!
name: String!
Expand All @@ -33,12 +30,10 @@ type Product @model @auth(rules: [{ allow: private, provider: iam }]) {
createdAt: AWSDateTime!
updatedAt: AWSDateTime!
comments: [Comment] @hasMany(indexName: "byProduct", fields: ["id"])
totalValue: Float
}

type Comment @model @auth(rules: [
{ allow: private, provider: iam },
{ allow: owner, ownerField: "authorId" }
]) {
type Comment @model @auth(rules: [{ allow: private, provider: iam }, { allow: owner, ownerField: "authorId" }]) {
id: ID!
productId: ID! @index(name: "byProduct")
authorId: String!
Expand All @@ -59,8 +54,7 @@ type LowStockResponse {
}

type Query {
checkLowStock: LowStockResponse @function(name: "lowstockproducts-${env}") @auth(rules: [
{ allow: private, provider: iam },
{ allow: public, provider: apiKey }
])
checkLowStock: LowStockResponse
@function(name: "lowstockproducts-${env}")
@auth(rules: [{ allow: private, provider: iam }, { allow: public, provider: apiKey }])
}
31 changes: 27 additions & 4 deletions amplify-migration-apps/product-catalog/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1208,17 +1208,40 @@ function App({ signOut, user }: AppProps) {
{product.brand}
</Badge>
)}
{product.stock !== undefined && product.stock !== null && (
{(product as any).stockStatus && (
<Badge
style={{
backgroundColor: (product.stock || 0) > 0 ? '#dcfce7' : '#fef3c7',
color: (product.stock || 0) > 0 ? '#166534' : '#92400e',
backgroundColor:
(product as any).stockStatus === 'OUT_OF_STOCK'
? '#fef2f2'
: (product as any).stockStatus === 'LOW_STOCK'
? '#fef3c7'
: '#dcfce7',
color:
(product as any).stockStatus === 'OUT_OF_STOCK'
? '#dc2626'
: (product as any).stockStatus === 'LOW_STOCK'
? '#92400e'
: '#166534',
fontWeight: '600',
borderRadius: '6px',
padding: '0.25rem 0.75rem',
}}
>
{(product.stock || 0) > 0 ? `${product.stock} in stock` : 'Out of stock'}
{(product as any).stockStatus.replace('_', ' ')}
</Badge>
)}
{(product as any).totalValue && (
<Badge
style={{
backgroundColor: '#f0f9ff',
color: '#0369a1',
fontWeight: '600',
borderRadius: '6px',
padding: '0.25rem 0.75rem',
}}
>
Total: ${(product as any).totalValue}
</Badge>
)}
</Flex>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ export const listProducts = /* GraphQL */ `query ListProducts(
images
createdBy
updatedBy
discountedPrice
savings
createdAt
updatedAt
totalValue
__typename
}
nextToken
Expand Down
Loading