|  | @@ -6,6 +6,7 @@ const { validate } = require('../utils/validation')
 | 
	
		
			
				|  |  |  const axios = require('axios')
 | 
	
		
			
				|  |  |  const https = require('https')
 | 
	
		
			
				|  |  |  const { addManyDokumen } = require('../utils/dokumenFunction')
 | 
	
		
			
				|  |  | +const pelanggaranModel = require('../model/pelanggaran.model')
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  exports.create = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |    const httpsAgent = new https.Agent({
 | 
	
	
		
			
				|  | @@ -14,24 +15,14 @@ exports.create = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |    req.body.dokumen = req.files
 | 
	
		
			
				|  |  |    const isValid = validate(res, req.body, {
 | 
	
		
			
				|  |  |      no_laporan: 'string',
 | 
	
		
			
				|  |  | -    user_id: 'string',
 | 
	
		
			
				|  |  |      pt_id: 'string',
 | 
	
		
			
				|  |  |      pelanggaran_id: 'string',
 | 
	
		
			
				|  |  |      keterangan: 'string',
 | 
	
		
			
				|  |  | -    is_public: { type: 'boolean', convert: true },
 | 
	
		
			
				|  |  |      dokumen: { type: 'array', items: 'object' },
 | 
	
		
			
				|  |  |    })
 | 
	
		
			
				|  |  |    if (!isValid) return
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  const {
 | 
	
		
			
				|  |  | -    no_laporan,
 | 
	
		
			
				|  |  | -    pt_id,
 | 
	
		
			
				|  |  | -    user_id,
 | 
	
		
			
				|  |  | -    keterangan,
 | 
	
		
			
				|  |  | -    pelanggaran_id,
 | 
	
		
			
				|  |  | -    role_data,
 | 
	
		
			
				|  |  | -    is_public,
 | 
	
		
			
				|  |  | -  } = req.body
 | 
	
		
			
				|  |  | +  const { no_laporan, pt_id, keterangan, pelanggaran_id, role_data } = req.body
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    const url = `https://api.kemdikbud.go.id:8243/pddikti/1.2/pt/${pt_id}`
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -41,21 +32,29 @@ exports.create = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |      },
 | 
	
		
			
				|  |  |      httpsAgent,
 | 
	
		
			
				|  |  |    })
 | 
	
		
			
				|  |  | -  const pt = responseAxios.data[0]
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  let user = {}
 | 
	
		
			
				|  |  | -  if (!is_public) {
 | 
	
		
			
				|  |  | -    user = await userModel.findOne({
 | 
	
		
			
				|  |  | -      user_id: user_id,
 | 
	
		
			
				|  |  | +  const pt = responseAxios.data[0]
 | 
	
		
			
				|  |  | +  if (!pt)
 | 
	
		
			
				|  |  | +    return response.error(res, {
 | 
	
		
			
				|  |  | +      message: 'pt_id tidak ditemukan',
 | 
	
		
			
				|  |  |      })
 | 
	
		
			
				|  |  | -  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    const dokumen = await addManyDokumen(req.files)
 | 
	
		
			
				|  |  |    const dokumen_id = dokumen.map((e) => e._id)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +  const pelanggaran = await pelanggaranModel.find({
 | 
	
		
			
				|  |  | +    _id: {
 | 
	
		
			
				|  |  | +      $in: pelanggaran_id.split(',').map((e) => e),
 | 
	
		
			
				|  |  | +    },
 | 
	
		
			
				|  |  | +  })
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  if (!pelanggaran.length)
 | 
	
		
			
				|  |  | +    return response.error(res, { message: 'pelanggaran_id tidak ada' })
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  const user = req.user
 | 
	
		
			
				|  |  |    let data = {
 | 
	
		
			
				|  |  |      no_laporan,
 | 
	
		
			
				|  |  | -    user: user._id || user_id,
 | 
	
		
			
				|  |  | +    user: user._id,
 | 
	
		
			
				|  |  |      dokumen: dokumen_id,
 | 
	
		
			
				|  |  |      pt,
 | 
	
		
			
				|  |  |      pelanggaran: pelanggaran_id.split(',').map((e) => e),
 | 
	
	
		
			
				|  | @@ -72,11 +71,27 @@ exports.create = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  exports.getAll = handleError(async (req, res) => {
 | 
	
		
			
				|  |  | -  const data = await laporanModel
 | 
	
		
			
				|  |  | +  const user = req.user
 | 
	
		
			
				|  |  | +  let data = await laporanModel
 | 
	
		
			
				|  |  |      .find()
 | 
	
		
			
				|  |  |      .populate('user')
 | 
	
		
			
				|  |  |      .populate('pelanggaran')
 | 
	
		
			
				|  |  |      .populate('dokumen')
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  if (!data) {
 | 
	
		
			
				|  |  | +    return response.error(res, {
 | 
	
		
			
				|  |  | +      message: 'Not Found',
 | 
	
		
			
				|  |  | +      code: 404,
 | 
	
		
			
				|  |  | +    })
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  if (user.role.id !== 2020) {
 | 
	
		
			
				|  |  | +    data = data.filter(
 | 
	
		
			
				|  |  | +      (e) =>
 | 
	
		
			
				|  |  | +        e.user.role.id === user.role.id && e.pt.pembina.id === user.lembaga.id
 | 
	
		
			
				|  |  | +    )
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |    return response.success(res, {
 | 
	
		
			
				|  |  |      message: '',
 | 
	
		
			
				|  |  |      data,
 | 
	
	
		
			
				|  | @@ -85,17 +100,21 @@ exports.getAll = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  exports.getOne = handleError(async (req, res) => {
 | 
	
		
			
				|  |  |    const { id } = req.params
 | 
	
		
			
				|  |  | -  const user_id = req.data
 | 
	
		
			
				|  |  | -  const user = await userModel.findOne({
 | 
	
		
			
				|  |  | -    user_id: user_id,
 | 
	
		
			
				|  |  | -  })
 | 
	
		
			
				|  |  | +  const user = req.user
 | 
	
		
			
				|  |  |    const data = await laporanModel
 | 
	
		
			
				|  |  |      .findOne({ _id: id, user: user._id })
 | 
	
		
			
				|  |  |      .populate('user')
 | 
	
		
			
				|  |  |      .populate('pelanggaran')
 | 
	
		
			
				|  |  |      .populate('dokumen')
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  if (!data) {
 | 
	
		
			
				|  |  | +    return response.error(res, {
 | 
	
		
			
				|  |  | +      message: 'Not Found',
 | 
	
		
			
				|  |  | +      code: 404,
 | 
	
		
			
				|  |  | +    })
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  |    return response.success(res, {
 | 
	
		
			
				|  |  | -    message: '',
 | 
	
		
			
				|  |  | -    data,
 | 
	
		
			
				|  |  | +    message: 'Berhasil ngambil data Laporan',
 | 
	
		
			
				|  |  | +    data: user,
 | 
	
		
			
				|  |  |    })
 | 
	
		
			
				|  |  |  })
 |