import { Subscription } from "../../domain/Subscriptions/Subscription" ; import { NullableRepository } from "ddd-base" ; import { splice } from "@immutable-array/prototype" ; const UncategorizedName = "Uncategorized" ; export class SubscriptionRepository extends NullableRepository < Subscription > { categoryMap : { [ index ] : Subscription [ ] ; } ; constructor ( ) { super ( ) ; this . categoryMap = { } ; } groupByCategory ( ) { return this . categoryMap ; } getAllCategoryNames ( ) { return Object . keys ( this . categoryMap ) ; } getAllByCategories ( category ) { return this . categoryMap [ category ] ; } param { Subscription [ ] } subscriptions saveBuild ( subscriptions ) { } save ( aSubscription ) { super . save ( aSubscription ) ; const categories = aSubscription . categories . length > 0 ? aSubscription . categories : [ UncategorizedName ] ; categories . forEach ( ( category ) => { if ( this . categoryMap [ category ] !== undefined ) { const subscriptions = this . categoryMap [ category ] ; const index = subscriptions . findIndex ( ( subscription ) => subscription . equals ( aSubscription ) ) ; if ( index === - 1 ) { this . categoryMap = { ... this . categoryMap , [ category ] : subscriptions . concat ( aSubscription ) } ; } else { this . categoryMap = { ... this . categoryMap , [ category ] : splice ( subscriptions , index , 1 , aSubscription ) } ; } } else { this . categoryMap = { ... this . categoryMap , [ category ] : [ aSubscription ] } ; } } ) ; } }