import { NotificationsApi as GeneratedNotificationsApi, type Configuration } from '../../api/generated/default' import { UninitializedEntityManagerError } from '../../errors' import type { EntityManagerService } from '../../services' import { Action, EntityType } from '../../services/EntityManager/types' import { parseParams } from '../../utils/parseParams' import { MarkAllNotificationsAsViewedRequest, UpdatePlaylistLastViewedAtRequest, MarkAllNotificationsAsViewedSchema, UpdatePlaylistLastViewedAtSchema, type NotificationsApiServicesConfig } from './types' export class NotificationsApi extends GeneratedNotificationsApi { private readonly entityManager?: EntityManagerService constructor(config: Configuration, services: NotificationsApiServicesConfig) { super(config) this.entityManager = services.entityManager } /** * When a user views all of their notifications */ async markAllNotificationsAsViewed( params: MarkAllNotificationsAsViewedRequest ) { const { userId } = await parseParams( 'markAllNotificationsAsViewed', MarkAllNotificationsAsViewedSchema )(params) if (!this.entityManager) { throw new UninitializedEntityManagerError() } return await this.entityManager.manageEntity({ userId, entityType: EntityType.NOTIFICATION, // In this case, we are the entityId since we are marking our own notifications as viewed entityId: userId, action: Action.VIEW, metadata: '' }) } /** * When a user views a playlist */ async updatePlaylistLastViewedAt(params: UpdatePlaylistLastViewedAtRequest) { const { playlistId, userId } = await parseParams( 'updatePlaylistLastViewedAt', UpdatePlaylistLastViewedAtSchema )(params) if (!this.entityManager) { throw new UninitializedEntityManagerError() } return await this.entityManager.manageEntity({ userId, entityType: EntityType.NOTIFICATION, entityId: playlistId, action: Action.VIEW_PLAYLIST, metadata: '' }) } }