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 * 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: Success * content: * application/json: * schema: * type: object * properties: * wbsGranted: * type: boolean */ 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}...`));