From dd5bb50ba38cba3ee0007e4d3a7977e4d184bc87 Mon Sep 17 00:00:00 2001 From: Sockenklaus Date: Wed, 27 Oct 2021 01:26:14 +0200 Subject: [PATCH] Started implementing provider for profile of logged in user. --- app/Controllers/Http/EmployeesController.ts | 61 ++++++++++++++++++++- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/app/Controllers/Http/EmployeesController.ts b/app/Controllers/Http/EmployeesController.ts index 177b943..76a697e 100644 --- a/app/Controllers/Http/EmployeesController.ts +++ b/app/Controllers/Http/EmployeesController.ts @@ -1,9 +1,29 @@ import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' import Employee from 'App/Models/Employee' +import User from 'App/Models/User' import UpdateEmployeeValidator from 'App/Validators/UpdateEmployeeValidator' import CreateEmployeeValidator from 'App/Validators/CreateEmployeeValidator' 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 { public async index ({bouncer, request}: HttpContextContract) { @@ -49,12 +69,47 @@ export default class EmployeesController { } - public async show ({params, bouncer}: HttpContextContract) { - const emp = await Employee.findOrFail(params.id) + public async show ({params, bouncer, auth}: HttpContextContract) { + 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) - return emp + return result } public async update ({params, bouncer, response, request}: HttpContextContract) {