-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from mitaai/blms/issue26
Create Document and Group POST, PATCH routes
- Loading branch information
Showing
7 changed files
with
299 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import nc from 'next-connect'; | ||
import { ObjectID } from 'mongodb'; | ||
import jwt from 'next-auth/jwt'; | ||
import middleware from '../../../middlewares/middleware'; | ||
|
||
const secret = process.env.AUTH_SECRET; | ||
|
||
const handler = nc() | ||
.use(middleware) | ||
.post( | ||
async (req, res) => { | ||
const token = await jwt.getJwt({ req, secret }); | ||
if (token && token.exp > 0) { | ||
const dateCreated = new Date(Date.now()); | ||
const { | ||
title, | ||
slug, | ||
groups, | ||
resourceType, | ||
authors, | ||
publisher, | ||
publicationDate, | ||
bookTitle, | ||
edition, | ||
url, | ||
accessed, | ||
rightsStatus, | ||
location, | ||
state, | ||
text, | ||
uploadContentType, | ||
editors, | ||
volume, | ||
issue, | ||
pageNumbers, | ||
publication, | ||
series, | ||
sesiesNumber, | ||
notes, | ||
} = req.body; | ||
const metadata = { | ||
title, | ||
slug, | ||
groups, | ||
resourceType, | ||
authors, | ||
publisher, | ||
publicationDate, | ||
bookTitle, | ||
edition, | ||
url, | ||
accessed, | ||
rightsStatus, | ||
location, | ||
state, | ||
text, | ||
uploadContentType, | ||
editors, | ||
volume, | ||
issue, | ||
pageNumbers, | ||
publication, | ||
series, | ||
sesiesNumber, | ||
notes, | ||
}; | ||
Object.keys(metadata).forEach((key) => { | ||
if (metadata[key] === undefined) { | ||
delete metadata[key]; | ||
} | ||
}); | ||
await req.db | ||
.collection('documents') | ||
.insert( | ||
{ | ||
owner: ObjectID(token.user.id), | ||
createdAt: dateCreated, | ||
updatedAt: dateCreated, | ||
...metadata, | ||
}, | ||
(err, doc) => { | ||
if (err) throw err; | ||
res.status(200).json(doc); | ||
}, | ||
); | ||
} else res.status(403).json({ error: '403 Invalid or expired token' }); | ||
}, | ||
); | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import nc from 'next-connect'; | ||
import { ObjectID } from 'mongodb'; | ||
import jwt from 'next-auth/jwt'; | ||
import middleware from '../../../middlewares/middleware'; | ||
|
||
const secret = process.env.AUTH_SECRET; | ||
|
||
const handler = nc() | ||
.use(middleware) | ||
.post( | ||
async (req, res) => { | ||
const token = await jwt.getJwt({ req, secret }); | ||
if (token && token.exp > 0) { | ||
const { name } = req.body; | ||
const members = [{ | ||
id: ObjectID(token.user.id), | ||
name: token.user.name, | ||
email: token.user.email, | ||
role: 'owner', | ||
}]; | ||
const documents = [{}]; | ||
await req.db | ||
.collection('groups') | ||
.insertOne( | ||
{ name, members, documents }, | ||
(err, doc) => { | ||
if (err) throw err; | ||
res.status(200).json(doc); | ||
}, | ||
); | ||
} else res.status(403).json({ error: '403 Invalid or expired token' }); | ||
}, | ||
); | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,17 @@ const dateCreated = new Date(Date.now()); | |
const group = { | ||
id: getObjectId(name), | ||
name, | ||
members: [getObjectId('FakeUserReplaceMe')], | ||
documents: [getObjectId(docTitle)], | ||
members: [{ | ||
id: getObjectId('FakeUserReplaceMe'), | ||
name: 'Fake User', | ||
email: '[email protected]', | ||
role: 'owner', | ||
}], | ||
documents: [{ | ||
id: getObjectId(docTitle), | ||
name: docTitle, | ||
slug: 'afterward', | ||
}], | ||
createdAt: dateCreated, | ||
updatedAt: dateCreated, | ||
}; | ||
|
Oops, something went wrong.