diff --git a/src/components/pages/orders/index.tsx b/src/components/pages/orders/index.tsx
index de00d23..21faec0 100644
--- a/src/components/pages/orders/index.tsx
+++ b/src/components/pages/orders/index.tsx
@@ -1,3 +1,5 @@
+import { HtmxTargets } from "../../common/constants";
+
export const OrdersPage = (
);
diff --git a/src/components/pages/orders/order_create_success.tsx b/src/components/pages/orders/order_create_success.tsx
index ef493fb..fc455a4 100644
--- a/src/components/pages/orders/order_create_success.tsx
+++ b/src/components/pages/orders/order_create_success.tsx
@@ -1,6 +1,8 @@
+import { HtmxTargets } from "../../common/constants"
+
export const orderCreateSuccess = () => {
return (
-
+
Order Created successfully.
)
diff --git a/src/components/pages/orders/order_tailwind.tsx b/src/components/pages/orders/order_tailwind.tsx
index 87ad0c6..6cc936f 100644
--- a/src/components/pages/orders/order_tailwind.tsx
+++ b/src/components/pages/orders/order_tailwind.tsx
@@ -1,10 +1,12 @@
+import { getConfig } from "../../..";
+
export const OrderExampleTailwindComponent = () => {
return (
-
+
diff --git a/src/components/pages/orders/orders.tsx b/src/components/pages/orders/orders.tsx
index e496db0..947e35d 100644
--- a/src/components/pages/orders/orders.tsx
+++ b/src/components/pages/orders/orders.tsx
@@ -1,3 +1,5 @@
+import { HtmxTargets } from "../../common/constants"
+
export const ViewOrdersSection = () => {
return (
@@ -5,7 +7,7 @@ export const ViewOrdersSection = () => {
-
+
diff --git a/src/components/pages/orders/unfinished_orders.tsx b/src/components/pages/orders/unfinished_orders.tsx
index e2e92d7..2da3a07 100644
--- a/src/components/pages/orders/unfinished_orders.tsx
+++ b/src/components/pages/orders/unfinished_orders.tsx
@@ -1,5 +1,6 @@
import { OrdersEntity, OrderItemEntity } from "../../../postgres/entities";
import { createOrderItemsDescription, filterOrderItemsForActiveItems, getTotalOrderCost } from "../../../services/common";
+import { HtmxTargets } from "../../common/constants";
const unfinishedItemRowDescription = (orderItems: OrderItemEntity[]): string => {
// We should have some active items due to 'getUnfinishedOrderItems' query
@@ -23,7 +24,7 @@ export const UnfinishedOrdersComponent = (unfinishedOrderitems: OrdersEntity[])
{unfinishedItemRowDescription(item.order_items)} |
{getTotalOrderCost(filterOrderItemsForActiveItems(item.order_items))}.00 KES |
- |
+ |
)
})}
diff --git a/src/components/pages/root_page.tsx b/src/components/pages/root_page.tsx
index 891dfee..344dbe3 100644
--- a/src/components/pages/root_page.tsx
+++ b/src/components/pages/root_page.tsx
@@ -1,5 +1,9 @@
+import { getConfig } from "../..";
+import { HtmxTargets } from "../../components/common/constants"
import { ServerHxTriggerEvents } from "../../services/common/constants";
+const config = getConfig();
+
/**
* Root page of the application. It's responsible for:
* - Loading application CSS markup and JS files required.
@@ -14,16 +18,16 @@ export const RootPage = () => {
Business Name
-
-
-
-
-
+
+
+
+
+
-
-
-
+
+
+
);
diff --git a/src/components/pages/tailwind.tsx b/src/components/pages/tailwind.tsx
index 6696656..97ecebe 100644
--- a/src/components/pages/tailwind.tsx
+++ b/src/components/pages/tailwind.tsx
@@ -1,9 +1,11 @@
+import { getConfig } from "../..";
+
export const TailWindComponent = () => {
return (
-
+
diff --git a/src/index.ts b/src/index.ts
index 6040ce7..89af4ea 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -9,6 +9,9 @@ import { Logger } from "ts-log"; // Holy crap, this should tslog, not ts-log
* Values here are to be read from an .env file. Bun has built in support for env files.
*/
export interface Config {
+ applicationPort: number;
+ baseUrl: string;
+ jwtSecret: string;
postgresUser: string;
postgresPassword: string;
postgresHost: string;
@@ -22,6 +25,9 @@ export interface Config {
*/
export function getConfig(): Config {
return {
+ applicationPort: Number(process.env.APPLICATION_PORT) || 3000,
+ baseUrl: process.env.BASE_URL || "",
+ jwtSecret: process.env.JWT_SECRET || "not_cool",
postgresUser: process.env.POSTGRES_USER || "",
postgresPassword: process.env.POSTGRES_PASSWORD || "",
postgresHost: process.env.POSTGRES_HOST || "",
@@ -42,7 +48,7 @@ const dataSource = await PostgresDataSourceSingleton.getInstance();
*/
const app = createApplicationServer(dataSource);
-app.listen(3000);
+app.listen(getConfig().applicationPort);
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port} with NODE_ENV ${process.env.NODE_ENV}`,
diff --git a/src/routes/auth.ts b/src/routes/auth.ts
index 13143d6..dc02d50 100644
--- a/src/routes/auth.ts
+++ b/src/routes/auth.ts
@@ -13,6 +13,7 @@ import {
CookieConstansts,
ServerHxTriggerEvents,
} from "../services/common/constants";
+import { getConfig } from "..";
const authSchemas = {
processLoginRequestSchema: z.object({
@@ -36,7 +37,7 @@ export const authRoutes = (dataSource: DataSource) => {
.use(
jwt({
name: "jwt",
- secret: "notSoSecretForTesting",
+ secret: getConfig().jwtSecret,
}),
)
// TODO: Update to use basic authentication instead of passing username & password in request body?
diff --git a/src/server.ts b/src/server.ts
index 7f2e284..a600b5f 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -17,6 +17,7 @@ import { LoginComponent } from "./components/pages/auth/login";
import { getUserByUsernameWithCredentials } from "./postgres/queries";
import { OrderExampleTailwindComponent } from "./components/pages/orders/order_tailwind";
import { RootPage } from "./components/pages/root_page";
+import { getConfig } from ".";
/**
* We're initializing the application server with the DataSource as a parameter so that we can
@@ -30,7 +31,7 @@ export const createApplicationServer = (dataSource: DataSource) => {
.use(
jwt({
name: "jwt",
- secret: "notSoSecretForTesting",
+ secret: getConfig().jwtSecret,
}),
)
.use(html())
@@ -54,7 +55,7 @@ export const createApplicationServer = (dataSource: DataSource) => {
.get("/root", async (ctx) => {
console.log(ctx);
const { auth } = ctx.cookie;
- if (!auth) {
+ if (!auth) {
return LoginComponent();
}
const authValue = await ctx.jwt.verify(auth);
@@ -74,4 +75,4 @@ export const createApplicationServer = (dataSource: DataSource) => {
}
});
return app;
-};
+}
diff --git a/src/services/orders/index.ts b/src/services/orders/index.ts
index 70741f6..88c6d06 100644
--- a/src/services/orders/index.ts
+++ b/src/services/orders/index.ts
@@ -1,5 +1,6 @@
import { DataSource } from "typeorm";
import * as queries from "../../postgres/queries";
+
import { InfoWrapper } from "../../components/common/info_wrapper";
import { CreateOrUpdateOrderSection } from "../../components/pages/orders/create";
import { ActiveOrderItems } from "../../components/pages/orders/active_order_items";
@@ -14,20 +15,12 @@ import { UnfinishedOrdersComponent } from "../../components/pages/orders/unfinis
* Initializes a new order and payment in so that we can keep track of the order even after exiting.
*/
export const createOrder = async (dataSource: DataSource) => {
- try {
- const initializeOrderResult = await queries.initializeOrder(dataSource);
- // We should always have identifiers[0].id from TypeORM
- await queries.initializePayment(dataSource, initializeOrderResult.identifiers[0].id);
- const inventoryItems = await queries.getInventoryItemsOrderByName(dataSource);
- /**
- * Return empty array for orderItemsInOrder since ther order was just created.
- */
- return CreateOrUpdateOrderSection(initializeOrderResult.identifiers[0].id, inventoryItems, []);
-
- } catch (e) {
- console.log(e);
- throw (e);
- }
+ const initializeOrderResult = await queries.initializeOrder(dataSource);
+ // We should always have identifiers[0].id from TypeORM
+ await queries.initializePayment(dataSource, initializeOrderResult.identifiers[0].id);
+ const inventoryItems = await queries.getInventoryItemsOrderByName(dataSource);
+ // Return empty array for orderItemsInOrder since ther order was just created.
+ return CreateOrUpdateOrderSection(initializeOrderResult.identifiers[0].id, inventoryItems, []);
};
/**
@@ -38,14 +31,9 @@ export const createOrder = async (dataSource: DataSource) => {
* items form the database.
*/
export const resumeOrder = async (dataSource: DataSource, orderId: number) => {
- try {
- const inventoryItems = await queries.getInventoryItemsOrderByName(dataSource);
- const orderItems = await queries.getOrderItemsInOrder(dataSource, orderId);
- return CreateOrUpdateOrderSection(orderId, inventoryItems, filterOrderItemsForActiveItems(orderItems));
- } catch (e) {
- console.log(e);
- throw (e);
- }
+ const inventoryItems = await queries.getInventoryItemsOrderByName(dataSource);
+ const orderItems = await queries.getOrderItemsInOrder(dataSource, orderId);
+ return CreateOrUpdateOrderSection(orderId, inventoryItems, filterOrderItemsForActiveItems(orderItems));
};
/**
@@ -57,60 +45,50 @@ export const resumeOrder = async (dataSource: DataSource, orderId: number) => {
* We will assume that all payments have been completed once an order is confirmed.
*/
export const confirmOrder = async (dataSource: DataSource, orderId: number, payemntId: number) => {
- try {
- await simulateNetworkLatency(2000);
- const getOrderResult = await queries.getOrderById(dataSource, orderId);
- const getPaymentResult = await queries.getPaymentByOrderId(dataSource, orderId);
-
- // ensure that both values are not null
- if (getOrderResult === null || getPaymentResult === null) {
- const message = `Missing order or payment. orderId: ${orderId} | paymentId: ${payemntId}`;
- console.error(message);
- throw new Error(message);
- }
-
- // ensure that payment passed in matches its order
- if (getPaymentResult.id !== payemntId) {
- const message = `Payment id in request [${payemntId}] did not match id [${getPaymentResult.id}] for order with identifier: ${orderId}`
- console.error(message);
- throw new Error(message);
- }
+ await simulateNetworkLatency(2000);
+ const getOrderResult = await queries.getOrderById(dataSource, orderId);
+ const getPaymentResult = await queries.getPaymentByOrderId(dataSource, orderId);
+
+ // ensure that both values are not null
+ if (getOrderResult === null || getPaymentResult === null) {
+ const message = `Missing order or payment. orderId: ${orderId} | paymentId: ${payemntId}`;
+ console.error(message);
+ throw new Error(message);
+ }
- // ensure that order is already not in a completed state
- if (getOrderResult.status === OrderStatus.COMPLETED) {
- const message = `Order with id [${orderId}] is already in a completed state`;
- console.error(message);
- throw new Error(message);
- }
+ // ensure that payment passed in matches its order
+ if (getPaymentResult.id !== payemntId) {
+ const message = `Payment id in request [${payemntId}] did not match id [${getPaymentResult.id}] for order with identifier: ${orderId}`
+ console.error(message);
+ throw new Error(message);
+ }
- // get all the items in the order
- // if confirm button is shown in the UI, there should be active items in the order
- // We can go ahead and complete the order and payment as well
- const orderItems = await queries.getOrderItemsInOrder(dataSource, orderId);
-
- await queries.completeOrder(dataSource, orderId);
- await queries.completePayment(dataSource, payemntId, getTotalOrderCost(filterOrderItemsForActiveItems(orderItems)));
- return orderCreateSuccess;
- } catch (e) {
- console.log(e);
- throw (e);
+ // ensure that order is already not in a completed state
+ if (getOrderResult.status === OrderStatus.COMPLETED) {
+ const message = `Order with id [${orderId}] is already in a completed state`;
+ console.error(message);
+ throw new Error(message);
}
+
+ // get all the items in the order
+ // if confirm button is shown in the UI, there should be active items in the order
+ // We can go ahead and complete the order and payment as well
+ const orderItems = await queries.getOrderItemsInOrder(dataSource, orderId);
+
+ await queries.completeOrder(dataSource, orderId);
+ await queries.completePayment(dataSource, payemntId, getTotalOrderCost(filterOrderItemsForActiveItems(orderItems)));
+ return orderCreateSuccess;
};
export const activeOrders = async (dataSource: DataSource, orderId: number) => {
- try {
- const orderItems = await queries.getOrderItemsInOrder(dataSource, orderId);
- const getPaymentResult = await queries.getPaymentByOrderId(dataSource, orderId);
- if (getPaymentResult === null) {
- const message = `Failed to get payment for order with id: ${orderId}`;
- console.error(message);
- throw new Error(message);
- }
- return ActiveOrderItems(orderId, filterOrderItemsForActiveItems(orderItems), getPaymentResult);
- } catch (e) {
- console.error(e);
- throw (e);
+ const orderItems = await queries.getOrderItemsInOrder(dataSource, orderId);
+ const getPaymentResult = await queries.getPaymentByOrderId(dataSource, orderId);
+ if (getPaymentResult === null) {
+ const message = `Failed to get payment for order with id: ${orderId}`;
+ console.error(message);
+ throw new Error(message);
}
+ return ActiveOrderItems(orderId, filterOrderItemsForActiveItems(orderItems), getPaymentResult);
};
/**
@@ -127,33 +105,27 @@ export const activeOrders = async (dataSource: DataSource, orderId: number) => {
*
*/
export const updateItemCounter = async (dataSource: DataSource, itemId: number, updateType: string) => {
- try {
- // ignore unknown actions
- if (updateType !== "INC" && updateType !== "DEC") {
- console.warn(`Unkown updateType of ${updateType} passed to updateItemCounter function`);
- return;
- }
-
- const orderItem = await queries.getOrderItemById(dataSource, itemId);
+ // ignore unknown actions
+ if (updateType !== "INC" && updateType !== "DEC") {
+ console.warn(`Unkown updateType of ${updateType} passed to updateItemCounter function`);
+ return;
+ }
- // ignore if order item is null
- if (orderItem === null) {
- console.warn(`Order item with id ${itemId} not found`);
- return;
- }
+ const orderItem = await queries.getOrderItemById(dataSource, itemId);
- // ignore if counter already at 1 and decrement action passed in
- if (orderItem.quantity === 1 && updateType === "DEC") {
- console.warn(`Order item with id ${itemId} is already at lowest value`);
- return;
- }
-
- await queries.updateOrderItemCount(dataSource, itemId, updateType === "DEC" ? orderItem.quantity - 1 : orderItem.quantity + 1);
+ // ignore if order item is null
+ if (orderItem === null) {
+ console.warn(`Order item with id ${itemId} not found`);
+ return;
+ }
- } catch (e) {
- console.error(e);
- throw (e);
+ // ignore if counter already at 1 and decrement action passed in
+ if (orderItem.quantity === 1 && updateType === "DEC") {
+ console.warn(`Order item with id ${itemId} is already at lowest value`);
+ return;
}
+
+ await queries.updateOrderItemCount(dataSource, itemId, updateType === "DEC" ? orderItem.quantity - 1 : orderItem.quantity + 1);
};
/**
@@ -166,25 +138,19 @@ export const updateItemCounter = async (dataSource: DataSource, itemId: number,
* This endpoint returns a list of all unfinished orders, so that it can be resumed.
*/
export const listUnfinishedOrders = async (dataSource: DataSource) => {
- try {
- const result = await queries.getOrders(dataSource);
- if (result.length === 0) {
- return InfoWrapper("No orders made yet. Create first order");
- } else {
- const unfinishedOrders = await queries.getUnfinishedOrderItems(dataSource);
- const filteredOrders = filterForOrdersWithActiveOrders(unfinishedOrders);
- if (filteredOrders.length === 0) {
- return InfoWrapper("No recent unfinished orders.");
- }
- else {
- return UnfinishedOrdersComponent(filteredOrders);
- }
+ const result = await queries.getOrders(dataSource);
+ if (result.length === 0) {
+ return InfoWrapper("No orders made yet. Create first order");
+ } else {
+ const unfinishedOrders = await queries.getUnfinishedOrderItems(dataSource);
+ const filteredOrders = filterForOrdersWithActiveOrders(unfinishedOrders);
+ if (filteredOrders.length === 0) {
+ return InfoWrapper("No recent unfinished orders.");
+ }
+ else {
+ return UnfinishedOrdersComponent(filteredOrders);
}
- } catch (e) {
- console.error(e);
- throw (e);
}
-
};
/**
@@ -195,20 +161,15 @@ export const listUnfinishedOrders = async (dataSource: DataSource) => {
* "Remove" in quotes because the backend doesn't actually remove it, just deactivates it. This enables easier addition back if removed errorneously, and also will still contain it's previous context.
*/
export const addOrRemoveOrderItem = async (dataSource: DataSource, orderId: number, inventoryId: number) => {
- try {
- const orderItem = await queries.getOrderItemByInventoryId(dataSource, orderId, inventoryId);
- console.log(orderItem);
-
- if (orderItem === null) {
- console.log("Item doesn't exist in order, creating it.");
- await queries.insertOrderitem(dataSource, orderId, inventoryId);
- } else {
- console.log("Item already exists in order. Toggling active state");
- await queries.toggleOrderItem(dataSource, orderItem.id, !orderItem.active);
- }
- } catch (e) {
- console.error(e);
- throw (e);
+ const orderItem = await queries.getOrderItemByInventoryId(dataSource, orderId, inventoryId);
+ console.log(orderItem);
+
+ if (orderItem === null) {
+ console.log("Item doesn't exist in order, creating it.");
+ await queries.insertOrderitem(dataSource, orderId, inventoryId);
+ } else {
+ console.log("Item already exists in order. Toggling active state");
+ await queries.toggleOrderItem(dataSource, orderItem.id, !orderItem.active);
}
}
@@ -216,18 +177,13 @@ export const addOrRemoveOrderItem = async (dataSource: DataSource, orderId: numb
* Updates the payment type radio buttons to show which payment type (currently CASH & M-Pesa) is to be associated with this transaction.
*/
export const updatePaymentTypeForOrder = async (dataSource: DataSource, paymentId: number, paymentType: PaymentTypes) => {
- try {
- const getPaymentResult = await queries.getPaymentById(dataSource, paymentId);
+ const getPaymentResult = await queries.getPaymentById(dataSource, paymentId);
- if (getPaymentResult === null) {
- const message = `Cannot update payment type as payment with id: ${paymentId} not found`;
- console.error(message);
- throw new Error(message);
- }
-
- await queries.updatePaymentType(dataSource, paymentId, paymentType);
- } catch (e) {
- console.error(e);
- throw (e);
+ if (getPaymentResult === null) {
+ const message = `Cannot update payment type as payment with id: ${paymentId} not found`;
+ console.error(message);
+ throw new Error(message);
}
+
+ await queries.updatePaymentType(dataSource, paymentId, paymentType);
};