Skip to content

Commit

Permalink
Merge branch 'develop' into creteria-setup-screen-ALCOpenSource#37
Browse files Browse the repository at this point in the history
update current branch with develop.
  • Loading branch information
asoluka committed May 24, 2023
2 parents b2abeca + c691289 commit c2a8009
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 61 deletions.
42 changes: 42 additions & 0 deletions backend/app/Controllers/Http/DashboardController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Roles from 'App/Enums/Roles'
import Program from 'App/Models/Program'
import Task from 'App/Models/Task'
import TaskReport from 'App/Models/TaskReport'
import User from 'App/Models/User'

export default class DashboardController {
public async index({ auth, response }: HttpContextContract) {
const user = auth.user
if (!user || !user.isAdmin) {
response.unauthorized({ message: 'You are not authorized to access this resource.' })
return
}
const currentDate = new Date().toISOString()

try {
const activePrograms = (await Program.query().where('is_archive', false)).length
const programList = await Program.query().where('is_archive', false).limit(3)
const mentors = (await User.query().where('role_id', Roles.MENTOR)).length
const mentorManagers = (await User.query().where('role_id', Roles.MENTOR_MANAGER)).length
const tasks = (await Task.query()).length
const reports = (await TaskReport.query()).length
const reportList = await TaskReport.query().limit(3)
const completedTaskList = await Task.query().where('end_date', '<', currentDate).limit(3)
const inprogressTaskList = await Task.query().where('end_date', '>', currentDate).limit(3)
response.ok({
active_programs: activePrograms,
program_list: programList,
mentor_managers: mentorManagers,
mentors,
tasks,
completed_task_list: completedTaskList,
inprogress_task_list: inprogressTaskList,
reports,
report_list: reportList,
})
} catch (error) {
return response.badRequest({ message: `server issue`, status: "error" })
}
}
}
8 changes: 5 additions & 3 deletions backend/app/Controllers/Http/ProfilesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ export default class ProfilesController {
profile.website = payload.website ?? profile.website
profile.city = payload.city ?? profile.city
profile.country = payload.country ?? profile.country
profile.socialMediaLinks = JSON.stringify(payload.socialMediaLinks) ?? profile.socialMediaLinks
profile.socialMediaLinks =
JSON.stringify(payload.socialMediaLinks) ?? profile.socialMediaLinks

if(payload.profileImagePath){
if (payload.profileImagePath) {
const profileImage = request.file('profileImagePath')
await profileImage?.moveToDisk('upload_file')
profile.profileImagePath = profileImage?.fileName ?? profile.profileImagePath
Expand All @@ -52,7 +53,8 @@ export default class ProfilesController {
profile.website = payload.website ?? profile.website
profile.city = payload.city ?? profile.city
profile.country = payload.country ?? profile.country
profile.socialMediaLinks = JSON.stringify(payload.socialMediaLinks) ?? profile.socialMediaLinks
profile.socialMediaLinks =
JSON.stringify(payload.socialMediaLinks) ?? profile.socialMediaLinks
await profile.save()

return response.ok({ status: 'success', message: 'Profile successfully updated', profile })
Expand Down
178 changes: 178 additions & 0 deletions backend/app/Controllers/Http/ProgramReportsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import ProgramReport from 'App/Models/ProgramReport'
import Program from 'App/Models/Program'
import UserProgram from 'App/Models/UserProgram'
import User from 'App/Models/User'
import Roles from 'App/Enums/Roles'

export default class ProgramReportsController {
public async createProgramReport({ auth, params, request, response }: HttpContextContract) {
try {
if (auth.user?.id) {
const userId = auth.user?.id
const { programId } = params
const { achievement, blocker, recommendation } = request.only([
'achievement',
'blocker',
'recommendation',
])

const isMentorManager = await UserProgram.query()
.where('user_id', userId)
.where('program_id', programId)
.whereHas('user', (query) => {
query.where('role_id', Roles.MENTOR_MANAGER)
})
.first()
console.log('Mentor Managers', isMentorManager)

if (!isMentorManager) {
return response.unauthorized({
message: 'You are not authorized to create a program report for this program.',
})
}

const programReport = new ProgramReport()
programReport.fill({
programId,
mentorManagerId: userId,
achievement,
blocker,
recommendation,
})
await programReport.save()

const program = await Program.query().where('id', programId).firstOrFail()
const creator = await User.query().where('id', program.userId).firstOrFail()

return response.created({
message: 'Program report created',
programId: program.id,
programTitle: program.name,
programCreator: `${creator.firstName} ${creator.lastName}`,
achievement: programReport.achievement,
blocker: programReport.blocker,
recommendation: programReport.recommendation,
})
}
} catch (error) {
return response.badRequest({ message: 'Server issue', status: 'Error' })
}
}

async getAllReports({ auth, request, response }: HttpContextContract) {
try {
const user = auth.user
if (!user || !user.isAdmin) {
return response.unauthorized({ error: 'You must be an admin to view program reports' })
}
const { page, limit, search } = request.qs()
const programReports = await ProgramReport.query()
.orderBy('created_at', 'desc')
.if(search, (q) => {
q.whereHas('program', (programQuery) => {
programQuery
.whereRaw('LOWER(title) like ?', [`%${search.toLowerCase()}%`])
.orWhereRaw('LOWER(description) like ?', [`%${search.toLowerCase()}%`])
})
})
.paginate(page || 1, limit || 10)

const responseData = await Promise.all(
programReports.all().map(async (report) => {
const program = await Program.query()
.where('id', report.programId)
.preload('user', (query) => {
query.select(['firstName', 'lastName'])
})
.firstOrFail()

const mentorManager = await User.findOrFail(report.mentorManagerId)

return {
id: report.id,
achievement: report.achievement,
blocker: report.blocker,
recommendation: report.recommendation,
program: {
id: program.id,
title: program.name,
creatorUserId: program.userId,
createdBy: `${program.user?.firstName} ${program.user?.lastName}`,
},
mentorManager: {
id: mentorManager.id,
firstName: mentorManager.firstName,
lastName: mentorManager.lastName,
},
}
})
)

return response.ok({
status: 'success',
message: 'All Program reports fetched successfully',
responseData,
})
} catch (error) {
response.badRequest({ message: 'Error getting report', status: 'Error' })
}
}

async getReport({ auth, params, response }: HttpContextContract) {
try {
const user = auth.user
if (!user || !user.isAdmin) {
return response.unauthorized({
error: 'You must be an admin to view program reports',
})
}

const report = await ProgramReport.query().where('id', params.reportId).firstOrFail()
const program = await Program.query()
.where('id', report.programId)
.preload('user', (query) => {
query.select(['firstName', 'lastName'])
})
.firstOrFail()
const mentorManager = await User.findOrFail(report.mentorManagerId)
const result = {
id: report.id,
achievement: report.achievement,
blocker: report.blocker,
recommendation: report.recommendation,
program: {
id: program.id,
title: program.name,
creatorUserId: program.userId,
createdBy: `${program.user?.firstName} ${program.user?.lastName}`,
},
mentorManager: {
id: mentorManager.id,
firstName: mentorManager.firstName,
lastName: mentorManager.lastName,
},
}
return response.ok({ status: 'success', message: 'Report fetched successfully', result })
} catch (error) {
response.badRequest({ message: 'Error getting request', status: 'Error' })
}
}

async deleteReport({ auth, params, response }: HttpContextContract) {
try {
const user = auth.user
if (!user || !user.isAdmin) {
return response.unauthorized({ error: 'You must be an admin to delete reports' })
}

const report = await ProgramReport.findOrFail(params.reportId)

await report.delete()

return response.ok({ message: 'Program Report deleted successfully' })
} catch (error) {
response.badRequest({ message: 'Error deleting report', status: 'Error' })
}
}
}
59 changes: 41 additions & 18 deletions backend/app/Controllers/Http/ProgramsController.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Roles from 'App/Enums/Roles'
import Program from 'App/Models/Program'
import ProgramReport from 'App/Models/ProgramReport'
import UserProgram from 'App/Models/UserProgram'

export default class ArchivesController {
export default class ProgramsController {
public async index({ request, response }: HttpContextContract) {
try {
const { page, limit, search } = request.qs()
Expand Down Expand Up @@ -51,24 +52,44 @@ export default class ArchivesController {
}
}

public async show({ auth, params, request, response }: HttpContextContract) {
const user = auth.user
if (!user || !user.isAdmin) {
response.unauthorized({ message: 'You are not authorized to access this resource.' })
return
}
const program = await Program.query().where('id', params.id).firstOrFail()
const { page, limit } = request.qs()
const users = await UserProgram.query()
.preload('user')
.where('program_id', params.id)
.paginate(page || 1, limit || 10)

if (!program) return response.status(404).send({ message: 'Program not found' })

program.users = users
public async show({ auth, params, response }: HttpContextContract) {
try {
const user = auth.user
if (!user || !user.isAdmin) {
return response.unauthorized({ error: 'You must be an admin to view program reports' })
}

return response.ok(program)
const { id } = params

const program = await Program.query().where('id', id).firstOrFail()
const reports = await ProgramReport.query().where('program_id', id).exec()
const mentors = await UserProgram.query()
.where('program_id', id)
.whereHas('user', (query) => {
query.where('role_id', Roles.MENTOR)
})
.preload('user')
.exec()
const mentorManagers = await UserProgram.query()
.where('program_id', id)
.whereHas('user', (query) => {
query.where('role_id', Roles.MENTOR_MANAGER)
})
.preload('user')
.exec()

return response.ok({
program,
reportCount: reports.length,
reports,
mentorCount: mentors.length,
mentors,
mentorManagerCount: mentorManagers.length,
mentorManagers,
})
} catch (error) {
return response.badRequest({ message: 'Server issue', status: 'Error' })
}
}

public async update({ auth, params, request, response }: HttpContextContract) {
Expand All @@ -87,6 +108,8 @@ export default class ArchivesController {
program.merge({ name, description, userId })
await program.save()

await UserProgram.query().where('programId', program.id).delete()

const users = [...mentors, ...mentorManagers]

if (users && users.length > 0) {
Expand Down
Loading

0 comments on commit c2a8009

Please sign in to comment.