Skip to content

Commit

Permalink
fix(localisation): add projection to listcategories
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed Sep 6, 2024
1 parent 389f2fa commit 847df53
Showing 1 changed file with 55 additions and 18 deletions.
73 changes: 55 additions & 18 deletions src/api/structures/localisations/localisations.middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,88 @@ 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();
};
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();
}

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 [];
Expand Down

0 comments on commit 847df53

Please sign in to comment.