diff --git a/src/app/core/record-affiliations-sort/record-affiliations-sort.service.ts b/src/app/core/record-affiliations-sort/record-affiliations-sort.service.ts index afc6f0ca1d..1c7eaabde6 100644 --- a/src/app/core/record-affiliations-sort/record-affiliations-sort.service.ts +++ b/src/app/core/record-affiliations-sort/record-affiliations-sort.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core' import { MonthDayYearDate } from 'src/app/types' import { AffiliationUIGroup, - AffiliationGroup, + AffiliationGroup, AffiliationType, AffiliationTypeValue, } from 'src/app/types/record-affiliation.endpoint' import { UserRecordOptions } from 'src/app/types/record.local' @@ -29,97 +29,44 @@ export class AffiliationsSortService { by = 'end', type = null ): AffiliationUIGroup[] { - affiliationGroups.forEach((x) => { - let affiliationGroup: AffiliationGroup[] = x.affiliationGroup - if (!type || type === x.type) { - if (by === 'start' || by === 'end') { - affiliationGroup = affiliationGroup.sort((a, b) => { - const dateStartA = this.yearMonthDaytoDate( - a.defaultAffiliation.startDate - ) - const dateStartB = this.yearMonthDaytoDate( - b.defaultAffiliation.startDate - ) - const dateFinishA = this.yearMonthDaytoDate( - a.defaultAffiliation.endDate - ) - const dateFinishB = this.yearMonthDaytoDate( - b.defaultAffiliation.endDate - ) + if (type === 'PROFESSIONAL_ACTIVITIES' && by === 'type') { + const affiliations = affiliationGroups.filter(affiliationGroup => affiliationGroup.type !== 'PROFESSIONAL_ACTIVITIES') + const professionalActivities = affiliationGroups.filter(affiliationGroups => affiliationGroups.type === 'PROFESSIONAL_ACTIVITIES')[0] - if (by === 'end') { - // If the end date is not the same use it to sort - if (dateFinishA.getTime() !== dateFinishB.getTime()) { - return ( - (dateFinishB.getTime() - dateFinishA.getTime()) * - (ascending ? -1 : 1) - ) - } else if (dateStartB.getTime() !== dateStartA.getTime()) { - // If the end date is the same use the start date to sort - return ( - (dateStartB.getTime() - dateStartA.getTime()) * - (ascending ? -1 : 1) - ) - } else { - return ( - ( - '' + a.defaultAffiliation.affiliationName.value - ).localeCompare( - '' + b.defaultAffiliation.affiliationName.value - ) * (ascending ? 1 : -1) - ) - } - } - if (by === 'start') { - // If the start is not the same use it to sort - if (dateStartB.getTime() !== dateStartA.getTime()) { - return ( - (dateStartB.getTime() - dateStartA.getTime()) * - (ascending ? -1 : 1) - ) - } else if (dateFinishB.getTime() !== dateFinishA.getTime()) { - // If the start date is the same use the end date to sort - return ( - (dateFinishB.getTime() - dateFinishA.getTime()) * - (ascending ? -1 : 1) - ) - } else { - return ( - ( - '' + a.defaultAffiliation.affiliationName.value - ).localeCompare( - '' + b.defaultAffiliation.affiliationName.value - ) * (ascending ? 1 : -1) - ) - } - } - }) - } + const distinction = this.sortByEndDateAndType(professionalActivities.affiliationGroup, AffiliationType.distinction) + const invitedPositions = this.sortByEndDateAndType(professionalActivities.affiliationGroup, AffiliationType['invited-position']) + const membership = this.sortByEndDateAndType(professionalActivities.affiliationGroup, AffiliationType.membership) + const service = this.sortByEndDateAndType(professionalActivities.affiliationGroup, AffiliationType.service) - if (by === 'title') { - affiliationGroup.sort((a, b) => { - return ( - '' + a.defaultAffiliation.affiliationName.value - ).localeCompare('' + b.defaultAffiliation.affiliationName.value) - }) - if (!ascending) { - affiliationGroup.reverse() + affiliations.push({ + type: 'PROFESSIONAL_ACTIVITIES', + affiliationGroup: ascending ? [...distinction, ...invitedPositions, ...membership, ...service] : [...service, ...membership, ...invitedPositions, ...distinction] + }) + return affiliations + } else { + affiliationGroups.forEach((x) => { + let affiliationGroup: AffiliationGroup[] = x.affiliationGroup + if (!type || type === x.type) { + if (by === 'start' || by === 'end') { + affiliationGroup = affiliationGroup.sort((a, b) => { + return this.sortByDate(a, b, ascending, by) + }) } - } - if (by === 'type') { - affiliationGroup.sort((a, b) => { - return ('' + a.affiliationType).localeCompare( - '' + b.affiliationType - ) - }) - if (!ascending) { - affiliationGroup.reverse() + if (by === 'title') { + affiliationGroup.sort((a, b) => { + return ( + '' + a.defaultAffiliation.affiliationName.value + ).localeCompare('' + b.defaultAffiliation.affiliationName.value) + }) + if (!ascending) { + affiliationGroup.reverse() + } } } - } - }) - return affiliationGroups + }) + return affiliationGroups + } } yearMonthDaytoDate(x: MonthDayYearDate): Date { @@ -140,4 +87,74 @@ export class AffiliationsSortService { } return date } + + private sortByDate(a: AffiliationGroup, b: AffiliationGroup, ascending: boolean, by: 'start' | 'end'): number { + const dateStartA = this.yearMonthDaytoDate( + a.defaultAffiliation.startDate + ) + const dateStartB = this.yearMonthDaytoDate( + b.defaultAffiliation.startDate + ) + const dateFinishA = this.yearMonthDaytoDate( + a.defaultAffiliation.endDate + ) + const dateFinishB = this.yearMonthDaytoDate( + b.defaultAffiliation.endDate + ) + + if (by === 'end') { + // If the end date is not the same use it to sort + if (dateFinishA.getTime() !== dateFinishB.getTime()) { + return ( + (dateFinishB.getTime() - dateFinishA.getTime()) * + (ascending ? -1 : 1) + ) + } else if (dateStartB.getTime() !== dateStartA.getTime()) { + // If the end date is the same use the start date to sort + return ( + (dateStartB.getTime() - dateStartA.getTime()) * + (ascending ? -1 : 1) + ) + } else { + return ( + ( + '' + a.defaultAffiliation.affiliationName.value + ).localeCompare( + '' + b.defaultAffiliation.affiliationName.value + ) * (ascending ? 1 : -1) + ) + } + } + if (by === 'start') { + // If the start is not the same use it to sort + if (dateStartB.getTime() !== dateStartA.getTime()) { + return ( + (dateStartB.getTime() - dateStartA.getTime()) * + (ascending ? -1 : 1) + ) + } else if (dateFinishB.getTime() !== dateFinishA.getTime()) { + // If the start date is the same use the end date to sort + return ( + (dateFinishB.getTime() - dateFinishA.getTime()) * + (ascending ? -1 : 1) + ) + } else { + return ( + ( + '' + a.defaultAffiliation.affiliationName.value + ).localeCompare( + '' + b.defaultAffiliation.affiliationName.value + ) * (ascending ? 1 : -1) + ) + } + } + } + + private sortByEndDateAndType(affiliationGroup: AffiliationGroup[], type: AffiliationType): AffiliationGroup[] { + return affiliationGroup + .filter(affiliationGroup => affiliationGroup.defaultAffiliation.affiliationType.value === type) + .sort((a, b) => { + return this.sortByDate(a, b, false, 'end') + }) + } }