Source

api/slackbot/SlackUserCollection.js

  1. import SimpleSchema from 'simpl-schema';
  2. import { Meteor } from 'meteor/meteor';
  3. import BaseCollection from '../base/BaseCollection';
  4. import { Slugs } from '../slug/SlugCollection';
  5. /**
  6. * SlackUserCollection stores the username, slackUser and dmChannel tuple for the users.
  7. * @memberOf api/slackbot
  8. * @extends api/base.BaseCollection
  9. */
  10. class SlackUserCollection extends BaseCollection {
  11. constructor() {
  12. super('SlackUser', new SimpleSchema({
  13. username: { type: String },
  14. slackUser: { type: String },
  15. dmChannel: { type: String },
  16. }));
  17. }
  18. /**
  19. * Defines a new tuple.
  20. * @param username {string} the user's name (e.g. their email address).
  21. * @param slackUser {string} the user's slack user.
  22. * @param dmChannel {string} the user's direct message channel id.
  23. */
  24. define({ username, slackUser, dmChannel }) {
  25. if (!Slugs.isDefined(username)) {
  26. throw new Meteor.Error(`${username} is not a defined user.`);
  27. }
  28. this._collection.insert({ username, slackUser, dmChannel });
  29. }
  30. /**
  31. * Removes the tuple.
  32. * @param name {string} the name/username.
  33. * @return {boolean}
  34. */
  35. removeIt(name) {
  36. return super.removeIt(name);
  37. }
  38. /**
  39. * Returns an object representing the given docID.
  40. * @param docID {string} the ID of the document.
  41. * @return {{dmChannel: *, slackUser: *, username: *}}
  42. * @throws {Meteor.Error} if docID is not defined.
  43. */
  44. dumpOne(docID) {
  45. this.assertDefined(docID);
  46. const { username, slackUser, dmChannel } = this.findDoc(docID);
  47. return { username, slackUser, dmChannel };
  48. }
  49. }
  50. /**
  51. * Singleton instance of the SlackUserCollection.
  52. * @type {api/slackbot.SlackUserCollection}
  53. * @memberOf api/slackbot
  54. */
  55. export const SlackUsers = new SlackUserCollection();