From 50d6697b5b28fa57db72c324293ce3f3ccb4b79e Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Mon, 27 Sep 2021 20:18:57 +0200 Subject: [PATCH] api: fix mongoose model imports --- api/src/accounts/account.service.js | 5 +- api/src/routes/api/profiles.js | 3 +- api/src/routes/api/stats.js | 4 +- api/src/routes/api/tags.js | 3 +- api/src/routes/api/tracks.js | 103 +++++++++++++++------------- 5 files changed, 59 insertions(+), 59 deletions(-) diff --git a/api/src/accounts/account.service.js b/api/src/accounts/account.service.js index 8b57aec..d1a1406 100644 --- a/api/src/accounts/account.service.js +++ b/api/src/accounts/account.service.js @@ -1,8 +1,7 @@ const crypto = require('crypto'); -const mongoose = require('mongoose'); const sendEmail = require('../_helpers/send-email'); -const User = mongoose.model('User'); const config = require('../config'); +const { User } = require('../models'); const baseUrl = config.baseUrl.replace(/\/+$/, ''); @@ -113,7 +112,7 @@ async function sendVerificationEmail(account) { '

Thanks for registering!

', '

Please click the below link to verify your email address:

', `

${verifyUrl}

`, - ].join('\n') + ].join('\n'); await sendEmail({ to: account.email, diff --git a/api/src/routes/api/profiles.js b/api/src/routes/api/profiles.js index eafbd86..d533493 100644 --- a/api/src/routes/api/profiles.js +++ b/api/src/routes/api/profiles.js @@ -1,8 +1,7 @@ const router = require('express').Router(); -const mongoose = require('mongoose'); -const User = mongoose.model('User'); const wrapRoute = require('../../_helpers/wrapRoute'); const auth = require('../../passport'); +const { User } = require('../../models'); // Preload user profile on routes with ':username' router.param('username', async function (req, res, next, username) { diff --git a/api/src/routes/api/stats.js b/api/src/routes/api/stats.js index 322fac6..a77480c 100644 --- a/api/src/routes/api/stats.js +++ b/api/src/routes/api/stats.js @@ -1,9 +1,7 @@ const router = require('express').Router(); -const mongoose = require('mongoose'); const { DateTime } = require('luxon'); -const Track = mongoose.model('Track'); -const User = mongoose.model('User'); +const { Track, User } = require('../../models'); const wrapRoute = require('../../_helpers/wrapRoute'); const auth = require('../../passport'); diff --git a/api/src/routes/api/tags.js b/api/src/routes/api/tags.js index b357df2..05f51a5 100644 --- a/api/src/routes/api/tags.js +++ b/api/src/routes/api/tags.js @@ -1,7 +1,6 @@ const router = require('express').Router(); -const mongoose = require('mongoose'); -const Track = mongoose.model('Track'); const wrapRoute = require('../../_helpers/wrapRoute'); +const { Track } = require('../../models'); // return a list of tags router.get( diff --git a/api/src/routes/api/tracks.js b/api/src/routes/api/tracks.js index 9f6065f..a03654d 100644 --- a/api/src/routes/api/tracks.js +++ b/api/src/routes/api/tracks.js @@ -1,15 +1,12 @@ const fs = require('fs'); const path = require('path'); const router = require('express').Router(); -const mongoose = require('mongoose'); -const Track = mongoose.model('Track'); -const Comment = mongoose.model('Comment'); -const User = mongoose.model('User'); +const { Track, User, Comment } = require('../../models'); const busboy = require('connect-busboy'); const auth = require('../../passport'); const { normalizeUserAgent, buildObsver1 } = require('../../logic/tracks'); const wrapRoute = require('../../_helpers/wrapRoute'); -const {PROCESSING_OUTPUT_DIR} = require('../../paths') +const { PROCESSING_OUTPUT_DIR } = require('../../paths'); function preloadByParam(target, getValueFromParam) { return async (req, res, next, paramValue) => { @@ -80,7 +77,13 @@ router.get( } 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(), ]); @@ -183,30 +186,30 @@ router.post( // TODO: Stream into temporary file, then move it later. 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({ ...trackBody, author: req.user, - public: public == null ? req.user.areTracksVisibleForAll : Boolean(trackBody.public) - }) - track.customizedTitle = track.title != null + public: public == null ? req.user.areTracksVisibleForAll : Boolean(trackBody.public), + }); + track.customizedTitle = track.title != null; track.slugify(); if (fileBody) { - await track.validateFileBodyUniqueness(fileBody) + await track.validateFileBodyUniqueness(fileBody); track.uploadedByUserAgent = normalizeUserAgent(req.headers['user-agent']); 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) { await track.queueProcessing(); } - await track.autoGenerateTitle() + await track.autoGenerateTitle(); return res.json({ track: track.toJSONFor(req.user) }); }), @@ -237,40 +240,43 @@ router.put( 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') { track.title = (trackBody.title || '').trim() || null; - track.customizedTitle = track.title != null + track.customizedTitle = track.title != null; } if (typeof trackBody.description !== 'undefined') { track.description = (trackBody.description || '').trim() || null; } - let process = false + let process = false; if (trackBody.public != null) { const public = Boolean(trackBody.public); - process |= public !== track.public - track.public = public + process |= public !== track.public; + track.public = public; } if (fileBody) { - await track.validateFileBodyUniqueness(fileBody) + await track.validateFileBodyUniqueness(fileBody); track.originalFileName = fileInfo.body ? fileInfo.body.filename : track.slug + '.csv'; track.uploadedByUserAgent = normalizeUserAgent(req.headers['user-agent']); - await track.writeToOriginalFile(fileBody) - process = true + await track.writeToOriginalFile(fileBody); + process = true; } await track.save(); if (process) { - await track.queueProcessing() + await track.queueProcessing(); } - await track.autoGenerateTitle() + await track.autoGenerateTitle(); return res.json({ track: track.toJSONFor(req.user) }); }), @@ -299,18 +305,17 @@ router.get( return res.sendStatus(403); } - await req.track - .populate({ - path: 'comments', - populate: { - path: 'author', + await req.track.populate({ + path: 'comments', + populate: { + path: 'author', + }, + options: { + sort: { + createdAt: 'asc', }, - options: { - sort: { - createdAt: 'asc', - }, - }, - }); + }, + }); return res.json({ comments: req.track.comments.map(function (comment) { @@ -362,33 +367,33 @@ router.get( allMeasurements: 'all_measurements.json', confirmedMeasurements: 'confirmed_measurements.json', track: 'track.json', - } + }; if (!req.track.isVisibleTo(req.user)) { return res.sendStatus(403); } - const result = {} + const result = {}; 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 { - stats = await fs.promises.stat(filePath) - } catch(err) { - continue + stats = await fs.promises.stat(filePath); + } catch (err) { + continue; } if (!stats.isFile()) { // file does not exist (yet) - continue + continue; } - const content = await fs.promises.readFile(filePath) - const contentJson = JSON.parse(content) + const content = await fs.promises.readFile(filePath); + const contentJson = JSON.parse(content); - result[key] = contentJson + result[key] = contentJson; } return res.json(result); @@ -404,8 +409,8 @@ router.get( 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;