import mongo from '../db'
import utils from '../utils'
import crypto from 'crypto'

/**
 * User :: {
 *    id: String,
 *    username: String,
 *    password: String,
 *    createdAt: String
 * }
 */

/** String -> String -> User */
function createUser(username, password) {
    return {
        "id": utils.UUID(),
        "username": username,
        "password": crypto.createHmac('sha256', password).update(password).digest('base64'),
        "createdAt": Date.now()
    }
}

/** String -> Promise(Boolean, Error) */
function userExist(username) {
    return mongo.connect()
        .then(db => db.collection('users').findOne({ username })
        .then(mongo.close(db)))
        .then(user => user != null)
}

/** User -> Promise(User, Error) */
function saveUser(user) {
    return mongo.connect()
        .then(db => db.collection('users').insertOne(user)
        .then(mongo.close(db)))
        .then(() => user)
}

/** String -> String -> Promise({id, username, token : String}, Error) */
export function register(username, password) {
    userExist(username)
        .then(exist => exist ?
            Promise.reject("The user already exist") :
            Promise.resolve(createUser(username, password)))
        .then(saveUser)
        .then(user => ({
            id: user.id,
            username: user.username,
            token: utils.genToken(user)
        }))
}

const mongo = require('../db')
const utils = require('../utils')

/**
 * User :: {
 *    id: String,
 *    username: String,
 *    password: String,
 *    createdAt: String
 * }
 */

/** String -> String -> User */
function createUser(username, password) {
    return {
        "id": utils.UUID(),
        "username": username,
        "password": crypto.createHmac('sha256', password).update(password).digest('base64'),
        "createdAt": Date.now()
    }
}

/** String -> Promise(Boolean, Error) */
function userExist(username) {
    return mongo.connect()
        .then(db => db.collection('users').findOne({ username })
        .then(mongo.close(db)))
        .then(user => user != null)
}

/** User -> Promise(User, Error) */
function saveUser(user) {
    return mongo.connect()
        .then(db => db.collection('users').insertOne(user)
        .then(mongo.close(db)))
        .then(() => user)
}

/** String -> String -> Promise({id, username, token : String}, Error) */
exports.register = function register(username, password) {
    userExist(username)
        .then(exist => exist ?
            Promise.reject("The user already exist") :
            Promise.resolve(createUser(username, password)))
        .then(saveUser)
        .then(user => ({
            id: user.id,
            username: user.username,
            token: utils.genToken(user)
        }))
}

Use of commonjs import/export syntaxe
Use of commonjs import/export syntaxe
