Source

api/stuff/StuffCollection.js

  1. import { Meteor } from 'meteor/meteor';
  2. import SimpleSchema from 'simpl-schema';
  3. import { check } from 'meteor/check';
  4. import { _ } from 'lodash';
  5. import { Roles } from 'meteor/alanning:roles';
  6. import BaseCollection from '../base/BaseCollection';
  7. /**
  8. * **deprecated**
  9. * @namespace api/stuff
  10. */
  11. /**
  12. * The different conditions for stuff.
  13. * @type {string[]}
  14. * @memberOf api/stuff
  15. */
  16. export const stuffConditions = ['excellent', 'good', 'fair', 'poor'];
  17. /**
  18. * The publication names for the StuffCollection.
  19. * @type {{stuffAdmin: string, stuff: string}}
  20. * @memberOf api/stuff
  21. */
  22. export const stuffPublications = {
  23. stuff: 'Stuff',
  24. stuffAdmin: 'StuffAdmin',
  25. };
  26. /**
  27. * The StuffCollection **deprecated**.
  28. * @memberOf api/stuff
  29. * @extends api/base.BaseCollection
  30. */
  31. class StuffCollection extends BaseCollection {
  32. constructor() {
  33. super('Stuffs', new SimpleSchema({
  34. name: String,
  35. quantity: Number,
  36. owner: String,
  37. condition: {
  38. type: String,
  39. allowedValues: stuffConditions,
  40. defaultValue: 'good',
  41. },
  42. }));
  43. }
  44. /**
  45. * Defines a new Stuff item.
  46. * @param name the name of the item.
  47. * @param quantity how many.
  48. * @param owner the owner of the item.
  49. * @param condition the condition of the item.
  50. * @return {String} the docID of the new document.
  51. */
  52. define({ name, quantity, owner, condition }) {
  53. const docID = this._collection.insert({
  54. name,
  55. quantity,
  56. owner,
  57. condition,
  58. });
  59. return docID;
  60. }
  61. /**
  62. * Updates the given document.
  63. * @param docID the id of the document to update.
  64. * @param name the new name (optional).
  65. * @param quantity the new quantity (optional).
  66. * @param condition the new condition (optional).
  67. */
  68. update(docID, { name, quantity, condition }) {
  69. const updateData = {};
  70. if (name) {
  71. updateData.name = name;
  72. }
  73. // if (quantity) { NOTE: 0 is falsy so we need to check if the quantity is a number.
  74. if (_.isNumber(quantity)) {
  75. updateData.quantity = quantity;
  76. }
  77. if (condition) {
  78. updateData.condition = condition;
  79. }
  80. this._collection.update(docID, { $set: updateData });
  81. }
  82. /**
  83. * A stricter form of remove that throws an error if the document or docID could not be found in this collection.
  84. * @param { String | Object } name A document or docID in this collection.
  85. * @returns true
  86. */
  87. removeIt(name) {
  88. const doc = this.findDoc(name);
  89. check(doc, Object);
  90. this._collection.remove(doc._id);
  91. return true;
  92. }
  93. /**
  94. * Default publication method for entities.
  95. * It publishes the entire collection for admin and just the stuff associated to an owner.
  96. */
  97. publish() {
  98. if (Meteor.isServer) {
  99. // get the StuffCollection instance.
  100. const instance = this;
  101. /** This subscription publishes only the documents associated with the logged in user */
  102. Meteor.publish(stuffPublications.stuff, function publish() {
  103. if (this.userId) {
  104. const username = Meteor.users.findOne(this.userId).username;
  105. return instance._collection.find({ owner: username });
  106. }
  107. return this.ready();
  108. });
  109. /** This subscription publishes all documents regardless of user, but only if the logged in user is the Admin. */
  110. Meteor.publish(stuffPublications.stuffAdmin, function publish() {
  111. if (this.userId && Roles.userIsInRole(this.userId, 'admin')) {
  112. return instance._collection.find();
  113. }
  114. return this.ready();
  115. });
  116. }
  117. }
  118. /**
  119. * Subscription method for stuff owned by the current user.
  120. */
  121. subscribeStuff() {
  122. if (Meteor.isClient) {
  123. return Meteor.subscribe(stuffPublications.stuff);
  124. }
  125. return null;
  126. }
  127. /**
  128. * Subscription method for admin users.
  129. * It subscribes to the entire collection.
  130. */
  131. subscribeStuffAdmin() {
  132. if (Meteor.isClient) {
  133. return Meteor.subscribe(stuffPublications.stuffAdmin);
  134. }
  135. return null;
  136. }
  137. }
  138. /**
  139. * Singleton instance of the StuffCollection, **deprecated**.
  140. * @type {api/stuff.StuffCollection}
  141. * @memberOf api/stuff
  142. */
  143. export const Stuffs = new StuffCollection();