Added settings API
This commit is contained in:
@@ -22,7 +22,7 @@ type ResultShow = {
|
||||
|
||||
export default class EmployeesController {
|
||||
public async index ({bouncer, request}: HttpContextContract) {
|
||||
await bouncer.authorize('employees.index')
|
||||
await bouncer.with('EmployeesPolicy').authorize('index')
|
||||
|
||||
const limit: number = request.qs().limit ?? 10
|
||||
const page: number = request.qs().page ?? 1
|
||||
@@ -46,7 +46,9 @@ export default class EmployeesController {
|
||||
return employees.paginate(page, limit)
|
||||
}
|
||||
|
||||
public async store ({request}: HttpContextContract) {
|
||||
public async store ({request, bouncer}: HttpContextContract) {
|
||||
await bouncer.with('EmployeesPolicy').authorize('store')
|
||||
|
||||
try {
|
||||
const payload = await request.validate(CreateEmployeeValidator)
|
||||
|
||||
@@ -79,7 +81,7 @@ export default class EmployeesController {
|
||||
emp = await Employee.findOrFail(params.id)
|
||||
}
|
||||
|
||||
await bouncer.authorize('employees.show', emp)
|
||||
await bouncer.with('EmployeesPolicy').authorize('show', emp)
|
||||
|
||||
return {
|
||||
id: emp.id,
|
||||
@@ -101,7 +103,7 @@ export default class EmployeesController {
|
||||
const employee : Employee = await Employee.findOrFail(params.id)
|
||||
const editContractHours : boolean = employee.contractHours !== request.input('contractHours')
|
||||
|
||||
await bouncer.authorize('employees.update', editContractHours, employee)
|
||||
await bouncer.with('EmployeesPolicy').authorize('update', editContractHours, employee)
|
||||
|
||||
const payload = await request.validate(UpdateEmployeeValidator)
|
||||
|
||||
@@ -131,7 +133,7 @@ export default class EmployeesController {
|
||||
}
|
||||
|
||||
public async destroy ({params, bouncer}: HttpContextContract) {
|
||||
await bouncer.authorize('employees.destroy')
|
||||
await bouncer.with('EmployeesPolicy').authorize('destroy')
|
||||
|
||||
return await Database.from('employees').where('id', params.id).delete()
|
||||
}
|
||||
@@ -186,7 +188,7 @@ export default class EmployeesController {
|
||||
|
||||
let arr = qs.split(',').filter(item => item !== 'password' && item !== '' && columns.hasOwnProperty(item))
|
||||
|
||||
if(arr.length === 0) arr = ['id', 'last_name', 'first_name', 'email', 'mobile', 'phone', 'role']
|
||||
if(arr.length === 0) arr = ['id', 'last_name', 'first_name', 'shorthand', 'email', 'mobile', 'phone', 'role']
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
82
app/Controllers/Http/SettingsController.ts
Normal file
82
app/Controllers/Http/SettingsController.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import Employee from 'App/Models/Employee'
|
||||
import SetSettingsValidator from 'App/Validators/SetSettingsValidator'
|
||||
|
||||
type ResultSetting = {
|
||||
key: string,
|
||||
value: string
|
||||
}
|
||||
|
||||
export default class SettingsController {
|
||||
|
||||
public async list({ params, bouncer }: HttpContextContract ): Promise<ResultSetting[]> {
|
||||
const userId = params.userId
|
||||
|
||||
try {
|
||||
const user = await Employee.findOrFail(userId)
|
||||
|
||||
await bouncer.with('SettingsPolicy').authorize('do', user)
|
||||
|
||||
const result = await user.related('settings').query().select(['key', 'value'])
|
||||
|
||||
return result
|
||||
}
|
||||
catch(error) {
|
||||
return error.message
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async get({params, bouncer}: HttpContextContract): Promise<ResultSetting | null> {
|
||||
const userId = params.userId
|
||||
const key = params.key
|
||||
|
||||
try {
|
||||
const user = await Employee.findOrFail(userId)
|
||||
|
||||
await bouncer.with('SettingsPolicy').authorize('do', user)
|
||||
|
||||
const result = user.related('settings').query().select(['key', 'value']).where('key', key).first()
|
||||
|
||||
return result
|
||||
}
|
||||
catch(error) {
|
||||
return error.message
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async set({params, request, bouncer}: HttpContextContract): Promise<'ok'> {
|
||||
const userId = params.userId
|
||||
|
||||
try {
|
||||
const payload = await request.validate(SetSettingsValidator)
|
||||
const user = await Employee.findOrFail(userId)
|
||||
|
||||
await bouncer.with('SettingsPolicy').authorize('do', user)
|
||||
|
||||
await user.related('settings').updateOrCreateMany(payload.settings, 'key')
|
||||
|
||||
return "ok"
|
||||
}
|
||||
catch(error){
|
||||
return error.message
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async delete({ params, bouncer }: HttpContextContract): Promise<(0 | 1)[]> {
|
||||
const userId = params.userId
|
||||
const key = params.key
|
||||
|
||||
try {
|
||||
const user = await Employee.findOrFail(userId)
|
||||
await bouncer.with('SettingsPolicy').authorize('do', user)
|
||||
|
||||
return await user.related('settings').query().where('key', key).delete()
|
||||
}
|
||||
catch(error){
|
||||
return error.message
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user