From 34ecec2da6bd617011c02198a0c376cddcb9889d Mon Sep 17 00:00:00 2001 From: Mike Johnston Date: Sat, 21 Mar 2026 21:47:47 -0400 Subject: [PATCH] try to fix active stocks losing price updates --- src/worker/index.ts | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/worker/index.ts b/src/worker/index.ts index 929f3e6..af1ba1f 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -144,25 +144,38 @@ const priceWorker = new Worker( } const shouldDeactivate = ttlExpired && ownerCount === 0 - await prisma.hashtag.update({ - where: { id: hashtagId }, - data: { - zeroCount: newZeroCount, - isActive: shouldDeactivate ? false : hashtag.isActive, - lastUpdated: new Date(), - }, - }) + const floorPrice = calcPrice(0) + await prisma.$transaction([ + prisma.hashtag.update({ + where: { id: hashtagId }, + data: { + zeroCount: newZeroCount, + isActive: shouldDeactivate ? false : hashtag.isActive, + lastUpdated: new Date(), + }, + }), + // Record floor price in history while the stock is still active so the chart has no gaps + ...(!shouldDeactivate ? [prisma.priceHistory.create({ + data: { hashtagId, price: floorPrice, postsPerHour: 0 }, + })] : []), + ]) console.log(`[price] #${tag} got 0 posts (zeroCount=${newZeroCount})${shouldDeactivate ? ' — deactivated (TTL expired, no holders)' : ''}`) return } - // If TTL expired and no holders, deactivate instead of updating + // If TTL expired and no holders, record final price then deactivate if (ttlExpired && ownerCount === 0) { - await prisma.hashtag.update({ - where: { id: hashtagId }, - data: { isActive: false, lastUpdated: new Date() }, - }) - console.log(`[price] #${tag} deactivated — TTL expired, no holders`) + const finalPrice = calcPrice(postsPerHour) + await prisma.$transaction([ + prisma.hashtag.update({ + where: { id: hashtagId }, + data: { currentPrice: finalPrice, isActive: false, lastUpdated: new Date() }, + }), + prisma.priceHistory.create({ + data: { hashtagId, price: finalPrice, postsPerHour }, + }), + ]) + console.log(`[price] #${tag} deactivated — TTL expired, no holders (final price $${finalPrice.toFixed(2)})`) return }