Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SAH-60]: Implement Order Processing Validation #131

Merged
merged 10 commits into from
Jan 21, 2024
2 changes: 2 additions & 0 deletions apps/agent/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Layout from "@/Layout/layout";
import { createApolloClient } from "@sahil/lib/graphql";

const graphqlUri = process.env.NEXT_PUBLIC_HASURA_GRAPHQL_ENDPOINT;
const ws = process.env.NEXT_PUBLIC_HASURA_GRAPHQL_WS;

const httpOptions = {
headers: {
Expand All @@ -17,6 +18,7 @@ const httpOptions = {
const client = createApolloClient({
uri: graphqlUri,
httpOptions,
ws: ws as string,
});

export default function App({
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/middleware/zodValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const validate =

next();
} catch (error) {
console.error(error);
Maiz27 marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore
return res.status(400).send(error.errors);
}
Expand Down
37 changes: 16 additions & 21 deletions apps/api/src/orders/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextFunction, Response, Router, Request } from "express";
import { pushIntoOrders } from "../enqueue";
import { object, z } from "zod";
import { z } from "zod";
import { logger } from "../lib/winston";
import { logRequest } from "../middleware/requestLogger";
import { validate } from "../middleware/zodValidate";
Expand All @@ -10,34 +10,29 @@ import { processOrder } from "./operations/processOrder";
const ordersRouter = Router();

const orderSchema = z.object({
orderId: z.string(),
customerId: z.string(),
order_items: z.object({
data: z
.object({
productId: z.string(),
price: z.number(),
quantity: z.number(),
})
.array(),
}),
});

ordersRouter.use(logRequest);

type OrdersActionType = {
created_at: Date;
customerId: string;
destination: string;
id: string;
orderId: string;
origin: string;
processedBy: string;
};

// place an order
ordersRouter.post(
"/",
validate(orderSchema),
async (req: Request, res: Response<OrdersActionType>, next: NextFunction) => {
async (req: Request, res: Response, next: NextFunction) => {
try {
// @ts-ignore
const order = await initOrder(req.locals);

// push into Queue
await pushIntoOrders(req.body);
res.send({
...order,
const { object } = req.body.input;
await pushIntoOrders(object);
Maiz27 marked this conversation as resolved.
Show resolved Hide resolved
return res.status(200).json({
order: object,
});
} catch (error) {
next(error);
Expand Down
6 changes: 3 additions & 3 deletions apps/client/src/Products/ProductsCatalogue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
HiArrowPath,
HiSignalSlash,
} from "react-icons/hi2";
import { Products } from "@sahil/lib/graphql/__generated__/graphql";

export const ProductsCatalogue = () => {
const [offset, setOffset] = useState(0);
Expand All @@ -38,8 +39,7 @@ export const ProductsCatalogue = () => {
);

useEffect(() => {
// @ts-ignore
setProducts(products);
setProducts(products as Products[]);
}, [products, setProducts]);
Maiz27 marked this conversation as resolved.
Show resolved Hide resolved

if (error) {
Expand Down Expand Up @@ -100,7 +100,7 @@ export const ProductsCatalogue = () => {
sizeLabel="Products"
/>
<List
data={products}
data={products as Products[]}
error={error}
loading={loading}
cols={4}
Expand Down
32 changes: 29 additions & 3 deletions apps/client/src/hooks/orders.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { useMutation, useQuery } from "@apollo/client";
import { useMutation, useQuery, useSubscription } from "@apollo/client";
import {
FETCH_ORDERS,
FETCH_ORDER_BY_PK,
INSERT_NEW_ORDER,
FETCH_ORDER_DELIVERIES,
FETCH_ORDERS_STATS,
INIT_ORDER_ACTION,
GET_ORDER_VALIDATION,
} from "@sahil/lib/graphql";
import {
InsertBusinessOrderMutation,
InsertBusinessOrderMutationVariables,
} from "@sahil/lib/graphql/__generated__/graphql";

export const useFetchOrders = () => {
const { error, data, loading } = useQuery(FETCH_ORDERS);
Expand All @@ -27,9 +33,12 @@ export const useFetchOrderByPK = (id: string) => {
};

export const usePlaceBusinessOrder = () => {
const [placeOrder, { data, loading, error }] = useMutation(INSERT_NEW_ORDER);
const [placeOrder, { data, loading, error }] = useMutation<
InsertBusinessOrderMutation,
InsertBusinessOrderMutationVariables
>(INSERT_NEW_ORDER);

return { loading, placeOrder, error };
return { data, loading, placeOrder, error };
};

export const useFetchOrderDeliveriesByPK = (id: string) => {
Expand All @@ -46,3 +55,20 @@ export const useGetOrdersStats = () => {

return { error, ordersCount: data?.orders_aggregate?.aggregate, loading };
};

export const useInitBusinessOrder = () => {
const [initOrder, { data, loading, error }] = useMutation(INIT_ORDER_ACTION);

return { data, loading, error, initOrder };
};

export const useOrderValidSubscription = (actionId: string) => {
const { data, loading, error } = useSubscription(GET_ORDER_VALIDATION, {
variables: {
id: actionId,
},
skip: !actionId,
});

return { data, loading, error };
};
6 changes: 4 additions & 2 deletions apps/client/src/hooks/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useRouter } from "next/router";
import {
GetProductsByNameQuery,
GetProductsByNameQueryVariables,
GetProductsQuery,
GetProductsQueryVariables,
} from "@sahil/lib/graphql/__generated__/graphql";

export const useFetchProducts = ({
Expand All @@ -21,8 +23,8 @@ export const useFetchProducts = ({
const graphqlQuery = name ? FETCH_PRODUCTS_BY_NAME : FETCH_PRODUCTS;

const { error, data, loading } = useQuery<
GetProductsByNameQuery,
GetProductsByNameQueryVariables
GetProductsQuery | GetProductsByNameQuery,
GetProductsQueryVariables | GetProductsByNameQueryVariables
>(graphqlQuery, {
variables: {
offset,
Expand Down
18 changes: 7 additions & 11 deletions apps/client/src/hooks/useOrderItemsStore.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { create } from "zustand";

type OrderItem = {
productId: string;
quantity: number;
};
import { Order_Item, Products } from "@sahil/lib/graphql/__generated__/graphql";

type OrderItemsStore = {
orderItems: OrderItem[];
products: any[];
addOrderItem: (item: OrderItem) => void;
removeOrderItem: (item: OrderItem) => void;
updateOrderItem: (item: OrderItem) => void;
setProducts: (products: any[]) => void;
orderItems: Order_Item[];
products: Products[];
addOrderItem: (item: Order_Item) => void;
removeOrderItem: (item: Order_Item) => void;
updateOrderItem: (item: Order_Item) => void;
setProducts: (products: Products[]) => void;
};

const INITIAL_STATE = {
Expand Down
2 changes: 2 additions & 0 deletions apps/client/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ApolloProvider } from "@apollo/client";
import { createApolloClient } from "@sahil/lib/graphql";

const graphqlUri = process.env.NEXT_PUBLIC_HASURA_GRAPHQL_ENDPOINT;
const ws = process.env.NEXT_PUBLIC_HASURA_GRAPHQL_WS;

const httpOptions = {
headers: {
Expand All @@ -16,6 +17,7 @@ const httpOptions = {
const client = createApolloClient({
uri: graphqlUri as string,
httpOptions,
ws: ws as string,
});

export default function App({ Component, pageProps }: AppProps) {
Expand Down
Loading