some shit
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import Employee from 'App/Models/Employee'
|
||||
import SetSettingsValidator from 'App/Validators/SetSettingsValidator'
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
|
||||
type ResultSetting = {
|
||||
key: string,
|
||||
@@ -9,15 +9,10 @@ type ResultSetting = {
|
||||
|
||||
export default class SettingsController {
|
||||
|
||||
public async list({ params, bouncer }: HttpContextContract ): Promise<ResultSetting[]> {
|
||||
const userId = params.userId
|
||||
public async list({ auth }: HttpContextContract ): Promise<ResultSetting[]> {
|
||||
|
||||
try {
|
||||
const user = await Employee.findOrFail(userId)
|
||||
|
||||
await bouncer.with('SettingsPolicy').authorize('do', user)
|
||||
|
||||
const result = await user.related('settings').query().select(['key', 'value'])
|
||||
const result = await auth.user.related('settings').query().select(['key', 'value'])
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -27,16 +22,12 @@ export default class SettingsController {
|
||||
|
||||
}
|
||||
|
||||
public async get({params, bouncer}: HttpContextContract): Promise<ResultSetting | null> {
|
||||
const userId = params.userId
|
||||
public async get({params, auth}: HttpContextContract): Promise<ResultSetting | null> {
|
||||
|
||||
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()
|
||||
const result = auth.user.related('settings').query().select(['key', 'value']).where('key', key).first()
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -46,34 +37,35 @@ export default class SettingsController {
|
||||
|
||||
}
|
||||
|
||||
public async set({params, request, bouncer}: HttpContextContract): Promise<'ok'> {
|
||||
const userId = params.userId
|
||||
/**
|
||||
* Expects:
|
||||
* {
|
||||
* settings: [
|
||||
* {key: 'key1', value: 'value1'},
|
||||
* ]
|
||||
* }
|
||||
*/
|
||||
public async set({request, auth}: HttpContextContract): Promise<'ok'> {
|
||||
|
||||
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')
|
||||
await auth.user.related('settings').updateOrCreateMany(payload.settings, 'key')
|
||||
|
||||
return "ok"
|
||||
}
|
||||
catch(error){
|
||||
Logger.error(error)
|
||||
return error.message
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async delete({ params, bouncer }: HttpContextContract): Promise<(0 | 1)[]> {
|
||||
const userId = params.userId
|
||||
public async delete({ params, auth }: HttpContextContract): Promise<(0 | 1)[]> {
|
||||
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()
|
||||
return await auth.user.related('settings').query().where('key', key).delete()
|
||||
}
|
||||
catch(error){
|
||||
return error.message
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { BasePolicy } from '@ioc:Adonis/Addons/Bouncer'
|
||||
import Employee from 'App/Models/Employee'
|
||||
|
||||
export default class SettingsPolicy extends BasePolicy {
|
||||
public async do(user: Employee, query: Employee){
|
||||
return user.isAdmin() || user.id === query.id
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { schema, rules } from '@ioc:Adonis/Core/Validator'
|
||||
import { schema, rules, validator } from '@ioc:Adonis/Core/Validator'
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class SetSettingsValidator {
|
||||
@@ -52,5 +52,11 @@ export default class SetSettingsValidator {
|
||||
* }
|
||||
*
|
||||
*/
|
||||
public messages = {}
|
||||
public messages = {
|
||||
'settings.required': 'Settings are required',
|
||||
'settings.key.required': 'Key is required',
|
||||
'settings.key.alpha': 'Key must be alphabetic',
|
||||
'settings.value.required': 'Value is required',
|
||||
'settings.value.alpha': 'Value must be alphabetic',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,5 +56,4 @@ export const { actions } = Bouncer
|
||||
*/
|
||||
export const { policies } = Bouncer.registerPolicies({
|
||||
EmployeesPolicy: () => import('App/Policies/EmployeesPolicy'),
|
||||
SettingsPolicy: () => import('App/Policies/SettingsPolicy'),
|
||||
})
|
||||
|
||||
@@ -31,10 +31,10 @@ Route.group(() => {
|
||||
|
||||
Route.resource('employees', 'EmployeesController').apiOnly()
|
||||
|
||||
Route.get('settings/:userId', 'SettingsController.list').as('settings.list')
|
||||
Route.get('settings/:userId/:key', 'SettingsController.get').as('settings.get')
|
||||
Route.post('settings/:userId', 'SettingsController.set').as('settings.set')
|
||||
Route.delete('settings/:userId/:key', 'SettingsController.delete').as('settings.delete')
|
||||
Route.get('settings', 'SettingsController.list').as('settings.list')
|
||||
Route.get('settings/:key', 'SettingsController.get').as('settings.get')
|
||||
Route.post('settings', 'SettingsController.set').as('settings.set')
|
||||
Route.delete('settings:key', 'SettingsController.delete').as('settings.delete')
|
||||
})
|
||||
.prefix('api/v1')
|
||||
.middleware('auth')
|
||||
|
||||
Reference in New Issue
Block a user