import { User } from "../models/user" ; import TopicAreaFactory from "../factories/topicarea-factory" ; import DashboardFactory from "../factories/dashboard-factory" ; import BaseRepository from "./base" ; import { TopicArea , TopicAreaList , TopicAreaItem } from "../models/topicarea" ; import { ItemNotFound } from "../errors" ; import { DashboardItem } from "../models/dashboard" ; import DashboardRepository from "./dashboard-repo" ; class TopicAreaRepository extends BaseRepository { private static instance : TopicAreaRepository ; private constructor ( ) { super ( ) ; } static getInstance ( ) { if ( ! TopicAreaRepository . instance ) { TopicAreaRepository . instance = new TopicAreaRepository ( ) ; } return TopicAreaRepository . instance ; } public async create ( topicArea ) { await this . dynamodb . put ( { TableName : this . tableName , Item : TopicAreaFactory . toItem ( topicArea ) , } ) ; } public async getTopicAreaById ( id ) { const result = await this . dynamodb . get ( { TableName : this . tableName , Key : { pk : TopicAreaFactory . itemId ( id ) , sk : TopicAreaFactory . itemId ( id ) , } , } ) ; if ( ! result . Item ) { throw new ItemNotFound ( ) ; } return TopicAreaFactory . fromItem ( result . Item as TopicAreaItem ) ; } public async list ( ) { const result = await this . dynamodb . query ( { TableName : this . tableName , IndexName : "byType" , KeyConditionExpression : "#type = :type" , ExpressionAttributeNames : { "#type" : "type" , } , ExpressionAttributeValues : { ":type" : "TopicArea" , } , } ) ; if ( ! result . Items ) { return [ ] ; } const topicareas = result . Items . map ( ( item ) => TopicAreaFactory . fromItem ( item as TopicAreaItem ) ) ; for await ( const topicarea of topicareas ) { const count = await this . getDashboardCount ( topicarea . id ) ; topicarea . dashboardCount = count ; } return topicareas ; } public async delete ( id ) { await this . dynamodb . delete ( { TableName : this . tableName , Key : { pk : TopicAreaFactory . itemId ( id ) , sk : TopicAreaFactory . itemId ( id ) , } , } ) ; } public async getDashboardCount ( topicAreaId ) { const result = await this . dynamodb . query ( { TableName : this . tableName , IndexName : "byTopicArea" , ProjectionExpression : "#pk, #sk, #type, #parentDashboardId, #version, #topicAreaId" , KeyConditionExpression : "#topicAreaId = :topicAreaId" , ExpressionAttributeNames : { "#topicAreaId" : "topicAreaId" , "#pk" : "pk" , "#sk" : "sk" , "#type" : "type" , "#parentDashboardId" : "parentDashboardId" , "#version" : "version" , } , ExpressionAttributeValues : { ":topicAreaId" : TopicAreaFactory . itemId ( topicAreaId ) , } , } ) ; if ( ! result . Items ) { return 0 ; } const dashboards = result . Items . map ( ( item ) => DashboardFactory . fromItem ( item as DashboardItem ) ) ; const uniqueParents = new Set ( ) ; dashboards . map ( ( dashboard ) => uniqueParents . add ( dashboard . parentDashboardId ) ) ; return uniqueParents . size ; } public async renameTopicArea ( topicAreaId , name , user ) { await this . dynamodb . update ( { TableName : this . tableName , Key : { pk : TopicAreaFactory . itemId ( topicAreaId ) , sk : TopicAreaFactory . itemId ( topicAreaId ) , } , UpdateExpression : "set #name = :name" , ExpressionAttributeNames : { "#name" : "name" , } , ExpressionAttributeValues : { ":name" : name , } , } ) ; const dashboardRepo = DashboardRepository . getInstance ( ) ; const dashboards = await dashboardRepo . listDashboardsInTopicArea ( topicAreaId ) ; await dashboardRepo . updateTopicAreaName ( dashboards , name , user ) ; } } export default TopicAreaRepository ;