{"size": 3593, "ext": "js", "max_stars_repo_path": "hub-linkedin-learning-connector/utils/card-objects.js", "max_stars_repo_name": "vmware/connectors-workspace-one", "max_stars_repo_stars_event_min_datetime": "2018-01-18T15:44:01.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-24T06:02:22.000Z", "max_issues_repo_path": "hub-linkedin-learning-connector/utils/card-objects.js", "max_issues_repo_name": "vmware/connectors-workspace-one", "max_issues_repo_issues_event_min_datetime": "2017-12-19T17:43:10.000Z", "max_issues_repo_issues_event_max_datetime": "2021-11-17T11:10:07.000Z", "max_forks_repo_path": "hub-linkedin-learning-connector/utils/card-objects.js", "max_forks_repo_name": "vmware/connectors-workspace-one", "max_forks_repo_forks_event_min_datetime": "2017-12-19T17:33:16.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-06T22:20:09.000Z"}
{"max_stars_count": 17, "max_issues_count": 171, "max_forks_count": 21, "avg_line_length": 27.015037594, "max_line_length": 96, "alphanum_fraction": 0.5794600612}
/*
 * Create card objects form linkedIn learning response.
 * @param res - LinkedIn learning response object.
 */
const { v4: uuid } = require('uuid')
const sha1 = require('sha1')
const utils = require('../utils/utility')

const forCardObjects = (res) => {
  return res.map(course => forSingleCourse(course))
}

/*
 * Create single bot object form cousre response.
 * @param courseRes - LinkedIn learning response object.
 */
const forSingleCourse = (courseRes) => {
  const backendId = courseRes.urn
  return {
    id: uuid(),
    name: 'LinkedIn Learning',
    creation_date: new Date().toISOString(),
    backend_id: backendId,
    hash: sha1(backendId),
    header: {
      title: 'LinkedIn Learning: New course available',
      subtitle: [
        'View course'
      ],
      links: {
        title: courseRes.details.urls.webLaunch,
        subtitle: [
          courseRes.details.urls.webLaunch
        ]
      }
    },
    image: {
      href: utils.LINKEDIN_LOGO_PATH
    },
    body: {
      fields: [
        {
          type: 'GENERAL',
          title: 'Title',
          description: courseRes.title.value
        },
        {
          type: 'GENERAL',
          title: 'Instructor',
          description: getContributerNames(courseRes.details.contributors)
        },
        {
          type: 'GENERAL',
          title: 'Duration',
          description: convertSecondsToReadableFormat(courseRes.details.timeToComplete.duration)
        },
        {
          type: 'SECTION',
          title: 'About Course',
          items: [
            {
              type: 'GENERAL',
              title: 'Description',
              description: courseRes.details.description.value
            },
            {
              type: 'GENERAL',
              title: 'Category',
              description: getCategories(courseRes.details.classifications)
            },
            {
              type: 'GENERAL',
              title: 'Released',
              description: new Date(courseRes.details.publishedAt).toISOString()
            },
            {
              type: 'GENERAL',
              title: 'Updated at',
              description: new Date(courseRes.details.lastUpdatedAt).toISOString()
            },
            {
              type: 'GENERAL',
              title: 'Difficulty',
              description: courseRes.details.level
            }
          ]
        }
      ]
    }
  }
}

/*
 * Helper method to get contributer name from response
 * @param contributers - contributers response.
 */
const getContributerNames = (contributers) => {
  return contributers.map(contributer => getSingleAuthorName(contributer)).join(', ')
}

const getSingleAuthorName = (contributer) => {
  return contributer.name.value
}

/*
 * Helper method to get course categories from response
 * @param categories - course category response.
 */
const getCategories = (categories) => {
  return categories.map(category => getSingleCategory(category)).join(', ')
}

const getSingleCategory = (category) => {
  return category.associatedClassification.name.value
}

/*
 * Helper method to convert seconds into Day hour format similar to linkedIn website
 * @param seconds - duration of course.
 */
const convertSecondsToReadableFormat = (seconds) => {
  const days = Math.floor(seconds / (24 * 60 * 60))
  seconds -= days * (24 * 60 * 60)
  const hours = Math.floor(seconds / (60 * 60))
  seconds -= hours * (60 * 60)
  const minutes = Math.floor(seconds / (60))
  return ((days > 0) ? (days + ' day, ') : '') + hours + 'h ' + minutes + 'm'
}

module.exports = {
  forCardObjects
}
