'use strict'; const mongoose = require('mongoose'), Schema = mongoose.Schema; const { ExportUnits, ExportJobStatus } = require('../helpers/constants'); /** * ExportJob model — tracks async CSV/GeoJSON export requests. * * Lifecycle: pending → processing → ready | error * Files are written to env.TEMP_DIR and expire after EXPORT_TTL_HOURS. * The download endpoint streams the file and then schedules deletion. */ const schema = new Schema({ owner: { type: Schema.Types.ObjectId, ref: 'User', required: true, index: true }, jobId: { type: Number, required: true }, format: { type: String, enum: ['csv', 'geojson'], required: true }, interval: { type: Number, default: null }, // GPS point thinning interval in seconds, null = all points units: { type: String, enum: Object.values(ExportUnits), default: ExportUnits.METRIC }, // output measurement system fm: { type: Boolean, default: false }, // include Flight Master / AgDisp fields when true status: { type: String, enum: Object.values(ExportJobStatus), default: ExportJobStatus.PENDING, index: true }, filePath: { type: String }, // absolute path on disk, set when ready errorMsg: { type: String }, createdAt: { type: Date, default: Date.now, index: true }, expiresAt: { type: Date } // TTL; file and record deleted after this time }); // Auto-expire documents from MongoDB after expiresAt (background cleanup) schema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 }); module.exports = mongoose.model('ExportJob', schema);