Started implementing provider for profile of logged in user.

This commit is contained in:
Sockenklaus
2021-10-27 01:26:14 +02:00
parent 5c8c51dd9d
commit dd5bb50ba3

View File

@@ -1,9 +1,29 @@
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Employee from 'App/Models/Employee' import Employee from 'App/Models/Employee'
import User from 'App/Models/User'
import UpdateEmployeeValidator from 'App/Validators/UpdateEmployeeValidator' import UpdateEmployeeValidator from 'App/Validators/UpdateEmployeeValidator'
import CreateEmployeeValidator from 'App/Validators/CreateEmployeeValidator' import CreateEmployeeValidator from 'App/Validators/CreateEmployeeValidator'
import Database from '@ioc:Adonis/Lucid/Database' import Database from '@ioc:Adonis/Lucid/Database'
import Logger from '@ioc:Adonis/Core/Logger'
type ResultShow = {
employee: {
id: number,
firstName: string,
lastName: string,
shorthand: string,
phone: string,
mobile: string,
email: string,
contractHours: number,
hasUser: Boolean,
},
user: {
id?: number,
username?: string
}
}
export default class EmployeesController { export default class EmployeesController {
public async index ({bouncer, request}: HttpContextContract) { public async index ({bouncer, request}: HttpContextContract) {
@@ -49,12 +69,47 @@ export default class EmployeesController {
} }
public async show ({params, bouncer}: HttpContextContract) { public async show ({params, bouncer, auth}: HttpContextContract) {
const emp = await Employee.findOrFail(params.id) let result : ResultShow = {
employee: {
id: NaN,
firstName: '',
lastName: '',
shorthand: '',
phone: '',
mobile: '',
email: '',
contractHours: NaN,
hasUser: false
},
user: {}
}
let emp
if(params.id === 'me' && auth.isLoggedIn && auth.user !== undefined){
result.employee.hasUser = true
result.user.username = auth.user.username
result.user.id = auth.user.id
emp = (await auth.user.related('employeeProfile').query().limit(1))[0]
}
else {
emp = await Employee.findOrFail(params.id)
}
if(emp !== undefined){
result.employee.id = emp.id
result.employee.firstName = emp.firstName
result.employee.lastName = emp.lastName
result.employee.phone = emp.phone
result.employee.mobile = emp.mobile
result.employee.email = emp.email
result.employee.contractHours = emp.contractHours
}
await bouncer.authorize('employees.show', emp) await bouncer.authorize('employees.show', emp)
return emp return result
} }
public async update ({params, bouncer, response, request}: HttpContextContract) { public async update ({params, bouncer, response, request}: HttpContextContract) {