Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use wikidata logo #1905

Closed
wants to merge 14 commits into from
6 changes: 4 additions & 2 deletions src/app/api/certificat/[idAdresse]/components/certificat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ interface CertificatNumerotationProps {
telephone?: string
email?: string
}
logoUrl: string
}

const CertificatNumerotation: React.FC<CertificatNumerotationProps> = ({ data, qrCodeDataURL, mairie }) => {
const CertificatNumerotation: React.FC<CertificatNumerotationProps> = ({ data, qrCodeDataURL, mairie, logoUrl }) => {
const nomCommune = data.full_address.districtDefaultLabel
const libelleVoie = data.full_address.commonToponymDefaultLabel
const numero = data.full_address.number
const suffix = data.full_address.suffix || ''
const { cog } = data.full_address
const parcelles = data.cadastre_ids.map(id => id.replace(/(\d+)([A-Z])/, '$1 $2'))

const logoUrl = `public/logos/certificat/${cog}.png`
// const logoUrl = `public/logos/certificat/${cog}.png`
const logoAdresse = `public/logos/certificat/adresse-logo.png`

const dateObj = new Date(data.createdAt)
Expand All @@ -66,6 +67,7 @@ const CertificatNumerotation: React.FC<CertificatNumerotationProps> = ({ data, q
<Page size="A4" style={stylesDSFR.page}>
<View style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between' }}>
<Image src={logoAdresse} style={stylesDSFR.logoAdresse} />
<Image src={logoUrl} style={stylesDSFR.logoBloc} />
</View>
<Text> {'\n'}</Text>
{/* Conteneur pour le logo de la mairie et les informations */}
Expand Down
2 changes: 0 additions & 2 deletions src/app/api/certificat/[idAdresse]/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ const isDistrictCertifiable = async (banIdDistrict: string | null): Promise<bool
if (!banIdDistrict) {
return false
}

const rawResponse = await getDistrict(banIdDistrict)
const district = rawResponse.response

if (!district) {
return false
}
Expand Down
6 changes: 6 additions & 0 deletions src/app/api/certificat/pdf/[idCertificat]/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ReactPDF from '@react-pdf/renderer'

import { getMairie } from '@/lib/api-etablissement-public'
import { CertificatNumerotation } from '@/app/api/certificat/[idAdresse]/components/certificat'
import { getCommuneLogo } from '@/lib/api-wikidata'

const NEXT_PUBLIC_ADRESSE_URL = process.env.NEXT_PUBLIC_ADRESSE_URL
const NEXT_PUBLIC_API_BAN_URL = process.env.NEXT_PUBLIC_API_BAN_URL
Expand All @@ -26,13 +27,18 @@ export async function GET(request: NextRequest, { params }: { params: { idCertif
const qrCodeDataURL = await QRCode.toDataURL(certificatUrl)

const mairie = await getMairie(data.full_address.cog)

const mairieData = mairie || { telephone: undefined, email: undefined }

const logoUrl = await getCommuneLogo(data.full_address.cog) || ' '

const pdfStream = await ReactPDF.renderToStream(
<CertificatNumerotation
data={data}
qrCodeDataURL={qrCodeDataURL}
mairie={mairieData}
logoUrl={logoUrl}

/>
)

Expand Down
29 changes: 29 additions & 0 deletions src/lib/api-wikidata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,32 @@ export const getCommuneFlag = async (codeCommune: string): Promise<string | unde

return flagUrl
}

const communeLogoQuery = (codeCommune: string) => `
SELECT ?city
?inseeCode
?cityLabel
?logo
WHERE
{
?city wdt:P374 "${codeCommune}".
?city wdt:P374 ?inseeCode.
OPTIONAL { ?city wdt:P154 ?logo. } # Récupère le logo de la ville si disponible
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?inseeCode
`

export const getCommuneLogo = async (codeCommune: string): Promise<string | undefined> => {
const url = `${BASE_URL}/sparql?query=${encodeURIComponent(communeLogoQuery(codeCommune))}`
// Wikidata blocks requests without a user agent specific enough
const response = await fetch(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
},
})
const responseBody = await response.text()
const flagUrl = responseBody.match(/http.*\.png/)?.[0]

return flagUrl
}