Express TS project
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

109 linhas
3.5 KiB

  1. import { ObjectId } from 'bson';
  2. import express from 'express';
  3. import passport from 'passport';
  4. import { MongoShelf } from '../models/shelf';
  5. import { DB_NAME, getDatabaseClient } from '../db-utils';
  6. import { MongoCategory } from '../models/category';
  7. import { MongoUser } from '../models/user';
  8. import { Word } from '../models/word';
  9. export const userProfileRoutes = express.Router();
  10. export const jwtAuthentication = passport.authenticate('jwt', { session: false });
  11. userProfileRoutes.get('/profile/', jwtAuthentication, async (request, response) => {
  12. const user: MongoUser = (request.user as any);
  13. response.json({
  14. _id: user._id,
  15. name: user.name,
  16. email: user.email,
  17. isVerified: user.isVerified,
  18. categories: user.categories,
  19. uncategorised: user.uncategorised
  20. });
  21. return;
  22. });
  23. userProfileRoutes.get('/profile/deep-copy/', jwtAuthentication, async (request, response) => {
  24. const user: MongoUser = (request.user as any);
  25. const categoryCollection = getDatabaseClient().db(DB_NAME).collection<MongoCategory>('categories');
  26. const shelfCollection = getDatabaseClient().db(DB_NAME).collection<MongoShelf>('shelves');
  27. const wordCollection = getDatabaseClient().db(DB_NAME).collection<Word>('words');
  28. let deepCategories = [];
  29. for (let i = 0; i < user.categories.length; i += 1) {
  30. let deepShelves = [];
  31. const matchedCategory: any = await categoryCollection.findOne({
  32. _id: new ObjectId(user.categories[i])
  33. });
  34. if (matchedCategory && matchedCategory.shelves) {
  35. for (let j = 0; j < matchedCategory.shelves.length; j += 1) {
  36. let deepAddedWords = [];
  37. const matchedShelf: any = await shelfCollection.findOne({
  38. _id: new ObjectId(matchedCategory.shelves[j])
  39. });
  40. if (matchedShelf && matchedShelf.addedWords) {
  41. for (let k = 0; k < matchedShelf.addedWords.length; k += 1) {
  42. const matchedWord = await wordCollection.findOne({
  43. _id: new ObjectId(matchedShelf.addedWords[k].word)
  44. });
  45. if (matchedWord) {
  46. deepAddedWords.push(matchedWord);
  47. }
  48. }
  49. }
  50. matchedShelf.addedWords = deepAddedWords;
  51. deepShelves.push(matchedShelf);
  52. }
  53. }
  54. matchedCategory.shelves = deepShelves;
  55. deepCategories.push(matchedCategory);
  56. }
  57. let deepUncategorisedAddedWords = [];
  58. const matchedUncategoisedShelf: any = await shelfCollection.findOne({
  59. _id: new ObjectId(user.uncategorised)
  60. });
  61. if (matchedUncategoisedShelf && matchedUncategoisedShelf.addedWords) {
  62. for (let k = 0; k < matchedUncategoisedShelf.addedWords.length; k += 1) {
  63. const matchedWord = await wordCollection.findOne({
  64. _id: new ObjectId(matchedUncategoisedShelf.addedWords[k].word)
  65. });
  66. if (matchedWord) {
  67. deepUncategorisedAddedWords.push(matchedWord);
  68. }
  69. }
  70. matchedUncategoisedShelf.addedWords = deepUncategorisedAddedWords;
  71. }
  72. response.json({
  73. _id: user._id,
  74. name: user.name,
  75. email: user.email,
  76. isVerified: user.isVerified,
  77. categories: deepCategories,
  78. uncategorised: matchedUncategoisedShelf
  79. });
  80. return;
  81. });