Skip to content

Commit

Permalink
Fix time tracking authorized directive
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmbabhazi committed Oct 25, 2024
1 parent 0ac3f94 commit fb2e34b
Showing 1 changed file with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeDetectorRef, Directive, Input, OnInit, TemplateRef, ViewContainerRef } from '@angular/core';
import { filter, tap, map, switchMap } from 'rxjs/operators';
import { filter, tap, map, switchMap, distinctUntilChanged } from 'rxjs/operators';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import * as camelcase from 'camelcase';
import { IOrganization, IUser } from '@gauzy/contracts';
Expand Down Expand Up @@ -39,32 +39,27 @@ export class TimeTrackingAuthorizedDirective implements OnInit {
.pipe(
distinctUntilChange(),
filter((organization: IOrganization) => !!organization),
tap(() => this._viewContainer.clear()),
switchMap((organization: IOrganization) =>
this._store.user$.pipe(
filter((user: IUser) => !!user),
map((user: IUser) => {
const permissions = Array.isArray(this.permission) ? this.permission : [this.permission];
// Determine permission based on employee existence
const hasPermission = user.employee
? camelcase(this.permission) in organization &&
organization[camelcase(this.permission)] &&
camelcase(this.permission) in user.employee &&
user.employee[camelcase(this.permission)]
: camelcase(this.permission) in organization &&
organization[camelcase(this.permission)];

const hasPermissions = (source: any) =>
permissions.every((permission) => {
const permKey = camelcase(permission);
return permKey in source && source[permKey];
});

const hasOrgPermission = hasPermissions(organization);

if (user.employee) {
// Check if the permission is in the organization and in the employee properties
const hasEmployeePermission = hasPermissions(user.employee);
return hasOrgPermission && hasEmployeePermission;
} else {
return hasOrgPermission;
}
})
return hasPermission;
}),
distinctUntilChanged() // Only emit when permission status changes
)
),
tap((hasPermission: boolean) => {
this._viewContainer.clear(); // Clear the container once per status change

if (hasPermission) {
this._viewContainer.createEmbeddedView(this._templateRef);
} else {
Expand Down

0 comments on commit fb2e34b

Please sign in to comment.