diff --git a/app/institutions/dashboard/preprints/controller.ts b/app/institutions/dashboard/preprints/controller.ts index e7cfcdb957..fde45219d2 100644 --- a/app/institutions/dashboard/preprints/controller.ts +++ b/app/institutions/dashboard/preprints/controller.ts @@ -1,6 +1,7 @@ import Controller from '@ember/controller'; import { inject as service } from '@ember/service'; import Intl from 'ember-intl/services/intl'; +import { getSingleOsfmapValue } from 'ember-osf-web/packages/osfmap/jsonld'; import { ResourceTypeFilterValue } from 'osf-components/components/search-page/component'; import { ObjectListColumn } from '../-components/object-list/component'; @@ -21,12 +22,12 @@ export default class InstitutionDashboardPreprints extends Controller { }, { // Date created name: this.intl.t('institutions.dashboard.object-list.table-headers.created_date'), - getValue: searchResult => searchResult.getResourceMetadataField('dateCreated'), + getValue: searchResult => getSingleOsfmapValue(searchResult.resourceMetadata, ['dateCreated']), sortKey: 'dateCreated', }, { // Date modified name: this.intl.t('institutions.dashboard.object-list.table-headers.modified_date'), - getValue: searchResult => searchResult.getResourceMetadataField('dateModified'), + getValue: searchResult => getSingleOsfmapValue(searchResult.resourceMetadata, ['dateModified']), sortKey: 'dateModified', }, { // DOI diff --git a/app/institutions/dashboard/projects/controller.ts b/app/institutions/dashboard/projects/controller.ts index 42568ef565..5efb24ef4e 100644 --- a/app/institutions/dashboard/projects/controller.ts +++ b/app/institutions/dashboard/projects/controller.ts @@ -1,6 +1,7 @@ import Controller from '@ember/controller'; import { inject as service } from '@ember/service'; import Intl from 'ember-intl/services/intl'; +import { getSingleOsfmapValue } from 'ember-osf-web/packages/osfmap/jsonld'; import humanFileSize from 'ember-osf-web/utils/human-file-size'; @@ -23,12 +24,12 @@ export default class InstitutionDashboardProjects extends Controller { }, { // Date created name: this.intl.t('institutions.dashboard.object-list.table-headers.created_date'), - getValue: searchResult => searchResult.getResourceMetadataField('dateCreated'), + getValue: searchResult => getSingleOsfmapValue(searchResult.resourceMetadata, ['dateCreated']), sortKey: 'dateCreated', }, { // Date modified name: this.intl.t('institutions.dashboard.object-list.table-headers.modified_date'), - getValue: searchResult => searchResult.getResourceMetadataField('dateModified'), + getValue: searchResult => getSingleOsfmapValue(searchResult.resourceMetadata, ['dateModified']), sortKey: 'dateModified', }, { // DOI @@ -42,7 +43,7 @@ export default class InstitutionDashboardProjects extends Controller { { // Total data stored name: this.intl.t('institutions.dashboard.object-list.table-headers.total_data_stored'), getValue: searchResult => { - const byteCount = searchResult.getResourceMetadataField('storageByteCount'); + const byteCount = getSingleOsfmapValue(searchResult.resourceMetadata, ['storageByteCount']); return byteCount ? humanFileSize(byteCount) : this.intl.t('institutions.dashboard.object-list.table-items.no-storage-info'); }, @@ -76,7 +77,7 @@ export default class InstitutionDashboardProjects extends Controller { name: this.intl.t('institutions.dashboard.object-list.table-headers.addons'), getValue: searchResult => { const field = this.intl.t('institutions.dashboard.object-list.table-headers.addons'); - return searchResult.configuredAddonNames || + return searchResult.configuredAddonNames.length ? searchResult.configuredAddonNames : this.intl.t('institutions.dashboard.object-list.table-items.no-info', { field }); }, }, diff --git a/app/institutions/dashboard/registrations/controller.ts b/app/institutions/dashboard/registrations/controller.ts index 209c2e146b..5a2dcd5a5a 100644 --- a/app/institutions/dashboard/registrations/controller.ts +++ b/app/institutions/dashboard/registrations/controller.ts @@ -1,6 +1,7 @@ import Controller from '@ember/controller'; import { inject as service } from '@ember/service'; import Intl from 'ember-intl/services/intl'; +import { getSingleOsfmapValue } from 'ember-osf-web/packages/osfmap/jsonld'; import humanFileSize from 'ember-osf-web/utils/human-file-size'; import { ResourceTypeFilterValue } from 'osf-components/components/search-page/component'; @@ -22,12 +23,12 @@ export default class InstitutionDashboardRegistrations extends Controller { }, { // Date created name: this.intl.t('institutions.dashboard.object-list.table-headers.created_date'), - getValue: searchResult => searchResult.getResourceMetadataField('dateCreated'), + getValue: searchResult => getSingleOsfmapValue(searchResult.resourceMetadata, ['dateCreated']), sortKey: 'dateCreated', }, { // Date modified name: this.intl.t('institutions.dashboard.object-list.table-headers.modified_date'), - getValue: searchResult => searchResult.getResourceMetadataField('dateModified'), + getValue: searchResult => getSingleOsfmapValue(searchResult.resourceMetadata, ['dateModified']), sortKey: 'dateModified', }, { // DOI @@ -41,7 +42,7 @@ export default class InstitutionDashboardRegistrations extends Controller { { // Total data stored name: this.intl.t('institutions.dashboard.object-list.table-headers.total_data_stored'), getValue: searchResult => { - const byteCount = searchResult.getResourceMetadataField('storageByteCount'); + const byteCount = getSingleOsfmapValue(searchResult.resourceMetadata, ['storageByteCount']); return byteCount ? humanFileSize(byteCount) : this.intl.t('institutions.dashboard.object-list.table-items.no-storage-info'); }, diff --git a/app/models/search-result.ts b/app/models/search-result.ts index b092ac5573..45a41c9c43 100644 --- a/app/models/search-result.ts +++ b/app/models/search-result.ts @@ -2,6 +2,7 @@ import Model, { attr, belongsTo } from '@ember-data/model'; import { inject as service } from '@ember/service'; import { htmlSafe } from '@ember/template'; import IntlService from 'ember-intl/services/intl'; +import { getOsfmapValues, getSingleOsfmapObject, getSingleOsfmapValue } from 'ember-osf-web/packages/osfmap/jsonld'; import { languageFromLanguageCode } from 'osf-components/components/file-metadata-manager/component'; import IndexCardModel from './index-card'; @@ -62,22 +63,22 @@ export default class SearchResultModel extends Model { get displayTitle() { if (this.resourceType === 'user') { - return this.resourceMetadata['name'][0]['@value']; + return getSingleOsfmapValue(this.resourceMetadata, ['name']); } else if (this.resourceType === 'file') { - return this.resourceMetadata['fileName'][0]['@value']; + return getSingleOsfmapValue(this.resourceMetadata, ['fileName']); } - return this.resourceMetadata['title']?.[0]['@value']; + return getSingleOsfmapValue(this.resourceMetadata, ['title']); } get fileTitle() { if (this.resourceType === 'file') { - return this.resourceMetadata.title?.[0]['@value']; + return getSingleOsfmapValue(this.resourceMetadata, ['title']); } return null; } get description() { - return this.resourceMetadata.description?.[0]?.['@value']; + return getSingleOsfmapValue(this.resourceMetadata, ['description']); } get absoluteUrl() { @@ -90,14 +91,14 @@ export default class SearchResultModel extends Model { if (this.resourceType === 'user') { if (this.resourceMetadata.affiliation) { return this.resourceMetadata.affiliation.map((item: any) => - ({ name: item.name[0]['@value'], absoluteUrl: item['@id'] })); + ({ name: getSingleOsfmapValue(item, ['name']), absoluteUrl: item['@id'] })); } } else if (this.resourceMetadata.creator) { return this.resourceMetadata.creator?.map((item: any) => - ({ name: item.name[0]['@value'], absoluteUrl: item['@id'] })); + ({ name: getSingleOsfmapValue(item, ['name']), absoluteUrl: item['@id'] })); } else if (this.isContainedBy?.[0]?.creator) { return this.isContainedBy[0].creator.map((item: any) => - ({ name: item.name?.[0]?.['@value'], absoluteUrl: item['@id'] })); + ({ name: getSingleOsfmapValue(item, ['name']), absoluteUrl: item['@id'] })); } } @@ -110,22 +111,22 @@ export default class SearchResultModel extends Model { return [ { label: this.intl.t('osf-components.search-result-card.date_registered'), - date: this.resourceMetadata.dateCreated?.[0]['@value'], + date: getSingleOsfmapValue(this.resourceMetadata, ['dateCreated']), }, { label: this.intl.t('osf-components.search-result-card.date_modified'), - date: this.resourceMetadata.dateModified?.[0]['@value'], + date: getSingleOsfmapValue(this.resourceMetadata, ['dateModified']), }, ]; default: return [ { label: this.intl.t('osf-components.search-result-card.date_created'), - date: this.resourceMetadata.dateCreated?.[0]['@value'], + date: getSingleOsfmapValue(this.resourceMetadata, ['dateCreated']), }, { label: this.intl.t('osf-components.search-result-card.date_modified'), - date: this.resourceMetadata.dateModified?.[0]['@value'], + date: getSingleOsfmapValue(this.resourceMetadata, ['dateModified']), }, ]; } @@ -163,8 +164,8 @@ export default class SearchResultModel extends Model { const isPartOfCollection = this.resourceMetadata.isPartOfCollection; if (isPartOfCollection) { return { - title: this.resourceMetadata.isPartOfCollection?.[0]?.title?.[0]?.['@value'], - absoluteUrl: this.resourceMetadata.isPartOfCollection?.[0]?.['@id'], + title: getSingleOsfmapValue(this.resourceMetadata, ['isPartOfCollection', 'title']), + absoluteUrl: getSingleOsfmapValue(this.resourceMetadata, ['isPartOfCollection']), }; } return null; @@ -172,7 +173,8 @@ export default class SearchResultModel extends Model { get languageFromCode() { if (this.resourceMetadata.language) { - return languageFromLanguageCode(this.resourceMetadata.language[0]['@value']); + const language = getSingleOsfmapValue(this.resourceMetadata, ['language']); + return languageFromLanguageCode(language); } return null; } @@ -180,8 +182,8 @@ export default class SearchResultModel extends Model { get funders() { if (this.resourceMetadata.funder) { return this.resourceMetadata.funder.map( (item: any) => ({ - name: item.name[0]['@value'], - identifier: item.identifier?.[0]['@value'], + name: getSingleOsfmapValue(item, ['name']), + identifier: getSingleOsfmapValue(item, ['identifier']), })); } return null; @@ -190,8 +192,8 @@ export default class SearchResultModel extends Model { get nodeFunders() { if (this.resourceMetadata.isContainedBy?.[0]?.funder) { return this.resourceMetadata.isContainedBy[0].funder.map( (item: any) => ({ - name: item.name[0]['@value'], - identifier: item.identifier?.[0]['@value'], + name: getSingleOsfmapValue(item, ['name']), + identifier: getSingleOsfmapValue(item, ['identifier']), })); } return null; @@ -200,8 +202,8 @@ export default class SearchResultModel extends Model { get provider() { if (this.resourceMetadata.publisher) { return { - name: this.resourceMetadata.publisher[0]?.name?.[0]['@value'], - identifier: this.resourceMetadata.publisher[0]['@id'], + name: getSingleOsfmapValue(this.resourceMetadata, ['publisher', 'name']), + identifier: getSingleOsfmapValue(this.resourceMetadata, ['publisher']), }; } return null; @@ -214,8 +216,8 @@ export default class SearchResultModel extends Model { get license() { if (this.resourceMetadata.rights) { return { - name: this.resourceMetadata.rights?.[0]?.name?.[0]?.['@value'], - identifier: this.resourceMetadata.rights?.[0]?.['@id'], + name: getSingleOsfmapValue(this.resourceMetadata, ['rights', 'name']), + identifier: getSingleOsfmapValue(this.resourceMetadata, ['rights']), }; } return null; @@ -224,9 +226,9 @@ export default class SearchResultModel extends Model { get nodeLicense() { if (this.resourceMetadata.isContainedBy?.[0]?.rights) { return { - name: this.resourceMetadata.isContainedBy[0].rights?.[0]?.name?.[0]?.['@value'], - identifier: this.resourceMetadata.rights?.[0]?.['@id'] || - this.resourceMetadata.isContainedBy[0].rights[0]?.['@id'], + name: getSingleOsfmapValue(this.resourceMetadata, ['isContainedBy', 'rights', 'name']), + identifier: getSingleOsfmapValue(this.resourceMetadata, ['rights']) || + getSingleOsfmapValue(this.resourceMetadata, ['isContainedBy', 'rights']), }; } return null; @@ -267,7 +269,7 @@ export default class SearchResultModel extends Model { } get resourceNature() { - return this.resourceMetadata.resourceNature?.[0]?.displayLabel?.[0]?.['@value']; + return getSingleOsfmapValue(this.resourceMetadata, ['resourceNature','displayLabel']); } get hasDataResource() { @@ -291,7 +293,7 @@ export default class SearchResultModel extends Model { } get registrationTemplate() { - return this.resourceMetadata.conformsTo?.[0]?.title?.[0]?.['@value']; + return getSingleOsfmapValue(this.resourceMetadata, ['conformsTo', 'title']); } get isWithdrawn() { @@ -299,30 +301,24 @@ export default class SearchResultModel extends Model { } get configuredAddonNames() { - return this.resourceMetadata.hasOsfAddon?.map((item: any) => item.prefLabel[0]['@value']); + return getOsfmapValues(this.resourceMetadata, ['hasOsfAddon', 'prefLabel']); } get storageRegion() { - return this.resourceMetadata.storageRegion?.[0]?.prefLabel[0]['@value']; + return getSingleOsfmapValue(this.resourceMetadata, ['storageRegion', 'prefLabel']); } get usageMetrics() { - if (!this.resourceMetadata.usage) { + const usage = getSingleOsfmapObject(this.resourceMetadata, ['usage']); + if (!usage) { return null; } - const temporalCoverage = this.resourceMetadata.usage[0]['temporalCoverage']; - const viewCount = this.resourceMetadata.usage[0]['viewCount']; - const downloadCount = this.resourceMetadata.usage[0]['downloadCount']; return { - period: temporalCoverage ? temporalCoverage[0]['@value'] : null, - viewCount: viewCount ? viewCount[0]['@value'] : null, - downloadCount: downloadCount ? downloadCount[0]['@value'] : null, + period: getSingleOsfmapValue(usage, ['temporalCoverage']), + viewCount: getSingleOsfmapValue(usage, ['viewCount']), + downloadCount: getSingleOsfmapValue(usage, ['downloadCount']), }; } - - getResourceMetadataField(field: string) { - return this.resourceMetadata[field]?.[0]?.['@value']; - } } declare module 'ember-data/types/registries/model' {