89 lines
2.6 KiB
TypeScript
89 lines
2.6 KiB
TypeScript
import { Router } from "express";
|
|
import { searchCurrentApartmentApi } from "../consts.js";
|
|
import { logger } from "../utils/logger.js";
|
|
import { IApartment } from "../types/apartment.js";
|
|
|
|
const router = Router();
|
|
|
|
function ConvertApartmentRes(apartment: IApartment) {
|
|
const convertedApartment: IApartment = {
|
|
Balcony_Area_Sqft: apartment.Balcony_Area_Sqft,
|
|
Floor: apartment.Floor,
|
|
Property_Status: apartment.Property_Status,
|
|
Unit_Type: apartment.Unit_Type,
|
|
Project_Name: apartment.Project_Name,
|
|
Suite_Area_Sqft: apartment.Suite_Area_Sqft,
|
|
No_Of_Bedrooms: apartment.No_Of_Bedrooms,
|
|
Unit_No: apartment.Unit_No,
|
|
id: apartment.id,
|
|
Total_Area_Sqft: apartment.Total_Area_Sqft,
|
|
No_of_Bathrooms: apartment.No_of_Bathrooms,
|
|
Property_Name: apartment.Property_Name,
|
|
Unit_View: apartment.Unit_View,
|
|
};
|
|
|
|
return convertedApartment;
|
|
}
|
|
|
|
router.get("/:id", async (req, res) => {
|
|
const accessToken = req?.headers?.authorization;
|
|
const { id } = req.params;
|
|
|
|
try {
|
|
const url = `${searchCurrentApartmentApi}/${id}`;
|
|
const requestHeaders: HeadersInit = new Headers();
|
|
requestHeaders.set("Authorization", `${accessToken}`);
|
|
const response = await fetch(url, {
|
|
headers: requestHeaders,
|
|
});
|
|
|
|
if (response.status === 404) {
|
|
return res
|
|
.status(404)
|
|
.json({ message: "Квартира не найдена", code: 404 });
|
|
}
|
|
if (response.status === 401) {
|
|
return res
|
|
.status(401)
|
|
.json({ message: "Неправильный токен или токен устарел", code: 401 });
|
|
}
|
|
const result = await response.json();
|
|
|
|
const convertedApartment = ConvertApartmentRes(result.data[0]);
|
|
|
|
return res.status(200).json({
|
|
message: "ok",
|
|
apartment: convertedApartment,
|
|
code: 200,
|
|
});
|
|
} catch (error) {
|
|
if (
|
|
(error as Error).message === "invalid oauth token" ||
|
|
(error as Error).message === "INVALID_TOKEN"
|
|
) {
|
|
console.log("error", error);
|
|
logger.error(error);
|
|
|
|
return res
|
|
.status(401)
|
|
.json({ message: "Неправильный токен или токен устарел", code: 401 });
|
|
}
|
|
if (
|
|
(error as Error).message ===
|
|
"Please check if the URL trying to access is a correct one"
|
|
) {
|
|
return res
|
|
.status(404)
|
|
.json({ message: "Квартира не найдена", code: 404 });
|
|
}
|
|
console.log("error", error);
|
|
logger.error(error);
|
|
|
|
return res.status(500).json({ message: "Server Error", code: 500 });
|
|
}
|
|
});
|
|
|
|
const apartmentRoute = router;
|
|
|
|
export default apartmentRoute;
|