|  | @@ -8,6 +8,8 @@ const qs = require('qs')
 | 
	
		
			
				|  |  |  const convertRole = require('../utils/convertRole')
 | 
	
		
			
				|  |  |  const { roleData } = require('../utils/constanta')
 | 
	
		
			
				|  |  |  const logModel = require('../model/log.model')
 | 
	
		
			
				|  |  | +const ip = require('ip')
 | 
	
		
			
				|  |  | +const osValue = require('../utils/osValue')
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  exports.login = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |    const isValid = validate(res, req.body, {
 | 
	
	
		
			
				|  | @@ -38,19 +40,21 @@ exports.login = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |    let cekUser = await userModel.findOne({
 | 
	
		
			
				|  |  |      user_id: user.id,
 | 
	
		
			
				|  |  |    })
 | 
	
		
			
				|  |  | -  let role =
 | 
	
		
			
				|  |  | -    process.env.ENV === 'production'
 | 
	
		
			
				|  |  | -      ? user.peran.filter((e) => roleData.includes(convertRole(e.peran.id)))[0]
 | 
	
		
			
				|  |  | -      : user.peran.filter((e) => roleData.includes(e.peran.id))[0]
 | 
	
		
			
				|  |  | -  let dataRole = {}
 | 
	
		
			
				|  |  | +  let role = null
 | 
	
		
			
				|  |  | +  if (process.env.ENV === 'production') {
 | 
	
		
			
				|  |  | +    role = user.peran.filter((e) =>
 | 
	
		
			
				|  |  | +      roleData.includes(convertRole(e.peran.id))
 | 
	
		
			
				|  |  | +    )[0]
 | 
	
		
			
				|  |  | +    role.peran.id = convertRole(role.peran.id)
 | 
	
		
			
				|  |  | +  } else {
 | 
	
		
			
				|  |  | +    role = user.peran.filter((e) => roleData.includes(e.peran.id))[0]
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  let dataRole = {
 | 
	
		
			
				|  |  | +    id: role.peran.id,
 | 
	
		
			
				|  |  | +    nama: role.peran.nama,
 | 
	
		
			
				|  |  | +    menu: role.peran.menu,
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  |    if (!cekUser) {
 | 
	
		
			
				|  |  | -    if (process.env.ENV === 'production') {
 | 
	
		
			
				|  |  | -      dataRole = {
 | 
	
		
			
				|  |  | -        id: convertRole(role.peran.id),
 | 
	
		
			
				|  |  | -        nama: role.peran.nama,
 | 
	
		
			
				|  |  | -        menu: role.peran.menu,
 | 
	
		
			
				|  |  | -      }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  |      cekUser = await userModel.create({
 | 
	
		
			
				|  |  |        user_id: user.id,
 | 
	
		
			
				|  |  |        nama: user.nama,
 | 
	
	
		
			
				|  | @@ -64,12 +68,7 @@ exports.login = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |      })
 | 
	
		
			
				|  |  |    } else {
 | 
	
		
			
				|  |  |      if (process.env.ENV === 'production') {
 | 
	
		
			
				|  |  | -      if (cekUser.role.id !== convertRole(role.peran.id)) {
 | 
	
		
			
				|  |  | -        dataRole = {
 | 
	
		
			
				|  |  | -          id: convertRole(role.peran.id),
 | 
	
		
			
				|  |  | -          nama: role.peran.nama,
 | 
	
		
			
				|  |  | -          menu: role.peran.menu,
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | +      if (cekUser.role.id !== role.peran.id) {
 | 
	
		
			
				|  |  |          await userModel.updateOne({ _id: cekUser._id }, { role: dataRole })
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  |        if (!cekUser.lembaga) {
 | 
	
	
		
			
				|  | @@ -78,15 +77,19 @@ exports.login = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |            { lembaga: role.organisasi }
 | 
	
		
			
				|  |  |          )
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  | -      cekUser = await userModel.findOne({
 | 
	
		
			
				|  |  | -        user_id: user.id,
 | 
	
		
			
				|  |  | -      })
 | 
	
		
			
				|  |  | +      if (cekUser.role.id !== role.peran.id || !cekUser.lembaga) {
 | 
	
		
			
				|  |  | +        cekUser = await userModel.findOne({
 | 
	
		
			
				|  |  | +          user_id: user.id,
 | 
	
		
			
				|  |  | +        })
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    await logModel.create({
 | 
	
		
			
				|  |  |      user: cekUser._id,
 | 
	
		
			
				|  |  |      aktivitas: `Berhasil login`,
 | 
	
		
			
				|  |  | +    os: osValue(),
 | 
	
		
			
				|  |  | +    ipv4: ip.address('public', 'ipv4'),
 | 
	
		
			
				|  |  |    })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    const accessToken = jwt.sign({ _id: cekUser._id }, process.env.SECRET, {
 |