wbs_rechner_express.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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: object
  31. * properties:
  32. * persons:
  33. * type: array
  34. * items:
  35. * type: object
  36. * properties:
  37. * incomes:
  38. * type: array
  39. * items:
  40. * type: object
  41. * properties:
  42. * amount:
  43. * type: number
  44. * frequency:
  45. * type: number
  46. * advertisingCosts:
  47. * type: number
  48. * example:
  49. * persons:
  50. * - incomes:
  51. * - amount: 500
  52. * frequency: 12
  53. * advertisingCosts: 500
  54. * - amount: 200
  55. * frequency: 1
  56. * advertisingCosts: 0
  57. * - incomes:
  58. * - amount: 1200
  59. * frequency: 12
  60. * advertisingCosts: 200
  61. * - amount: 2000
  62. * frequency: 1
  63. * advertisingCosts: 0
  64. * responses:
  65. * 200:
  66. * description: Success
  67. * content:
  68. * application/json:
  69. * schema:
  70. * type: object
  71. * properties:
  72. * wbsGranted:
  73. * type: boolean
  74. */
  75. app.post('/calculate', (req, res) => {
  76. let totalIncome = 0;
  77. const persons = req.body.persons;
  78. for (const person of persons) {
  79. for (const income of person.incomes) {
  80. let annualIncome = income.amount * income.frequency;
  81. annualIncome -= income.advertisingCosts;
  82. totalIncome += annualIncome;
  83. }
  84. }
  85. //Beispiel Berechnung entspricht nicht der WBS Berechnung
  86. let maxIncome = persons.length * 12000;
  87. let wbsGranted = maxIncome <= totalIncome;
  88. res.send({ wbsGranted: wbsGranted });
  89. });
  90. /**
  91. * @swagger
  92. * /frequencies:
  93. * get:
  94. * summary: Returns the available income frequencies.
  95. * responses:
  96. * 200:
  97. * description: Success
  98. * content:
  99. * application/json:
  100. * schema:
  101. * type: array
  102. * items:
  103. * type: object
  104. * properties:
  105. * value:
  106. * type: number
  107. * description:
  108. * type: string
  109. */
  110. app.get('/frequencies', (req, res) => {
  111. const frequencies = [
  112. { value: 365, description: 'täglich' },
  113. { value: 12, description: 'monatlich' },
  114. { value: 4, description: 'vierteljährlich' },
  115. { value: 1, description: 'jährlich' }
  116. //usw...
  117. ];
  118. res.send(frequencies);
  119. });
  120. /**
  121. * @swagger
  122. * /types:
  123. * get:
  124. * summary: Returns the available income types.
  125. * responses:
  126. * 200:
  127. * description: Success
  128. * content:
  129. * application/json:
  130. * schema:
  131. * type: array
  132. * items:
  133. * type: object
  134. * properties:
  135. * value:
  136. * type: string
  137. * description:
  138. * type: string
  139. */
  140. app.get('/types', (req, res) => {
  141. const types = [
  142. { value: 'work', description: 'als Arbeitnehmer' },
  143. { value: 'pensioner', description: 'als Rentner' },
  144. { value: 'social', description: 'Einnahme aus Sozialeistungen' },
  145. { value: 'entertains', description: 'Unterhalt' }
  146. //usw...
  147. ];
  148. res.send(types);
  149. });
  150. const port = process.env.PORT || 3000;
  151. app.listen(port, () => console.log(`Server läuft auf Port ${port}...`));