diff --git a/backend/assets/functions/functionObject.js b/backend/assets/functions/functionObject.js index d14d987..fd7d290 100644 --- a/backend/assets/functions/functionObject.js +++ b/backend/assets/functions/functionObject.js @@ -5,6 +5,7 @@ const { convertTemperatureOutput } = require('./ma const { armstrongNumberOutput } = require('./main/armstrongNumber'); const { weatherRequestOutput } = require('./main/weatherRequest'); const { convertCurrencyOutput } = require('./main/convertCurrency'); +const { calculateAgeOutput } = require('./main/calculateAge'); const functionObject = { randomNumber : randomNumberOutput, @@ -14,7 +15,8 @@ const functionObject = { convertTemperature : convertTemperatureOutput, armstrongNumber : armstrongNumberOutput, weatherRequest : weatherRequestOutput, - convertCurrency : convertCurrencyOutput + convertCurrency : convertCurrencyOutput, + calculateAge : calculateAgeOutput }; // Choisi la fonction à exécuter diff --git a/backend/assets/functions/main/calculateAge.js b/backend/assets/functions/main/calculateAge.js new file mode 100644 index 0000000..af1a729 --- /dev/null +++ b/backend/assets/functions/main/calculateAge.js @@ -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); +} \ No newline at end of file diff --git a/backend/package-lock.json b/backend/package-lock.json index c94caa5..ebed359 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -879,6 +879,11 @@ "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", "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": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", diff --git a/backend/package.json b/backend/package.json index ad19a2b..8317cec 100644 --- a/backend/package.json +++ b/backend/package.json @@ -14,6 +14,7 @@ "cors": "^2.8.5", "express": "^4.17.1", "helmet": "^3.21.3", + "moment": "^2.24.0", "smart-request-balancer": "^2.1.1" }, "devDependencies": {