wbs_rechner_express.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. const express = require('express');
  2. const app = express();
  3. app.use(express.json()); // for parsing JSON data
  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: 'An API for calculating total income and granting 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 whether a WBS can be granted based on the provided incomes
  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: WBS calculation result
  67. * content:
  68. * application/json:
  69. * schema:
  70. * type: object
  71. * properties:
  72. * wbsGranted:
  73. * type: boolean
  74. * 400:
  75. * description: Bad Request
  76. * 500:
  77. * description: Internal Server Error
  78. */
  79. app.post('/calculate', (req, res) => {
  80. /**
  81. * Calculates whether a WBS can be granted based on the provided incomes.
  82. * @param {object[]} persons - An array of persons.
  83. * @param {object[]} persons.incomes - An array of incomes for each person.
  84. * @param {number} persons.incomes[].amount - The amount of income.
  85. * @param {number} persons.incomes[].frequency - The frequency of income.
  86. * @param {number} persons.incomes[].advertisingCosts - The advertising costs for the income.
  87. * @returns {object} - The result object containing the wbsGranted property.
  88. */
  89. let totalIncome = 0;
  90. const persons = req.body.persons;
  91. for (const person of persons) {
  92. for (const income of person.incomes) {
  93. let annualIncome = income.amount * income.frequency;
  94. annualIncome -= income.advertisingCosts;
  95. totalIncome += annualIncome;
  96. }
  97. }
  98. // Example calculation does not correspond to the WBS calculation
  99. let maxIncome = persons.length * 12000;
  100. let wbsGranted = totalIncome <= maxIncome;
  101. res.send({ wbsGranted: wbsGranted });
  102. });
  103. /**
  104. * @swagger
  105. * /frequencies:
  106. * get:
  107. * summary: Retrieves all possible frequencies of income
  108. * responses:
  109. * 200:
  110. * description: An array of frequency objects
  111. * content:
  112. * application/json:
  113. * schema:
  114. * type: array
  115. * items:
  116. * type: object
  117. * properties:
  118. * value:
  119. * type: number
  120. * description:
  121. * type: string
  122. * example:
  123. * - value: 365
  124. * description: "täglich"
  125. * - value: 12
  126. * description: "monatlich"
  127. * - value: 4
  128. * description: "vierteljährlich"
  129. * - value: 1
  130. * description: "jährlich"
  131. * 400:
  132. * description: Bad Request
  133. * 500:
  134. * description: Internal Server Error
  135. */
  136. app.get('/frequencies', (req, res) => {
  137. /**
  138. * Retrieves all possible frequencies of income.
  139. * @returns {object[]} - An array of frequency objects.
  140. * @property {number} value - The value of the frequency.
  141. * @property {string} description - The description of the frequency.
  142. */
  143. const frequencies = [
  144. { value: 365, description: 'täglich' },
  145. { value: 12, description: 'monatlich' },
  146. { value: 4, description: 'vierteljährlich' },
  147. { value: 1, description: 'jährlich' }
  148. // etc...
  149. ];
  150. res.send(frequencies);
  151. });
  152. /**
  153. * @swagger
  154. * /types:
  155. * get:
  156. * summary: Retrieves all types of income
  157. * responses:
  158. * 200:
  159. * description: An array of income type objects
  160. * content:
  161. * application/json:
  162. * schema:
  163. * type: array
  164. * items:
  165. * type: object
  166. * properties:
  167. * value:
  168. * type: string
  169. * description:
  170. * type: string
  171. * example:
  172. * - value: 'work'
  173. * description: 'als Arbeitnehmer'
  174. * - value: 'pensioner'
  175. * description: 'als Rentner'
  176. * - value: 'social'
  177. * description: 'Einnahme aus Sozialeistungen'
  178. * - value: 'entertains'
  179. * description: 'Unterhalt'
  180. * 400:
  181. * description: Bad Request
  182. * 500:
  183. * description: Internal Server Error
  184. */
  185. app.get('/types', (req, res) => {
  186. /**
  187. * Retrieves all types of income.
  188. * @returns {object[]} - An array of income type objects.
  189. * @property {string} value - The value of the income type.
  190. * @property {string} description - The description of the income type.
  191. */
  192. const types = [
  193. { value: 'work', description: 'als Arbeitnehmer' },
  194. { value: 'pensioner', description: 'als Rentner' },
  195. { value: 'social', description: 'Einnahme aus Sozialeistungen' },
  196. { value: 'entertains', description: 'Unterhalt' }
  197. // etc...
  198. ];
  199. res.send(types);
  200. });
  201. const port = process.env.PORT || 3000;
  202. app.listen(port, () => console.log(`Server läuft auf Port ${port}...`));