@@ -12,7 +12,7 @@ import { OpenaireSuggestionTarget } from '../../core/openaire/reciter-suggestion
1212import { ResearcherProfileService } from '../../core/profile/researcher-profile.service' ;
1313import { AuthService } from '../../core/auth/auth.service' ;
1414import { EPerson } from '../../core/eperson/models/eperson.model' ;
15- import { isNotEmpty } from '../../shared/empty.util' ;
15+ import { hasValue , isNotEmpty } from '../../shared/empty.util' ;
1616import { ResearcherProfile } from '../../core/profile/model/researcher-profile.model' ;
1717import {
1818 getAllSucceededRemoteDataPayload ,
@@ -24,6 +24,9 @@ import { OpenaireSuggestion } from '../../core/openaire/reciter-suggestions/mode
2424import { WorkspaceitemDataService } from '../../core/submission/workspaceitem-data.service' ;
2525import { TranslateService } from '@ngx-translate/core' ;
2626import { NoContent } from '../../core/shared/NoContent.model' ;
27+ import { environment } from '../../../environments/environment' ;
28+ import { SuggestionConfig } from '../../../config/layout-config.interfaces' ;
29+ import { WorkspaceItem } from '../../core/submission/models/workspaceitem.model' ;
2730
2831export interface SuggestionBulkResult {
2932 success : number ;
@@ -170,8 +173,10 @@ export class SuggestionsService {
170173 */
171174 public approveAndImport ( workspaceitemService : WorkspaceitemDataService ,
172175 suggestion : OpenaireSuggestion ,
173- collectionId : string ) : Observable < string > {
174- return workspaceitemService . importExternalSourceEntry ( suggestion . externalSourceUri , collectionId )
176+ collectionId : string ) : Observable < WorkspaceItem > {
177+
178+ const resolvedCollectionId = this . resolveCollectionId ( suggestion , collectionId ) ;
179+ return workspaceitemService . importExternalSourceEntry ( suggestion . externalSourceUri , resolvedCollectionId )
175180 . pipe (
176181 getFirstSucceededRemoteDataPayload ( ) ,
177182 catchError ( ( error ) => of ( null ) )
@@ -200,7 +205,7 @@ export class SuggestionsService {
200205
201206 return forkJoin ( suggestions . map ( ( suggestion : OpenaireSuggestion ) =>
202207 this . approveAndImport ( workspaceitemService , suggestion , collectionId ) ) )
203- . pipe ( map ( ( results : string [ ] ) => {
208+ . pipe ( map ( ( results : WorkspaceItem [ ] ) => {
204209 return {
205210 success : results . filter ( ( result ) => result != null ) . length ,
206211 fails : results . filter ( ( result ) => result == null ) . length
@@ -240,10 +245,52 @@ export class SuggestionsService {
240245 public getNotificationSuggestionInterpolation ( suggestionTarget : OpenaireSuggestionTarget ) : any {
241246 return {
242247 count : suggestionTarget . total ,
243- source : this . translateService . instant ( 'reciter.suggestion.source.' + suggestionTarget . source ) ,
248+ source : this . translateService . instant ( this . translateSuggestionSource ( suggestionTarget . source ) ) ,
249+ type : this . translateService . instant ( this . translateSuggestionType ( suggestionTarget . source ) ) ,
244250 suggestionId : suggestionTarget . id ,
245251 displayName : suggestionTarget . display
246252 } ;
247253 }
248254
255+ public translateSuggestionType ( source : string ) : string {
256+ return 'reciter.suggestion.type.' + source ;
257+ }
258+
259+ public translateSuggestionSource ( source : string ) : string {
260+ return 'reciter.suggestion.source.' + source ;
261+ }
262+
263+ /**
264+ * If the provided collectionId ha no value, tries to resolve it by suggestion source.
265+ * @param suggestion
266+ * @param collectionId
267+ */
268+ public resolveCollectionId ( suggestion : OpenaireSuggestion , collectionId ) : string {
269+ if ( hasValue ( collectionId ) ) {
270+ return collectionId ;
271+ }
272+ return environment . suggestion
273+ . find ( ( suggestionConf : SuggestionConfig ) => suggestionConf . source === suggestion . source )
274+ . collectionId ;
275+ }
276+
277+ /**
278+ * Return true if all the suggestion are configured with the same fixed collection
279+ * in the configuration.
280+ * @param suggestions
281+ */
282+ public isCollectionFixed ( suggestions : OpenaireSuggestion [ ] ) : boolean {
283+ return this . getFixedCollectionIds ( suggestions ) . length === 1 ;
284+ }
285+
286+ private getFixedCollectionIds ( suggestions : OpenaireSuggestion [ ] ) : string [ ] {
287+ const collectionIds = { } ;
288+ suggestions . forEach ( ( suggestion : OpenaireSuggestion ) => {
289+ const conf = environment . suggestion . find ( ( suggestionConf : SuggestionConfig ) => suggestionConf . source === suggestion . source ) ;
290+ if ( hasValue ( conf ) ) {
291+ collectionIds [ conf . collectionId ] = true ;
292+ }
293+ } ) ;
294+ return Object . keys ( collectionIds ) ;
295+ }
249296}
0 commit comments