fix(apicore-sync): keep internal notes out of the shop and protect edited drafts
- Harvest descriptions no longer include ApiCore's internal harvest notes. The description becomes public on "Veröffentlichen", so a draft published without a careful read could have carried operational notes into the shop. - A repeated sync overwrote unpublished harvest drafts, undoing the admin's corrections (season instead of honey type, location, sales text). Drafts with an admin audit entry are now left alone and reported as protected. - Every ApiCore request has a 20 s timeout; before, an ApiCore that accepted the connection but never answered left the admin action hanging. - The sync panel shows the time of the last complete run. - Client tests: timeout, failed fetch is never treated as an empty source, malformed responses are rejected as a whole. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
e0b47faf45
commit
0482fd490e
9 files changed
+194
-17
No files matched your search
@@ -37,8 +37,11 @@ import { requireAdmin } from "./_shared";
|
||||
* ein Mensch freigibt.
|
||||
*
|
||||
* - Ernten: werden als unveroeffentlichte Entwuerfe angelegt (isPublished
|
||||
* false) bzw. als solche aktualisiert. Bereits veroeffentlichte, frueher
|
||||
* synchronisierte Ernten bleiben unberuehrt.
|
||||
* false) bzw. als solche aktualisiert — aber nur, solange niemand den
|
||||
* Entwurf im Admin bearbeitet hat (erkennbar am Audit-Log-Eintrag der
|
||||
* Bearbeitung). Sonst wuerde jeder weitere Lauf die Korrekturen des Admins
|
||||
* (Erntesaison statt Honigsorte, Standort, Verkaufstext) wieder
|
||||
* zuruecksetzen. Bereits veroeffentlichte Ernten bleiben ohnehin unberuehrt.
|
||||
* - Produkte: werden als deaktivierte Entwuerfe angelegt (isActive false,
|
||||
* Preis 0) und nur so lange aktualisiert, wie sie deaktiviert sind. Ein
|
||||
* bereits aktives Produkt wird nie angefasst — sonst wuerde ein Sync Preis,
|
||||
@@ -68,6 +71,23 @@ interface SyncCounts {
|
||||
failed: number;
|
||||
}
|
||||
|
||||
interface HarvestSyncCounts extends SyncCounts {
|
||||
/** Entwuerfe, die ein Mensch bearbeitet hat und die deshalb unveraendert blieben. */
|
||||
protectedDrafts: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hat jemand diese Ernte im Admin angefasst (bearbeitet, veroeffentlicht,
|
||||
* zurueckgezogen)? Der Sync selbst protokolliert ohne entityId und zaehlt
|
||||
* damit nicht mit.
|
||||
*/
|
||||
async function wasEditedByAdmin(harvestId: string): Promise<boolean> {
|
||||
const entries = await prisma.auditLog.count({
|
||||
where: { entityType: "Harvest", entityId: harvestId, action: { not: "harvest.create" } },
|
||||
});
|
||||
return entries > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Produkte kennen bewusst kein "updated" — sie werden nur neu angelegt,
|
||||
* siehe syncProducts().
|
||||
@@ -95,8 +115,8 @@ function describeError(error: unknown): string {
|
||||
// Ernten
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function syncHarvests(sources: ApiCoreHarvestWithApiaryName[]): Promise<SyncCounts> {
|
||||
const counts: SyncCounts = { created: 0, updated: 0, skipped: 0, failed: 0 };
|
||||
async function syncHarvests(sources: ApiCoreHarvestWithApiaryName[]): Promise<HarvestSyncCounts> {
|
||||
const counts: HarvestSyncCounts = { created: 0, updated: 0, skipped: 0, failed: 0, protectedDrafts: 0 };
|
||||
|
||||
for (const source of sources) {
|
||||
const slug = slugForApiCoreSource(source.id);
|
||||
@@ -114,6 +134,10 @@ async function syncHarvests(sources: ApiCoreHarvestWithApiaryName[]): Promise<Sy
|
||||
counts.skipped++;
|
||||
continue;
|
||||
}
|
||||
if (existing && (await wasEditedByAdmin(existing.id))) {
|
||||
counts.protectedDrafts++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const data = {
|
||||
year: date.getFullYear(),
|
||||
@@ -419,7 +443,9 @@ export async function syncApiCoreAction(formData: FormData): Promise<void> {
|
||||
await logAdminAction(admin, {
|
||||
action: "harvest.sync_apicore",
|
||||
entityType: "Harvest",
|
||||
detail: `${harvests.created} neu, ${harvests.updated} aktualisiert, ${harvests.skipped} übersprungen, ${harvests.failed} fehlgeschlagen`,
|
||||
detail:
|
||||
`${harvests.created} neu, ${harvests.updated} aktualisiert, ${harvests.skipped} übersprungen, ` +
|
||||
`${harvests.protectedDrafts} bearbeitete Entwürfe geschützt, ${harvests.failed} fehlgeschlagen`,
|
||||
});
|
||||
|
||||
// 2. Chargen — Abfuellungen einmal laden und fuer Schritt 3 weiterreichen.
|
||||
@@ -458,6 +484,7 @@ export async function syncApiCoreAction(formData: FormData): Promise<void> {
|
||||
redirect(
|
||||
`${returnTo}?syncDone=1` +
|
||||
`&hNew=${harvests.created}&hUpdated=${harvests.updated}&hSkipped=${harvests.skipped}&hFailed=${harvests.failed}` +
|
||||
`&hProtected=${harvests.protectedDrafts}` +
|
||||
`&bNew=${batches.created}&bSkipped=${batches.skipped}&bFailed=${batches.failed}` +
|
||||
`&pNew=${products.created}&pSkipped=${products.skipped}&pFailed=${products.failed}` +
|
||||
`&pPending=${products.pending}&pDiscontinued=${products.discontinued}` +
|
||||
|
||||
Reference in new issue
Block a user