share-link.js 923 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // disable no-return-await for model functions
  2. /* eslint-disable no-return-await */
  3. const mongoose = require('mongoose');
  4. const uniqueValidator = require('mongoose-unique-validator');
  5. const mongoosePaginate = require('mongoose-paginate-v2');
  6. const ObjectId = mongoose.Schema.Types.ObjectId;
  7. /*
  8. * define schema
  9. */
  10. const schema = new mongoose.Schema({
  11. relatedPage: {
  12. type: ObjectId,
  13. ref: 'Page',
  14. required: true,
  15. index: true,
  16. },
  17. expiredAt: { type: Date },
  18. description: { type: String },
  19. createdAt: { type: Date, default: Date.now, required: true },
  20. });
  21. schema.plugin(mongoosePaginate);
  22. schema.plugin(uniqueValidator);
  23. module.exports = function(crowi) {
  24. schema.methods.isExpired = function() {
  25. if (this.expiredAt == null) {
  26. return false;
  27. }
  28. return this.expiredAt.getTime() < new Date().getTime();
  29. };
  30. const model = mongoose.model('ShareLink', schema);
  31. return model;
  32. };