| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- const express = require('express');
- const app = express();
- app.use(express.json()); //für das Parsing von JSON-Daten
- // 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: 'Eine API für die Berechnung des Gesamteinkommens und die Gewährung von WBS.',
- },
- },
- apis: ['wbs_rechner_express.js'],
- };
- const specs = swaggerJsdoc(options);
- app.use('/api', swaggerUi.serve, swaggerUi.setup(specs));
- /**
- * @swagger
- * /calculate:
- * post:
- * summary: Calculates the total income and determines if WBS is granted.
- * 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
- * responses:
- * 200:
- * description: Success
- * content:
- * application/json:
- * schema:
- * type: object
- * properties:
- * wbsGranted:
- * type: boolean
- * requestBody:
- * description: Example request body
- * required: true
- * content:
- * application/json:
- * schema:
- * type: object
- * 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
- */
- app.post('/calculate', (req, res) => {
- 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;
- }
- }
- //Beispiel Berechnung entspricht nicht der WBS Berechnung
- let maxIncome = persons.length * 12000;
- let wbsGranted = maxIncome <= totalIncome;
- res.send({ wbsGranted: wbsGranted });
- });
- /**
- * @swagger
- * /frequencies:
- * get:
- * summary: Returns the available income frequencies.
- * responses:
- * 200:
- * description: Success
- * content:
- * application/json:
- * schema:
- * type: array
- * items:
- * type: object
- * properties:
- * value:
- * type: number
- * description:
- * type: string
- */
- app.get('/frequencies', (req, res) => {
- const frequencies = [
- { value: 365, description: 'täglich' },
- { value: 12, description: 'monatlich' },
- { value: 4, description: 'vierteljährlich' },
- { value: 1, description: 'jährlich' }
- //usw...
- ];
- res.send(frequencies);
- });
- /**
- * @swagger
- * /types:
- * get:
- * summary: Returns the available income types.
- * responses:
- * 200:
- * description: Success
- * content:
- * application/json:
- * schema:
- * type: array
- * items:
- * type: object
- * properties:
- * value:
- * type: string
- * description:
- * type: string
- */
- app.get('/types', (req, res) => {
- const types = [
- { value: 'work', description: 'als Arbeitnehmer' },
- { value: 'pensioner', description: 'als Rentner' },
- { value: 'social', description: 'Einnahme aus Sozialeistungen' },
- { value: 'entertains', description: 'Unterhalt' }
- //usw...
- ];
- res.send(types);
- });
- const port = process.env.PORT || 3000;
- app.listen(port, () => console.log(`Server läuft auf Port ${port}...`));
|