Add daily price change to /available-units endpoint
All checks were successful
CI/CD Pipeline - Apartment API / Run Tests (push) Successful in 9m42s
CI/CD Pipeline - Apartment API / Build & Push Image (push) Successful in 23s
CI/CD Pipeline - Apartment API / Deploy to Production (push) Successful in 16s

Add yesterdayPrice and priceChangeToday fields to show price delta
between today and yesterday instead of high-low spread.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-23 12:19:31 -07:00
parent b584dc94d4
commit ad5555d759

View File

@ -291,7 +291,8 @@ app.get('/price-history', requireAuth, async (req, res) => {
app.get('/available-units', requireAuth, async (req, res) => {
try {
const today = await getLatestDateWithData();
const yesterday = getYesterdayDate(today);
// Get units that have prices today (excluding furnished units)
const availableUnits = await db.collection(UNITS_COLLECTION).aggregate([
{
@ -315,11 +316,17 @@ app.get('/available-units', requireAuth, async (req, res) => {
},
{
$addFields: {
currentPrice: {
currentPriceDoc: {
$arrayElemAt: [
{ $filter: { input: "$price_info", cond: { $eq: ["$$this.date_checked", today] } } },
0
]
},
yesterdayPriceDoc: {
$arrayElemAt: [
{ $filter: { input: "$price_info", cond: { $eq: ["$$this.date_checked", yesterday] } } },
0
]
}
}
},
@ -408,6 +415,17 @@ app.get('/available-units', requireAuth, async (req, res) => {
}
}
},
{
$addFields: {
priceChangeToday: {
$cond: {
if: { $and: [{ $ne: ["$currentPriceDoc.price", null] }, { $ne: ["$yesterdayPriceDoc.price", null] }] },
then: { $subtract: ["$currentPriceDoc.price", "$yesterdayPriceDoc.price"] },
else: null
}
}
}
},
{
$project: {
unitCode: "$unit_code",
@ -418,7 +436,9 @@ app.get('/available-units', requireAuth, async (req, res) => {
community: "$community",
available: "$available",
soonest: "$soonest",
currentPrice: "$currentPrice.price",
currentPrice: "$currentPriceDoc.price",
yesterdayPrice: "$yesterdayPriceDoc.price",
priceChangeToday: { $ifNull: ["$priceChangeToday", null] },
minPrice: "$priceStats.minPrice",
maxPrice: "$priceStats.maxPrice",
daysAvailable: {