-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename nextjs-site to food ordering and refactor (#2)
Co-authored-by: Sam Sussman <sussmansa@gmail.com>
- Loading branch information
Showing
87 changed files
with
2,396 additions
and
1,484 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
NEXT_PUBLIC_EVENTUAL_API_DOMAIN=https://gecml2jl79.execute-api.us-east-1.amazonaws.com/ | ||
NEXT_PUBLIC_USER_POOL_ID=us-east-1_Pl1wNTMfV | ||
NEXT_PUBLIC_USER_POLL_CLIENT_ID=a4f5eusm699it63gfa5r2g7lp |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
.../nextjs-site/apps/frontend/src/layout.tsx → ...ood-ordering/apps/frontend/src/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
24 changes: 11 additions & 13 deletions
24
...site/apps/frontend/src/pages/checkout.tsx → ...ring/apps/frontend/src/pages/checkout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ServiceClient } from "@eventual/client"; | ||
import * as FoodOrderingService from "@food-ordering/service"; | ||
import type { CognitoUserSession } from "amazon-cognito-identity-js"; | ||
|
||
let apiDomain: string | undefined = process.env.NEXT_PUBLIC_EVENTUAL_API_DOMAIN; | ||
|
||
export function useService(session: CognitoUserSession | null | undefined) { | ||
return new ServiceClient<typeof FoodOrderingService>({ | ||
serviceUrl: apiDomain!, | ||
beforeRequest: async (request) => { | ||
if (session) { | ||
request.headers ??= {}; | ||
request.headers.Authorization = `Basic ${session | ||
.getAccessToken() | ||
.getJwtToken()}`; | ||
} | ||
return request; | ||
}, | ||
}); | ||
} |
2 changes: 1 addition & 1 deletion
2
...extjs-site/apps/frontend/src/use-user.tsx → ...d-ordering/apps/frontend/src/use-user.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
examples/food-ordering/apps/service/src/api/create-order.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { PutCommand } from "@aws-sdk/lib-dynamodb"; | ||
import { CreateOrderRequest, Order } from "@food-ordering/core"; | ||
import { ulid } from "ulidx"; | ||
import { dynamo } from "../util/clients.js"; | ||
import { OrderRecord } from "../util/order-record.js"; | ||
import { TABLE_NAME } from "../util/variables.js"; | ||
import { processOrder } from "../workflows/process-order.js"; | ||
import { privateAccess } from "./middleware/default.js"; | ||
|
||
export const createOrder = privateAccess.command( | ||
"createOrder", | ||
async (orderRequest: CreateOrderRequest, { user }) => { | ||
const order: Order = { | ||
...orderRequest, | ||
userId: user.username, | ||
id: ulid(), | ||
status: "CREATED", | ||
timestamp: new Date().toISOString(), | ||
}; | ||
|
||
const orderRecord: OrderRecord = { | ||
...order, | ||
pk: OrderRecord.partitionKey, | ||
sk: OrderRecord.sortKey(order.id), | ||
// the sort key we will use to order user records by timestamp | ||
user_time: OrderRecord.userTime(order.userId, order.timestamp), | ||
}; | ||
|
||
await dynamo.send( | ||
new PutCommand({ | ||
Item: orderRecord, | ||
TableName: TABLE_NAME, | ||
ConditionExpression: "attribute_not_exists(sk)", | ||
}) | ||
); | ||
|
||
await processOrder.startExecution({ | ||
input: { orderId: orderRecord.id }, | ||
executionName: orderRecord.id, | ||
}); | ||
|
||
return order; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { GetCommand } from "@aws-sdk/lib-dynamodb"; | ||
import { HttpError } from "@eventual/core"; | ||
import type { Order } from "@food-ordering/core"; | ||
import { dynamo } from "../util/clients.js"; | ||
import { orderFromRecord, OrderRecord } from "../util/order-record.js"; | ||
import { TABLE_NAME } from "../util/variables.js"; | ||
import { privateAccess } from "./middleware/default.js"; | ||
/** | ||
* Gets the record for an {@link Order} by its ID. | ||
* | ||
* Users must be logged in and the owner of the {@link Order} record. | ||
*/ | ||
export const getOrder = privateAccess.command( | ||
"getOrder", | ||
async (orderId: string, { user }) => { | ||
const order = await getOrderRecord(orderId); | ||
if (order === undefined) { | ||
return undefined; | ||
} else if (order?.userId !== user.username) { | ||
throw new HttpError({ | ||
code: 401, | ||
message: `User does not have permissions to view order ${orderId}`, | ||
}); | ||
} else { | ||
return order; | ||
} | ||
} | ||
); | ||
|
||
// this is annoyingly redundant - we want to use it in a workflow | ||
// and therefore have to pull it out separate;y | ||
// would be better to just call the command from the workflow | ||
// will impact how we design middleware | ||
export async function getOrderRecord(orderId: string) { | ||
const item = await dynamo.send( | ||
new GetCommand({ | ||
Key: { | ||
pk: OrderRecord.partitionKey, | ||
sk: OrderRecord.sortKey(orderId), | ||
}, | ||
TableName: TABLE_NAME, | ||
ConsistentRead: true, | ||
}) | ||
); | ||
|
||
return item.Item ? orderFromRecord(item.Item as OrderRecord) : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./create-order.js"; | ||
export * from "./get-order.js"; | ||
export * from "./list-orders.js"; | ||
export * from "./middleware/auth.js"; | ||
export * from "./update-order-status.js"; |
27 changes: 27 additions & 0 deletions
27
examples/food-ordering/apps/service/src/api/list-orders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { QueryCommand } from "@aws-sdk/lib-dynamodb"; | ||
import { dynamo } from "../util/clients.js"; | ||
import { orderFromRecord, OrderRecord } from "../util/order-record.js"; | ||
import { TABLE_NAME } from "../util/variables.js"; | ||
import { privateAccess } from "./middleware/default.js"; | ||
|
||
export const listOrders = privateAccess.command( | ||
"listOrders", | ||
async (_: any, { user }) => { | ||
const { Items } = await dynamo.send( | ||
new QueryCommand({ | ||
TableName: TABLE_NAME, | ||
IndexName: OrderRecord.userTimeIndex, | ||
KeyConditionExpression: "pk=:pk and begins_with(user_time, :userId)", | ||
// newest first | ||
ScanIndexForward: false, | ||
ExpressionAttributeValues: { | ||
":userId": user.username, | ||
":pk": OrderRecord.partitionKey, | ||
}, | ||
ConsistentRead: true, | ||
}) | ||
); | ||
|
||
return (Items as OrderRecord[]).map(orderFromRecord) ?? {}; | ||
} | ||
); |
Oops, something went wrong.