api: fix mongoose model imports
This commit is contained in:
parent
22ce863ad4
commit
50d6697b5b
|
@ -1,8 +1,7 @@
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const mongoose = require('mongoose');
|
|
||||||
const sendEmail = require('../_helpers/send-email');
|
const sendEmail = require('../_helpers/send-email');
|
||||||
const User = mongoose.model('User');
|
|
||||||
const config = require('../config');
|
const config = require('../config');
|
||||||
|
const { User } = require('../models');
|
||||||
|
|
||||||
const baseUrl = config.baseUrl.replace(/\/+$/, '');
|
const baseUrl = config.baseUrl.replace(/\/+$/, '');
|
||||||
|
|
||||||
|
@ -113,7 +112,7 @@ async function sendVerificationEmail(account) {
|
||||||
'<p>Thanks for registering!</p>',
|
'<p>Thanks for registering!</p>',
|
||||||
'<p>Please click the below link to verify your email address:</p>',
|
'<p>Please click the below link to verify your email address:</p>',
|
||||||
`<p><a href="${verifyUrl}">${verifyUrl}</a></p>`,
|
`<p><a href="${verifyUrl}">${verifyUrl}</a></p>`,
|
||||||
].join('\n')
|
].join('\n');
|
||||||
|
|
||||||
await sendEmail({
|
await sendEmail({
|
||||||
to: account.email,
|
to: account.email,
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
const mongoose = require('mongoose');
|
|
||||||
const User = mongoose.model('User');
|
|
||||||
const wrapRoute = require('../../_helpers/wrapRoute');
|
const wrapRoute = require('../../_helpers/wrapRoute');
|
||||||
const auth = require('../../passport');
|
const auth = require('../../passport');
|
||||||
|
const { User } = require('../../models');
|
||||||
|
|
||||||
// Preload user profile on routes with ':username'
|
// Preload user profile on routes with ':username'
|
||||||
router.param('username', async function (req, res, next, username) {
|
router.param('username', async function (req, res, next, username) {
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
const mongoose = require('mongoose');
|
|
||||||
const { DateTime } = require('luxon');
|
const { DateTime } = require('luxon');
|
||||||
|
|
||||||
const Track = mongoose.model('Track');
|
const { Track, User } = require('../../models');
|
||||||
const User = mongoose.model('User');
|
|
||||||
const wrapRoute = require('../../_helpers/wrapRoute');
|
const wrapRoute = require('../../_helpers/wrapRoute');
|
||||||
const auth = require('../../passport');
|
const auth = require('../../passport');
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
const mongoose = require('mongoose');
|
|
||||||
const Track = mongoose.model('Track');
|
|
||||||
const wrapRoute = require('../../_helpers/wrapRoute');
|
const wrapRoute = require('../../_helpers/wrapRoute');
|
||||||
|
const { Track } = require('../../models');
|
||||||
|
|
||||||
// return a list of tags
|
// return a list of tags
|
||||||
router.get(
|
router.get(
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
const mongoose = require('mongoose');
|
const { Track, User, Comment } = require('../../models');
|
||||||
const Track = mongoose.model('Track');
|
|
||||||
const Comment = mongoose.model('Comment');
|
|
||||||
const User = mongoose.model('User');
|
|
||||||
const busboy = require('connect-busboy');
|
const busboy = require('connect-busboy');
|
||||||
const auth = require('../../passport');
|
const auth = require('../../passport');
|
||||||
const { normalizeUserAgent, buildObsver1 } = require('../../logic/tracks');
|
const { normalizeUserAgent, buildObsver1 } = require('../../logic/tracks');
|
||||||
const wrapRoute = require('../../_helpers/wrapRoute');
|
const wrapRoute = require('../../_helpers/wrapRoute');
|
||||||
const {PROCESSING_OUTPUT_DIR} = require('../../paths')
|
const { PROCESSING_OUTPUT_DIR } = require('../../paths');
|
||||||
|
|
||||||
function preloadByParam(target, getValueFromParam) {
|
function preloadByParam(target, getValueFromParam) {
|
||||||
return async (req, res, next, paramValue) => {
|
return async (req, res, next, paramValue) => {
|
||||||
|
@ -80,7 +77,13 @@ router.get(
|
||||||
}
|
}
|
||||||
|
|
||||||
const [tracks, tracksCount] = await Promise.all([
|
const [tracks, tracksCount] = await Promise.all([
|
||||||
Track.find(query).sort('-createdAt').limit(Number(limit)).skip(Number(offset)).sort({ createdAt: 'desc' }).populate('author').exec(),
|
Track.find(query)
|
||||||
|
.sort('-createdAt')
|
||||||
|
.limit(Number(limit))
|
||||||
|
.skip(Number(offset))
|
||||||
|
.sort({ createdAt: 'desc' })
|
||||||
|
.populate('author')
|
||||||
|
.exec(),
|
||||||
Track.countDocuments(query).exec(),
|
Track.countDocuments(query).exec(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -183,30 +186,30 @@ router.post(
|
||||||
// TODO: Stream into temporary file, then move it later.
|
// TODO: Stream into temporary file, then move it later.
|
||||||
const { body, fileInfo } = await getMultipartOrJsonBody(req, (body) => body.track);
|
const { body, fileInfo } = await getMultipartOrJsonBody(req, (body) => body.track);
|
||||||
|
|
||||||
const { body: fileBody, public, ...trackBody } = body
|
const { body: fileBody, public, ...trackBody } = body;
|
||||||
|
|
||||||
const track = new Track({
|
const track = new Track({
|
||||||
...trackBody,
|
...trackBody,
|
||||||
author: req.user,
|
author: req.user,
|
||||||
public: public == null ? req.user.areTracksVisibleForAll : Boolean(trackBody.public)
|
public: public == null ? req.user.areTracksVisibleForAll : Boolean(trackBody.public),
|
||||||
})
|
});
|
||||||
track.customizedTitle = track.title != null
|
track.customizedTitle = track.title != null;
|
||||||
track.slugify();
|
track.slugify();
|
||||||
|
|
||||||
if (fileBody) {
|
if (fileBody) {
|
||||||
await track.validateFileBodyUniqueness(fileBody)
|
await track.validateFileBodyUniqueness(fileBody);
|
||||||
track.uploadedByUserAgent = normalizeUserAgent(req.headers['user-agent']);
|
track.uploadedByUserAgent = normalizeUserAgent(req.headers['user-agent']);
|
||||||
track.originalFileName = fileInfo.body ? fileInfo.body.filename : track.slug + '.csv';
|
track.originalFileName = fileInfo.body ? fileInfo.body.filename : track.slug + '.csv';
|
||||||
await track.writeToOriginalFile(fileBody)
|
await track.writeToOriginalFile(fileBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
await track.save()
|
await track.save();
|
||||||
|
|
||||||
if (fileBody) {
|
if (fileBody) {
|
||||||
await track.queueProcessing();
|
await track.queueProcessing();
|
||||||
}
|
}
|
||||||
|
|
||||||
await track.autoGenerateTitle()
|
await track.autoGenerateTitle();
|
||||||
|
|
||||||
return res.json({ track: track.toJSONFor(req.user) });
|
return res.json({ track: track.toJSONFor(req.user) });
|
||||||
}),
|
}),
|
||||||
|
@ -237,40 +240,43 @@ router.put(
|
||||||
return res.sendStatus(403);
|
return res.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { body: {body: fileBody, ...trackBody}, fileInfo } = await getMultipartOrJsonBody(req, (body) => body.track);
|
const {
|
||||||
|
body: { body: fileBody, ...trackBody },
|
||||||
|
fileInfo,
|
||||||
|
} = await getMultipartOrJsonBody(req, (body) => body.track);
|
||||||
|
|
||||||
if (typeof trackBody.title !== 'undefined') {
|
if (typeof trackBody.title !== 'undefined') {
|
||||||
track.title = (trackBody.title || '').trim() || null;
|
track.title = (trackBody.title || '').trim() || null;
|
||||||
track.customizedTitle = track.title != null
|
track.customizedTitle = track.title != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof trackBody.description !== 'undefined') {
|
if (typeof trackBody.description !== 'undefined') {
|
||||||
track.description = (trackBody.description || '').trim() || null;
|
track.description = (trackBody.description || '').trim() || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let process = false
|
let process = false;
|
||||||
|
|
||||||
if (trackBody.public != null) {
|
if (trackBody.public != null) {
|
||||||
const public = Boolean(trackBody.public);
|
const public = Boolean(trackBody.public);
|
||||||
process |= public !== track.public
|
process |= public !== track.public;
|
||||||
track.public = public
|
track.public = public;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fileBody) {
|
if (fileBody) {
|
||||||
await track.validateFileBodyUniqueness(fileBody)
|
await track.validateFileBodyUniqueness(fileBody);
|
||||||
track.originalFileName = fileInfo.body ? fileInfo.body.filename : track.slug + '.csv';
|
track.originalFileName = fileInfo.body ? fileInfo.body.filename : track.slug + '.csv';
|
||||||
track.uploadedByUserAgent = normalizeUserAgent(req.headers['user-agent']);
|
track.uploadedByUserAgent = normalizeUserAgent(req.headers['user-agent']);
|
||||||
await track.writeToOriginalFile(fileBody)
|
await track.writeToOriginalFile(fileBody);
|
||||||
process = true
|
process = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
await track.save();
|
await track.save();
|
||||||
|
|
||||||
if (process) {
|
if (process) {
|
||||||
await track.queueProcessing()
|
await track.queueProcessing();
|
||||||
}
|
}
|
||||||
|
|
||||||
await track.autoGenerateTitle()
|
await track.autoGenerateTitle();
|
||||||
|
|
||||||
return res.json({ track: track.toJSONFor(req.user) });
|
return res.json({ track: track.toJSONFor(req.user) });
|
||||||
}),
|
}),
|
||||||
|
@ -299,18 +305,17 @@ router.get(
|
||||||
return res.sendStatus(403);
|
return res.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
await req.track
|
await req.track.populate({
|
||||||
.populate({
|
path: 'comments',
|
||||||
path: 'comments',
|
populate: {
|
||||||
populate: {
|
path: 'author',
|
||||||
path: 'author',
|
},
|
||||||
|
options: {
|
||||||
|
sort: {
|
||||||
|
createdAt: 'asc',
|
||||||
},
|
},
|
||||||
options: {
|
},
|
||||||
sort: {
|
});
|
||||||
createdAt: 'asc',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.json({
|
return res.json({
|
||||||
comments: req.track.comments.map(function (comment) {
|
comments: req.track.comments.map(function (comment) {
|
||||||
|
@ -362,33 +367,33 @@ router.get(
|
||||||
allMeasurements: 'all_measurements.json',
|
allMeasurements: 'all_measurements.json',
|
||||||
confirmedMeasurements: 'confirmed_measurements.json',
|
confirmedMeasurements: 'confirmed_measurements.json',
|
||||||
track: 'track.json',
|
track: 'track.json',
|
||||||
}
|
};
|
||||||
|
|
||||||
if (!req.track.isVisibleTo(req.user)) {
|
if (!req.track.isVisibleTo(req.user)) {
|
||||||
return res.sendStatus(403);
|
return res.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = {}
|
const result = {};
|
||||||
for (const [key, filename] of Object.entries(FILE_BY_KEY)) {
|
for (const [key, filename] of Object.entries(FILE_BY_KEY)) {
|
||||||
const filePath = path.join(PROCESSING_OUTPUT_DIR, req.track.filePath, filename)
|
const filePath = path.join(PROCESSING_OUTPUT_DIR, req.track.filePath, filename);
|
||||||
|
|
||||||
let stats
|
let stats;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stats = await fs.promises.stat(filePath)
|
stats = await fs.promises.stat(filePath);
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
continue
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stats.isFile()) {
|
if (!stats.isFile()) {
|
||||||
// file does not exist (yet)
|
// file does not exist (yet)
|
||||||
continue
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const content = await fs.promises.readFile(filePath)
|
const content = await fs.promises.readFile(filePath);
|
||||||
const contentJson = JSON.parse(content)
|
const contentJson = JSON.parse(content);
|
||||||
|
|
||||||
result[key] = contentJson
|
result[key] = contentJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json(result);
|
return res.json(result);
|
||||||
|
@ -404,8 +409,8 @@ router.get(
|
||||||
return res.sendStatus(403);
|
return res.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.download(req.track.getOriginalFilePath(), req.track.originalFileName)
|
return res.download(req.track.getOriginalFilePath(), req.track.originalFileName);
|
||||||
})
|
}),
|
||||||
)
|
);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Reference in a new issue