File

src/app/modules/search/components/user-edit/user-edit.component.ts

Implements

OnInit OnDestroy AfterViewInit

Metadata

Index

Properties
Methods

Constructor

constructor(userSearchService: UserSearchService, activatedRoute: ActivatedRoute, permissionService: PermissionService, resourceService: ResourceService, route: Router, toasterService: ToasterService, formBuilder: UntypedFormBuilder, routerNavigationService: RouterNavigationService, profileService: ProfileService, userService: UserService, orgDetailsService: OrgDetailsService, navigationhelperService: NavigationHelperService)
Parameters :
Name Type Optional
userSearchService UserSearchService No
activatedRoute ActivatedRoute No
permissionService PermissionService No
resourceService ResourceService No
route Router No
toasterService ToasterService No
formBuilder UntypedFormBuilder No
routerNavigationService RouterNavigationService No
profileService ProfileService No
userService UserService No
orgDetailsService OrgDetailsService No
navigationhelperService NavigationHelperService No

Methods

getAllRoles
getAllRoles()
Returns : void
getBlock
getBlock(districtId)
Parameters :
Name Optional
districtId No
Returns : void
getDistrict
getDistrict()
Returns : void
getLoggedInUserDetails
getLoggedInUserDetails()
Returns : void
getRolesReqBody
getRolesReqBody(newRoles, currentRoles, orgId)
Parameters :
Name Optional
newRoles No
currentRoles No
orgId No
Returns : {}
getSchool
getSchool(blockId)
Parameters :
Name Optional
blockId No
Returns : void
initializeFormFields
initializeFormFields()
Returns : void
ngAfterViewInit
ngAfterViewInit()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
onBlockChange
onBlockChange()
Returns : void
onDistrictChange
onDistrictChange()
Returns : void
onFormValueChange
onFormValueChange()
Returns : void
onSubmitForm
onSubmitForm()
Returns : void
populateUserDetails
populateUserDetails()
Returns : void
redirect
redirect()
Returns : void
settelemetryData
settelemetryData()
Returns : void
updateProfile
updateProfile()
Returns : void

Properties

Public activatedRoute
Type : ActivatedRoute
allBlocks
Type : any
allDistricts
Type : any
allRoles
Type : Array<RolesAndPermissions>
allSchools
Type : any
blockLoader
Default value : false
enableSubmitBtn
Type : boolean
locationCodes
Type : Array<string>
modal
Decorators :
@ViewChild('modal')
Public navigationhelperService
Type : NavigationHelperService
Public orgDetailsService
Type : OrgDetailsService
Public profileService
Type : ProfileService
queryParams
Type : any
Public resourceService
Type : ResourceService
rootOrgRoles
Type : Array<string>
Public route
Type : Router
Public routerNavigationService
Type : RouterNavigationService
sbFormBuilder
Type : UntypedFormBuilder
schoolLoader
Default value : false
selectedOrgId
Type : string
selectedOrgName
Type : string
selectedOrgUserRoles
Type : Array<string>
selectedOrgUserRolesNew
Type : any
Default value : []
selectedSchoolId
Type : any
showMainLoader
Default value : true
stateId
Type : string
submitInteractEdata
Type : IInteractEventEdata
telemetryImpression
Type : IImpressionEventInput
telemetryInteractObject
Type : IInteractEventObject
userDetails
Type : any
userDetailsForm
Type : UntypedFormGroup
userId
Type : string
userProfile
Type : any
Public userService
Type : UserService
import { Component, OnInit, ViewChild, OnDestroy, AfterViewInit } from '@angular/core';
import { UntypedFormBuilder, UntypedFormGroup, UntypedFormControl } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { UserSearchService } from './../../services';
import { UserService, PermissionService, RolesAndPermissions, OrgDetailsService } from '@sunbird/core';
import { IInteractEventObject, IInteractEventEdata, IImpressionEventInput } from '@sunbird/telemetry';
import { ResourceService, ToasterService, RouterNavigationService, ServerResponse, IUserData,
  NavigationHelperService } from '@sunbird/shared';
import { ProfileService } from '@sunbird/profile';
import * as _ from 'lodash-es';

@Component({
  selector: 'app-user-edit',
  templateUrl: './user-edit.component.html',
  styleUrls: ['./user-edit-component.scss']
})
export class UserEditComponent implements OnInit, OnDestroy, AfterViewInit {

