Express TS project
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

114 righe
3.2 KiB

  1. import express from 'express';
  2. import passport, { use } from 'passport';
  3. import { MongoUser } from '../models/user';
  4. import { Category, MongoCategory } from '../models/category';
  5. import { DB_NAME } from '../db-utils';
  6. import { getDatabaseClient } from '../db-utils';
  7. import { ObjectId } from 'bson';
  8. export const userProfileRoutes = express.Router();
  9. export const jwtAuthentication = passport.authenticate('jwt', { session: false });
  10. userProfileRoutes.get('/profile/', jwtAuthentication, async (request, response) => {
  11. const user: MongoUser = (request.user as any);
  12. response.json({
  13. _id: user._id,
  14. name: user.name,
  15. email: user.email,
  16. isVerified: user.isVerified,
  17. categories: user.categories,
  18. uncategorised: user.uncategorised
  19. });
  20. return;
  21. });
  22. userProfileRoutes.post('/category/', jwtAuthentication, async (request, response) => {
  23. const user: MongoUser = (request.user as any);
  24. const categoryCollection = getDatabaseClient().db(DB_NAME).collection('categories');
  25. const userCollection = getDatabaseClient().db(DB_NAME).collection('users');
  26. if (!request.body.name || !request.body.icon) {
  27. response.status(400);
  28. response.send("Category Name or icon(base64) missing");
  29. return;
  30. }
  31. try {
  32. const newCategory = await categoryCollection.insertOne({
  33. name: request.body.name,
  34. icon: request.body.icon,
  35. isArchived: false,
  36. });
  37. if (!user.categories) {
  38. user.categories = [];
  39. }
  40. user.categories.push(newCategory.insertedId.toHexString());
  41. await userCollection.updateOne({
  42. _id: user._id
  43. }, {
  44. $set: {
  45. categories: user.categories
  46. }
  47. });
  48. response.sendStatus(200);
  49. } catch(e) {
  50. response.sendStatus(500);
  51. return;
  52. }
  53. return;
  54. });
  55. userProfileRoutes.put('/category/', jwtAuthentication, async (request, response) => {
  56. const categoryCollection = getDatabaseClient().db(DB_NAME).collection('categories');
  57. let currentCategory;
  58. try {
  59. currentCategory = await categoryCollection.findOne({
  60. _id: new ObjectId(request.body._id)
  61. });
  62. } catch {
  63. if (!currentCategory) {
  64. response.status(400);
  65. response.send("Category ID did not match");
  66. return;
  67. }
  68. }
  69. if (request.body.isArchived) {
  70. if (typeof request.body.isArchived !== "boolean") {
  71. response.status(400);
  72. response.send("Archived should be a boolean flag");
  73. return;
  74. }
  75. }
  76. try {
  77. await categoryCollection.updateOne({
  78. _id: new ObjectId(request.body._id),
  79. }, {
  80. $set: {
  81. name: request.body.name ? request.body.name : currentCategory.name,
  82. icon: request.body.icon ? request.body.icon : currentCategory.icon,
  83. isArchived: request.body.isArchived !== undefined ? request.body.isArchived : currentCategory.isArchived
  84. }
  85. });
  86. response.sendStatus(200);
  87. } catch (e) {
  88. response.status(400);
  89. response.json(e);
  90. }
  91. return;
  92. });