File size: 1,392 Bytes
9a92a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Schema, model, Document, Types } from 'mongoose';

interface ILocationPoint {
  coordinates: [number, number]; // [lng, lat]
  timestamp: Date;
}

interface ILocationHistory extends Document {
  user: Types.ObjectId;
  date: string; // YYYY-MM-DD format
  locations: ILocationPoint[];
  lastLocation: {
    type: "Point";
    coordinates: [number, number];
  };
  createdAt: Date;
  updatedAt: Date;
}

const LocationHistorySchema = new Schema<ILocationHistory>({
  user: {
    type: Schema.Types.ObjectId,
    ref: "User",
    required: true
  },
  date: {
    type: String, // Store as "2025-11-25" for easy querying
    required: true
  },
  locations: [{
    coordinates: {
      type: [Number],
      required: true
    },
    timestamp: {
      type: Date,
      default: Date.now
    }
  }],
  lastLocation: {
    type: {
      type: String,
      enum: ["Point"],
      default: "Point"
    },
    coordinates: {
      type: [Number],
      required: true
    }
  }
}, { timestamps: true });

// Compound unique index: one document per user per day
LocationHistorySchema.index({ user: 1, date: 1 }, { unique: true });

// For spatial queries on last known location
LocationHistorySchema.index({ lastLocation: "2dsphere" });

// For date range queries
LocationHistorySchema.index({ date: 1 });

export default model<ILocationHistory>("LocationHistory", LocationHistorySchema);