diff --git a/services/targo-hub/lib/legacy-reports.js b/services/targo-hub/lib/legacy-reports.js index 7c817ac..b6f0af2 100644 --- a/services/targo-hub/lib/legacy-reports.js +++ b/services/targo-hub/lib/legacy-reports.js @@ -117,9 +117,15 @@ function buildQuery (url) { const cats = includeAddons ? [...CAT_INTERNET_CORE, ...CAT_INTERNET_ADDONS] : CAT_INTERNET_CORE const catPlaceholders = cats.map(() => '?').join(',') + // A non-empty company name means a business even when the legacy `commercial` + // flag wasn't set — 98 such accounts (Ferme X Inc., Assurances …, Ville de + // Farnham, Les Jardins Sorel) leaked into "residential". So the rule is: + // commercial = (commercial flag set) OR (company name present). Residential + // therefore excludes any account carrying a company name. + const HAS_COMPANY = "(a.company IS NOT NULL AND TRIM(a.company) <> '')" let commercialClause = '' - if (segment === 'residential') commercialClause = 'AND a.commercial = 0' - else if (segment === 'commercial') commercialClause = 'AND a.commercial = 1' + if (segment === 'residential') commercialClause = `AND a.commercial = 0 AND NOT ${HAS_COMPANY}` + else if (segment === 'commercial') commercialClause = `AND (a.commercial = 1 OR ${HAS_COMPANY})` const sql = ` SELECT