<commit_before>jraphical = require 'jraphical'

module.exports = class JIntroSnippet extends jraphical.Module

  {secure} = require 'bongo'

  @share()

  @set
    sharedMethods :
      instance    : [ "addChild", "delete", "deleteChild", "updateChild", "update" ]
      static      : [ "create", "fetchAll" ]
    schema        :
      title       :
        type      : String
        required  : yes
      expiryDate  :
        type      : String
        required  : yes
      visibility  :
        type      : String
        required  : yes
      overlay     :
        type      : String
        required  : yes
      snippets    :
        type      : Array
        required  : yes

  @create = secure (client, data, callback) ->
    return unless JIntroSnippet.checkPermission client

    introSnippet = new JIntroSnippet data
    introSnippet.save (err)=>
      return callback? err if err
      callback null, introSnippet
      console.log "new intro snippet saved to database"

  update: secure (client, data, callback) ->
    return unless data
    @checkPermission client
    @[key] = value for key, value of data
    @save()
    callback?()

  @fetchAll = secure (client, callback) ->
    JIntroSnippet.some {}, {}, (err, records) ->
      callback? err, records

  addChild: secure (client, data, callback) ->
    @checkPermission client
    @snippets.push data
    @save()
    callback?()

  deleteChild: secure (client, introId, callback) ->
    return unless introId
    @checkPermission client
    {snippets} = @
    for snippet in snippets
      if snippet?.introId is introId
        snippets.splice snippets.indexOf(snippet), 1
    @save()
    callback?()

  updateChild: secure (client, data, callback) ->
    return unless data
    @checkPermission client
    {snippets} = @
    for snippet in snippets
      if snippet?.introId is data.oldIntroId
        snippet.introId    = data.introId
        snippet.introTitle = data.introTitle
        snippet.snippet    = data.snippet
        snippet.placement  = data.placement

    @save()
    callback?()

  @checkPermission = (client) ->
    {globalFlags}  = client.connection.delegate
    status         = no
    status         = yes for flag of globalFlags when globalFlags[flag] is "super-admin"
    return status

  checkPermission: @checkPermission # is it valid?

  delete: secure (client, callback) ->
    @remove callback<commit_msg>introduction-tooltip: Added delayForNext, placement and callback to intro snippet model.<commit_after>jraphical = require 'jraphical'

module.exports = class JIntroSnippet extends jraphical.Module

  {secure} = require 'bongo'

  @share()

  @set
    sharedMethods :
      instance    : [ "addChild", "delete", "deleteChild", "updateChild", "update" ]
      static      : [ "create", "fetchAll" ]
    schema        :
      title       :
        type      : String
        required  : yes
      expiryDate  :
        type      : String
        required  : yes
      visibility  :
        type      : String
        required  : yes
      overlay     :
        type      : String
        required  : yes
      snippets    :
        type      : Array
        required  : yes

  @create = secure (client, data, callback) ->
    return unless JIntroSnippet.checkPermission client

    introSnippet = new JIntroSnippet data
    introSnippet.save (err)=>
      return callback? err if err
      callback null, introSnippet
      console.log "new intro snippet saved to database"

  update: secure (client, data, callback) ->
    return unless data
    @checkPermission client
    @[key] = value for key, value of data
    @save()
    callback?()

  @fetchAll = secure (client, callback) ->
    JIntroSnippet.some {}, {}, (err, records) ->
      callback? err, records

  addChild: secure (client, data, callback) ->
    @checkPermission client
    @snippets.push data
    @save()
    callback?()

  deleteChild: secure (client, introId, callback) ->
    return unless introId
    @checkPermission client
    {snippets} = @
    for snippet in snippets
      if snippet?.introId is introId
        snippets.splice snippets.indexOf(snippet), 1
    @save()
    callback?()

  updateChild: secure (client, data, callback) ->
    return unless data
    @checkPermission client
    {snippets} = @
    for snippet in snippets
      if snippet?.introId is data.oldIntroId
        snippet.introId      = data.introId
        snippet.introTitle   = data.introTitle
        snippet.snippet      = data.snippet
        snippet.placement    = data.placement
        snippet.delayForNext = data.delayForNext
        snippet.callback     = data.callback

    @save()
    callback?()

  @checkPermission = (client) ->
    {globalFlags}  = client.connection.delegate
    status         = no
    status         = yes for flag of globalFlags when globalFlags[flag] is "super-admin"
    return status

  checkPermission: @checkPermission # is it valid?

  delete: secure (client, callback) ->
    @remove callback
{'fraction_non_alphanumeric': 0.09136445602083751, 'fraction_numerical': 0.0004007212983370066, 'mean_word_length': 2.2842105263157895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '5446387', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1385, 'n_words': 414}