Skip to content

Commit d4ae63c

Browse files
Mattia VianelliAndrea Barbasso
authored andcommitted
Merged in task/dspace-cris-2025_02_x/DSC-2764 (pull request DSpace#4333)
Task/dspace cris 2025 02 x/DSC-2764 Approved-by: Andrea Barbasso
2 parents 286d02b + 3459b85 commit d4ae63c

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 } 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
@@ -30,6 +30,10 @@ import {
3030
withLatestFrom,
3131
} from 'rxjs/operators';
3232

33+
import {
34+
APP_CONFIG,
35+
AppConfig,
36+
} from '../../../config/app-config.interface';
3337
import { getBitstreamDownloadRoute } from '../../app-routing-paths';
3438
import {
3539
BitstreamDataService,
@@ -127,6 +131,7 @@ export class LuckySearchComponent implements OnInit {
127131
private hardRedirectService: HardRedirectService,
128132
private notificationService: NotificationsService,
129133
private translateService: TranslateService,
134+
@Inject(APP_CONFIG) private appConfig: AppConfig,
130135
) {}
131136

132137
ngOnInit(): void {
@@ -236,12 +241,21 @@ export class LuckySearchComponent implements OnInit {
236241

237242
public redirect(url: string): void {
238243
if (isPlatformServer(this.platformId)) {
239-
this.hardRedirectService.redirect(url, 302);
244+
this.hardRedirectService.redirect(url, this.getRedirectCode());
240245
} else {
241246
this.router.navigateByUrl(url, { replaceUrl: true });
242247
}
243248
}
244249

250+
private getRedirectCode(): number {
251+
let code: number = this.appConfig?.luckySearchRedirects?.default || 302;
252+
if (Object.keys(this.appConfig?.luckySearchRedirects).includes(this.currentFilter.identifier)) {
253+
code = this.appConfig.luckySearchRedirects[this.currentFilter.identifier];
254+
}
255+
256+
return code;
257+
}
258+
245259
private parseBitstreamFilters(queryParams: Params): MetadataFilter[] {
246260
const metadataName = queryParams?.bitstreamMetadata;
247261
const metadataValue = queryParams?.bitstreamValue;

src/config/app-config.interface.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
LayoutConfig,
3939
} from './layout-config.interfaces';
4040
import { LoaderConfig } from './loader-config.interfaces';
41+
import { LuckySearchRedirectConfig } from './lucky-search-redirect-config';
4142
import { MarkdownConfig } from './markdown-config.interface';
4243
import { MatomoConfig } from './matomo-config.interface';
4344
import { MediaViewerConfig } from './media-viewer-config.interface';
@@ -109,6 +110,8 @@ interface AppConfig extends Config {
109110
metadataLinkViewPopoverData: MetadataLinkViewPopoverDataConfig;
110111
identifierSubtypes: IdentifierSubtypesConfig[];
111112
datadogRum?: DatadogRumConfig;
113+
permanentRedirectPaths?: string[];
114+
luckySearchRedirects?: LuckySearchRedirectConfig;
112115
}
113116

114117
/**

src/config/default-app-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
SuggestionConfig,
3939
} from './layout-config.interfaces';
4040
import { LoaderConfig } from './loader-config.interfaces';
41+
import { LuckySearchRedirectConfig } from './lucky-search-redirect-config';
4142
import { MarkdownConfig } from './markdown-config.interface';
4243
import { MatomoConfig } from './matomo-config.interface';
4344
import { MediaViewerConfig } from './media-viewer-config.interface';
@@ -1054,4 +1055,9 @@ export class DefaultAppConfig implements AppConfig {
10541055
trackLongTasks: true,
10551056
defaultPrivacyLevel: 'mask-user-input',
10561057
};
1058+
1059+
luckySearchRedirects: LuckySearchRedirectConfig = {
1060+
'legacy-id': 301,
1061+
default: 302,
1062+
};
10571063
}
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)