backend: Ajout de calculateAge

This commit is contained in:
Divlo 2020-03-19 21:46:54 +01:00
parent e281b3dbec
commit c324676e89
4 changed files with 52 additions and 1 deletions

View File

@ -5,6 +5,7 @@ const { convertTemperatureOutput } = require('./ma
const { armstrongNumberOutput } = require('./main/armstrongNumber'); const { armstrongNumberOutput } = require('./main/armstrongNumber');
const { weatherRequestOutput } = require('./main/weatherRequest'); const { weatherRequestOutput } = require('./main/weatherRequest');
const { convertCurrencyOutput } = require('./main/convertCurrency'); const { convertCurrencyOutput } = require('./main/convertCurrency');
const { calculateAgeOutput } = require('./main/calculateAge');
const functionObject = { const functionObject = {
randomNumber : randomNumberOutput, randomNumber : randomNumberOutput,
@ -14,7 +15,8 @@ const functionObject = {
convertTemperature : convertTemperatureOutput, convertTemperature : convertTemperatureOutput,
armstrongNumber : armstrongNumberOutput, armstrongNumber : armstrongNumberOutput,
weatherRequest : weatherRequestOutput, weatherRequest : weatherRequestOutput,
convertCurrency : convertCurrencyOutput convertCurrency : convertCurrencyOutput,
calculateAge : calculateAgeOutput
}; };
// Choisi la fonction à exécuter // Choisi la fonction à exécuter

View File

@ -0,0 +1,43 @@
const sendResponse = require('../../utils/sendResponse');
const moment = require('moment');
const { requiredFields } = require('../../config/errors');
function calculateAge(currentDate, { birthDateDay, birthDateMonth, birthDateYear }) {
const day = currentDate.getDate();
const month = currentDate.getMonth();
const currentDateMoment = moment([currentDate.getFullYear(), month, day]);
const birthDateMoment = moment([birthDateYear, birthDateMonth - 1, birthDateDay]);
// Calcule l'âge - Moment.js
const ageYears = currentDateMoment.diff(birthDateMoment, 'year');
birthDateMoment.add(ageYears, 'years');
const ageMonths = currentDateMoment.diff(birthDateMoment, 'months');
birthDateMoment.add(ageMonths, 'months');
const ageDays = currentDateMoment.diff(birthDateMoment, 'days');
const isBirthday = (birthDateDay === day && birthDateMonth === (month + 1));
return { ageYears, ageMonths, ageDays, isBirthday };
}
/* OUTPUTS */
exports.calculateAgeOutput = (res, argsObject) => {
let { birthDateDay, birthDateMonth, birthDateYear } = argsObject;
birthDateDay = parseInt(birthDateDay);
birthDateMonth = parseInt(birthDateMonth);
birthDateYear = parseInt(birthDateYear);
// S'il n'y a pas les champs obligatoire
if (!(birthDateDay && birthDateMonth && birthDateYear)) {
return sendResponse(res, requiredFields);
}
// Si ce n'est pas une date valide
const currentDate = new Date();
const birthDate = new Date(birthDateYear, birthDateMonth - 1, birthDateDay);
if (!(currentDate > birthDate)) {
return sendResponse(res, { result: "Veuillez rentré une date valide...", httpStatus: 400 });
}
const result = calculateAge(currentDate, { birthDateYear, birthDateMonth, birthDateDay });
return sendResponse(res, { result }, true);
}

View File

@ -879,6 +879,11 @@
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
"dev": true "dev": true
}, },
"moment": {
"version": "2.24.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
},
"morgan": { "morgan": {
"version": "1.9.1", "version": "1.9.1",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz",

View File

@ -14,6 +14,7 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.17.1", "express": "^4.17.1",
"helmet": "^3.21.3", "helmet": "^3.21.3",
"moment": "^2.24.0",
"smart-request-balancer": "^2.1.1" "smart-request-balancer": "^2.1.1"
}, },
"devDependencies": { "devDependencies": {