// resource-visit.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const { GeoIp } = require('./lib/geo-types');

const ResourceVisitSchema = new Schema({
  created: { type: Date, required: true, default: Date.now, index: -1, expires: '7d' },
  resourceType: { type: String, enum: ['Page','Post','User'], required: true },
  resource: { type: Schema.ObjectId, required: true, index: 1, ref: 'Link' },
  user: { type: Schema.ObjectId, ref: 'User' },
  geoip: { type: GeoIp },
});

ResourceVisitSchema.index({
  user: 1,
}, {
  partialFilterExpression: {
    user: { $exists: true },
  },
  name: 'resource_visits_for_user',
});

module.exports = mongoose.model('ResourceVisit', ResourceVisitSchema);