import { Injectable } from '@angular/core'; import { GrantManagerUser } from '@core/typings/grant-manager.typing'; import { 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 { GrantManagerResources } from './grant-manager.resources'; import { GrantManagerState } from './grant-manager.state'; @AttachYCState(GrantManagerState) @Injectable({ providedIn: 'root' }) export class GrantManagerService extends BaseYCService { constructor ( private logger: LogService, private grantManagerResources: GrantManagerResources, private notifier: NotifierService, private i18n: I18nService ) { super(); } get grantManagerPaginationOptions () { return this.get('grantManagerPaginationOptions'); } setGrantManagerPaginationOptions (options: PaginationOptions) { this.set('grantManagerPaginationOptions', options); } adminGrantManagerPaginated ( options: PaginationOptions ) { return this.grantManagerResources.adminGrantManagerPaginated(options); } async handleUpdateGrantManager (updatedManager: GrantManagerUser) { try { await this.grantManagerResources.updateGrantManager(updatedManager); this.notifier.success(this.i18n.translate( 'ADMIN:notificationSuccesfulUserSubmission', {}, 'User submission was successful' )); } catch (error) { this.logger.error(error); this.notifier.error(this.i18n.translate( 'ADMIN:notificationErrorSendingUser', {}, 'There was an error sending the user information.' )); } } async handleConfirmEmailAddress (userId: number) { try { const result = await this.grantManagerResources.confirmEmailAddress(userId); if (!result) { this.notifier.error(this.i18n.translate( 'ADMIN:errorEmailAddressNotConfirmed', {}, 'Email address could not be confirmed' )); return; } this.notifier.success(this.i18n.translate( 'GLOBAL:notificationEmailConfirmed', {}, 'Email confirmed' )); } catch (error) { this.notifier.error(this.i18n.translate( 'ADMIN:errorConfirmingEmailAddress', {}, 'There was an error confirming the email address' )); } } async passwordResetForGrantManager ( email: string, purpose: string ) { return this.grantManagerResources.passwordResetForGrantManager( email, purpose ); } }