// core-node-request.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

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

const { RequestTokenSchema } = require('./lib/core-request-token');

/*
 * Used for authenticating responses received and gathering performance and use
 * metrics for communications with Cores.
 * 
 * When a request is created, an authentication token is generated and
 * information about the request is stored. This also provides the request ID.
 * 
 * When a resonse is received for a request, this record is fetched. The token
 * claimed status and value are checked. Information about the response is
 * recorded, and request execution time information is recorded.
 */

const CoreNodeRequestSchema = new Schema({
  created: { type: Date, default: Date.now, required: true, index: 1, expires: '7d' },
  core: { type: Schema.ObjectId, required: true, ref: 'CoreNode' },
  token: { type: RequestTokenSchema },
  method: { type: String, required: true },
  url: { type: String, required: true },
  response: {
    received: { type: Date },
    elapsed: { type: Number },
    statusCode: { type: String },
    success: { type: Boolean },
  },
});

module.exports = mongoose.model('CoreNodeRequest', CoreNodeRequestSchema);