<commit_before>
jraphical       = require 'jraphical'
JCredentialData = require './credentialdata'
JName           = require '../name'
JUser           = require '../user'
JGroup          = require '../group'

# TODO Credential relations ~g

module.exports = class JStackTemplate extends jraphical.Module

  KodingError        = require '../../error'

  {Inflector, secure, ObjectId, signature, daisy} = require 'bongo'
  {Relationship}     = jraphical
  {permit}           = require '../group/permissionset'
  Validators         = require '../group/validators'

  @trait __dirname, '../../traits/protected'

  @share()

  @set

    softDelete        : yes

    permissions       :

      'create stack template'     : []
      'list stack templates'      : []

      'delete own stack template' : []
      'update own stack template' : []

      'delete stack template'     : []
      'update stack template'     : []

    sharedMethods     :

      static          :
        create        :
          (signature Object, Function)
        some          : [
          (signature Object, Function)
          (signature Object, Object, Function)
        ]

      instance        :
        delete        :
          (signature Function)
        setAccess     :
          (signature String, Function)
        update        :
          (signature Object, Function)

    sharedEvents      :
      static          : [ ]
      instance        : [
        { name : 'updateInstance' }
      ]

    schema            :

      rules           : [ Object ]
      domains         : [ Object ]
      machines        : [ Object ]
      extras          : [ Object ]

      connections     : [ Object ]

      title           :
        type          : String
        required      : yes

      description     : String
      config          : Object

      accessLevel     :
        type          : String
        enum          : ["Wrong level specified!",
          ["private", "group", "public"]
        ]
        default       : "private"

      originId        :
        type          : ObjectId
        required      : yes

      meta            : require 'bongo/bundles/meta'

      group           : String

      template        : String


  @create = permit 'create stack template',

    success: (client, data, callback)->

      { delegate } = client.connection
      { profile:{nickname} } = delegate

      { title, description, config } = data
      return callback new KodingError "Title required."  unless title

      template = new JStackTemplate {
        title, description, config
        rules         : data.rules       ? []
        domains       : data.domains     ? []
        machines      : data.machines    ? []
        extras        : data.extras      ? []
        connections   : data.connections ? []
        accessLevel   : data.accessLevel ? "private"
        group         : client.context.group
        originId      : delegate.getId()
      }

      template.save (err)->
        if err
          callback new KodingError "Failed to save stack template", err<commit_msg>JStackTemplate: keep associated credentials in JStackTemplate<commit_after>
jraphical       = require 'jraphical'
JCredentialData = require './credentialdata'
JName           = require '../name'
JUser           = require '../user'
JGroup          = require '../group'

# TODO Credential relations ~g

module.exports = class JStackTemplate extends jraphical.Module

  KodingError        = require '../../error'

  {Inflector, secure, ObjectId, signature, daisy} = require 'bongo'
  {Relationship}     = jraphical
  {permit}           = require '../group/permissionset'
  Validators         = require '../group/validators'

  @trait __dirname, '../../traits/protected'

  @share()

  @set

    softDelete        : yes

    permissions       :

      'create stack template'     : []
      'list stack templates'      : []

      'delete own stack template' : []
      'update own stack template' : []

      'delete stack template'     : []
      'update stack template'     : []

    sharedMethods     :

      static          :
        create        :
          (signature Object, Function)
        some          : [
          (signature Object, Function)
          (signature Object, Object, Function)
        ]

      instance        :
        delete        :
          (signature Function)
        setAccess     :
          (signature String, Function)
        update        :
          (signature Object, Function)

    sharedEvents      :
      static          : [ ]
      instance        : [
        { name : 'updateInstance' }
      ]

    schema            :

      rules           : [ Object ]
      domains         : [ Object ]
      machines        : [ Object ]
      extras          : [ Object ]

      connections     : [ Object ]

      title           :
        type          : String
        required      : yes

      description     : String
      config          : Object

      accessLevel     :
        type          : String
        enum          : ["Wrong level specified!",
          ["private", "group", "public"]
        ]
        default       : "private"

      originId        :
        type          : ObjectId
        required      : yes

      meta            : require 'bongo/bundles/meta'

      group           : String

      template        : String

      # Public keys of JCredentials
      credentials     : [ String ]


  @create = permit 'create stack template',

    success: (client, data, callback)->

      { delegate } = client.connection
      { profile:{nickname} } = delegate

      { title, description, config } = data
      return callback new KodingError "Title required."  unless title

      template = new JStackTemplate {
        title, description, config
        rules         : data.rules       ? []
        domains       : data.domains     ? []
        machines      : data.machines    ? []
        extras        : data.extras      ? []
        connections   : data.connections ? []
        accessLevel   : data.accessLevel ? "private"
        group         : client.context.group
        originId      : delegate.getId()
      }

      template.save (err)->
        if err
          callback new KodingError "Failed to save stack template", err
{'fraction_non_alphanumeric': 0.09326342548458849, 'fraction_numerical': 0.0, 'mean_word_length': 1.3889943074003794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7661527', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1544, 'n_words': 424}