Fixed addition of new location w/ listing

This commit is contained in:
Soham Chari 2025-11-28 22:59:02 +05:30
parent 17e918da47
commit 96f0f0a76b
2 changed files with 33 additions and 10 deletions

View File

@ -11,7 +11,6 @@ baseConfig.user = process.env.PGUSER || "postgres";
baseConfig.password = process.env.PGPASSWORD || "postgres";
baseConfig.database = process.env.PGDATABASE || "postgres";
console.log("Base Config:", baseConfig);
const pool = new Pool(baseConfig);
pool.on("error", (err) => {

View File

@ -121,22 +121,17 @@ router.post("/", async (req, res) => {
} = req.body;
// Add a new location if provided
if (!animal.location_id && req.body.new_location) {
if (!animal.location_id && animal.new_location) {
const newLocation = {
...req.body.new_location,
...animal.new_location,
};
if (
newLocation.save_as_address &&
newLocation.is_saved_address &&
!newLocation.user_id &&
seller_id
) {
newLocation.user_id = seller_id;
}
if (typeof newLocation.is_saved_address === "undefined") {
newLocation.is_saved_address = Boolean(
newLocation.save_as_address
);
}
animal.location_id = await addNewLocation(client, newLocation);
}
@ -158,12 +153,41 @@ router.post("/", async (req, res) => {
listingValues
);
if (
req.body.media &&
Array.isArray(req.body.media) &&
req.body.media.length > 0
) {
const listingId = listingResult.rows[0].id;
const mediaInsertQuery = `
INSERT INTO listing_media (listing_id, media_url, media_type, is_primary, sort_order, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, NOW(), NOW())
RETURNING *;
`;
for (let i = 0; i < req.body.media.length; i++) {
const media = req.body.media[i];
await client.query(mediaInsertQuery, [
listingId,
media.media_url,
media.media_type,
typeof media.is_primary !== "undefined"
? media.is_primary
: false,
typeof media.sort_order !== "undefined"
? media.sort_order
: i + 1,
]);
}
}
await client.query("COMMIT");
res.status(201).json(listingResult.rows[0]);
} catch (error) {
await client.query("ROLLBACK");
res.status(500).json({
error: "Internal Server Error in creating new listing",
error:
"Internal Server Error in creating new listing: " +
error.message,
});
} finally {
client.release();