feat: include recordedAt and numEvents in script/reconstruct...

This commit is contained in:
Paul Bienkowski 2020-12-01 19:22:32 +01:00
parent 2ffb9b7851
commit 460c114301
2 changed files with 27 additions and 1 deletions

View file

@ -52,8 +52,25 @@ async function main() {
console.log('Rebuilding', track.title, 'with', track.trackData.points.length, 'data points.'); console.log('Rebuilding', track.title, 'with', track.trackData.points.length, 'data points.');
track.body = buildObsver1(track.trackData.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();
} }
} }

View file

@ -10,6 +10,7 @@ const schema = new mongoose.Schema(
body: String, body: String,
visible: Boolean, visible: Boolean,
uploadedByUserAgent: String, uploadedByUserAgent: String,
recordedAt: Date,
numEvents: { type: Number, default: 0 }, numEvents: { type: Number, default: 0 },
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }], comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
@ -50,6 +51,7 @@ class Track extends mongoose.Model {
} }
toJSONFor(user, include) { toJSONFor(user, include) {
const seePrivateFields = user && user._id.equals(this.author._id);
return { return {
slug: this.slug, slug: this.slug,
title: this.title, title: this.title,
@ -60,6 +62,13 @@ class Track extends mongoose.Model {
visible: this.visible, visible: this.visible,
author: this.author.toProfileJSONFor(user), author: this.author.toProfileJSONFor(user),
...(include && include.body ? { body: this.body } : {}), ...(include && include.body ? { body: this.body } : {}),
...(seePrivateFields
? {
uploadedByUserAgent: this.uploadedByUserAgent,
recordedAt: this.recordedAt,
numEvents: this.numEvents,
}
: {}),
}; };
} }
} }