-
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.
- Loading branch information
Showing
2 changed files
with
85 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,9 @@ | ||
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; | ||
import postDocument from '../../../utils/dbUtil'; | ||
|
||
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' }); | ||
}, | ||
); | ||
.post(postDocument); | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ObjectID } from 'mongodb'; | ||
import jwt from 'next-auth/jwt'; | ||
|
||
const secret = process.env.AUTH_SECRET; | ||
|
||
const postDocument = 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 postDocument; |