|
| 1 | +import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges } from '@angular/core'; |
| 2 | +import { LoginInfo } from 'src/app/models/interfaces'; |
| 3 | +import { AccountServiceService } from 'src/app/services/account-service.service'; |
| 4 | +import { LocalStorageService } from 'src/app/services/local-storage.service'; |
| 5 | +import { ProductSpecServiceService } from 'src/app/services/product-spec-service.service'; |
| 6 | + |
| 7 | +type ValidationEvidenceRow = { |
| 8 | + type: string; |
| 9 | + name: string; |
| 10 | + value: string; |
| 11 | + missing: boolean; |
| 12 | +}; |
| 13 | +type OrganizationInfo = { |
| 14 | + organizationName: string; |
| 15 | + organizationVat: string; |
| 16 | + organizationAddress: string; |
| 17 | + organizationCountry: string; |
| 18 | + organizationWebsite: string; |
| 19 | + organizationEmail: string; |
| 20 | +}; |
| 21 | + |
| 22 | +@Component({ |
| 23 | + selector: 'request-validation-modal', |
| 24 | + templateUrl: './request-validation-modal.component.html', |
| 25 | + styleUrl: './request-validation-modal.component.css' |
| 26 | +}) |
| 27 | +export class RequestValidationModalComponent implements OnChanges { |
| 28 | + @Input() showModal: boolean = false; |
| 29 | + @Input() productSpec: any; |
| 30 | + @Input() selfAtt: any; |
| 31 | + @Input() selectedISOS: any[] = []; |
| 32 | + @Input() additionalISOS: any[] = []; |
| 33 | + requestValidationLoading: boolean = false; |
| 34 | + requestValidationError: string = ''; |
| 35 | + organizationInfo: OrganizationInfo = this.getEmptyOrganizationInfo(); |
| 36 | + |
| 37 | + @Output() closeModal = new EventEmitter<void>(); |
| 38 | + |
| 39 | + constructor( |
| 40 | + private localStorage: LocalStorageService, |
| 41 | + private accountService: AccountServiceService, |
| 42 | + private prodSpecService: ProductSpecServiceService |
| 43 | + ) {} |
| 44 | + |
| 45 | + ngOnChanges(changes: SimpleChanges): void { |
| 46 | + if (changes['showModal']?.currentValue === true) { |
| 47 | + this.requestValidationError = ''; |
| 48 | + this.loadOrganizationInfoFromCurrentSession(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + get validationEvidenceRows(): ValidationEvidenceRow[] { |
| 53 | + const rows: ValidationEvidenceRow[] = []; |
| 54 | + const selfAttestationValue = this.selfAtt?.productSpecCharacteristicValue?.[0]?.value ?? ''; |
| 55 | + |
| 56 | + rows.push({ |
| 57 | + type: 'Self attestation', |
| 58 | + name: 'Self attestation', |
| 59 | + value: selfAttestationValue, |
| 60 | + missing: !selfAttestationValue |
| 61 | + }); |
| 62 | + |
| 63 | + for (const certification of this.selectedISOS) { |
| 64 | + const value = certification?.url ?? ''; |
| 65 | + rows.push({ |
| 66 | + type: 'Third-party certification', |
| 67 | + name: this.normalizeName(certification?.name), |
| 68 | + value, |
| 69 | + missing: !value |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + for (const certification of this.additionalISOS) { |
| 74 | + const value = certification?.url ?? ''; |
| 75 | + rows.push({ |
| 76 | + type: 'Additional certification', |
| 77 | + name: this.normalizeName(certification?.name), |
| 78 | + value, |
| 79 | + missing: !value |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + return rows; |
| 84 | + } |
| 85 | + |
| 86 | + normalizeName(name?: string): string { |
| 87 | + return name?.replace(/compliance:/i, '').trim() ?? ''; |
| 88 | + } |
| 89 | + |
| 90 | + isHttpUrl(value?: string): boolean { |
| 91 | + return /^https?:\/\//i.test(value ?? ''); |
| 92 | + } |
| 93 | + |
| 94 | + hasMissingOrganizationInfo(): boolean { |
| 95 | + return [ |
| 96 | + this.organizationInfo.organizationName, |
| 97 | + this.organizationInfo.organizationVat, |
| 98 | + this.organizationInfo.organizationAddress, |
| 99 | + this.organizationInfo.organizationCountry, |
| 100 | + this.organizationInfo.organizationWebsite, |
| 101 | + this.organizationInfo.organizationEmail |
| 102 | + ].some((value) => this.isMissing(value)); |
| 103 | + } |
| 104 | + |
| 105 | + isMissing(value?: string): boolean { |
| 106 | + return !value || value.trim() === ''; |
| 107 | + } |
| 108 | + |
| 109 | + submitRequestValidation(): void { |
| 110 | + if (this.hasMissingOrganizationInfo() || this.requestValidationLoading) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (!this.productSpec) { |
| 115 | + this.requestValidationError = 'Product specification information is not available.'; |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + this.requestValidationLoading = true; |
| 120 | + this.requestValidationError = ''; |
| 121 | + |
| 122 | + this.prodSpecService.requestComplianceCertificate(this.buildRequestPayload(this.productSpec)).subscribe({ |
| 123 | + next: () => { |
| 124 | + this.requestValidationLoading = false; |
| 125 | + this.closeModal.emit(); |
| 126 | + }, |
| 127 | + error: (error) => { |
| 128 | + console.error('There was an error while requesting validation!', error); |
| 129 | + this.requestValidationLoading = false; |
| 130 | + this.requestValidationError = this.extractErrorMessage(error); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private async loadOrganizationInfoFromCurrentSession(): Promise<void> { |
| 136 | + const organizationPartyId = this.getOrganizationPartyId(); |
| 137 | + if (!organizationPartyId) { |
| 138 | + this.organizationInfo = this.getEmptyOrganizationInfo(); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + try { |
| 143 | + const organization = await this.accountService.getOrgInfo(organizationPartyId); |
| 144 | + this.organizationInfo = this.mapOrganizationInfo(organization); |
| 145 | + } catch (error) { |
| 146 | + console.error('There was an error while loading organization info!', error); |
| 147 | + this.organizationInfo = this.getEmptyOrganizationInfo(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private getOrganizationPartyId(): string { |
| 152 | + const loginInfo = this.localStorage.getObject('login_items') as LoginInfo; |
| 153 | + |
| 154 | + if (!loginInfo || JSON.stringify(loginInfo) === '{}') { |
| 155 | + return ''; |
| 156 | + } |
| 157 | + |
| 158 | + if (loginInfo.logged_as === loginInfo.id) { |
| 159 | + return ''; |
| 160 | + } |
| 161 | + |
| 162 | + const organizations = Array.isArray(loginInfo.organizations) ? loginInfo.organizations : []; |
| 163 | + const loggedOrganization = organizations.find((organization: any) => organization?.id == loginInfo.logged_as); |
| 164 | + return loggedOrganization?.partyId ?? ''; |
| 165 | + } |
| 166 | + |
| 167 | + private mapOrganizationInfo(organization: any): OrganizationInfo { |
| 168 | + const contactMedium = Array.isArray(organization?.contactMedium) ? organization.contactMedium : []; |
| 169 | + const partyCharacteristics = Array.isArray(organization?.partyCharacteristic) ? organization.partyCharacteristic : []; |
| 170 | + const externalReferences = Array.isArray(organization?.externalReference) ? organization.externalReference : []; |
| 171 | + |
| 172 | + const emailMedium = contactMedium.find((medium: any) => medium?.mediumType === 'Email'); |
| 173 | + const postalMedium = contactMedium.find((medium: any) => medium?.mediumType === 'PostalAddress'); |
| 174 | + const postalCharacteristic = postalMedium?.characteristic ?? {}; |
| 175 | + const characteristicCountry = this.getPartyCharacteristicValue(partyCharacteristics, 'country'); |
| 176 | + |
| 177 | + return { |
| 178 | + organizationName: organization?.tradingName ?? organization?.name ?? '', |
| 179 | + organizationVat: this.getOrganizationVat(externalReferences), |
| 180 | + organizationAddress: this.formatPostalAddress(postalCharacteristic), |
| 181 | + organizationCountry: characteristicCountry, |
| 182 | + organizationWebsite: this.getPartyCharacteristicValue(partyCharacteristics, 'website'), |
| 183 | + organizationEmail: emailMedium?.characteristic?.emailAddress ?? '' |
| 184 | + }; |
| 185 | + } |
| 186 | + |
| 187 | + private getOrganizationVat(externalReferences: any[]): string { |
| 188 | + if (!externalReferences.length) { |
| 189 | + return ''; |
| 190 | + } |
| 191 | + |
| 192 | + const idmReference = externalReferences.find((reference: any) => |
| 193 | + (reference?.externalReferenceType ?? '').toLowerCase() === 'idm_id' |
| 194 | + ); |
| 195 | + |
| 196 | + return idmReference?.name ?? ''; |
| 197 | + } |
| 198 | + |
| 199 | + private getPartyCharacteristicValue(characteristics: any[], key: string): string { |
| 200 | + const characteristic = characteristics.find((item: any) => item?.name === key); |
| 201 | + return characteristic?.value ?? ''; |
| 202 | + } |
| 203 | + |
| 204 | + private formatPostalAddress(address: any): string { |
| 205 | + if (!address) { |
| 206 | + return ''; |
| 207 | + } |
| 208 | + |
| 209 | + return [ |
| 210 | + address.street1, |
| 211 | + address.postCode, |
| 212 | + address.city, |
| 213 | + address.stateOrProvince |
| 214 | + ].filter((value: any) => !!value).join(', '); |
| 215 | + } |
| 216 | + |
| 217 | + private getEmptyOrganizationInfo(): OrganizationInfo { |
| 218 | + return { |
| 219 | + organizationName: '', |
| 220 | + organizationVat: '', |
| 221 | + organizationAddress: '', |
| 222 | + organizationCountry: '', |
| 223 | + organizationWebsite: '', |
| 224 | + organizationEmail: '' |
| 225 | + }; |
| 226 | + } |
| 227 | + |
| 228 | + private buildRequestPayload(productSpec: any): any { |
| 229 | + return JSON.parse(JSON.stringify(productSpec)); |
| 230 | + } |
| 231 | + |
| 232 | + private extractErrorMessage(error: any): string { |
| 233 | + const message = error?.error?.error ?? error?.error?.message ?? error?.message; |
| 234 | + return message ? `Error: ${message}` : 'Error: there was a problem requesting the validation.'; |
| 235 | + } |
| 236 | +} |
0 commit comments