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-58]: Implement Business Registration Validation #136

Merged
merged 12 commits into from
Jan 25, 2024
2 changes: 2 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"graphql": "^16.8.1",
"graphql-request": "^6.1.0",
"helmet": "^7.1.0",
"ioredis": "^5.3.2",
"morgan": "^1.10.0",
Expand Down
28 changes: 28 additions & 0 deletions apps/api/src/businesses/operations/registerBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { z } from "zod";
import { gql } from "graphql-request";

import { client } from "../../lib/graphql-request";

const query = gql`
query getBusinesses {
business {
created_at
id
updated_at
name
}
}
`;

export const businessSchema = z.object({
name: z.string(),
});

export type BusinessAttributes = z.infer<typeof businessSchema>;

export const registerBusiness = async (business: BusinessAttributes) => {
const data = await client.request(query);
console.log(business);
console.log(data);
return business;
};
31 changes: 31 additions & 0 deletions apps/api/src/businesses/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NextFunction, Response, Router, Request } from "express";
import { logger } from "../lib/winston";
import { logRequest } from "../middleware/requestLogger";
import { validate } from "../middleware/zodValidate";
import {
registerBusiness,
businessSchema,
} from "./operations/registerBusiness";

const businessRouter = Router();

businessRouter.use(logRequest);

// register a business
businessRouter.post(
"/",
validate(businessSchema),
async (req: Request, res: Response, next: NextFunction) => {
try {
// @ts-ignore
const business = await registerBusiness(req.body);
res.send({
business,
});
} catch (error) {
next(error);
}
}
);

export default businessRouter;
Empty file.
9 changes: 7 additions & 2 deletions apps/api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ export const fromEmailAddress =
process.env.FROM_EMAIL_ADDRESS || "[email protected]";
export const host = process.env.HOST || "localhost:3000";

export const WebPushPublicVapidKey = process.env.WEB_PUSH_PUBLIC_VAPID_KEY;
export const WebPushPrivateVapidKey = process.env.WEB_PUSH_PRIVATE_VAPID_KEY;
export const WebPushPublicVapidKey =
process.env.WEB_PUSH_PUBLIC_VAPID_KEY || "";
export const WebPushPrivateVapidKey =
process.env.WEB_PUSH_PRIVATE_VAPID_KEY || "";

export const hasuraEndpoint = process.env.HASURA_GRAPHQL_ENDPOINT || "";
export const hasuraAdminSecret = process.env.HASURA_GRAPHQL_ADMIN_SECRET || "";
12 changes: 12 additions & 0 deletions apps/api/src/lib/graphql-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { graphQLClient } from "@sahil/lib/graphql/graphql-request";
import { hasuraAdminSecret, hasuraEndpoint } from "../config";

const endpoint = hasuraEndpoint;

export const client = graphQLClient(endpoint, {
headers: {
authorization: `Bearer MY_TOKEN`,
Fixed Show fixed Hide fixed
"x-hasura-admin-secret": hasuraAdminSecret,
"x-hasura-role": "user",
},
});
16 changes: 11 additions & 5 deletions apps/api/src/lib/webPush.ts → apps/api/src/lib/web-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import { WebPushPrivateVapidKey, WebPushPublicVapidKey } from "../config";

const webPush = require("web-push");

webPush.setVapidDetails(
"mailto:[email protected]",
WebPushPublicVapidKey,
WebPushPrivateVapidKey
);
export const setVapidKeys = () => {
if (WebPushPrivateVapidKey && WebPushPublicVapidKey) {
webPush.setVapidDetails(
"mailto:[email protected]",
WebPushPublicVapidKey,
WebPushPrivateVapidKey
);
}
};

setVapidKeys();

export const sendPushNotification = (subscription: any, payload: any) => {
return webPush.sendNotification(subscription, payload);
Expand Down
6 changes: 2 additions & 4 deletions apps/api/src/middleware/zodValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export const validate =
};

