wbs_rechner_express.js 4.8 KB

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