Skip to content

Commit

Permalink
Merge pull request #108 from harena-lab/development
Browse files Browse the repository at this point in the history
Improvements: Filters, Share cases, Permission system.
  • Loading branch information
HeitorMatt authored Feb 8, 2021
2 parents de50693 + 78c09ce commit 46dd3fd
Show file tree
Hide file tree
Showing 14 changed files with 816 additions and 285 deletions.
489 changes: 290 additions & 199 deletions harena-manager.postman_collection.json

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions harena-manager_-_production.postman_environment.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/adonisjs/app/Controllers/Http/v1/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
const Logger = use('Logger')

const User = use('App/Models/v1/User')
const Institution = use('App/Models/v1/Institution')

class AuthController {
async checkToken ({ request, auth, response }) {
try {
// console.log('====Checking token...')
if(await auth.check()){
response.json({token:'token valid', username: auth.user.username})
let userInstitution = await Institution.findBy('id', auth.user.institution_id)
response.json({token:'token valid', username: auth.user.username,
grade: auth.user.grade, institution: userInstitution.acronym, institutionId: auth.user.institution_id})
}
// console.log('====Token valid')
} catch (error) {
Expand Down
161 changes: 154 additions & 7 deletions src/adonisjs/app/Controllers/Http/v1/CaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const Case = use('App/Models/v1/Case')
const CaseVersion = use('App/Models/v1/CaseVersion')
const Institution = use('App/Models/v1/Institution')
const Permission = use('App/Models/v1/Permission')
const Property = use('App/Models/v1/Property')
const CaseProperty = use('App/Models/v1/CaseProperty')

const uuidv4 = require('uuid/v4')

Expand All @@ -36,18 +38,33 @@ class CaseController {
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async show ({ params, response }) {
async show ({ request, response }) {
try {
const c = await Case.find(params.id)

const c = await Case.find(request.input('caseId'))

if (c != null) {
const versions = await CaseVersion.query()
.where('case_id', '=', params.id)
.where('case_id', '=', request.input('caseId'))
.orderBy('created_at', 'asc')
.fetch()

const properties = await Database
.select(['properties.title', 'case_properties.value'])
.from('case_properties')
.leftJoin('properties', 'case_properties.property_id', 'properties.id')
.where('case_properties.case_id', request.input('caseId'))

var prop = {}
for(let p in properties){
let title = properties[p].title
let value = properties[p].value
prop[title] = value
}

c.source = versions.last().source
c.versions = versions
c.property = prop

const institution = await Institution.find(c.institution_id)
c.institution = institution.acronym
Expand Down Expand Up @@ -112,11 +129,11 @@ class CaseController {
}

/** * Update case details. PUT or PATCH case/:id */
async update ({ params, request, response }) {
async update ({ request, response }) {
const trx = await Database.beginTransaction()

try {
const c = await Case.find(params.id)
const c = await Case.find(request.input('caseId'))

if (c != null) {
c.title = request.input('title') || null
Expand Down Expand Up @@ -171,10 +188,10 @@ class CaseController {
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async destroy ({ params, response }) {
async destroy ({ request, response }) {
const trx = await Database.beginTransaction()
try {
const c = await Case.findBy('id', params.id)
const c = await Case.findBy('id', request.input('caseId'))

if (c != null) {
await c.versions().delete()
Expand All @@ -198,6 +215,136 @@ class CaseController {
}
}

async share ({params, request, response}){
const trx = await Database.beginTransaction()

try {
const entity = request.input('entity')
const subject = request.input('subject')
const subject_grade = request.input('subject_grade')
const clearance = request.input('clearance')
const table_id = request.input('table_id').split(',')
// console.log(entity)
// console.log(subject)
// console.log(clearance)
// console.log(table_id)

for (let c in table_id){
// console.log('============ case for')
console.log(table_id[c])
if(await Case.findBy('id', table_id[c])){
// console.log('================================================ case added')
// console.log(table_id[c])
let permission = new Permission()
permission.id = await uuidv4()
permission.entity = entity
permission.subject = subject
permission.subject_grade = subject_grade
permission.clearance = clearance
permission.table = 'cases'
permission.table_id = table_id[c]
await permission.save(trx)
}else return response.json('Could not find the case id, please review and try again')
}

trx.commit()
return response.json('Cases shared successfully!')
} catch (e) {
trx.rollback()
console.log(e)
return response.status(500).json({ message: e.message })
}

}

async storeProperty ({params, request, auth, response}) {
const trx = await Database.beginTransaction()
try {
const case_id = request.input('case_id')
const property_title = request.input('property_title')
const property_value = request.input('property_value')

const property = await Property.findOrCreate(
{ title: property_title },
{ id: await uuidv4(), title: property_title }, trx
)
// const caseProperty = new CaseProperty()
// caseProperty.case_id = case_id
// caseProperty.property_id = property.id
// caseProperty.value = property_value
let caseProperty = await CaseProperty.findOrCreate(
{ case_id: case_id, property_id: property.id},
{ case_id: case_id, property_id: property.id, value: property_value}, trx
)

await property.save(trx)
await caseProperty.save(trx)

trx.commit()

return response.json({property: property, case_property: caseProperty})

} catch (e) {
trx.rollback()
console.log('============catch error storeProperty')
console.log(e)
return response.status(e.status).json({ message: e.message})
}
}

async updateProperty ({request, auth, response}) {
const trx = await Database.beginTransaction()
try {
const case_id = request.input('case_id')
const property_title = request.input('property_title')
const property_value = request.input('property_value')

const property = await Property.findBy('title', property_title)

// const caseProperty = await CaseProperty.findOrCreate(
// { case_id: case_id, property_id: property.id },
// { case_id: case_id, property_id: property.id}, trx
// )

let caseProperty = await CaseProperty
.query()
.where('property_id', property.id)
.where('case_id', case_id)
.fetch()
caseProperty = caseProperty.last()

await trx
.table('case_properties')
.where('property_id', property.id)
.where('case_id', case_id)
.update({ value: property_value,})
// console.log('============ db case property')
// console.log(caseProperty)

caseProperty.value = property_value

// console.log('============ value')
// console.log(caseProperty.value)
trx.commit()


return response.json(caseProperty)
} catch (e) {
trx.rollback()
console.log('============catch error updateProperty')
console.log(e)
return response.status(e.status).json({ message: e.message })
}
}

async deleteProperty ({params, request, auth, response}) {
try {

} catch (e) {
return response.status(e.status).json({ message: e.message })
}
}

}

module.exports = CaseController
37 changes: 30 additions & 7 deletions src/adonisjs/app/Controllers/Http/v1/CategoryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,55 @@ class CategoryController {
async listCases ({ request, response, auth }) {
try {
const user = await auth.user


const clearance = parseInt(request.input('clearance'))
const categoryId = request.input('categoryId')
const category = await Category.find(categoryId)

var publishedFilter = parseInt(request.input('published')) || 0

const institutionFilter = request.input('fInstitution') || `%`
const userTypeFilter = request.input('fUserType') || `%`
const specialtyFilter = request.input('fSpecialty') || `%`


const test = await Database
.select([ 'cases.id', 'cases.title','cases.description', 'cases.language', 'cases.domain',
'cases.specialty', 'cases.keywords', 'cases.complexity', 'cases.original_date',
'cases.author_grade', 'cases.published', 'users.username'])
'cases.specialty', 'cases.keywords', 'cases.complexity', 'cases.original_date',
'cases.author_grade', 'cases.published', 'users.username',
'institutions.title AS institution', 'institutions.acronym AS institution_acronym',
'institutions.country AS institution_country', 'cases.created_at'])
.distinct('cases.id')
.from('cases')
.leftJoin('permissions', 'cases.id', 'permissions.table_id')
.join('users', 'users.id', 'cases.author_id')
.join('institutions', 'users.institution_id', 'institutions.id')
.where('cases.category_id', category.id)
.where('cases.published', '>=', publishedFilter)
.where('cases.institution_id', 'like', institutionFilter)
.where('cases.author_grade', 'like', userTypeFilter)
.where(function(){
if (specialtyFilter != '%')
this.where('cases.specialty', 'like', specialtyFilter)
})

.where(function(){
this
.where('cases.author_id', user.id)
.orWhere(function () {
.where('cases.author_id', user.id)
.orWhere(function () {
this
.where('permissions.entity', 'institution')
.where('permissions.subject', user.institution_id)
.where('permissions.clearance', '>=', clearance)
.where(function(){
this
.where('permissions.entity', 'institution')
.where('permissions.subject', user.institution_id)
.where('permissions.clearance', '>=', clearance)
.whereNull('permissions.subject_grade')
.orWhere('permissions.subject_grade', user.grade)
})
})
})
.orderBy('cases.created_at', 'desc')

return response.json(test)
} catch (e) {
Expand Down
16 changes: 16 additions & 0 deletions src/adonisjs/app/Controllers/Http/v1/InstitutionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ class InstitutionController {
return response.status(500).json({ message: e.message })
}
}

async listInstitutions ({ request, response }) {
try {
const institutions = await Institution
.query()
.where('acronym', '!=', 'uni')
.fetch()


return response.json(institutions)
} catch (e) {
console.log(e)
return response.status(500).json({ message: e.message })
}
}

}

module.exports = InstitutionController
Loading

0 comments on commit 46dd3fd

Please sign in to comment.