 
     if not email
 
-      callback new KodingError "E-mail is not provided!"
+      callback new KodingError 'E-mail is not provided!'
 
     else
 
-      @one {username, action}, (err, confirmation)=>
+      @one { username, action }, (err, confirmation) =>
 
         if confirmation and isAlive confirmation
           if resendIfExists
-            confirmation.sendEmail {username, subject, firstName, action}, callback
+            confirmation.sendEmail { username, subject, firstName, action }, callback
           else
             callback if err then err else new PINExistsError \
-              "PIN exists and not expired,
-              try again after 20 min for new PIN."
+              'PIN exists and not expired,
+              try again after 20 min for new PIN.'
 
           return
 
         # Remove all waiting pins for given action and email
-        @remove {username, action}, (err, count)=>
+        @remove { username, action }, (err, count) =>
 
           return console.warn err  if err
 
           if count > 0 then console.log "#{count} expired PIN removed."
-          else console.log "No such waiting PIN found."
+          else console.log 'No such waiting PIN found.'
 
-          @createNewPin {username, action, email}, (err, confirmation)->
+          @createNewPin { username, action, email }, (err, confirmation) ->
             return callback err  if err
 
-            confirmation.sendEmail {username, subject, firstName, action}, callback
+            confirmation.sendEmail { username, subject, firstName, action }, callback
 
 
-  @confirmByPin = (options, callback)->
+  @confirmByPin = (options, callback) ->
 
-    {pin, email, action, username} = options
+    { pin, email, action, username } = options
 
     email = emailsanitize email
 
-    @one {email, action, pin, username}, (err, confirmation)->
+    @one { email, action, pin, username }, (err, confirmation) ->
 
       return callback err  if err
 

        callback null, false


  sendEmail: ({subject, firstName, username, action}, callback)->

    Tracker.track username, {to:@email, subject}, {firstName, @pin, action}
    callback null


  @invalidatePin = (options, callback)->
    {email, action, username} = options

    email = emailsanitize email

    @one {email, action, username}, (err, verify)->
      return callback err  if err
      return callback new KodingError "token not found" unless verify
      verify.remove()
      callback null


  @createNewPin = (options, callback)->
    {username, action, email} = options
    pin = hat 16

    email = emailsanitize email

    confirmation = new JVerificationToken {username, action, email, pin}
    confirmation.save (err)-> callback err, confirmation

# coffeelint: disable=no_implicit_braces
{ Module } = require 'jraphical'
KodingError = require '../error'

emailsanitize = require './user/emailsanitize'

class PINExistsError extends Error
  constructor:(message) ->
    return new PINExistsError(message) unless this instanceof PINExistsError
    Error.call this
    @message = message
    @name = 'PINExistsError'

module.exports = class JVerificationToken extends Module

  { secure } = require 'bongo'
  crypto     = require 'crypto'
  hat        = require 'hat'
  Tracker    = require './tracker'

  @share()