next();
} catch (error) {
logger.error(error);
// @ts-ignore
return res.status(400).send(error.errors);
} catch (error: any) {
return res.status(400).json(error.errors);
}
};
2 changes: 1 addition & 1 deletion apps/api/src/notifications/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router, Request, Response } from "express";
import { sendPushNotification } from "../lib/webPush";
import { sendPushNotification } from "../lib/web-push";

const router = Router();

Expand Down
22 changes: 13 additions & 9 deletions apps/api/src/orders/operations/initOrder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
type OrderAttributes = {
created_at: Date;
customerId: string;
destination: string;
id: string;
orderId: string;
origin: string;
processedBy: string;
};
import { z } from "zod";

export const orderSchema = z.object({
orderId: z.string(),
created_at: z.date(),
customerId: z.string(),
destination: z.string(),
id: z.string(),
origin: z.string(),
processedBy: z.string(),
});

export type OrderAttributes = z.infer<typeof orderSchema>;

export const initOrder = (
attributes: OrderAttributes
Expand Down
15 changes: 1 addition & 14 deletions apps/api/src/orders/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,11 @@ import { z } from "zod";
import { logger } from "../lib/winston";
import { logRequest } from "../middleware/requestLogger";
import { validate } from "../middleware/zodValidate";
import { initOrder } from "./operations/initOrder";
import { initOrder, orderSchema } from "./operations/initOrder";
import { processOrder } from "./operations/processOrder";

const ordersRouter = Router();

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

ordersRouter.use(logRequest);

ordersRouter.post(
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Queue } from "bullmq";
import { connection } from "./lib/ioredis";

export enum Queues {
Business = "Business",
Event = "Event",
Mail = "Mail",
Notification = "Notification",
Expand Down
6 changes: 4 additions & 2 deletions apps/api/src/routers/api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Router } from "express";

import businesses from "../businesses/router";
import notifications from "../notifications/router";
import orders from "../orders/router";
import users from "../users/router";
import notifications from "../notifications/router";

const router = Router();

router.use("/businesses", businesses);
router.use("/notifications", notifications);
router.use("/orders", orders);
router.use("/users", users);
router.use("/notifications", notifications);

export default router;
2 changes: 1 addition & 1 deletion apps/api/src/routers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import home from "./home";
const router = express.Router();

router.use("/api", api);
router.use("/", home)
router.use("/", home);

export default router;
21 changes: 21 additions & 0 deletions packages/lib/graphql/graphql-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { request, gql } from "graphql-request";
import { GraphQLClient } from "graphql-request";

type GraphQLClientOptions = {
headers?: {
authorization?: string;
"x-hasura-admin-secret"?: string;
"x-hasura-role"?: string;
};
};

export const graphQLClient = (
endpoint: string,
options: GraphQLClientOptions = {}
) => {
return new GraphQLClient(endpoint, {
headers: {
...options.headers,
},
});
};
2 changes: 1 addition & 1 deletion packages/lib/graphql/queries/businesses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gql } from "@apollo/client";

export const FETCH_BUSINESSES = gql`
query getClients {
query getBusinesses {
business {
created_at
id
Expand Down
6 changes: 5 additions & 1 deletion packages/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"name": "@sahil/lib",
"version": "1.0.0",
"main": "./index.ts",
"license": "MIT"
"license": "MIT",
"dependencies": {
"graphql": "^16.8.1",
"graphql-request": "^6.1.0"
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5145,7 +5145,7 @@ graphql-config@^5.0.2:
string-env-interpolation "^1.0.1"
tslib "^2.4.0"

graphql-request@^6.0.0:
graphql-request@^6.0.0, graphql-request@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/graphql-request/-/graphql-request-6.1.0.tgz#f4eb2107967af3c7a5907eb3131c671eac89be4f"
integrity sha512-p+XPfS4q7aIpKVcgmnZKhMNqhltk20hfXtkaIkTfjjmiKMJ5xrt5c743cL03y/K7y1rg3WrIC49xGiEQ4mxdNw==
Expand Down