@@ -7,6 +7,8 @@ import type { DB } from "~/server/db";
77import { campaignDeadlines , companies , gipMdsData } from "~/server/db/schema" ;
88import { fetchCompanyBySiren } from "./weez" ;
99
10+ const ISO_DATE_RE = / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / ;
11+
1012/**
1113 * CSV metadata extracted from the first 2 lines of a GIP MDS file.
1214 * Line 1: destinataire;projet;horodatage;date_debut;date_fin;nb_lignes
@@ -233,30 +235,28 @@ export async function importGipCsvToDb(
233235 } ) ) ,
234236 ) ;
235237
236- // Record the SUIT `horodatage` as the GIP publication date on the
237- // matching campaign-deadlines row. Admins cannot edit this field — the
238- // import is the single source of truth.
239- if ( / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( metadata . publicationDate ) ) {
240- await tx
241- . insert ( campaignDeadlines )
242- . values ( {
243- year,
244- gipPublicationDate : metadata . publicationDate ,
245- // Non-null columns need a safe placeholder: the import-time
246- // write only touches gipPublicationDate on an existing row,
247- // so these fallbacks only matter when no campaign has been
248- // configured yet.
249- decl1ModificationDeadline : `${ year } -06-01` ,
250- decl1JustificationDeadline : `${ year } -06-01` ,
251- decl1JointEvaluationDeadline : `${ year } -08-01` ,
252- decl2ModificationDeadline : `${ year } -12-01` ,
253- decl2JustificationDeadline : `${ year } -12-01` ,
254- decl2JointEvaluationDeadline : `${ year + 1 } -02-01` ,
255- } )
256- . onConflictDoUpdate ( {
257- target : campaignDeadlines . year ,
258- set : { gipPublicationDate : metadata . publicationDate } ,
259- } ) ;
238+ // Record the SUIT `horodatage` as the GIP publication date — but only
239+ // on an EXISTING `campaign_deadline` row. We never synthesise a row
240+ // with placeholder deadlines here: admins must configure the campaign
241+ // first (decl1/decl2 deadlines are NOT NULL and those values must be
242+ // decided by a human, not made up by the import).
243+ if ( ! ISO_DATE_RE . test ( metadata . publicationDate ) ) {
244+ console . warn (
245+ `[gip-mds/import] Ignoring invalid horodatage "${ metadata . publicationDate } " for year ${ year } ` ,
246+ ) ;
247+ return ;
248+ }
249+
250+ const updated = await tx
251+ . update ( campaignDeadlines )
252+ . set ( { gipPublicationDate : metadata . publicationDate } )
253+ . where ( eq ( campaignDeadlines . year , year ) )
254+ . returning ( { year : campaignDeadlines . year } ) ;
255+
256+ if ( updated . length === 0 ) {
257+ console . warn (
258+ `[gip-mds/import] No campaign_deadline row for year ${ year } — gipPublicationDate not stored. Ask an admin to configure deadlines first.` ,
259+ ) ;
260260 }
261261 } ) ;
262262
0 commit comments