  @ViewChild('modal') modal;
  userId: string;
  allRoles: Array<RolesAndPermissions>;
  userDetailsForm: UntypedFormGroup;
  sbFormBuilder: UntypedFormBuilder;
  selectedOrgName: string;
  selectedOrgId: string;
  rootOrgRoles: Array<string>;
  selectedOrgUserRoles: Array<string>;
  selectedOrgUserRolesNew: any = [];
  stateId: string;
  allDistricts: any;
  allBlocks: any;
  allSchools: any;
  userProfile: any;
  userDetails: any;
  telemetryImpression: IImpressionEventInput;
  submitInteractEdata: IInteractEventEdata;
  telemetryInteractObject: IInteractEventObject;
  blockLoader = false;
  schoolLoader = false;
  showMainLoader = true;
  locationCodes: Array<string>;
  queryParams: any;
  selectedSchoolId: any;
  enableSubmitBtn: boolean;

  constructor(private userSearchService: UserSearchService, public activatedRoute: ActivatedRoute,
    private permissionService: PermissionService, public resourceService: ResourceService,
    public route: Router, private toasterService: ToasterService, formBuilder: UntypedFormBuilder,
    public routerNavigationService: RouterNavigationService, public profileService: ProfileService,
    public userService: UserService, public orgDetailsService: OrgDetailsService,
    public navigationhelperService: NavigationHelperService) {
    this.sbFormBuilder = formBuilder;
  }

  ngOnInit() {
    this.activatedRoute.queryParams
      .subscribe(params => {
        this.queryParams = { ...params };
      });
    this.getLoggedInUserDetails();
  }
  getLoggedInUserDetails() {
    this.userService.userData$.subscribe(
      (user: IUserData) => {
        if (user && !user.err) {
          this.userProfile = user.userProfile;
          const rootOrgDetails = _.filter(this.userProfile.organisations, (org) => {
            return org.organisationId === this.userProfile.rootOrgId;
          });
          this.stateId = _.get(rootOrgDetails[0], 'locationIds[0]');
          this.getAllRoles();
        } else {
          this.toasterService.error(this.resourceService.messages.emsg.m0005);
          this.redirect();
        }
      });
  }

  getAllRoles() {
    this.activatedRoute.params.subscribe(params => {
      this.userId = params.userId;
    });
    this.populateUserDetails();
    this.permissionService.availableRoles$.subscribe(params => {
      this.allRoles = this.permissionService.allRoles;
      this.allRoles = _.filter(this.allRoles, (role) => {
        return role.role !== 'ORG_ADMIN' && role.role !== 'SYSTEM_ADMINISTRATION' && role.role !== 'ADMIN';
      });
      _.remove(this.allRoles, { role: 'PUBLIC' });
    });
  }

  populateUserDetails() {
    const option = { userId: this.userId };
    this.userSearchService.getUserByIdV5(option).subscribe(
      (apiResponse: ServerResponse) => {
        this.userDetails = apiResponse.result.response;
        const rootOrgDetails = _.filter(this.userDetails.organisations, (org) => {
          return org.organisationId === this.userDetails.rootOrgId;
        });
        const subOrgDetails = _.filter(this.userDetails.organisations, (org) => {
          return org.organisationId !== this.userDetails.rootOrgId;
        });
        if (!_.isEmpty(rootOrgDetails)) {
          this.rootOrgRoles = rootOrgDetails[0].roles;
          this.selectedOrgUserRoles = rootOrgDetails[0].roles;
         }
        if (!_.isEmpty(subOrgDetails)) {
          _.forEach(subOrgDetails, (org) => {
            this.selectedOrgUserRoles = _.union(this.selectedOrgUserRoles, org.roles);
          });
        }
        if (_.get(this.userDetails, 'roles') && !_.isEmpty(this.userDetails.roles)) {
          this.selectedOrgUserRoles = _.map(this.userDetails.roles, 'role');
        }
        if (!_.isEmpty(subOrgDetails)) {
          const orgs = _.sortBy(subOrgDetails, ['orgjoindate']);
          this.selectedSchoolId = orgs[0].organisationId;
        }
        this.initializeFormFields();
      },
      err => {
        this.toasterService.error(this.resourceService.messages.emsg.m0005);
        this.redirect();
      }
    );
  }

  initializeFormFields() {
    this.userDetailsForm = this.sbFormBuilder.group({
      role: new UntypedFormControl(this.selectedOrgUserRoles),
      district: new UntypedFormControl(null),
      block: new UntypedFormControl(null),
      school: new UntypedFormControl(null)
    });
    this.getDistrict();
    this.onDistrictChange();
    this.onBlockChange();
    this.settelemetryData();
    this.onFormValueChange();
  }

