Skip to content

Commit

Permalink
Merge branch 'main' into jugurtha/fix_widget_error
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouhadoun authored Nov 28, 2024
2 parents 70b9b67 + e7f1203 commit c75c9f4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
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 @@ -30,17 +30,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 @@ -67,6 +68,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 @@ -13,10 +13,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 @@ -5,6 +5,7 @@ import ReactPDF from '@react-pdf/renderer'
import { getMairie } from '@/lib/api-etablissement-public'
import { CertificatNumerotation } from '@/app/api/certificat/[idAdresse]/components/certificat'
import { env } from 'next-runtime-env'
import { getCommuneLogo } from '@/lib/api-wikidata'

const NEXT_PUBLIC_ADRESSE_URL = env('NEXT_PUBLIC_ADRESSE_URL')
const NEXT_PUBLIC_API_BAN_URL = env('NEXT_PUBLIC_API_BAN_URL')
Expand All @@ -27,13 +28,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
}

0 comments on commit c75c9f4

Please sign in to comment.