{"size": 1313, "ext": "js", "max_stars_repo_path": "src/models/address.model.js", "max_stars_repo_name": "galaxyme114/habitat-api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/models/address.model.js", "max_issues_repo_name": "galaxyme114/habitat-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/models/address.model.js", "max_forks_repo_name": "galaxyme114/habitat-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 17.9863013699, "max_line_length": 57, "alphanum_fraction": 0.5712109673}
const mongoose = require('mongoose');
const { toJSON, paginate } = require('./plugins');

const addressSchema = mongoose.Schema(
  {
    latitude: {
      type: Number,
      required: true,
    },
    longitude: {
      type: Number,
      required: true,
    },
    country: {
      type: String,
      required: true,
    },
    locality: {
      type: String,
    },
    postal_code: {
      type: String,
    },
    route: {
      type: String,
    },
    street_number: {
      type: String,
    },
    state: {
      type: String,
    },
  },
  {
    timestamps: true,
    toJSON: {
      virtuals: true,
      versionKey: false,
    },
  }
);

// add plugin that converts mongoose to json
addressSchema.plugin(toJSON);
addressSchema.plugin(paginate);

addressSchema.virtual('fullAddress').get(function () {
  let fullAddress = '';
  if (this.street_number) {
    fullAddress += this.street_number;
  }
  if (this.route) {
    fullAddress += ` ${this.route},`;
  }
  if (this.locality) {
    fullAddress += ` ${this.locality}`;
  }
  if (this.state) {
    fullAddress += ` ${this.state}`;
  }
  if (this.postal_code) {
    fullAddress += ` ${this.postal_code}`;
  }
  return fullAddress;
});

/**
 * @typedef Address
 */
const Address = mongoose.model('Address', addressSchema);

module.exports = Address;
