diff --git a/.env b/.env deleted file mode 100644 index 17d76ae..0000000 --- a/.env +++ /dev/null @@ -1,5 +0,0 @@ -POSTGRES_USER="betninja" -POSTGRES_PASSWORD="swipathefox" -POSTGRES_HOST="localhost" -POSTGRES_PORT="5432" -POSTGRES_DATABASE_NAME="backend" diff --git a/.gitignore b/.gitignore index 87e5610..574eb2c 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,7 @@ yarn-error.log* **/*.tgz **/*.log package-lock.json -**/*.bun \ No newline at end of file +**/*.bun + +# remove .env files +.env diff --git a/public/htmx_debug.js b/public/htmx_debug.js new file mode 100644 index 0000000..bfa3d54 --- /dev/null +++ b/public/htmx_debug.js @@ -0,0 +1,11 @@ +htmx.defineExtension('debug', { + onEvent: function(name, evt) { + if (console.debug) { + console.debug(name, evt); + } else if (console) { + console.log("DEBUG:", name, evt); + } else { + throw "NO CONSOLE SUPPORTED" + } + } +}); diff --git a/src/components/common/constants.ts b/src/components/common/constants.ts new file mode 100644 index 0000000..58d673a --- /dev/null +++ b/src/components/common/constants.ts @@ -0,0 +1,10 @@ +export enum HtmxTargets { + CREATE_ORDER_SECTION = "create-order-section", + INVENTORY_SECTION = "inventory-section", + INVENTORY_DATA_LIST = "inventory-data-list", + INVENTORY_SUBMIT_RESULT_VIEW = "inventory-submit-result-view", + LOGIN_SUBMIT_RESULT_VIEW = "login-submit-result-view", + MAIN_SECTION = "main-section", + ORDERS_SECTION = "orders-section", + ROOT_DIV = "root-div", +} diff --git a/src/components/pages/auth/login.tsx b/src/components/pages/auth/login.tsx index d9d9ab4..677fefc 100644 --- a/src/components/pages/auth/login.tsx +++ b/src/components/pages/auth/login.tsx @@ -1,9 +1,12 @@ +import { getConfig } from "../../.."; +import { HtmxTargets } from "../../common/constants"; + export const LoginComponent = () => { return (

Log in to get started.

