Add daily price change to /available-units endpoint
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:
26
server.js
26
server.js
@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user