Aller au contenu principal

POST /api/menages

Crée un nouveau ménage avec son chef de ménage et son logement.

Endpoint​

POST /api/menages

Headers​

HeaderValeurRequis
AuthorizationBearer {token}Oui
Content-Typeapplication/jsonOui

Corps de la requête​

{
"tailleMenage": 5,
"region": "/api/regions/1",
"secteur": "/api/secteurs/1",
"localite": "/api/localites/1",
"latitude": "12.2833",
"longitude": "-15.2167",
"altitude": "12.5",
"precisionGps": "5.0",
"nbEnfants04": 1,
"nbEnfants514": 2,
"nbAdultes1564": 2,
"nbPersonnes65Plus": 0,
"nbHandicapes": 0,
"nbActifsOccupes": 2,
"chefMenage": {
"nomComplet": "Mamadou Diallo",
"sexe": "M",
"age": 45,
"niveauEducation": "secondaire",
"occupationPrincipale": "commerce",
"statutMatrimonial": "marie",
"telephone": "771234567",
"numeroMobileMoney": "771234567"
},
"logement": {
"typeLogement": "maison",
"statutOccupation": "proprietaire",
"nbPieces": 4,
"materiauMur": "ciment",
"materiauToit": "tole",
"materiauSol": "ciment",
"sourceEau": "robinet",
"typeToilette": "wc_chasse",
"sourceEclairage": "electricite",
"combustibleCuisine": "gaz"
}
}

Champs du ménage​

ChampTypeRequisDescription
tailleMenageintegerOuiNombre de personnes (> 0)
regionIRIOuiRéférence à la région (ex: Oio)
secteurIRIOuiRéférence au secteur (ex: Farim)
localiteIRIOuiRéférence à la localité/tabanca
latitudestringNonLatitude GPS (decimal)
longitudestringNonLongitude GPS (decimal)
nbEnfants04integerNonEnfants 0-4 ans
nbEnfants514integerNonEnfants 5-14 ans
nbAdultes1564integerNonAdultes 15-64 ans
nbPersonnes65PlusintegerNonPersonnes 65+ ans

Champs du chef de ménage​

ChampTypeRequisDescription
nomCompletstringOuiNom et prénom
sexestringNon"M" ou "F"
ageintegerNonÂge
niveauEducationstringNonNiveau d'études
occupationPrincipalestringNonActivité
statutMatrimonialstringNonSituation familiale
telephonestringNonNuméro de téléphone
numeroMobileMoneystringNonNuméro Mobile Money

Champs du logement​

ChampTypeRequisDescription
typeLogementstringNonType de logement
statutOccupationstringNonStatut d'occupation
nbPiecesintegerNonNombre de pièces
materiauMurstringNonMatériau des murs
sourceEaustringNonSource d'eau

Réponse succès​

Code: 201 Created

{
"@context": "/api/contexts/Menage",
"@id": "/api/menages/123",
"@type": "Menage",
"id": 123,
"codeMenage": "MEN-2024-00123",
"tailleMenage": 5,
"statut": "en_attente",
"chefMenage": {
"@id": "/api/chef_menages/123",
"nomComplet": "Mamadou Diallo"
},
"createdAt": "2024-01-15T10:30:00+00:00"
}

Réponses erreur​

422 Unprocessable Entity - Erreur de validation​

{
"@context": "/api/contexts/ConstraintViolationList",
"@type": "ConstraintViolationList",
"hydra:title": "An error occurred",
"hydra:description": "tailleMenage: This value should be positive.",
"violations": [
{
"propertyPath": "tailleMenage",
"message": "This value should be positive."
},
{
"propertyPath": "chefMenage.nomComplet",
"message": "This value should not be blank."
}
]
}

400 Bad Request​

{
"@context": "/api/contexts/Error",
"@type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "Syntax error"
}

Exemples​

cURL​

curl -X POST https://sig.example.com/api/menages \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{
"tailleMenage": 5,
"region": "/api/regions/1",
"secteur": "/api/secteurs/1",
"localite": "/api/localites/1",
"chefMenage": {
"nomComplet": "Mamadou Diallo",
"sexe": "M",
"age": 45,
"telephone": "+245955123456"
},
"logement": {
"typeLogement": "maison",
"nbPieces": 4
}
}'

JavaScript​

const createMenage = async (data) => {
const response = await fetch('https://sig.example.com/api/menages', {
method: 'POST',
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});

if (response.status === 422) {
const errors = await response.json();
throw new Error(errors['hydra:description']);
}

if (!response.ok) {
throw new Error('Erreur lors de la création');
}

return response.json();
};

// Utilisation
const newMenage = await createMenage({
tailleMenage: 5,
region: '/api/regions/1',
secteur: '/api/secteurs/1',
localite: '/api/localites/1',
chefMenage: {
nomComplet: 'Mamadou Diallo',
sexe: 'M',
telephone: '+245955123456'
},
logement: {
typeLogement: 'maison',
nbPieces: 4
}
});

console.log(`Créé: ${newMenage.codeMenage}`);

Python​

import requests

def create_menage(token, data):
response = requests.post(
'https://sig.example.com/api/menages',
headers={
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
},
json=data
)

if response.status_code == 422:
errors = response.json()
raise Exception(errors['hydra:description'])

response.raise_for_status()
return response.json()

# Utilisation
menage = create_menage(token, {
'tailleMenage': 5,
'region': '/api/regions/1',
'secteur': '/api/secteurs/1',
'localite': '/api/localites/1',
'chefMenage': {
'nomComplet': 'Mamadou Diallo',
'sexe': 'M'
},
'logement': {
'typeLogement': 'maison'
}
})

print(f"Créé: {menage['codeMenage']}")

Notes​

  • Le codeMenage est généré automatiquement
  • Le statut initial est en_attente (nécessite validation)
  • Les champs GPS acceptent des chaînes (format decimal)
  • Les références (region, secteur, localite) utilisent des IRI
  • Le chef de ménage et le logement sont créés automatiquement