  getDistrict() {
    if (this.stateId) {
      const requestData = { 'filters': { 'type': 'district', parentId: this.stateId } };
      this.profileService.getUserLocation(requestData).subscribe(res => {
        this.allDistricts = res.result.response;
        const location = _.find(this.userDetails.userLocations, (locations) => {
          return locations.type === 'district';
        });
        let locationExist: any;
        if (location) {
          locationExist = _.find(this.allDistricts, (locations) => {
            return locations.code === location.code;
          });
        }

        locationExist ? this.userDetailsForm.controls['district'].setValue(locationExist.code) :
          this.userDetailsForm.controls['district'].setValue('');
        this.showMainLoader = false;
      }, err => {
        this.toasterService.error(this.resourceService.messages.emsg.m0005);
        this.redirect();
      });
    } else {
      this.showMainLoader = false;
    }
  }

  onDistrictChange() {
    const districtControl = this.userDetailsForm.get('district');
    let districtValue = '';
    districtControl.valueChanges.subscribe(
      (data: string) => {
        if (districtControl.status === 'VALID' && districtValue !== districtControl.value) {
          const district = _.find(this.allDistricts, (dis) => {
            return dis.code === districtControl.value;
          });
          this.getBlock(district.id);
          districtValue = districtControl.value;
        }
      });
  }

  getBlock(districtId) {
    this.blockLoader = true;
    const requestData = { 'filters': { 'type': 'block', parentId: districtId } };
    this.profileService.getUserLocation(requestData).subscribe(res => {
      this.allBlocks = res.result.response;
      const location = _.find(this.userDetails.userLocations, (locations) => {
        return locations.type === 'block';
      });
      let locationExist: any;
      if (location) {
        locationExist = _.find(this.allBlocks, (locations) => {
          return locations.code === location.code;
        });
      }

      locationExist ? this.userDetailsForm.controls['block'].setValue(locationExist.code) :
        this.userDetailsForm.controls['block'].setValue('');
      this.blockLoader = false;
    }, err => {
      this.toasterService.error(this.resourceService.messages.emsg.m0005);
      this.redirect();
    });
  }

  onBlockChange() {
    const blockControl = this.userDetailsForm.get('block');
    let blockValue = '';
    blockControl.valueChanges.subscribe(
      (data: string) => {
        this.userDetailsForm.controls['school'].setValue('');
        if (blockControl.status === 'VALID' && blockValue !== blockControl.value) {
          this.userDetailsForm.controls['school'].setValue('');
          this.allSchools = [];
          const block = _.find(this.allBlocks, (blocks) => {
            return blocks.code === blockControl.value;
          });
          if (block && block.id) { this.getSchool(block.id); }
          blockValue = blockControl.value;
        }
      });
  }

  getSchool(blockId) {
    this.schoolLoader = true;
    const option = { 'filters': { 'locationIds': [blockId] } };
    this.orgDetailsService.fetchOrgs(option).subscribe(
      (apiResponse: ServerResponse) => {
        this.allSchools = apiResponse.result.response.content;
        this.schoolLoader = false;
        this.userDetailsForm.controls['school'].setValue(this.selectedSchoolId);
      },
      err => {
        this.toasterService.error(this.resourceService.messages.emsg.m0005);
        this.redirect();
      }
    );
  }

  onSubmitForm() {
    this.updateProfile();
  }

  updateProfile() {
    // create school and roles data
    const roles = !_.isEmpty(this.userDetailsForm.value.role) ? this.userDetailsForm.value.role : ['PUBLIC'];
    const orgId = this.userDetails.rootOrgId;
    const newRoles = [...roles];
    const mainRoles = ['ORG_ADMIN', 'SYSTEM_ADMINISTRATION', 'ADMIN', 'SYSTEM_ADMIN'];
    _.remove(newRoles, (role) => {
        return _.includes(mainRoles, role);
    });
    const rolesAdded =  _.concat(newRoles, _.intersection(mainRoles, this.rootOrgRoles));
    const data = { userId: this.userId, roles: this.getRolesReqBody(rolesAdded, this.selectedOrgUserRoles, orgId) };
    this.userSearchService.updateRoles(data)
    .subscribe(
      (apiResponse: ServerResponse) => {
        this.toasterService.success(this.resourceService.messages.smsg.m0049);
        this.redirect();
      },
      err => {
        this.toasterService.error(this.resourceService.messages.emsg.m0020);
        this.redirect();
      }
    );
  }

