wbs_rechner_express.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. const express = require('express');
  2. const app = express();
  3. const cors = require('cors');
  4. app.use(express.json()); // for parsing JSON data
  5. // Swagger Setup
  6. const swaggerJsdoc = require('swagger-jsdoc');
  7. const swaggerUi = require('swagger-ui-express');
  8. const options = {
  9. definition: {
  10. openapi: '3.0.0',
  11. info: {
  12. title: 'WBS RECHNER',
  13. version: '1.0.0',
  14. description: 'An API for calculating total income and granting WBS.',
  15. },
  16. },
  17. apis: ['wbs_rechner_express.js'],
  18. };
  19. const specs = swaggerJsdoc(options);
  20. app.use('/api', swaggerUi.serve, swaggerUi.setup(specs));
  21. app.use(cors())
  22. /**
  23. * @swagger
  24. * /calculate:
  25. * post:
  26. * summary: Calculates whether a WBS can be granted based on the provided incomes
  27. * requestBody:
  28. * required: true
  29. * content:
  30. * application/json:
  31. * schema:
  32. * type: object
  33. * properties:
  34. * persons:
  35. * type: array
  36. * items:
  37. * type: object
  38. * properties:
  39. * incomes:
  40. * type: array
  41. * items:
  42. * type: object
  43. * properties:
  44. * amount:
  45. * type: number
  46. * frequency:
  47. * type: number
  48. * advertisingCosts:
  49. * type: number
  50. * example:
  51. * persons:
  52. * - incomes:
  53. * - amount: 500
  54. * frequency: 12
  55. * advertisingCosts: 500
  56. * - amount: 200
  57. * frequency: 1
  58. * advertisingCosts: 0
  59. * - incomes:
  60. * - amount: 1200
  61. * frequency: 12
  62. * advertisingCosts: 200
  63. * - amount: 2000
  64. * frequency: 1
  65. * advertisingCosts: 0
  66. * responses:
  67. * 200:
  68. * description: WBS calculation result
  69. * content:
  70. * application/json:
  71. * schema:
  72. * type: object
  73. * properties:
  74. * wbsGranted:
  75. * type: string
  76. * yearlyAmount:
  77. * type: number
  78. * yearlyAmountMax:
  79. * type: number
  80. * difference:
  81. * type: number
  82. * percent:
  83. * type: number
  84. * 400:
  85. * description: Bad Request
  86. * 500:
  87. * description: Internal Server Error
  88. */
  89. app.post('/calculate', (req, res) => {
  90. /**
  91. * Calculates whether a WBS can be granted based on the provided incomes.
  92. * @param {object[]} persons - An array of persons.
  93. * @param {object[]} persons.incomes - An array of incomes for each person.
  94. * @param {number} persons.incomes[].amount - The amount of income.
  95. * @param {number} persons.incomes[].frequency - The frequency of income.
  96. * @param {number} persons.incomes[].advertisingCosts - The advertising costs for the income.
  97. * @returns {object} - The result object containing the wbsGranted property.
  98. */
  99. let totalIncome = 0;
  100. const persons = req.body;
  101. console.log(req.body)
  102. for (const person of persons) {
  103. for (const income of person.incomes) {
  104. let annualIncome = income.amount * income.frequency;
  105. annualIncome -= income.advertisingCosts;
  106. totalIncome += annualIncome;
  107. }
  108. }
  109. // Example calculation does not correspond to the WBS calculation
  110. let maxIncome = persons.length * 12000;
  111. let difference = totalIncome - maxIncome;
  112. let percent = 100 * totalIncome / maxIncome;
  113. let wbsGranted = "Kein WBS"
  114. if(percent <= 100) {
  115. wbsGranted = "WBS 100";
  116. } else if (percent <= 140) {
  117. wbsGranted = "WBS 140"
  118. }
  119. res.send({ wbsGranted: wbsGranted,
  120. yearlyAmount: totalIncome,
  121. yearlyAmountMax: maxIncome,
  122. difference: difference,
  123. percent: percent});
  124. });
  125. /**
  126. * @swagger
  127. * /frequencies:
  128. * get:
  129. * summary: Retrieves all possible frequencies of income
  130. * responses:
  131. * 200:
  132. * description: An array of frequency objects
  133. * content:
  134. * application/json:
  135. * schema:
  136. * type: array
  137. * items:
  138. * type: object
  139. * properties:
  140. * value:
  141. * type: number
  142. * description:
  143. * type: string
  144. * example:
  145. * - value: 365
  146. * description: "täglich"
  147. * - value: 12
  148. * description: "monatlich"
  149. * - value: 4
  150. * description: "vierteljährlich"
  151. * - value: 1
  152. * description: "jährlich"
  153. * 400:
  154. * description: Bad Request
  155. * 500:
  156. * description: Internal Server Error
  157. */
  158. app.get('/frequencies', (req, res) => {
  159. /**
  160. * Retrieves all possible frequencies of income.
  161. * @returns {object[]} - An array of frequency objects.
  162. * @property {number} value - The value of the frequency.
  163. * @property {string} description - The description of the frequency.
  164. */
  165. const frequencies = [
  166. { value: 365, description: 'täglich' },
  167. { value: 12, description: 'monatlich' },
  168. { value: 4, description: 'vierteljährlich' },
  169. { value: 1, description: 'jährlich' }
  170. // etc...
  171. ];
  172. res.send(frequencies);
  173. });
  174. /**
  175. * @swagger
  176. * /types:
  177. * get:
  178. * summary: Retrieves all types of income
  179. * responses:
  180. * 200:
  181. * description: An array of income type objects
  182. * content:
  183. * application/json:
  184. * schema:
  185. * type: array
  186. * items:
  187. * type: object
  188. * properties:
  189. * value:
  190. * type: string
  191. * description:
  192. * type: string
  193. * example:
  194. * - value: 'work'
  195. * description: 'als Arbeitnehmer'
  196. * - value: 'pensioner'
  197. * description: 'als Rentner'
  198. * - value: 'social'
  199. * description: 'Einnahme aus Sozialeistungen'
  200. * - value: 'entertains'
  201. * description: 'Unterhalt'
  202. * 400:
  203. * description: Bad Request
  204. * 500:
  205. * description: Internal Server Error
  206. */
  207. app.get('/types', (req, res) => {
  208. /**
  209. * Retrieves all types of income.
  210. * @returns {object[]} - An array of income type objects.
  211. * @property {string} value - The value of the income type.
  212. * @property {string} description - The description of the income type.
  213. */
  214. const types = [
  215. { value: 'work', description: 'als Arbeitnehmer' },
  216. { value: 'pensioner', description: 'als Rentner' },
  217. { value: 'social', description: 'Einnahme aus Sozialeistungen' },
  218. { value: 'entertains', description: 'Unterhalt' }
  219. // etc...
  220. ];
  221. res.send(types);
  222. });
  223. const port = process.env.PORT || 3100;
  224. app.listen(port, () => console.log(`Server läuft auf Port ${port}...`));