PageRenderTime 29ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/src/Users/Users.router.ts

https://gitlab.com/dreamteam109/stuckwithticketsfinal
TypeScript | 352 lines | 16 code | 13 blank | 323 comment | 0 complexity | 0f6402a3679316a1c187d387af6b4fb3 MD5 | raw file
  1. import { Router } from "express"
  2. import { updateScoresForTicketsAndUsers } from "../algorithm/algorithm";
  3. import { authMiddleware, refreshTokenMiddleware } from "../auth/auth";
  4. import { editUserDetails, getContactInfoById } from "../db/users";
  5. import { contactInfoHandler, editUserDetailsHandler, getMyUserDetailsHandler, loginHandler, logoutHandler, refreshTokenHandler, registerHandler } from "./Users.handlers";
  6. /**
  7. * @swagger
  8. * tags:
  9. * name: Users
  10. * description: The Users API
  11. */
  12. /**
  13. * @swagger
  14. * components:
  15. * securitySchemes:
  16. * accessToken:
  17. * type: http
  18. * scheme: bearer
  19. * bearerFormat: JWT
  20. * refreshToken:
  21. * type: http
  22. * scheme: bearer
  23. * bearerFormat: JWT
  24. */
  25. /**
  26. * @swagger
  27. * components:
  28. * responses:
  29. * UnauthorizedError:
  30. * description: Access token is missing or invalid
  31. */
  32. /**
  33. * @swagger
  34. * components:
  35. * schemas:
  36. * RegisterRequestBody:
  37. * type: object
  38. * properties:
  39. * email:
  40. * type: string
  41. * description: The user's email
  42. * password:
  43. * type: string
  44. * description: The user's passowrd
  45. * firstName:
  46. * type: string
  47. * description: The user's first name
  48. * lastName:
  49. * type: string
  50. * description: The user's last name
  51. * phoneNumber:
  52. * type: string
  53. * description: The user's phone number
  54. * imageId:
  55. * type: string
  56. * nullable: true
  57. * description: The user's image id
  58. * interests:
  59. * type: string
  60. * nullable: false
  61. * description: The user's interests
  62. * example:
  63. * email: "IsraelIsraeli@gmail.com"
  64. * password: "123456"
  65. * firstName: "Israel"
  66. * lastName: "Israeli"
  67. * phoneNumber: "050-1234567"
  68. * imageId: "e49bb982bd2e419682565e285a899a2f"
  69. * interests: "I like rock music"
  70. * LoginRequestBody:
  71. * type: object
  72. * properties:
  73. * email:
  74. * type: string
  75. * description: The user's email
  76. * password:
  77. * type: string
  78. * description: The user's password
  79. * example:
  80. * email: "IsraelIsraeli@gmail.com"
  81. * password: "123456"
  82. * AccessAndRefreshTokens:
  83. * type: object
  84. * properties:
  85. * accessToken:
  86. * type: string
  87. * description: The access token
  88. * refreshToken:
  89. * type: string
  90. * description: The refresh token
  91. * example:
  92. * accessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoxLCJlbWFpbCI6Iml0YXk2NDdAZ21haWwuY29tIn0sImlhdCI6MTY1MzA2NjkxNywiZXhwIjoxNjUzMDY3MjE3fQ.d-lPAX_2IX4YqDFA2wtiKd3WGHSYWA6Kt5U-thyg-nc"
  93. * refreshToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoxLCJlbWFpbCI6Iml0YXk2NDdAZ21haWwuY29tIn0sImlhdCI6MTY1MzA2NjkxN30.HHhUnt3dPVLSlgl9st92iZ6ClZfo3mQ3lVc0kSN1m5Q"
  94. * EditUserRequestBody:
  95. * type: object
  96. * properties:
  97. * firstName:
  98. * type: string
  99. * description: The user's first name
  100. * lastName:
  101. * type: string
  102. * description: The user's last name
  103. * phoneNumber:
  104. * type: string
  105. * description: The user's phone number
  106. * imageId:
  107. * type: string
  108. * nullable: true
  109. * description: The user's image id
  110. * interests:
  111. * type: string
  112. * nullable: false
  113. * description: The user's interests
  114. * example:
  115. * firstName: "Israel"
  116. * lastName: "Israeli"
  117. * phoneNumber: "050-1234567"
  118. * imageId: "e49bb982bd2e419682565e285a899a2f"
  119. * interests: "I like rock music"
  120. * User:
  121. * type: object
  122. * properties:
  123. * id:
  124. * type: number
  125. * description: The user's id
  126. * firstName:
  127. * type: string
  128. * description: The user's first name
  129. * lastName:
  130. * type: string
  131. * description: The user's last name
  132. * email:
  133. * type: string
  134. * description: The user's email
  135. * password:
  136. * type: string
  137. * description: The user's passowrd
  138. * phoneNumber:
  139. * type: string
  140. * description: The user's phone number
  141. * imageId:
  142. * type: string
  143. * nullable: true
  144. * description: The user's image id
  145. * interests:
  146. * type: string
  147. * nullable: false
  148. * description: The user's interests
  149. * example:
  150. * id: 1
  151. * firstName: "Israel"
  152. * lastName: "Israeli"
  153. * email: "IsraelIsraeli@gmail.com"
  154. * password: "123456"
  155. * phoneNumber: "050-1234567"
  156. * imageId: "e49bb982bd2e419682565e285a899a2f"
  157. * interests: "I like rock music"
  158. * ContactInfo:
  159. * type: object
  160. * properties:
  161. * id:
  162. * type: number
  163. * description: The user's id
  164. * firstName:
  165. * type: string
  166. * description: The user's first name
  167. * lastName:
  168. * type: string
  169. * description: The user's last name
  170. * email:
  171. * type: string
  172. * description: The user's email
  173. * phoneNumber:
  174. * type: string
  175. * description: The user's phone number
  176. * imageId:
  177. * type: string
  178. * nullable: true
  179. * description: The user's image id
  180. * example:
  181. * id: 1
  182. * firstName: "Israel"
  183. * lastName: "Israeli"
  184. * email: "IsraelIsraeli@gmail.com"
  185. * phoneNumber: "050-1234567"
  186. * imageId: "e49bb982bd2e419682565e285a899a2f"
  187. */
  188. export const createUsersRouter = () => {
  189. const router = Router();
  190. /**
  191. * @swagger
  192. * /users/register:
  193. * post:
  194. * summary: Register a new user
  195. * tags: [Users]
  196. * requestBody:
  197. * required: true
  198. * content:
  199. * application/json:
  200. * schema:
  201. * $ref: '#/components/schemas/RegisterRequestBody'
  202. * responses:
  203. * 201:
  204. * description: Register successful
  205. * 400:
  206. * description: Invalid or missing registration data
  207. * 409:
  208. * description: The given email is already registered
  209. */
  210. router.post('/register', registerHandler);
  211. /**
  212. * @swagger
  213. * /users/login:
  214. * post:
  215. * summary: Login with email and password
  216. * tags: [Users]
  217. * requestBody:
  218. * required: true
  219. * content:
  220. * application/json:
  221. * schema:
  222. * $ref: '#/components/schemas/LoginRequestBody'
  223. * responses:
  224. * 200:
  225. * description: Login successful
  226. * content:
  227. * application/json:
  228. * schema:
  229. * $ref: '#/components/schemas/AccessAndRefreshTokens'
  230. * 400:
  231. * description: Invalid or missing email or password
  232. * 401:
  233. * description: Wrong username or password
  234. */
  235. router.post('/login', loginHandler);
  236. /**
  237. * @swagger
  238. * /users/refreshToken:
  239. * post:
  240. * summary: Request a new access and refresh tokens
  241. * tags: [Users]
  242. * security:
  243. * - refreshToken: []
  244. * responses:
  245. * 200:
  246. * description: Created new tokens successfully
  247. * content:
  248. * application/json:
  249. * schema:
  250. * $ref: '#/components/schemas/AccessAndRefreshTokens'
  251. * 401:
  252. * description: Refresh token is missing or invalid
  253. * 403:
  254. * description: Invalid request
  255. */
  256. router.post('/refreshToken', refreshTokenMiddleware, refreshTokenHandler);
  257. /**
  258. * @swagger
  259. * /users/logout:
  260. * post:
  261. * summary: Logout
  262. * tags: [Users]
  263. * security:
  264. * - refreshToken: []
  265. * responses:
  266. * 200:
  267. * description: Logout successful
  268. * 401:
  269. * description: Refresh token is missing or invalid
  270. * 403:
  271. * description: Invalid request
  272. */
  273. router.post('/logout', refreshTokenMiddleware, logoutHandler);
  274. /**
  275. * @swagger
  276. * /users/edit:
  277. * post:
  278. * summary: Edit my user info
  279. * tags: [Users]
  280. * security:
  281. * - accessToken: []
  282. * requestBody:
  283. * required: true
  284. * content:
  285. * application/json:
  286. * schema:
  287. * $ref: '#/components/schemas/EditUserRequestBody'
  288. * responses:
  289. * 200:
  290. * description: The edit completed successfully
  291. * 400:
  292. * description: Invalid or missing user data on edit
  293. * 401:
  294. * $ref: "#/components/responses/UnauthorizedError"
  295. */
  296. router.post('/edit', authMiddleware, editUserDetailsHandler(editUserDetails, updateScoresForTicketsAndUsers));
  297. /**
  298. * @swagger
  299. * /users/me:
  300. * get:
  301. * summary: Get my user info
  302. * tags: [Users]
  303. * security:
  304. * - accessToken: []
  305. * responses:
  306. * 200:
  307. * description: The user info
  308. * content:
  309. * application/json:
  310. * schema:
  311. * $ref: "#/components/schemas/User"
  312. * 401:
  313. * $ref: "#/components/responses/UnauthorizedError"
  314. */
  315. router.get('/me', authMiddleware, getMyUserDetailsHandler);
  316. /**
  317. * @swagger
  318. * /users/contactInfo/{id}:
  319. * get:
  320. * summary: Get the contact info by user id
  321. * tags: [Users]
  322. * parameters:
  323. * - in: path
  324. * name: id
  325. * schema:
  326. * type: number
  327. * required: true
  328. * description: The user id
  329. * responses:
  330. * 200:
  331. * description: The contact info
  332. * content:
  333. * application/json:
  334. * schema:
  335. * $ref: "#/components/schemas/ContactInfo"
  336. */
  337. router.get('/contactInfo/:id', contactInfoHandler(getContactInfoById));
  338. return router;
  339. }