import { Component , Input , OnInit , ViewChild } from '@angular/core' ; import { InvitationTypeEnum , IInvite , IOrganizationProject , IOrganizationContact , IOrganizationDepartment , IOrganization } from '@gauzy/contracts' ; import { NbDialogRef } from '@nebular/theme' ; import { TranslateService } from '@ngx-translate/core' ; import { filter , tap } from 'rxjs/operators' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import { EmailInviteFormComponent } from '../forms' ; import { TranslationBaseComponent } from '../../language-base' ; import { OrganizationContactService , OrganizationDepartmentsService , OrganizationProjectsService , Store , ToastrService } from '../../../@core/services' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ga-invite-mutation' , templateUrl : './invite-mutation.component.html' , styleUrls : [ './invite-mutation.component.scss' ] } ) export class InviteMutationComponent extends TranslationBaseComponent implements OnInit { _invitationType : InvitationTypeEnum ; get invitationType ( ) : InvitationTypeEnum { return this . _invitationType ; } @ Input ( ) set invitationType ( value ) { this . _invitationType = value ; } @ ViewChild ( 'emailInviteForm' ) emailInviteForm : EmailInviteFormComponent ; organizationProjects : IOrganizationProject [ ] = [ ] ; organizationContacts : IOrganizationContact [ ] = [ ] ; organizationDepartments : IOrganizationDepartment [ ] = [ ] ; public organization : IOrganization ; constructor ( private readonly dialogRef , private readonly organizationProjectsService , private readonly organizationContactService , private readonly organizationDepartmentsService , public readonly translateService , private readonly toastrService , private readonly store ) { super ( translateService ) ; } ngOnInit ( ) { this . store . selectedOrganization$ . pipe ( filter ( ( organization ) => ! ! organization ) , tap ( ( organization ) => this . organization = organization ) , tap ( ( ) => this . loadOrganizationData ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } async loadOrganizationData ( ) { if ( ! this . organization ) { return ; } try { await this . loadProjects ( ) ; await this . loadOrganizationContacts ( ) ; await this . loadDepartments ( ) ; } catch ( error ) { this . toastrService . danger ( error ) ; } } async loadProjects ( ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { items = [ ] } = await this . organizationProjectsService . getAll ( [ ] , { organizationId , tenantId } ) ; this . organizationProjects = items ; } async loadOrganizationContacts ( ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { items = [ ] } = await this . organizationContactService . getAll ( [ ] , { organizationId , tenantId } ) ; this . organizationContacts = items ; } async loadDepartments ( ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { items = [ ] } = await this . organizationDepartmentsService . getAll ( [ ] , { organizationId , tenantId } ) ; this . organizationDepartments = items ; } closeDialog ( savedInvites = [ ] ) { this . dialogRef . close ( savedInvites ) ; } async add ( ) { try { const { items , total , ignored } = await this . emailInviteForm . saveInvites ( ) ; if ( ignored > 0 ) { this . toastrService . warning ( 'INVITE_PAGE.IGNORED' , { total , ignored } ) ; } else { this . toastrService . success ( 'INVITE_PAGE.SENT' , { total } ) ; } this . closeDialog ( items ) ; } catch ( error ) { this . toastrService . danger ( error ) ; } } }