|
1 | 1 | import { Injectable } from '@angular/core'
|
2 |
| -import { InitService } from '@exlibris/exl-cloudapp-angular-lib' |
| 2 | +import { CloudAppEventsService, CloudAppStoreService, InitData } from '@exlibris/exl-cloudapp-angular-lib' |
| 3 | +import * as _ from 'lodash' |
| 4 | +import { last } from 'rxjs/operators' |
| 5 | +import { COLUMNS_DEFINITIONS } from './requested-resources/column-definitions' // Direct to avoid circular dependency |
| 6 | +import { ColumnOption } from './column-options' |
| 7 | +import { ConfigService } from './config/config.service' |
| 8 | +import { SlipReportError } from './slip-report' |
3 | 9 |
|
4 | 10 |
|
5 | 11 |
|
| 12 | +const FALLBACK_DEFAULT_CIRC_DESK_CODE = 'DEFAULT_CIRC_DESK' |
| 13 | +const STORAGE_KEY = 'last-used-options' |
| 14 | + |
| 15 | + |
6 | 16 | @Injectable({
|
7 | 17 | providedIn: 'root',
|
8 | 18 | })
|
9 | 19 | export class AppService {
|
10 | 20 |
|
| 21 | + initData?: InitData |
| 22 | + private _libraryCode?: string |
| 23 | + private _circDeskCode?: string |
| 24 | + columnOptions: ColumnOption[] | undefined = undefined |
| 25 | + lastSlipReportError: SlipReportError | undefined = undefined |
| 26 | + |
| 27 | + |
11 | 28 | constructor(
|
12 |
| - private initService: InitService |
13 |
| - ) {} |
| 29 | + private configService: ConfigService, |
| 30 | + private eventsService: CloudAppEventsService, |
| 31 | + private storeService: CloudAppStoreService, |
| 32 | + ) { |
| 33 | + this.eventsService.getInitData().subscribe(initData => { this.initData = initData }) |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + get circDeskCode(): string { |
| 38 | + return this._circDeskCode ?? FALLBACK_DEFAULT_CIRC_DESK_CODE |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + set circDeskCode(c) { |
| 43 | + this._circDeskCode = c.trim() |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + get columnOptionsAreCustomised(): boolean { |
| 48 | + let selected = this.columnOptions.filter(_ => _.include) |
| 49 | + let selectedByDefault = this.defaultColumnOptions.filter(_ => _.include) |
| 50 | + let r = ( |
| 51 | + selected.length !== selectedByDefault.length |
| 52 | + || ! _.every( |
| 53 | + _.zip(selected, selectedByDefault), |
| 54 | + ([a, b]) => { |
| 55 | + let r2 = ( |
| 56 | + a.code === b.code |
| 57 | + && a.hiddenInApp === b.hiddenInApp |
| 58 | + && a.limit === b.limit |
| 59 | + ) |
| 60 | + console.log('columnOptionsAreCustomised inner result', r2) |
| 61 | + return r2 |
| 62 | + } |
| 63 | + ) |
| 64 | + ) |
| 65 | + console.log('columnOptionsAreCustomised result', r) |
| 66 | + return r |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + get defaultCircDeskCode(): string { |
| 71 | + let libConfig = this.configService.config?.libraryConfigs?.filter(x => x.libraryCode == this.libraryCode) |
| 72 | + return libConfig?.[0]?.defaultCircDeskCode ?? FALLBACK_DEFAULT_CIRC_DESK_CODE |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + get defaultColumnOptions(): ColumnOption[] { |
| 77 | + let missingColumnDefinitions = new Map(COLUMNS_DEFINITIONS) // Copy because we are going to mutate it |
| 78 | + return [ |
| 79 | + // Start with the columns in the order they are from the app configuration, |
| 80 | + ...( |
| 81 | + (this.configService.config?.columnDefaults ?? []) |
| 82 | + // ... minus any that aren't defined anymore |
| 83 | + .filter(c => missingColumnDefinitions.has(c.code)) |
| 84 | + .map(c => { |
| 85 | + let name = missingColumnDefinitions.get(c.code).name |
| 86 | + missingColumnDefinitions.delete(c.code) |
| 87 | + return { include: false, limit: 0, hiddenInApp: false, ...c, name } |
| 88 | + }) |
| 89 | + ), |
| 90 | + // Add any columns not in the app configuration, in the order they appear in the column definitions |
| 91 | + ...( |
| 92 | + Array.from(missingColumnDefinitions.values()) |
| 93 | + .map(c => ({ code: c.code, name: c.name, include: false, limit: 0, hiddenInApp: false })) |
| 94 | + ) |
| 95 | + ] |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + get includedColumnOptions() { |
| 100 | + return this.columnOptions.filter(c => c.include) |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + get libraryCode(): string { |
| 105 | + return this._libraryCode |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + set libraryCode(c) { |
| 110 | + this._libraryCode = c.trim() |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + popLastSlipReportError(): SlipReportError | undefined { |
| 115 | + let e = this.lastSlipReportError |
| 116 | + this.lastSlipReportError = undefined |
| 117 | + return e |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + async resetColumnsToDefaults() { |
| 122 | + await this.configService.load() |
| 123 | + this.columnOptions = this.defaultColumnOptions |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + async loadLastUsed() { |
| 128 | + await this.configService.load() |
| 129 | + let lastUsedOptions = await this.storeService.get(STORAGE_KEY).toPromise() |
| 130 | + |
| 131 | + this.libraryCode = ( |
| 132 | + this.initData?.user?.currentlyAtLibCode |
| 133 | + ? this.initData.user.currentlyAtLibCode |
| 134 | + : (lastUsedOptions?.libraryCode ?? '') |
| 135 | + ) |
| 136 | + |
| 137 | + this.circDeskCode = lastUsedOptions?.circDeskCode ?? this.defaultCircDeskCode |
| 138 | + |
| 139 | + if (!lastUsedOptions?.columnOptions?.length) { |
| 140 | + // If there are no last used options saved, reset them |
| 141 | + return await this.resetColumnsToDefaults() |
| 142 | + } |
| 143 | + |
| 144 | + if (lastUsedOptions?.columnOptions) { |
| 145 | + let missingColumnDefinitions = new Map(COLUMNS_DEFINITIONS) // Copy because we are going to mutate it |
| 146 | + this.columnOptions = [ |
| 147 | + // Start with the columns in the order they are from the last used options, |
| 148 | + ...( |
| 149 | + (lastUsedOptions?.columnOptions ?? []) |
| 150 | + // ... minus any that aren't defined anymore |
| 151 | + .filter(c => missingColumnDefinitions.has(c.code)) |
| 152 | + .map(c => { |
| 153 | + let name = missingColumnDefinitions.get(c.code).name |
| 154 | + missingColumnDefinitions.delete(c.code) |
| 155 | + return { include: false, limit: 0, hiddenInApp: false, ...c, name } |
| 156 | + }) |
| 157 | + ), |
| 158 | + // Add any columns not in the app configuration, in the order they appear in the column definitions |
| 159 | + ...( |
| 160 | + Array.from(missingColumnDefinitions.values()) |
| 161 | + .map(c => ({ code: c.code, name: c.name, include: false, limit: 0, hiddenInApp: false })) |
| 162 | + ) |
| 163 | + ] |
| 164 | + } else { |
| 165 | + this.columnOptions = this.defaultColumnOptions |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + async saveLastUsed() { |
| 171 | + let lastUsedOptions = { |
| 172 | + libraryCode: this.libraryCode, |
| 173 | + circDeskCode: this.circDeskCode, |
| 174 | + columnOptions: ( |
| 175 | + this.columnOptionsAreCustomised |
| 176 | + ? this.columnOptions.map(c => ({ |
| 177 | + code: c.code, include: c.include, limit: c.limit, hiddenInApp: c.hiddenInApp |
| 178 | + })) |
| 179 | + : undefined |
| 180 | + ) |
| 181 | + } |
| 182 | + if (lastUsedOptions.columnOptions) { |
| 183 | + console.log('Saving customised column options') |
| 184 | + } else { |
| 185 | + console.log('Saving default column options') |
| 186 | + } |
| 187 | + return this.storeService.set(STORAGE_KEY, lastUsedOptions).toPromise() |
| 188 | + } |
14 | 189 |
|
15 | 190 | }
|
0 commit comments