Source

api/interest/SampleInterests.ts

  1. import moment from 'moment';
  2. import faker from 'faker';
  3. import { InterestTypes } from './InterestTypeCollection';
  4. import { Interests } from './InterestCollection';
  5. import slugify, { Slugs } from '../slug/SlugCollection';
  6. /**
  7. * Creates an InterestType with a unique slug and returns its docID.
  8. * @returns { String } The docID of the newly generated InterestType.
  9. * @memberOf api/interest
  10. */
  11. export const makeSampleInterestType = (): string => {
  12. const name = faker.lorem.word();
  13. const slug = `${name}-type-${moment().format('YYYY-MM-DD-HH-mm-ss-SSSSS')}`;
  14. const description = faker.lorem.paragraph();
  15. // console.log('makeSampleInterestType', name, slug, description);
  16. return InterestTypes.define({ name, slug, description });
  17. };
  18. /**
  19. * Creates an Interest with a unique slug and returns its docID.
  20. * Also creates a new InterestType.
  21. * @returns { String } The docID for the newly generated Interest.
  22. * @memberOf api/interest
  23. */
  24. export const makeSampleInterest = (): string => {
  25. const interestType = makeSampleInterestType();
  26. const name = faker.lorem.word();
  27. const slug = slugify(`${name}-${moment().format('YYYY-MM-DD-HH-mm-ss-SSSSS')}`);
  28. const description = faker.lorem.paragraph();
  29. // console.log('makeSampleInterest', { name, slug, description, interestType });
  30. return Interests.define({ name, slug, description, interestType });
  31. };
  32. /**
  33. * Returns an array of interestIDs.
  34. * @param {number} numInterests the number of interestIDs. Defaults to 1.
  35. * @returns {string[]}
  36. */
  37. export const makeSampleInterestArray = (numInterests = 1): string[] => {
  38. const retVal = [];
  39. for (let i = 0; i < numInterests; i++) {
  40. retVal.push(makeSampleInterest());
  41. }
  42. return retVal;
  43. };
  44. /**
  45. * Returns an array of defined Interest slugs.
  46. * @param numInterests the number of Interests to define. Defaults to 1.
  47. * @return {string[]} An array of defined Interest Slugs.
  48. */
  49. export const makeSampleInterestSlugArray = (numInterests = 1): string[] => {
  50. const ids = makeSampleInterestArray(numInterests);
  51. return ids.map((id) => {
  52. const doc = Interests.findDoc(id);
  53. return Slugs.getNameFromID(doc.slugID);
  54. });
  55. };