  getRolesReqBody(newRoles, currentRoles, orgId) {
    const reqBody = [];
    // Get newly added roles array comparing to existing roles
    const newlyAddedRoles = _.difference(newRoles, currentRoles);
    // Get deleted roles from existing roles
    const deletedRoles = _.difference(currentRoles, newRoles);
    // Consolidated master role array of existing and newly added roles
    const masterRoles = [...currentRoles, ...newlyAddedRoles];
    _.forEach(masterRoles, (role) => {
      if (_.indexOf(deletedRoles, role) > -1) {
        reqBody.push({
          role: role,
          operation: 'remove',
          scope: [{
            organisationId: orgId
          }]
        });
      } else {
        reqBody.push({
          role: role,
          operation: 'add',
          scope: [{
            organisationId: orgId
          }]
        });
      }
    });
    return reqBody;
  }

  redirect(): void {
    this.route.navigate(['../../'], { relativeTo: this.activatedRoute, queryParams: this.queryParams });
  }

  onFormValueChange() {
    this.userDetailsForm.valueChanges.subscribe(val => {
      this.settelemetryData();
    });
  }

  settelemetryData() {
    this.submitInteractEdata = {
      id: 'user-update',
      type: 'click',
      pageid: 'user-edit'
    };

    this.submitInteractEdata = {
      id: 'user-update',
      type: 'click',
      pageid: 'user-edit',
      extra: { filters: this.userDetailsForm.value }
    };

    this.telemetryInteractObject = {
      id: this.userId,
      type: 'User',
      ver: '1.0'
    };
  }

  ngAfterViewInit () {
    setTimeout(() => {
      this.telemetryImpression = {
        context: {
          env: this.activatedRoute.snapshot.data.telemetry.env
        },
        object: {
          id: this.activatedRoute.snapshot.params.userId,
          type: 'user',
          ver: '1.0'
        },
        edata: {
          type: this.activatedRoute.snapshot.data.telemetry.type,
          pageid: this.activatedRoute.snapshot.data.telemetry.pageid,
          uri: this.route.url,
          subtype: this.activatedRoute.snapshot.data.telemetry.subtype,
          duration: this.navigationhelperService.getPageLoadTime()
        }
      };
    });
  }

  ngOnDestroy() {
    if (this.modal && this.modal.deny) {
      this.modal.deny();
    }
  }
}
<app-modal-wrapper [config]="{disableClose: true, size: 'normal'}" (dismiss)="redirect()"
  [appTelemetryImpression]="telemetryImpression" #modal>
  <ng-template sbModalContent>
    <div class="sb-modal">
      <div class="transition ui dimmer page modals active visible">
        <div class="ui modal transition active visible normal">
          <!--Header-->
          <div class="sb-modal-header" *ngIf="userDetails" role="heading" aria-level="2">
            {{resourceService.frmelmnts?.lbl?.setRole | translate}} {{userDetails.firstName}}
            {{userDetails.lastName}}
          </div>
          <!--/Header-->

          <!--Content-->
          <div class="sb-modal-content" *ngIf="showMainLoader">
            <div class="ui grid">
              <div class="twelve wide column">
                <app-loader></app-loader>
              </div>
            </div>
          </div>
          <div class="sb-modal-content o-y-visible" *ngIf="!showMainLoader">
            <div class="ui grid">
              <div class="ten wide column mb-30">
                <span class="ui header">{{resourceService.frmelmnts?.lbl?.editUserDetails | translate}}</span>
              </div>
            </div>
            <form class="ui form" [formGroup]="userDetailsForm" *ngIf="userDetails">
              <div class="field">
                <label>{{resourceService.frmelmnts?.lbl?.role | translate}}</label>
              </div>
              <div class="field">
                <sui-multi-select [isSearchable]="false" class="selection"
                  (selectedOptionsChange)="enableSubmitBtn = true;" formControlName="role" [options]="allRoles"
                  labelField="roleName" valueField="role" name="role"
                  placeholder="{{resourceService?.frmelmnts?.btn?.selrole | translate}}" #allroles>
                  <sui-select-option *ngFor="let role of allroles.filteredOptions" [value]="role"></sui-select-option>
                </sui-multi-select>
              </div>
            </form>
          </div>
          <!--/Content-->

          <!--Actions-->
          <div class="sb-modal-actions">
            <button [disabled]="!enableSubmitBtn" appTelemetryInteract
              [telemetryInteractObject]="telemetryInteractObject" [telemetryInteractEdata]="submitInteractEdata"
              (click)="onSubmitForm()" tabindex="0" class="sb-btn sb-btn-normal sb-btn-primary">
              {{resourceService.frmelmnts?.btn?.save | translate}}
            </button>
          </div>
          <!--/Actions-->
        </div>
      </div>
    </div>
  </ng-template>
</app-modal-wrapper>

./user-edit-component.scss

@use "@project-sunbird/sb-styles/assets/mixins/mixins" as *;

.save-btn {
    margin: calculateRem(40px) 0 0;
    .wide-btn {
        min-width: calculateRem(200px);
    }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""