Source

api/skill/SkillCollection.js

  1. import SimpleSchema from 'simpl-schema';
  2. import { slugify, Slugs } from '../slug/SlugCollection';
  3. import BaseSlugCollection from '../base/BaseSlugCollection';
  4. /** @namespace api/skill */
  5. /**
  6. * Represents a specific Skill, such as "Software Engineering".
  7. * @extends api/base.BaseSlugCollection
  8. * @memberOf api/skill
  9. */
  10. class SkillCollection extends BaseSlugCollection {
  11. /**
  12. * Creates the Skill collection.
  13. */
  14. constructor() {
  15. super('Skill', new SimpleSchema({
  16. name: { type: String },
  17. slugID: { type: SimpleSchema.RegEx.Id },
  18. description: { type: String },
  19. }));
  20. }
  21. /**
  22. * Defines a new Skill and its associated Slug.
  23. * @example
  24. * Skills.define({ name: 'Software Engineering',
  25. * description: 'Methods for group development of large, high quality software systems',
  26. * });
  27. * @param { Object } description Object with keys name, slug, description.
  28. * Slug must be previously undefined.
  29. * @throws {Meteor.Error} If the Skill definition includes a defined slug.
  30. * @returns The newly created docID.
  31. */
  32. define({
  33. name,
  34. description,
  35. }) {
  36. const slug = slugify(name); // we automatically build the slug from the name.
  37. // Get SlugID, throw error if found.
  38. const slugID = Slugs.define({ name: slug });
  39. // Define the Skill and get its ID
  40. const SkillID = this._collection.insert({
  41. name, description, slugID,
  42. });
  43. // Connect the Slug to this Skill
  44. Slugs.updateEntityID(slugID, SkillID);
  45. return SkillID;
  46. }
  47. /**
  48. * Update an Skill.
  49. * @param docID The docID to be updated.
  50. * @param name The new name (optional).
  51. * @param description The new description (optional).
  52. * @throws { Meteor.Error } If docID is not defined.
  53. */
  54. update(docID, {
  55. name, description,
  56. }) {
  57. this.assertDefined(docID);
  58. const updateData = {};
  59. if (name) {
  60. updateData.name = name;
  61. }
  62. if (description) {
  63. updateData.description = description;
  64. }
  65. this._collection.update(docID, { $set: updateData });
  66. }
  67. /**
  68. * Remove the Skill.
  69. * @param instance The docID or slug of the entity to be removed.
  70. * @throws { Meteor.Error } If Skill is associated with any Challenge.
  71. */
  72. removeIt(instance) {
  73. const docID = this.getID(instance);
  74. // Check that this Skill is not referenced by any Team, Developer, or Challenge.
  75. // OK, clear to delete.
  76. super.removeIt(docID);
  77. }
  78. /**
  79. * Returns an object representing the given Skill.
  80. * @param docID {string} the ID of the Skill.
  81. * @return {{name: *, description: *}}
  82. */
  83. dumpOne(docID) {
  84. const doc = this.findDoc(docID);
  85. const { name, description } = doc;
  86. return { name, description };
  87. }
  88. }
  89. /**
  90. * Singleton instance of the SkillCollection.
  91. * @type {api/skill.SkillCollection}
  92. * @memberOf api/skill
  93. */
  94. export const Skills = new SkillCollection();