<commit_before>BaseService = require './base'
Q = require 'q'

###*
 * Creates a new ChannelService.
 * @class ChannelService
###
class ChannelService extends BaseService

  ###*
   * @const
   * @private
   * Base path for a Channels API resource endpoint
   * @type {String}
  ###
  @baseResourceEndpoint: '/channels'


  ###*
   * Retrieves the first found channel result for a given key and role.
   * If not existing, the channel will be created or the channel role will be
   * added if absent.

   * @param {String} key A unique identifier for channel within the project.
   * @param {ChannelRole} role The ChannelRole the channel must have ().
   * @return {Promise} A promise, fulfilled with an {Object} or rejected with
   *           a {SphereError}
  ###
  byKeyOrCreate: (key, role) ->

    deferred = Q.defer()

    unless key
      deferred.reject new Error 'Key is required.'
      return deferred.promise

    unless role
      deferred.reject new Error 'Role is required.'
      return deferred.promise

    @where("key=\"#{key}\"")
    .page(1).fetch()
    .then (result) =>
      if result.body.total is 1
        channel = result.body.results[0]
        if role not in channel.roles
          update =
            version: channel.version
            actions: [
              {
                action: 'addRoles'
                roles: [role]
              }
            ]
          deferred.resolve @byId(channel.id).update(update)
        else
          deferred.resolve
            statusCode: result.statusCode
            body: channel
      else
        channel =
          key: key
          roles: [role]
        deferred.resolve @save(channel)

    .fail (result) ->
      deferred.reject result

    deferred.promise

###*
 * The {@link ChannelService} service.
###
module.exports = ChannelService<commit_msg>Throw an error if multiple channels with same key were found.<commit_after>BaseService = require './base'
Q = require 'q'

###*
 * Creates a new ChannelService.
 * @class ChannelService
###
class ChannelService extends BaseService

  ###*
   * @const
   * @private
   * Base path for a Channels API resource endpoint
   * @type {String}
  ###
  @baseResourceEndpoint: '/channels'


  ###*
   * Retrieves the first found channel result for a given key and role.
   * If not existing, the channel will be created or the channel role will be
   * added if absent.

   * @param {String} key A unique identifier for channel within the project.
   * @param {ChannelRole} role The ChannelRole the channel must have ().
   * @return {Promise} A promise, fulfilled with an {Object} or rejected with
   *           a {SphereError}
  ###
  byKeyOrCreate: (key, role) ->

    deferred = Q.defer()

    unless key
      deferred.reject new Error 'Key is required.'
      return deferred.promise

    unless role
      deferred.reject new Error 'Role is required.'
      return deferred.promise

    @where("key=\"#{key}\"")
    .page(1).fetch()
    .then (result) =>
      if result.body.total is 1
        channel = result.body.results[0]
        if role not in channel.roles
          update =
            version: channel.version
            actions: [
              {
                action: 'addRoles'
                roles: [role]
              }
            ]
          deferred.resolve @byId(channel.id).update(update)
        else
          deferred.resolve
            statusCode: result.statusCode
            body: channel
      else if result.body.total is 0
        channel =
          key: key
          roles: [role]
        deferred.resolve @save(channel)
      else
        deferred.reject new Error "#{result.body.total} channels with key = '#{key}' found (key should be unique for a project)."

    .fail (result) ->
      deferred.reject result

    deferred.promise

###*
 * The {@link ChannelService} service.
###
module.exports = ChannelService
{'fraction_non_alphanumeric': 0.1006418485237484, 'fraction_numerical': 0.001797175866495507, 'mean_word_length': 2.1268057784911716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4210615', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1061, 'n_words': 364}