| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- const express = require('express');
- const app = express();
- 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));
- /**
- * @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: boolean
- * 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.persons;
- 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 wbsGranted = totalIncome <= maxIncome;
- res.send({ wbsGranted: wbsGranted });
- });
- /**
- * @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 || 3000;
- app.listen(port, () => console.log(`Server läuft auf Port ${port}...`));
|