|
| 1 | +import express, { Request, Response } from "express"; |
| 2 | +import { CustomError, Result } from "@repo/types/backend"; |
| 3 | +import { prisma } from "@repo/postgres_db/prisma"; |
| 4 | +import axios from "axios"; |
| 5 | +import { getUserCookie } from "../endpoints.js"; |
| 6 | +import { UserDetails } from "@repo/types/web"; |
| 7 | +import { platform } from "os"; |
| 8 | +import { totalErrors } from "../server.js"; |
| 9 | + |
| 10 | +const Router = express.Router(); |
| 11 | + |
| 12 | +Router.get("/", async (req: Request, res: Response): Promise<any> => { |
| 13 | + try { |
| 14 | + const { userId } = res.locals; |
| 15 | + if (!userId) { |
| 16 | + throw new CustomError( |
| 17 | + "Passed authentication and userId is missing.", |
| 18 | + "Server Error!" |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + const userData = await prisma.user.findUnique({ |
| 23 | + where: { |
| 24 | + id: userId, |
| 25 | + }, |
| 26 | + select: { |
| 27 | + firstName: true, |
| 28 | + lastName: true, |
| 29 | + email: true, |
| 30 | + contact: true, |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + return res.status(200).json( |
| 35 | + new Result({ |
| 36 | + success: true, |
| 37 | + data: { |
| 38 | + "First Name": userData?.firstName, |
| 39 | + "Last Name": userData?.lastName, |
| 40 | + Email: userData?.email, |
| 41 | + Contact: userData?.contact, |
| 42 | + }, |
| 43 | + }) |
| 44 | + ); |
| 45 | + } catch (error: any) { |
| 46 | + console.warn(error); |
| 47 | + totalErrors.inc(); |
| 48 | + return res.status(500).json( |
| 49 | + new Result({ |
| 50 | + success: false, |
| 51 | + }) |
| 52 | + ); |
| 53 | + } |
| 54 | +}); |
| 55 | + |
| 56 | +Router.put("/", async (req: Request, res: Response): Promise<any> => { |
| 57 | + try { |
| 58 | + const { userId } = res.locals; |
| 59 | + const { "First Name": firstName, "Last Name": lastName } = req.body; |
| 60 | + |
| 61 | + if (!userId || !firstName || !lastName) { |
| 62 | + throw new CustomError( |
| 63 | + "Kindly provide all the required parameters.", |
| 64 | + "Invalid credentials!" |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + const userData = await prisma.user.update({ |
| 69 | + where: { |
| 70 | + id: userId, |
| 71 | + }, |
| 72 | + data: { |
| 73 | + firstName, |
| 74 | + lastName, |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + return res.status(200).json( |
| 79 | + new Result({ |
| 80 | + success: true, |
| 81 | + data: { |
| 82 | + userData, |
| 83 | + }, |
| 84 | + }) |
| 85 | + ); |
| 86 | + } catch (error: any) { |
| 87 | + console.warn(error); |
| 88 | + totalErrors.inc(); |
| 89 | + return res.status(500).json( |
| 90 | + new Result({ |
| 91 | + success: false, |
| 92 | + }) |
| 93 | + ); |
| 94 | + } |
| 95 | +}); |
| 96 | + |
| 97 | +export default Router; |
| 98 | + |
| 99 | +Router.get( |
| 100 | + "/userDetails", |
| 101 | + async (req: Request, res: Response): Promise<any> => { |
| 102 | + try { |
| 103 | + const cookie = getUserCookie(req); |
| 104 | + |
| 105 | + const result = await axios({ |
| 106 | + url: "https://seller.indiamart.com/miscreact/ajaxrequest/seller/UserDetails/?sourcescreen=BusinessProfile", |
| 107 | + headers: { |
| 108 | + accept: "*/*", |
| 109 | + "accept-language": "en-US,en;q=0.6", |
| 110 | + "cache-control": "no-cache", |
| 111 | + "content-type": "application/json", |
| 112 | + pragma: "no-cache", |
| 113 | + priority: "u=1, i", |
| 114 | + "sec-ch-ua": |
| 115 | + '"Not)A;Brand";v="8", "Chromium";v="138", "Brave";v="138"', |
| 116 | + "sec-ch-ua-mobile": "?0", |
| 117 | + "sec-ch-ua-platform": platform(), |
| 118 | + "sec-fetch-dest": "empty", |
| 119 | + "sec-fetch-mode": "cors", |
| 120 | + "sec-fetch-site": "same-origin", |
| 121 | + "sec-gpc": "1", |
| 122 | + cookie, |
| 123 | + Referer: "https://seller.indiamart.com/companyprofile/manageprofile/", |
| 124 | + }, |
| 125 | + method: "POST", |
| 126 | + }); |
| 127 | + |
| 128 | + const userDetails: UserDetails = { |
| 129 | + firstName: result.data.ceo_fname, |
| 130 | + lastName: result.data.ceo_lname, |
| 131 | + glid: result.data.glid, |
| 132 | + country: result.data.country, |
| 133 | + companyName: result.data.company_name, |
| 134 | + phoneNumber: result.data.glusr_usr_ph_mobile, |
| 135 | + image: result.data.image, |
| 136 | + }; |
| 137 | + |
| 138 | + return res.status(200).json( |
| 139 | + new Result({ |
| 140 | + success: true, |
| 141 | + data: userDetails, |
| 142 | + }) |
| 143 | + ); |
| 144 | + } catch (error: any) { |
| 145 | + console.warn(error); |
| 146 | + totalErrors.inc(); |
| 147 | + return res.status(500).json( |
| 148 | + new Result({ |
| 149 | + success: false, |
| 150 | + }) |
| 151 | + ); |
| 152 | + } |
| 153 | + } |
| 154 | +); |
0 commit comments