feat(12-01): wire matchDelta into pollDueSources; register TenderMatchingService

pollDueSources now collects genuinely-new tender IDs via an indexed
dedupKey pre-check (existing upsert doesn't report create-vs-update),
and calls TenderMatchingService.matchDelta(newTenderIds) once at the
end of the tick — the delta-only matching boundary (D-07). Changed/
re-seen rows are excluded, only genuinely new rows trigger matching.

TenderMatchingService registered as a provider in TendersModule and
injected into TenderIngestionService. Ingestion spec extended to
assert matchDelta receives only the new IDs, and is not called when
no new tenders were ingested this tick.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-22 09:05:41 +02:00
parent 92d4c969de
commit b45047f0b3
3 changed files with 111 additions and 3 deletions
@@ -1,6 +1,7 @@
import { Injectable, Logger } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { DoeOpenDataAdapter } from './adapters/doe-opendata.adapter';
import { TenderMatchingService } from './tender-matching.service';
import { TenderNormalizerService } from './tender-normalizer.service';
const DOE_SOURCE_TYPE = 'doe-opendata';
@@ -45,6 +46,13 @@ function addOneDay(day: string): string {
* queries in the tenant RLS extension — these are platform-global,
* RLS-exempt tables. Applying that extension here would silently filter
* out platform data for a 2nd tenant's session.
*
* Plan 12-01 (NOTIFY-03): pollDueSources now collects the IDs of genuinely
* NEW Tender rows created this tick (via an indexed pre-check against the
* existing upsert) and calls `TenderMatchingService.matchDelta` with only
* those IDs at the end of the tick — this is the delta-only matching
* boundary (D-07) that structurally prevents a backfill flood. Rows that
* merely changed (SCHEMA-02 contentHash update) are NOT included.
*/
@Injectable()
export class TenderIngestionService {
@@ -57,6 +65,7 @@ export class TenderIngestionService {
private readonly prisma: PrismaService,
private readonly doeAdapter: DoeOpenDataAdapter,
private readonly normalizer: TenderNormalizerService,
private readonly matching: TenderMatchingService,
) {}
/**
@@ -85,6 +94,10 @@ export class TenderIngestionService {
});
let fetchedAtLeastOneDay = false;
let isFirstFetch = true;
// Delta-only matching boundary (D-07, NOTIFY-03): only genuinely NEW
// tender rows created THIS tick are collected here and handed to
// matchDelta below — never changed/re-seen rows, never historical rows.
const newTenderIds: string[] = [];
while (cursorDay && cursorDay < berlinToday) {
if (!isFirstFetch) {
@@ -98,7 +111,17 @@ export class TenderIngestionService {
const normalized = rawRecords.map((r) => this.normalizer.normalize(r));
for (const tender of normalized) {
await this.prisma.tender.upsert({
// Indexed pre-check (dedupKey @unique) — the plain upsert below
// does not report create-vs-update, so we check existence first
// to determine whether this row is genuinely NEW this tick
// (D-07 delta boundary). Negligible cost: a handful of records/
// day for the DÖE source.
const existing = await this.prisma.tender.findUnique({
where: { dedupKey: tender.dedupKey },
select: { id: true },
});
const saved = await this.prisma.tender.upsert({
where: { dedupKey: tender.dedupKey },
update: {
title: tender.title,
@@ -135,6 +158,8 @@ export class TenderIngestionService {
publishedAt: tender.publishedAt,
},
});
if (!existing) newTenderIds.push(saved.id); // genuinely NEW row this tick (D-07)
}
await this.prisma.tenderSourcePollConfig.update({
@@ -149,6 +174,13 @@ export class TenderIngestionService {
if (fetchedAtLeastOneDay) {
await this.pruneExpiredTenders();
}
if (newTenderIds.length) {
// Matching failures must never crash the ingestion tick — the
// outer catch-and-log already covers this, but matchDelta itself
// also catches per-profile (defense in depth).
await this.matching.matchDelta(newTenderIds);
}
} catch (err) {
this.logger.error(`Tender poll tick failed: ${(err as Error).message}`);
}