import { Component , ViewChild , ElementRef , Input , OnInit , AfterViewInit } from '@angular/core' ; import { Validators , FormBuilder , FormGroup } from '@angular/forms' ; import { RolesEnum , ITag , IUser , IRole , IOrganization , IEmployeeCreateInput , ICandidateSource , ICandidateCreateInput , ICandidate } from '@gauzy/contracts' ; import { filter , firstValueFrom , tap } from 'rxjs' ; import { TranslateService } from '@ngx-translate/core' ; import { distinctUntilChange } from '@gauzy/common-angular' ; import { TranslationBaseComponent } from '../../../language-base/translation-base.component' ; import { AuthService , CandidatesService , EmployeesService , RoleService , Store } from './../../../../@core/services' ; import { CompareDateValidator , UrlPatternValidator } from './../../../../@core/validators' ; import { FormHelpers } from '../../../forms/helpers' ; @ Component ( { selector : 'ga-user-basic-info-form' , templateUrl : 'basic-info-form.component.html' , styleUrls : [ 'basic-info-form.component.scss' ] } ) export class BasicInfoFormComponent extends TranslationBaseComponent implements OnInit , AfterViewInit { @ ViewChild ( 'imagePreview' ) imagePreviewElement : ElementRef ; @ Input ( ) public selectedTags : ITag [ ] ; private _isCandidate : boolean = false ; public get isCandidate ( ) : boolean { return this . _isCandidate ; } @ Input ( ) public set isCandidate ( value ) { this . _isCandidate = value ; } private _isEmployee : boolean = false ; public get isEmployee ( ) : boolean { return this . _isEmployee ; } @ Input ( ) public set isEmployee ( value ) { this . _isEmployee = value ; } private _isShowRole : boolean = false ; public get isShowRole ( ) : boolean { return this . _isShowRole ; } @ Input ( ) public set isShowRole ( value ) { this . _isShowRole = value ; this . setRoleValidations ( value ) ; } FormHelpers : typeof FormHelpers = FormHelpers ; public excludes : RolesEnum [ ] = [ ] ; public organization : IOrganization ; public form : FormGroup = BasicInfoFormComponent . buildForm ( this . fb , this ) ; static buildForm ( fb , self ) { return fb . group ( { firstName : [ ] , lastName : [ ] , username : [ ] , email : [ '' , Validators . compose ( [ Validators . required , Validators . email ] ) ] , imageUrl : [ '' ] , password : [ '' , Validators . compose ( [ Validators . required , Validators . minLength ( 4 ) ] ) ] , startedWorkOn : [ ] , role : [ ] , offerDate : [ ] , acceptDate : [ ] , appliedDate : [ ] , hiredDate : [ ] , rejectDate : [ ] , source : [ ] , tags : [ self . selectedTags ] , featureAsEmployee : [ false ] } , { validators : [ CompareDateValidator . validateDate ( 'offerDate' , 'acceptDate' ) , CompareDateValidator . validateDate ( 'offerDate' , 'rejectDate' ) , UrlPatternValidator . imageUrlValidator ( 'imageUrl' ) ] } ) ; } constructor ( private readonly fb , private readonly authService , private readonly roleService , private readonly employeesService , private readonly candidatesService , public readonly translateService , private readonly store ) { super ( translateService ) ; } ngOnInit ( ) { this . excludeRoles ( ) ; this . store . selectedOrganization$ . pipe ( distinctUntilChange ( ) , filter ( ( organization ) => ! ! organization ) , tap ( ( organization ) => this . organization = organization ) ) . subscribe ( ) ; } /**
     * Exclude SUPER_ADMIN role, if don't have permissions
     */ async excludeRoles ( ) { const hasSuperAdminRole = await firstValueFrom ( this . authService . hasRole ( [ RolesEnum . SUPER_ADMIN ] ) ) ; if ( ! hasSuperAdminRole ) { this . excludes . push ( RolesEnum . SUPER_ADMIN ) ; } } public enableEmployee ( ) { return this . form . get ( 'role' ) . value && ( ( this . form . get ( 'role' ) . value ) . name === RolesEnum . SUPER_ADMIN || ( this . form . get ( 'role' ) . value ) . name === RolesEnum . ADMIN ) ; } get showImageMeta ( ) { return this . form . get ( 'imageUrl' ) && this . form . get ( 'imageUrl' ) . value ; } async registerUser ( defaultRoleName , organizationId ? , createdById ? ) { if ( this . form . invalid ) { return ; } const { firstName , lastName , email , username , password } = this . form . getRawValue ( ) ; const { tags , imageUrl , featureAsEmployee , role : { name } } = this . form . getRawValue ( ) ; const { tenantId , tenant } = this . store . user ; /**
        * Removed feature organizations from payload,
        * which is not necessary to send into the payload
        */ if ( tenant . hasOwnProperty ( 'featureOrganizations' ) ) { delete tenant [ 'featureOrganizations' ] ; } const role = await firstValueFrom ( this . roleService . getRoleByName ( { name : name || defaultRoleName , tenantId } ) ) ; const user = { firstName : firstName , lastName : lastName , email : email , username : username || null , imageUrl : imageUrl , role : role , tenant : tenant , tags : tags } ; if ( role . name === RolesEnum . EMPLOYEE ) { return this . createEmployee ( user ) ; } else if ( role . name === RolesEnum . CANDIDATE ) { return this . createCandidate ( user ) ; } else { if ( featureAsEmployee === true ) { return await firstValueFrom ( this . employeesService . create ( { user : user , organization : this . organization , password : password } ) ) ; } else { return await firstValueFrom ( this . authService . register ( { user : user , password : password , confirmPassword : password , organizationId , createdById } ) ) ; } } } deleteImageUrl ( ) { this . form . get ( 'imageUrl' ) . setValue ( '' ) ; this . form . get ( 'imageUrl' ) . updateValueAndValidity ( ) ; } selectedTagsHandler ( tags ) { this . form . get ( 'tags' ) . setValue ( tags ) ; this . form . get ( 'tags' ) . updateValueAndValidity ( ) ; } ngAfterViewInit ( ) { this . _setupLogoUrlValidation ( ) ; } private _setupLogoUrlValidation ( ) { this . imagePreviewElement . nativeElement . onload = ( ) => { this . form . get ( 'imageUrl' ) . setErrors ( null ) ; } ; this . imagePreviewElement . nativeElement . onerror = ( ) => { if ( this . showImageMeta ) { this . form . get ( 'imageUrl' ) . setErrors ( { invalidUrl : true } ) ; } } ; } param role onSelectionChange ( role ) { if ( this . isShowRole ) { this . isCandidate = ( role . name === RolesEnum . CANDIDATE ) ; this . isEmployee = ( role . name === RolesEnum . EMPLOYEE ) ; } } param value setRoleValidations ( value ) { if ( value === true ) { this . form . get ( 'role' ) . setValidators ( [ Validators . required ] ) ; } else { this . form . get ( 'role' ) . clearValidators ( ) ; } this . form . get ( 'role' ) . updateValueAndValidity ( ) ; } param user returns async createEmployee ( user ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { password , tags } = this . form . getRawValue ( ) ; const { offerDate = null , acceptDate = null , rejectDate = null , startedWorkOn = null } = this . form . getRawValue ( ) ; const employee = { tenantId , user , startedWorkOn : startedWorkOn , password : password , organizationId , offerDate , acceptDate , rejectDate , tags : tags } ; return await firstValueFrom ( this . employeesService . create ( employee ) ) ; } param user returns async createCandidate ( user ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { password , tags } = this . form . getRawValue ( ) ; const { appliedDate = null , rejectDate = null , hiredDate = null , source : sourceName = null } = this . form . getRawValue ( ) ; let source = null ; if ( sourceName !== null ) { source = { name : sourceName , tenantId , organizationId } ; } const candidate = { user , password , documents : [ ] , appliedDate , hiredDate , source , rejectDate , tags , tenantId , organizationId } ; return await firstValueFrom ( this . candidatesService . create ( candidate ) ) ; } }