diff --git a/web/src/app/v1/consumption/daily/route.ts b/web/src/app/v1/consumption/daily/route.ts index a42f94a..896b232 100644 --- a/web/src/app/v1/consumption/daily/route.ts +++ b/web/src/app/v1/consumption/daily/route.ts @@ -7,13 +7,13 @@ export const runtime = "nodejs"; export const dynamic = "force-dynamic"; /** - * Upsert on (installationId, date), incrementally. The car sends a 30-minute delta chunk + * Upsert on (installationId, date), incrementally. The car sends a 2-minute delta chunk * (not a cumulative total), so each PUT adds to the day's running totals rather than * overwriting them. Lifetime is a snapshot — the latest one wins, so it is $set not $inc. * - * The old cumulative-overwrite format still works: a record whose day totals already - * represent the full day simply adds them again, which is wrong — but the app no longer - * sends that format, and the legacy 189 km document is never re-sent. New data only. + * $setOnInsert seeds the whole `day` subdocument as a nested object (not dotted paths) + * because $inc and $setOnInsert cannot touch the same field — MongoDB rejects that with + * a 500, which is what held the 20 cached chunks back. */ export async function PUT(request: Request) { const denied = requireIngest(request); @@ -23,27 +23,26 @@ export async function PUT(request: Request) { await ensureIndexes(); const collection = await consumptionCollection(); const updatedAt = new Date(); - const setOnInsert: Record = { - createdAt: updatedAt, - installationId: record.installationId, - date: record.date, - "day.km": 0, "day.kwh": 0, "day.hours": 0, "day.socDrop": 0, - }; - const update: Record> = { - $inc: { - "day.km": record.day.km, - "day.kwh": record.day.kwh, - "day.hours": record.day.hours, - "day.socDrop": record.day.socDrop, - }, - $set: { updatedAt }, - $setOnInsert: setOnInsert, - }; - if (record.lifetime) update.$set.lifetime = record.lifetime; - if (record.sohPercent !== undefined) update.$set.sohPercent = record.sohPercent; + const $set: Record = { updatedAt }; + if (record.lifetime) $set.lifetime = record.lifetime; + if (record.sohPercent !== undefined) $set.sohPercent = record.sohPercent; await collection.updateOne( { installationId: record.installationId, date: record.date }, - update, + { + $inc: { + "day.km": record.day.km, + "day.kwh": record.day.kwh, + "day.hours": record.day.hours, + "day.socDrop": record.day.socDrop, + }, + $set, + $setOnInsert: { + createdAt: updatedAt, + installationId: record.installationId, + date: record.date, + day: { km: 0, kwh: 0, hours: 0, socDrop: 0 }, + }, + }, { upsert: true }, ); return new NextResponse(null, { status: 204 });