|
|
|
@@ -1,17 +1,17 @@ |
|
|
|
import express from 'express'; |
|
|
|
import passport from 'passport'; |
|
|
|
import { IUser } from '../models/user'; |
|
|
|
import { v4 as uuidv4 } from 'uuid'; |
|
|
|
import { Category } from '../models/category'; |
|
|
|
import passport, { use } from 'passport'; |
|
|
|
import { MongoUser } from '../models/user'; |
|
|
|
import { Category, MongoCategory } from '../models/category'; |
|
|
|
import { DB_NAME } from '../db-utils'; |
|
|
|
import { getDatabaseClient } from '../db-utils'; |
|
|
|
import { ObjectId } from 'bson'; |
|
|
|
|
|
|
|
export const userProfileRoutes = express.Router(); |
|
|
|
|
|
|
|
export const jwtAuthentication = passport.authenticate('jwt', { session: false }); |
|
|
|
|
|
|
|
userProfileRoutes.get('/profile/', jwtAuthentication, async (request, response) => { |
|
|
|
const user: IUser = (request.user as any); |
|
|
|
const user: MongoUser = (request.user as any); |
|
|
|
response.json({ |
|
|
|
_id: user._id, |
|
|
|
name: user.name, |
|
|
|
@@ -23,21 +23,91 @@ userProfileRoutes.get('/profile/', jwtAuthentication, async (request, response) |
|
|
|
return; |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
userProfileRoutes.post('/category/', jwtAuthentication, async (request, response) => { |
|
|
|
const user: IUser = (request.user as any); |
|
|
|
const user: MongoUser = (request.user as any); |
|
|
|
|
|
|
|
const userCollection = getDatabaseClient().db(DB_NAME).collection('categories'); |
|
|
|
const categoryCollection = getDatabaseClient().db(DB_NAME).collection('categories'); |
|
|
|
const userCollection = getDatabaseClient().db(DB_NAME).collection('users'); |
|
|
|
|
|
|
|
const newCategory: Category = { |
|
|
|
_id: uuidv4(), |
|
|
|
name: request.body.name, |
|
|
|
icon: request.body.icon, |
|
|
|
shelves: [], |
|
|
|
isArchived: false, |
|
|
|
if (!request.body.name || !request.body.icon) { |
|
|
|
response.status(400); |
|
|
|
response.send("Category Name or icon(base64) missing"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
response.status(200); |
|
|
|
try { |
|
|
|
const newCategory = await categoryCollection.insertOne({ |
|
|
|
name: request.body.name, |
|
|
|
icon: request.body.icon, |
|
|
|
isArchived: false, |
|
|
|
}); |
|
|
|
|
|
|
|
if (!user.categories) { |
|
|
|
user.categories = []; |
|
|
|
} |
|
|
|
|
|
|
|
user.categories.push(newCategory.insertedId.toHexString()); |
|
|
|
|
|
|
|
await userCollection.updateOne({ |
|
|
|
_id: user._id |
|
|
|
}, { |
|
|
|
$set: { |
|
|
|
categories: user.categories |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
response.sendStatus(200); |
|
|
|
} catch(e) { |
|
|
|
response.sendStatus(500); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
userProfileRoutes.put('/category/', jwtAuthentication, async (request, response) => { |
|
|
|
const categoryCollection = getDatabaseClient().db(DB_NAME).collection('categories'); |
|
|
|
|
|
|
|
let currentCategory; |
|
|
|
|
|
|
|
try { |
|
|
|
currentCategory = await categoryCollection.findOne({ |
|
|
|
_id: new ObjectId(request.body._id) |
|
|
|
}); |
|
|
|
} catch { |
|
|
|
if (!currentCategory) { |
|
|
|
response.status(400); |
|
|
|
response.send("Category ID did not match"); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (request.body.isArchived) { |
|
|
|
if (typeof request.body.isArchived !== "boolean") { |
|
|
|
response.status(400); |
|
|
|
response.send("Archived should be a boolean flag"); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
await categoryCollection.updateOne({ |
|
|
|
_id: new ObjectId(request.body._id), |
|
|
|
}, { |
|
|
|
$set: { |
|
|
|
name: request.body.name ? request.body.name : currentCategory.name, |
|
|
|
icon: request.body.icon ? request.body.icon : currentCategory.icon, |
|
|
|
isArchived: request.body.isArchived !== undefined ? request.body.isArchived : currentCategory.isArchived |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
response.sendStatus(200); |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
response.status(400); |
|
|
|
response.json(e); |
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
}); |