From 460c114301121573acf9d0a4e697c4f5a2722fff Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Tue, 1 Dec 2020 19:22:32 +0100 Subject: [PATCH] feat: include recordedAt and numEvents in script/reconstruct... --- scripts/reconstruct-obsver1-body.js | 19 ++++++++++++++++++- src/models/Track.js | 9 +++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/scripts/reconstruct-obsver1-body.js b/scripts/reconstruct-obsver1-body.js index da97f02..689734f 100644 --- a/scripts/reconstruct-obsver1-body.js +++ b/scripts/reconstruct-obsver1-body.js @@ -52,8 +52,25 @@ async function main() { console.log('Rebuilding', track.title, 'with', track.trackData.points.length, 'data points.'); track.body = buildObsver1(track.trackData.points); - await track.save(); } + + if (!track.recordedAt) { + const firstPointWithDate = track.trackData.points.find((p) => p.date && p.time); + if (firstPointWithDate) { + const [day, month, year] = firstPointWithDate.date.split('.'); + const combinedString = `${year}-${month}-${day} ${firstPointWithDate.time}.000+2000`; + const parsedDate = new Date(combinedString); + if (!isNaN(parsedDate.getDate())) { + track.recordedAt = parsedDate; + } + } + } + + if (!track.numEvents) { + track.numEvents = track.trackData.points.filter((p) => p.flag).length; + } + + await track.save(); } } diff --git a/src/models/Track.js b/src/models/Track.js index 6ddda0b..4f00222 100644 --- a/src/models/Track.js +++ b/src/models/Track.js @@ -10,6 +10,7 @@ const schema = new mongoose.Schema( body: String, visible: Boolean, uploadedByUserAgent: String, + recordedAt: Date, numEvents: { type: Number, default: 0 }, comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }], author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, @@ -50,6 +51,7 @@ class Track extends mongoose.Model { } toJSONFor(user, include) { + const seePrivateFields = user && user._id.equals(this.author._id); return { slug: this.slug, title: this.title, @@ -60,6 +62,13 @@ class Track extends mongoose.Model { visible: this.visible, author: this.author.toProfileJSONFor(user), ...(include && include.body ? { body: this.body } : {}), + ...(seePrivateFields + ? { + uploadedByUserAgent: this.uploadedByUserAgent, + recordedAt: this.recordedAt, + numEvents: this.numEvents, + } + : {}), }; } }