Source: reminder.js

  1. /** @module Models */
  2. const ObjectID = require('bson-objectid');
  3. /**
  4. * Class that represents a Reminder used when creating a {@link Task}. The reminder
  5. * will go off at {propertiesOrQty} {units} before the date set for the {@link Task}. Or at the
  6. * specified {propertiesOrQty.trigger}.
  7. * @class
  8. * @param {Object|Number} propertiesOrQty - Properties of the {@link Reminder} or the quantity of
  9. * time before the start date in which the reminder will trigger
  10. * @param {string=} propertiesOrQty.id - Object ID of the Reminder, only defined when this is an
  11. * instantiation of a pre-existing reminder. On new reminders this must be empty in order
  12. * to generate a new ObjectID
  13. * @param {string=} propertiesOrQty.trigger - Custom trigger in the pre-defined format
  14. * /^TRIGGER:-?PT[0-9]+[SMHD]$/
  15. * @param {Reminder.TimeUnit} unit - Unit of time
  16. */
  17. function Reminder(propertiesOrQty, unit) {
  18. if (unit === undefined) {
  19. this.id = propertiesOrQty.id || ObjectID();
  20. this.trigger = propertiesOrQty.trigger || 'TRIGGER:PT0S';
  21. } else {
  22. let unitChar;
  23. if (unit === Reminder.TimeUnit.SECONDS) {
  24. unitChar = 'S';
  25. } else if (unit === Reminder.TimeUnit.MINUTES) {
  26. unitChar = 'M';
  27. } else if (unit === Reminder.TimeUnit.HOURS) {
  28. unitChar = 'H';
  29. } else if (unit === Reminder.TimeUnit.DAYS) {
  30. unitChar = 'D';
  31. } else {
  32. throw new Reminder.InvalidUnitError();
  33. }
  34. this.id = ObjectID();
  35. this.trigger = `TRIGGER:-PT${propertiesOrQty}${unitChar}`;
  36. }
  37. }
  38. /**
  39. * Enum for the time unit used in the creation of a reminder
  40. * @readonly
  41. * @enum {Number}
  42. */
  43. Reminder.TimeUnit = {
  44. /** Used when the reminder is set in seconds */
  45. SECONDS: 0,
  46. /** Used when the reminder is set in minutes */
  47. MINUTES: 1,
  48. /** Used when the reminder is set in hours */
  49. HOURS: 2,
  50. /** Used when the reminder is set in days */
  51. DAYS: 3,
  52. };
  53. /**
  54. * Error thrown when a call to create a {@link Reminder} is made
  55. * with an invalid time unit parameter.
  56. * @class
  57. * @ignore
  58. */
  59. Reminder.InvalidUnitError = class extends Error {
  60. constructor(unit) {
  61. super(`The unit "${unit}" is invalid`);
  62. }
  63. };
  64. module.exports = Reminder;
  65. // Reminder structure from api response
  66. // {
  67. // id: '5c342818e4b04a7154bfea3a',
  68. // trigger: 'TRIGGER:PT0S',
  69. // }