@@ -472,14 +472,19 @@ export class TaskService extends TenantAwareCrudService<Task> {
472472 const { where } = options ;
473473 const { members } = where ;
474474
475- const hasPermission = RequestContext . hasPermission ( PermissionsEnum . CHANGE_SELECTED_EMPLOYEE ) ;
475+ const hasPermission =
476+ RequestContext . hasPermission ( PermissionsEnum . ORG_EMPLOYEES_EDIT ) &&
477+ ! RequestContext . hasPermission ( PermissionsEnum . VIEW_ASSIGNED_PROJECTS_ONLY ) ;
478+ const isManager =
479+ RequestContext . hasPermission ( PermissionsEnum . ORG_EMPLOYEES_EDIT ) &&
480+ RequestContext . hasPermission ( PermissionsEnum . VIEW_ASSIGNED_PROJECTS_ONLY ) ;
476481
477482 const query = this . typeOrmRepository . createQueryBuilder ( this . tableName ) ;
478483 query . innerJoin ( `${ query . alias } .members` , 'members' ) ;
479484
480- if ( ! hasPermission ) {
485+ if ( ! hasPermission || isManager ) {
481486 // Employee without permission: get their projects first
482- const employeeId = RequestContext . currentEmployeeId ( ) ;
487+ const employeeId = RequestContext . currentEmployeeId ( ) ?? RequestContext . currentUser ( ) . employeeId ;
483488 if ( isNotEmpty ( employeeId ) ) {
484489 const tenantId = RequestContext . currentTenantId ( ) ;
485490 const organizationId = where ?. organizationId as string ;
@@ -512,6 +517,30 @@ export class TaskService extends TenantAwareCrudService<Task> {
512517 const employeeId = members [ 'id' ] ;
513518 subQuery . andWhere ( p ( '"task_employee"."employeeId" = :employeeId' ) , { employeeId } ) ;
514519 }
520+ } else if ( isManager ) {
521+ const selectedEmployeeId = members ?. [ 'id' ] ;
522+ const managerId = RequestContext . currentUser ( ) . employeeId ;
523+
524+ if ( isNotEmpty ( selectedEmployeeId ) && selectedEmployeeId !== managerId ) {
525+ subQuery
526+ . andWhere ( p ( '"task_employee"."employeeId" = :employeeId' ) , {
527+ employeeId : selectedEmployeeId
528+ } )
529+ . andWhere (
530+ `EXISTS (
531+ SELECT 1 FROM "task_employee" te2
532+ WHERE te2."taskId" = "task_employee"."taskId"
533+ AND te2."employeeId" = :managerId
534+ )`
535+ )
536+ . setParameters ( { managerId } ) ;
537+ } else {
538+ if ( isNotEmpty ( managerId ) ) {
539+ subQuery . andWhere ( p ( '"task_employee"."employeeId" = :employeeId' ) , {
540+ employeeId : managerId
541+ } ) ;
542+ }
543+ }
515544 } else {
516545 // If employee has login and don't have permission to change employee
517546 const employeeId = RequestContext . currentEmployeeId ( ) ;
@@ -623,14 +652,19 @@ export class TaskService extends TenantAwareCrudService<Task> {
623652 const { teams = [ ] } = where ;
624653 const { members } = where ;
625654
626- const hasPermission = RequestContext . hasPermission ( PermissionsEnum . CHANGE_SELECTED_EMPLOYEE ) ;
655+ const hasPermission =
656+ RequestContext . hasPermission ( PermissionsEnum . ORG_EMPLOYEES_EDIT ) &&
657+ ! RequestContext . hasPermission ( PermissionsEnum . VIEW_ASSIGNED_PROJECTS_ONLY ) ;
658+ const isManager =
659+ RequestContext . hasPermission ( PermissionsEnum . ORG_EMPLOYEES_EDIT ) &&
660+ RequestContext . hasPermission ( PermissionsEnum . VIEW_ASSIGNED_PROJECTS_ONLY ) ;
627661 const query = this . typeOrmRepository . createQueryBuilder ( this . tableName ) ;
628662 query . leftJoin ( `${ query . alias } .teams` , 'teams' ) ;
629663
630664 let projectIds : string [ ] = [ ] ;
631665
632- if ( ! hasPermission ) {
633- const employeeId = RequestContext . currentEmployeeId ( ) ;
666+ if ( ! hasPermission || isManager ) {
667+ const employeeId = RequestContext . currentEmployeeId ( ) ?? RequestContext . currentUser ( ) . employeeId ;
634668 if ( isNotEmpty ( employeeId ) ) {
635669 const tenantId = RequestContext . currentTenantId ( ) ;
636670 const organizationId = where ?. organizationId as string ;
@@ -668,6 +702,31 @@ export class TaskService extends TenantAwareCrudService<Task> {
668702 const employeeId = members [ 'id' ] ;
669703 subQuery . andWhere ( p ( '"organization_team_employee"."employeeId" = :employeeId' ) , { employeeId } ) ;
670704 }
705+ } else if ( isManager ) {
706+ const selectedEmployeeId = members ?. [ 'id' ] ;
707+ const managerId = RequestContext . currentUser ( ) . employeeId ;
708+
709+ if ( isNotEmpty ( selectedEmployeeId ) && selectedEmployeeId !== managerId ) {
710+ subQuery
711+ . andWhere ( p ( '"organization_team_employee"."employeeId" = :employeeId' ) , {
712+ employeeId : selectedEmployeeId
713+ } )
714+ . andWhere (
715+ `EXISTS (
716+ SELECT 1
717+ FROM "organization_team_employee" te
718+ WHERE te."organizationTeamId" = "organization_team_employee"."organizationTeamId"
719+ AND te."employeeId" = :managerId
720+ )`
721+ )
722+ . setParameters ( { managerId } ) ;
723+ } else {
724+ if ( isNotEmpty ( managerId ) ) {
725+ subQuery . andWhere ( p ( '"organization_team_employee"."employeeId" = :employeeId' ) , {
726+ employeeId : managerId
727+ } ) ;
728+ }
729+ }
671730 } else {
672731 // If employee has login and don't have permission to change employee
673732 const employeeId = RequestContext . currentEmployeeId ( ) ;
@@ -769,11 +828,17 @@ export class TaskService extends TenantAwareCrudService<Task> {
769828 const employeeId = RequestContext . currentEmployeeId ( ) ;
770829 const tenantId = RequestContext . currentTenantId ( ) ;
771830 const organizationId = where . organizationId as string ;
772- const hasPermission = RequestContext . hasPermission ( PermissionsEnum . CHANGE_SELECTED_EMPLOYEE ) ;
831+ const hasPermission =
832+ RequestContext . hasPermission ( PermissionsEnum . ORG_EMPLOYEES_EDIT ) &&
833+ ! RequestContext . hasPermission ( PermissionsEnum . VIEW_ASSIGNED_PROJECTS_ONLY ) ;
834+ const isManager =
835+ RequestContext . hasPermission ( PermissionsEnum . ORG_EMPLOYEES_EDIT ) &&
836+ RequestContext . hasPermission ( PermissionsEnum . VIEW_ASSIGNED_PROJECTS_ONLY ) ;
773837 const userProvidedProjectId = ! ! options . where ?. projectId ;
774838
775- if ( ! userProvidedProjectId && ! hasPermission ) {
776- const projects = await this . _organizationProjectService . findByEmployee ( employeeId , {
839+ if ( ! userProvidedProjectId && ( isManager || ! hasPermission ) ) {
840+ const emplId = employeeId ?? RequestContext . currentUser ( ) . employeeId ;
841+ const projects = await this . _organizationProjectService . findByEmployee ( emplId , {
777842 tenantId,
778843 organizationId,
779844 relations : [ 'members' ]
0 commit comments