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) => {
|
app.get('/available-units', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const today = await getLatestDateWithData();
|
const today = await getLatestDateWithData();
|
||||||
|
const yesterday = getYesterdayDate(today);
|
||||||
|
|
||||||
// Get units that have prices today (excluding furnished units)
|
// Get units that have prices today (excluding furnished units)
|
||||||
const availableUnits = await db.collection(UNITS_COLLECTION).aggregate([
|
const availableUnits = await db.collection(UNITS_COLLECTION).aggregate([
|
||||||
{
|
{
|
||||||
@ -315,11 +316,17 @@ app.get('/available-units', requireAuth, async (req, res) => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
$addFields: {
|
$addFields: {
|
||||||
currentPrice: {
|
currentPriceDoc: {
|
||||||
$arrayElemAt: [
|
$arrayElemAt: [
|
||||||
{ $filter: { input: "$price_info", cond: { $eq: ["$$this.date_checked", today] } } },
|
{ $filter: { input: "$price_info", cond: { $eq: ["$$this.date_checked", today] } } },
|
||||||
0
|
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: {
|
$project: {
|
||||||
unitCode: "$unit_code",
|
unitCode: "$unit_code",
|
||||||
@ -418,7 +436,9 @@ app.get('/available-units', requireAuth, async (req, res) => {
|
|||||||
community: "$community",
|
community: "$community",
|
||||||
available: "$available",
|
available: "$available",
|
||||||
soonest: "$soonest",
|
soonest: "$soonest",
|
||||||
currentPrice: "$currentPrice.price",
|
currentPrice: "$currentPriceDoc.price",
|
||||||
|
yesterdayPrice: "$yesterdayPriceDoc.price",
|
||||||
|
priceChangeToday: { $ifNull: ["$priceChangeToday", null] },
|
||||||
minPrice: "$priceStats.minPrice",
|
minPrice: "$priceStats.minPrice",
|
||||||
maxPrice: "$priceStats.maxPrice",
|
maxPrice: "$priceStats.maxPrice",
|
||||||
daysAvailable: {
|
daysAvailable: {
|
||||||
|
|||||||
Reference in New Issue
Block a user