import { Injectable } from '@angular/core'; import { ALL_SKIP_FILTER, ArrayHelpersService, PaginationOptions, SimpleStringMap, TopLevelFilter } 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 { AudienceResources } from './audience.resources'; import { AudienceState } from './audience.state'; import { Audience, AudienceModalResponse } from './audience.typing'; @AttachYCState(AudienceState) @Injectable({ providedIn: 'root' }) export class AudienceService extends BaseYCService { constructor ( private logger: LogService, private audienceResources: AudienceResources, private notifier: NotifierService, private i18n: I18nService, private arrayHelper: ArrayHelpersService ) { super(); } get allAudiences () { return this.get('allAudiences'); } get audienceDetailMap () { return this.get('audienceDetailMap'); } get audienceSimpleMap () { return this.get('audienceSimpleMap'); } getTopLevelFiltersForUserAudienceTable ( forExternal = true ) { const statusOptions = { selectOptions: [{ display: this.i18n.translate( 'common:lblAllCap', {}, 'All' ), value: ALL_SKIP_FILTER }, { display: this.i18n.translate( forExternal ? 'common:textInternal' : 'GLOBAL:textViewOnly', {}, forExternal ? 'Internal' : 'View only' ), value: false }, { display: this.i18n.translate( forExternal ? 'common:textExternal' : 'APPLY:textManage', {}, forExternal ? 'External' : 'Manage' ), value: true }] }; return [ new TopLevelFilter( 'text', 'nameOrEmail', '', this.i18n.translate( 'GLOBAL:textSearchByNameOrEmail', {}, 'Search by name or email' ), undefined, undefined, [{ column: 'name', filterType: 'cn' }, { column: 'email', filterType: 'cn' }] ), new TopLevelFilter( 'typeaheadSingleEquals', forExternal ? 'external' : 'canManage', ALL_SKIP_FILTER, '', statusOptions, this.i18n.translate( 'common:textUserPosition', {}, 'User position' ) ) ]; } async setAllAudiences () { if (!this.allAudiences) { const options: PaginationOptions = { returnAll: true, retrieveTotalRecordCount: false, filterColumns: [], sortColumns: [], rowsPerPage: 1000000, pageNumber: 0 }; const response = await this.audienceResources.getAudiencesPaginated(options); this.set( 'allAudiences', this.arrayHelper.sort(response.records, 'name') ); const audienceSimpleMap: SimpleStringMap = {}; response.records.forEach((audience) => { audienceSimpleMap[audience.id] = audience; }); this.set('audienceSimpleMap', audienceSimpleMap); } } async resetAllAudiences () { this.set('allAudiences', undefined); await this.setAllAudiences(); } async setAudienceDetail (audience: Audience) { const id = audience.id; if (!this.audienceDetailMap[id]) { const members = await this.audienceResources.getAudienceMembers(id); const detail = { ...audience, members }; this.set('audienceDetailMap', { ...this.audienceDetailMap, [id]: detail }); } } clearAudienceDetail (id: number) { this.set('audienceDetailMap', { ...this.audienceDetailMap, [id]: undefined }); } async createOrEditAudience ( response: AudienceModalResponse ) { try { await this.audienceResources.createOrEditAudience(response); this.notifier.success(this.i18n.translate( !response.id ? 'CONFIG:textSuccessfullyCreatedAudience' : 'CONFIG:textSuccessfullyUpdatedAudience', {}, !response.id ? 'Successfully created the audience' : 'Successfully updated the audience' )); if (response.id) { this.clearAudienceDetail(response.id); } if (this.allAudiences) { await this.resetAllAudiences(); } return true; } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( !response.id ? 'CONFIG:textErrorCreatingTheAudience' : 'CONFIG:textErrorUpdatingTheAudience', {}, !response.id ? 'There was an error creating the audience' : 'There was an error updating the audience' )); return false; } } async deleteAudience (id: number) { try { await this.audienceResources.deleteAudience(id); this.notifier.success(this.i18n.translate( 'CONFIG:textSuccessfullyDeletedTheAudience', {}, 'Successfully deleted the audience' )); if (this.allAudiences) { await this.resetAllAudiences(); } return true; } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'CONFIG:textErrorDeletingTheAudience', {}, 'There was an error deleting the audience' )); return false; } } }