wbs_rechner_express.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const express = require('express');
  2. const app = express();
  3. app.use(express.json()); //für das Parsing von JSON-Daten
  4. // Swagger Setup
  5. const swaggerJsdoc = require('swagger-jsdoc');
  6. const swaggerUi = require('swagger-ui-express');
  7. const options = {
  8. definition: {
  9. openapi: '3.0.0',
  10. info: {
  11. title: 'WBS RECHNER',
  12. version: '1.0.0',
  13. description: 'Eine API für die Berechnung des Gesamteinkommens und die Gewährung von WBS.',
  14. },
  15. },
  16. apis: ['wbs_rechner_express.js'],
  17. };
  18. const specs = swaggerJsdoc(options);
  19. app.use('/api', swaggerUi.serve, swaggerUi.setup(specs));
  20. /**
  21. * @swagger
  22. * /calculate:
  23. * post:
  24. * summary: Calculates the total income and determines if WBS is granted.
  25. * requestBody:
  26. * required: true
  27. * content:
  28. * application/json:
  29. * schema:
  30. * type: array
  31. * items:
  32. * type: object
  33. * properties:
  34. * incomes:
  35. * type: array
  36. * items:
  37. * type: object
  38. * properties:
  39. * amount:
  40. * type: number
  41. * frequency:
  42. * type: number
  43. * advertisingCosts:
  44. * type: number
  45. * responses:
  46. * 200:
  47. * description: Success
  48. * content:
  49. * application/json:
  50. * schema:
  51. * type: object
  52. * properties:
  53. * wbsGranted:
  54. * type: boolean
  55. */
  56. app.post('/calculate', (req, res) => {
  57. let totalIncome = 0;
  58. const persons = req.body;
  59. for (const person of persons) {
  60. for (const income of person.incomes) {
  61. let annualIncome = income.amount * income.frequency;
  62. annualIncome -= income.advertisingCosts;
  63. totalIncome += annualIncome;
  64. }
  65. }
  66. //Beispiel Berechnung entspricht nicht der WBS Berechnung
  67. let maxIncome = persons.length * 12000;
  68. let wbsGranted = maxIncome <= totalIncome;
  69. res.send({ wbsGranted: wbsGranted });
  70. });
  71. /**
  72. * @swagger
  73. * /frequencies:
  74. * get:
  75. * summary: Returns the available income frequencies.
  76. * responses:
  77. * 200:
  78. * description: Success
  79. * content:
  80. * application/json:
  81. * schema:
  82. * type: array
  83. * items:
  84. * type: object
  85. * properties:
  86. * value:
  87. * type: number
  88. * description:
  89. * type: string
  90. */
  91. app.get('/frequencies', (req, res) => {
  92. const frequencies = [
  93. { value: 365, description: 'täglich' },
  94. { value: 12, description: 'monatlich' },
  95. { value: 4, description: 'vierteljährlich' },
  96. { value: 1, description: 'jährlich' }
  97. //usw...
  98. ];
  99. res.send(frequencies);
  100. });
  101. /**
  102. * @swagger
  103. * /types:
  104. * get:
  105. * summary: Returns the available income types.
  106. * responses:
  107. * 200:
  108. * description: Success
  109. * content:
  110. * application/json:
  111. * schema:
  112. * type: array
  113. * items:
  114. * type: object
  115. * properties:
  116. * value:
  117. * type: string
  118. * description:
  119. * type: string
  120. */
  121. app.get('/types', (req, res) => {
  122. const types = [
  123. { value: 'work', description: 'als Arbeitnehmer' },
  124. { value: 'pensioner', description: 'als Rentner' },
  125. { value: 'social', description: 'Einnahme aus Sozialeistungen' },
  126. { value: 'entertains', description: 'Unterhalt' }
  127. //usw...
  128. ];
  129. res.send(types);
  130. });
  131. const port = process.env.PORT || 3000;
  132. app.listen(port, () => console.log(`Server läuft auf Port ${port}...`));