diff --git a/src/api/structures/localisations/localisations.middlewares.js b/src/api/structures/localisations/localisations.middlewares.js index a5eacad1..01f4209d 100644 --- a/src/api/structures/localisations/localisations.middlewares.js +++ b/src/api/structures/localisations/localisations.middlewares.js @@ -6,13 +6,24 @@ import { structureLocalisationsRepository } from '../../commons/repositories'; export const setGeoJSON = (req, res, next) => { const { coordinates, ...rest } = req.body; if (coordinates?.lat && coordinates?.lng) { - const geometry = { type: 'Point', coordinates: [coordinates.lng, coordinates.lat] }; + const geometry = { + type: 'Point', + coordinates: [coordinates.lng, coordinates.lat], + }; req.body = { geometry, ...rest }; } else { req.body = { ...rest }; } - if ((coordinates?.lat && !coordinates?.lng) || (coordinates?.lng && !coordinates?.lat)) { - throw new BadRequestError('Validation error', [{ path: '.body.coordinates', message: 'lat and lng must be set together' }]); + if ( + (coordinates?.lat && !coordinates?.lng) + || (coordinates?.lng && !coordinates?.lat) + ) { + throw new BadRequestError('Validation error', [ + { + path: '.body.coordinates', + message: 'lat and lng must be set together', + }, + ]); } return next(); }; @@ -20,19 +31,35 @@ export async function validatePhoneNumberAndIso3(req, res, next) { const phoneRegex = /^(\+(?:\d{1,3}))?[-. ]?\(?(\d{1,4})\)?[-. ]?(\d{1,4})[-. ]?(\d{1,4})[-. ]?(\d{1,9})$/; const { id, resourceId } = req.params; const { country, iso3, phonenumber } = req.body; - const nextCountry = country || await structureLocalisationsRepository.get(resourceId, id, { useQuery: readQuery }).country; - if (phonenumber && nextCountry === 'France' && !phonenumber.match(phoneRegex)) { - throw new BadRequestError('Validation error', [{ - path: '.body.phonenumber', - message: `Phone number from France should match pattern ${phoneRegex}`, - }]); + const nextCountry = country + || (await structureLocalisationsRepository.get(resourceId, id, { + useQuery: readQuery, + }).country); + if ( + phonenumber + && nextCountry === 'France' + && !phonenumber.match(phoneRegex) + ) { + throw new BadRequestError('Validation error', [ + { + path: '.body.phonenumber', + message: `Phone number from France should match pattern ${phoneRegex}`, + }, + ]); } if (iso3) { - if (!iso3.toString().toUpperCase().match(/^[A-Z]{3}$/)) { - throw new BadRequestError('Validation error', [{ - path: '.body.iso3', - message: 'iso3 for structure should be 3 letters in uppercase', - }]); + if ( + !iso3 + .toString() + .toUpperCase() + .match(/^[A-Z]{3}$/) + ) { + throw new BadRequestError('Validation error', [ + { + path: '.body.iso3', + message: 'iso3 for structure should be 3 letters in uppercase', + }, + ]); } } return next(); @@ -40,17 +67,27 @@ export async function validatePhoneNumberAndIso3(req, res, next) { export const listLocalisations = async (req, res, next) => { const { resourceId } = req.params; - const localisations = await structureLocalisationsRepository.find({ resourceId, useQuery: readQuery }); + const localisations = await structureLocalisationsRepository.find({ + resourceId, + useQuery: readQuery, + }); const queries = localisations?.data?.map((el) => { if (el?.coordinates?.lng && el?.coordinates?.lat) { - return db.collection('geographicalcategories') + return db + .collection('geographicalcategories') .find({ geometry: { $geoIntersects: { - $geometry: { type: 'Point', coordinates: [el?.coordinates?.lng, el?.coordinates?.lat] }, + $geometry: { + type: 'Point', + coordinates: [el?.coordinates?.lng, el?.coordinates?.lat], + }, }, }, - }) + + }, { projection: { + geometry: 0, + } }) .toArray(); } return [];