Skip to content

Commit

Permalink
add routes
Browse files Browse the repository at this point in the history
  • Loading branch information
abhayrp2000 committed Jul 14, 2023
1 parent ab69d2d commit ed6b37e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/routes/contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Request, Response, Router } from "express";
import { body } from "express-validator";

import { validateRequest } from "../middleware/request-validator";
import { getOrCreateIdentity } from "../service/contact";
import { handleError } from "../utils/custom-error";
import { Format } from "../utils/formatters";
import { logger } from "..";

const router = Router();

router.post(
"/",
body("email").if(body("phoneNumber").isEmpty()).exists().isString().trim(),
body("phoneNumber").if(body("email").isEmpty()).exists().isString().trim(),
validateRequest,
async (req: Request, res: Response) => {
try {
const { email, phoneNumber } = req.body;
const contacts = await getOrCreateIdentity(email, phoneNumber);

res.status(200).json(Format.identity(contacts));
} catch (err: any) {
console.log(err);
logger.error(err);
const { statusCode, code, message } = handleError(err);

res.status(statusCode).json({
code,
message,
});
}
}
);

export { router as ContactRouter };
12 changes: 12 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router, Request, Response } from "express";
import { ContactRouter } from "./contacts";

const router = Router();

router.get("/hello", async (req: Request, res: Response) => {
res.status(200).send("hello, ha bhai zinda hoon!");
});

router.use("/identity", ContactRouter);

export default router;

0 comments on commit ed6b37e

Please sign in to comment.