Skip to content

Commit 82d2c2e

Browse files
Mattia VianelliAndrea Barbasso
authored andcommitted
Merged in task/dspace-cris-2024_02_x/DSC-2764 (pull request DSpace#4332)
Task/dspace cris 2024 02 x/DSC-2764 Approved-by: Andrea Barbasso
2 parents 05e9e68 + 2568b81 commit 82d2c2e

5 files changed

Lines changed: 85 additions & 1 deletion

File tree

src/app/lucky-search/search/lucky-search.component.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { TranslateModule } from '@ngx-translate/core';
1515
import { of as observableOf } from 'rxjs';
1616

17+
import { APP_CONFIG } from '../../../config/app-config.interface';
1718
import { getBitstreamDownloadRoute } from '../../app-routing-paths';
1819
import {
1920
SortDirection,
@@ -131,6 +132,7 @@ describe('LuckySearchComponent', () => {
131132
{ provide: HardRedirectService, useValue: hardRedirectService },
132133
{ provide: PLATFORM_ID, useValue: 'browser' },
133134
{ provide: NotificationsService, useValue: new NotificationsServiceStub() },
135+
{ provide: APP_CONFIG, useValue: {} },
134136
],
135137
}).overrideComponent(LuckySearchComponent, {
136138
remove: {
@@ -355,4 +357,57 @@ describe('LuckySearchComponent', () => {
355357
});
356358

357359
});
360+
361+
describe('', () => {
362+
beforeEach(() => {
363+
fixture = TestBed.createComponent(LuckySearchComponent);
364+
component = fixture.componentInstance;
365+
});
366+
367+
it('should return default code when no specific identifier is found', () => {
368+
// @ts-ignore: Accessing private method for testing
369+
component.appConfig = {
370+
luckySearchRedirects: {
371+
default: 301,
372+
},
373+
} as any;
374+
// @ts-ignore: Accessing private method for testing
375+
component.currentFilter = { identifier: 'unknown' };
376+
377+
// @ts-ignore: Accessing private method for testing
378+
const result = component.getRedirectCode();
379+
380+
expect(result).toBe(301);
381+
});
382+
383+
it('should return 302 when default is not set and identifier is not found', () => {
384+
// @ts-ignore: Accessing private method for testing
385+
component.appConfig = {
386+
luckySearchRedirects: {},
387+
} as any;
388+
// @ts-ignore: Accessing private method for testing
389+
component.currentFilter = { identifier: 'unknown' };
390+
391+
// @ts-ignore: Accessing private method for testing
392+
const result = component.getRedirectCode();
393+
expect(result).toBe(302);
394+
});
395+
396+
it('should return specific code for known identifier', () => {
397+
// @ts-ignore: Accessing private method for testing
398+
component.appConfig = {
399+
luckySearchRedirects: {
400+
default: 302,
401+
'legacy-id': 301,
402+
},
403+
};
404+
// @ts-ignore: Accessing private method for testing
405+
component.currentFilter = { identifier: 'legacy-id' };
406+
407+
// @ts-ignore: Accessing private method for testing
408+
const result = component.getRedirectCode();
409+
expect(result).toBe(301);
410+
});
411+
412+
});
358413
});

src/app/lucky-search/search/lucky-search.component.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import {
3232
withLatestFrom,
3333
} from 'rxjs/operators';
3434

35+
import {
36+
APP_CONFIG,
37+
AppConfig,
38+
} from '../../../config/app-config.interface';
3539
import { getBitstreamDownloadRoute } from '../../app-routing-paths';
3640
import {
3741
BitstreamDataService,
@@ -132,6 +136,7 @@ export class LuckySearchComponent implements OnInit {
132136
private hardRedirectService: HardRedirectService,
133137
private notificationService: NotificationsService,
134138
private translateService: TranslateService,
139+
@Inject(APP_CONFIG) private appConfig: AppConfig,
135140
) {}
136141

137142
ngOnInit(): void {
@@ -241,12 +246,21 @@ export class LuckySearchComponent implements OnInit {
241246

242247
public redirect(url: string): void {
243248
if (isPlatformServer(this.platformId)) {
244-
this.hardRedirectService.redirect(url, 302);
249+
this.hardRedirectService.redirect(url, this.getRedirectCode());
245250
} else {
246251
this.router.navigateByUrl(url, { replaceUrl: true });
247252
}
248253
}
249254

255+
private getRedirectCode(): number {
256+
let code: number = this.appConfig?.luckySearchRedirects?.default || 302;
257+
if (Object.keys(this.appConfig?.luckySearchRedirects).includes(this.currentFilter.identifier)) {
258+
code = this.appConfig.luckySearchRedirects[this.currentFilter.identifier];
259+
}
260+
261+
return code;
262+
}
263+
250264
private parseBitstreamFilters(queryParams: Params): MetadataFilter[] {
251265
const metadataName = queryParams?.bitstreamMetadata;
252266
const metadataValue = queryParams?.bitstreamValue;

src/config/app-config.interface.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
LayoutConfig,
3838
} from './layout-config.interfaces';
3939
import { LoaderConfig } from './loader-config.interfaces';
40+
import { LuckySearchRedirectConfig } from './lucky-search-redirect-config';
4041
import { MarkdownConfig } from './markdown-config.interface';
4142
import { MatomoConfig } from './matomo-config.interface';
4243
import { MediaViewerConfig } from './media-viewer-config.interface';
@@ -108,6 +109,8 @@ interface AppConfig extends Config {
108109
identifierSubtypes: IdentifierSubtypesConfig[];
109110
datadogRum?: DatadogRumConfig;
110111
matomo?: MatomoConfig;
112+
permanentRedirectPaths?: string[];
113+
luckySearchRedirects?: LuckySearchRedirectConfig;
111114
}
112115

113116
/**

src/config/default-app-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
SuggestionConfig,
3838
} from './layout-config.interfaces';
3939
import { LoaderConfig } from './loader-config.interfaces';
40+
import { LuckySearchRedirectConfig } from './lucky-search-redirect-config';
4041
import { MarkdownConfig } from './markdown-config.interface';
4142
import { MatomoConfig } from './matomo-config.interface';
4243
import { MediaViewerConfig } from './media-viewer-config.interface';
@@ -1026,4 +1027,9 @@ export class DefaultAppConfig implements AppConfig {
10261027
};
10271028

10281029
matomo: MatomoConfig = {};
1030+
1031+
luckySearchRedirects: LuckySearchRedirectConfig = {
1032+
'legacy-id': 301,
1033+
default: 302,
1034+
};
10291035
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Config } from './config.interface';
2+
3+
export interface LuckySearchRedirectConfig extends Config {
4+
[indexName: string]: number;
5+
default: number;
6+
}

0 commit comments

Comments
 (0)