const express = require('express'); const app = express(); const cors = require('cors'); app.use(express.json()); // for parsing JSON data // Swagger Setup const swaggerJsdoc = require('swagger-jsdoc'); const swaggerUi = require('swagger-ui-express'); const options = { definition: { openapi: '3.0.0', info: { title: 'WBS RECHNER', version: '1.0.0', description: 'An API for calculating total income and granting WBS.', }, }, apis: ['wbs_rechner_express.js'], }; const specs = swaggerJsdoc(options); app.use('/api', swaggerUi.serve, swaggerUi.setup(specs)); app.use(cors()) /** * @swagger * /calculate: * post: * summary: Calculates whether a WBS can be granted based on the provided incomes * requestBody: * required: true * content: * application/json: * schema: * type: object * properties: * persons: * type: array * items: * type: object * properties: * incomes: * type: array * items: * type: object * properties: * amount: * type: number * frequency: * type: number * advertisingCosts: * type: number * example: * persons: * - incomes: * - amount: 500 * frequency: 12 * advertisingCosts: 500 * - amount: 200 * frequency: 1 * advertisingCosts: 0 * - incomes: * - amount: 1200 * frequency: 12 * advertisingCosts: 200 * - amount: 2000 * frequency: 1 * advertisingCosts: 0 * responses: * 200: * description: WBS calculation result * content: * application/json: * schema: * type: object * properties: * wbsGranted: * type: string * yearlyAmount: * type: number * yearlyAmountMax: * type: number * difference: * type: number * percent: * type: number * 400: * description: Bad Request * 500: * description: Internal Server Error */ app.post('/calculate', (req, res) => { /** * Calculates whether a WBS can be granted based on the provided incomes. * @param {object[]} persons - An array of persons. * @param {object[]} persons.incomes - An array of incomes for each person. * @param {number} persons.incomes[].amount - The amount of income. * @param {number} persons.incomes[].frequency - The frequency of income. * @param {number} persons.incomes[].advertisingCosts - The advertising costs for the income. * @returns {object} - The result object containing the wbsGranted property. */ let totalIncome = 0; const persons = req.body; console.log(req.body); for (const person of persons) { for (const income of person.incomes) { let annualIncome = income.amount * income.frequency; annualIncome -= income.advertisingCosts; totalIncome += annualIncome; } } // Example calculation does not correspond to the WBS calculation let maxIncome = persons.length * 12000; let difference = totalIncome - maxIncome; let percent = 100 * totalIncome / maxIncome; let wbsGranted = "Kein WBS" if(percent <= 100) { wbsGranted = "WBS 100"; } else if (percent <= 140) { wbsGranted = "WBS 140" } res.send({ wbsGranted: wbsGranted, yearlyAmount: totalIncome, yearlyAmountMax: maxIncome, difference: difference, percent: percent}); }); /** * @swagger * /frequencies: * get: * summary: Retrieves all possible frequencies of income * responses: * 200: * description: An array of frequency objects * content: * application/json: * schema: * type: array * items: * type: object * properties: * value: * type: number * description: * type: string * example: * - value: 365 * description: "täglich" * - value: 12 * description: "monatlich" * - value: 4 * description: "vierteljährlich" * - value: 1 * description: "jährlich" * 400: * description: Bad Request * 500: * description: Internal Server Error */ app.get('/frequencies', (req, res) => { /** * Retrieves all possible frequencies of income. * @returns {object[]} - An array of frequency objects. * @property {number} value - The value of the frequency. * @property {string} description - The description of the frequency. */ const frequencies = [ { value: 365, description: 'täglich' }, { value: 12, description: 'monatlich' }, { value: 4, description: 'vierteljährlich' }, { value: 1, description: 'jährlich' } // etc... ]; res.send(frequencies); }); /** * @swagger * /types: * get: * summary: Retrieves all types of income * responses: * 200: * description: An array of income type objects * content: * application/json: * schema: * type: array * items: * type: object * properties: * value: * type: string * description: * type: string * example: * - value: 'work' * description: 'als Arbeitnehmer' * - value: 'pensioner' * description: 'als Rentner' * - value: 'social' * description: 'Einnahme aus Sozialeistungen' * - value: 'entertains' * description: 'Unterhalt' * 400: * description: Bad Request * 500: * description: Internal Server Error */ app.get('/types', (req, res) => { /** * Retrieves all types of income. * @returns {object[]} - An array of income type objects. * @property {string} value - The value of the income type. * @property {string} description - The description of the income type. */ const types = [ { value: 'work', description: 'als Arbeitnehmer' }, { value: 'pensioner', description: 'als Rentner' }, { value: 'social', description: 'Einnahme aus Sozialeistungen' }, { value: 'entertains', description: 'Unterhalt' } // etc... ]; res.send(types); }); const port = process.env.PORT || 3100; app.listen(port, () => console.log(`Server läuft auf Port ${port}...`));