-
+
- +
diff --git a/src/components/pages/index_component.tsx b/src/components/pages/index_component.tsx index 159d323..269efc2 100644 --- a/src/components/pages/index_component.tsx +++ b/src/components/pages/index_component.tsx @@ -1,11 +1,13 @@ +import { getConfig } from "../.."; import { UsersEntity } from "../../postgres/entities"; +import { HtmxTargets } from "../common/constants"; export const IndexComponent = (user: UsersEntity) => { return (
-
+
); diff --git a/src/components/pages/inventory/create_or_update_inventory_item.tsx b/src/components/pages/inventory/create_or_update_inventory_item.tsx index 73004ad..adaf382 100644 --- a/src/components/pages/inventory/create_or_update_inventory_item.tsx +++ b/src/components/pages/inventory/create_or_update_inventory_item.tsx @@ -1,4 +1,5 @@ import { InventoryEntity } from "../../../postgres/entities"; +import { HtmxTargets } from "../../common/constants"; export const CreateOrUpdateInventoryComponent = (inventoryItem?: InventoryEntity) => { return ( @@ -8,7 +9,7 @@ export const CreateOrUpdateInventoryComponent = (inventoryItem?: InventoryEntity
  • {inventoryItem === undefined ? "Create" : "Update"} Inventory
  • @@ -23,7 +24,7 @@ export const CreateOrUpdateInventoryComponent = (inventoryItem?: InventoryEntity
    - +
    ); diff --git a/src/components/pages/inventory/index.tsx b/src/components/pages/inventory/index.tsx index e3ef822..b1ff68d 100644 --- a/src/components/pages/inventory/index.tsx +++ b/src/components/pages/inventory/index.tsx @@ -1,3 +1,5 @@ +import { HtmxTargets } from "../../common/constants"; + export const InventoryPage = (
    @@ -6,7 +8,7 @@ export const InventoryPage = (
    -
    +
    ); diff --git a/src/components/pages/inventory/inventory.tsx b/src/components/pages/inventory/inventory.tsx index 4e4bac1..e748cfc 100644 --- a/src/components/pages/inventory/inventory.tsx +++ b/src/components/pages/inventory/inventory.tsx @@ -1,20 +1,22 @@ +import { HtmxTargets } from "../../common/constants" + export const ViewInventorySection = () => { return (
    - +
    - +
    -
    +
    ); }; diff --git a/src/components/pages/inventory/inventory_list.tsx b/src/components/pages/inventory/inventory_list.tsx index a05a2fc..1f666a7 100644 --- a/src/components/pages/inventory/inventory_list.tsx +++ b/src/components/pages/inventory/inventory_list.tsx @@ -1,4 +1,5 @@ import { InventoryEntity } from "../../../postgres/entities"; +import { HtmxTargets } from "../../common/constants"; export const inventoryList = (inventoryItems: InventoryEntity[]) => { return ( @@ -20,7 +21,7 @@ export const inventoryList = (inventoryItems: InventoryEntity[]) => { {inventoryItem.id} {inventoryItem.name} {inventoryItem.price}.00 KES - + ); })} diff --git a/src/components/pages/inventory/view_inventory_orders.tsx b/src/components/pages/inventory/view_inventory_orders.tsx index 6943110..ddb0f68 100644 --- a/src/components/pages/inventory/view_inventory_orders.tsx +++ b/src/components/pages/inventory/view_inventory_orders.tsx @@ -1,5 +1,6 @@ import { InventoryEntity, OrdersEntity } from "../../../postgres/entities/index" import { createOrderItemsDescription, getTotalOrderCost } from "../../../services/common"; +import { HtmxTargets } from "../../common/constants"; import { InfoWrapper } from "../../common/info_wrapper"; export const ViewInventoryItemOrdersComponent = (inventoryItem: InventoryEntity, orders: OrdersEntity[]) => { @@ -10,7 +11,7 @@ export const ViewInventoryItemOrdersComponent = (inventoryItem: InventoryEntity,
  • Orders for: {inventoryItem.name}
  • { diff --git a/src/components/pages/orders/active_order_items.tsx b/src/components/pages/orders/active_order_items.tsx index f923ee5..4c9d90b 100644 --- a/src/components/pages/orders/active_order_items.tsx +++ b/src/components/pages/orders/active_order_items.tsx @@ -1,6 +1,7 @@ import { PaymentTypes } from "../../../postgres/common/constants"; import { OrderItemEntity, PaymentEntity } from "../../../postgres/entities"; import { getTotalOrderCost } from "../../../services/common"; +import { HtmxTargets } from "../../common/constants"; export const ActiveOrderItems = (orderId: number, orderItems: OrderItemEntity[], paymentEntity: PaymentEntity) => { return ( @@ -75,7 +76,7 @@ export const ActiveOrderItems = (orderId: number, orderItems: OrderItemEntity[], - {orderItems.length === 0 ? "" : } + {orderItems.length === 0 ? "" : } ); }; diff --git a/src/components/pages/orders/create.tsx b/src/components/pages/orders/create.tsx index 72cb8fd..de69b7d 100644 --- a/src/components/pages/orders/create.tsx +++ b/src/components/pages/orders/create.tsx @@ -1,5 +1,6 @@ import { InventoryEntity, OrderItemEntity } from "../../../postgres/entities"; import { ServerHxTriggerEvents } from "../../../services/common/constants"; +import { HtmxTargets } from "../../common/constants"; import { InventoryItemSelectDuringOrder } from "./inventory_item_select_during_order"; export const CreateOrUpdateOrderSection = (orderId: number, inventoryData: InventoryEntity[], orderItemsInOrder: OrderItemEntity[]) => { @@ -10,10 +11,10 @@ export const CreateOrUpdateOrderSection = (orderId: number, inventoryData: Inven
  • Create Order
  • -
    +
    {InventoryItemSelectDuringOrder(orderId, inventoryData, orderItemsInOrder)}
    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 = (
    @@ -6,7 +8,7 @@ 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/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); };