'use strict';
//const JwtHelper = require('../../helpers/jwt.helper');
const models = require('../../core/models');

const extraMethods = { 
    
    findUser: async (params, context) => {
        return await models.User.findOne({
            where: params,
        });
    },

    findUserByPhone: async (Phone) => {
        return await models.User.findOne({
            where: {phone: Phone},
        });
    },

    createUser: async (params, context) => {
//        return models.sequelize.transaction(async transaction => {
            //const result = await models.User.create(params, { transaction });
        const result = await models.User.create(params);            
            return result;
//        });
    },

    findUserByRefreshToken: async(refreshToken) => {
        return await models.User.findOne({
            where: {
                token: refreshToken,
                state: 'active'
            }
        })
    },

    updateUserRefreshToken: async(userId, newRefreshToken) => {
        return await models.User.update(
            { token: newRefreshToken },
            { where: { id: userId }}
        );
    },

    deleteRefreshToken: async (userId, refreshToken) => {
        return await models.User.update(
            { token: null },
            { where: { id: userId } }
        );
    }
}

module.exports = {
    extraMethods
};


