import { HttpErrorResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { SFTPAPI } from '@core/typings/api/sftp.typing'; import { ArrayHelpersService, PaginationOptions } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { NotifierService } from '@yourcause/common/notifier'; import { AttachYCState, BaseYCService } from '@yourcause/common/state'; import { SFTPResources } from './sftp.resources'; import { SFTPState } from './sftp.state'; @AttachYCState(SFTPState) @Injectable({ providedIn: 'root' }) export class SFTPService extends BaseYCService { errorEstablishingConnectionText = this.i18n.translate( 'CONFIG:textErrorEstablishingSFTPConnection', {}, 'There was an error establishing this connection. Please check the address and credentials provided.' ); errorAccessingFolderText = this.i18n.translate( 'CONFIG:textErrorAccessingFolder', {}, 'There was an error accessing the folder provided. Please check that the folder exists.' ); errorTestingSFTPConnectionText = this.i18n.translate( 'CONFIG:textErrorTestingSFTPConnection', {}, 'There was an error testing the SFTP connection.' ); constructor ( private logger: LogService, private sftpResources: SFTPResources, private notifier: NotifierService, private i18n: I18nService, private arrayHelper: ArrayHelpersService ) { super(); } get basicCredentialsList () { return this.get('basicCredentialsList'); } get userCredentialsList () { return this.get('userCredentialsList'); } get userCredentials () { return this.get('userCredentials'); } getCredentialsList (options: PaginationOptions) { return this.sftpResources.getCredentialsList(options); } async setUserCredentialsAndList (force = false) { const credentials = this.get('userCredentials'); if (!credentials || force) { const list = await this.sftpResources.getUserCredentialsList(); this.set( 'userCredentials', list ); this.set( 'userCredentialsList', this.arrayHelper.sort( list.map((option: SFTPAPI.UserSFTPCredentials) => { return { label: option.name, value: option.id }; }), 'label') ); return list; } else { return credentials; } } async setBasicCredentialsList (force = false) { if (!this.basicCredentialsList || force) { const list = await this.sftpResources.getBasicCredentialsList(); this.set( 'basicCredentialsList', this.arrayHelper.sort(list, 'siteName') ); } } async createUpdateSFTPCredentials (payload: SFTPAPI.SFTPCredentialsPayload) { try { await this.sftpResources.createUpdateSFTPCredential(payload); await this.setBasicCredentialsList(true); await this.setUserCredentialsAndList(true); this.notifier.success( this.i18n.translate( 'CONFIG:textSuccessfullyUpdatedSFTPCredentials', {}, 'Successfully updated SFTP credentials' ) ); return true; } catch (e) { this.logger.error(e); this.notifier.error( this.i18n.translate( 'CONFIG:textErrorUpdatingSFTPCredentials', {}, 'There was an error updating SFTP credentials') ); return false; } } async testSFTPCredentials (payload: SFTPAPI.SFTPCredentialsPayload) { try { const msg: SFTPAPI.SFTPCredentialsListView = await this.sftpResources.testSFTPCredential(payload); return msg; } catch (err) { const e = err as HttpErrorResponse; let errorMessage; if (e.error.message.includes('provided credentials')) { errorMessage = this.errorEstablishingConnectionText; } else if (e.error.message.includes('but the folder ')) { errorMessage = this.errorAccessingFolderText; } else { errorMessage = this.errorTestingSFTPConnectionText; } return errorMessage; } } getReportsTiedToCredentials (id: number) { return this.sftpResources.getReportsTiedToCredential(id); } async deleteSFTPCredentials (credentialsID: number) { try { await this.sftpResources.deleteSFTPCredentials(credentialsID); await this.setBasicCredentialsList(true); this.notifier.success( this.i18n.translate( 'CONFIG:textSuccessfullyDeletedSFTPCredentials', {}, 'Successfully deleted SFTP credentials' ) ); } catch (e) { this.logger.error(e); this.notifier.error( this.i18n.translate( 'CONFIG:errorDeletingSFTPCredentials', {}, 'There was an error deleting SFTP credentials' ) ); } } }