import type { AxiosInstance } from 'axios'; import type { WatchlistItem, WatchlistItemHalListSchemaType, WatchlistItemHalSchemaType, WatchlistProductionIdsHal, } from '../../types'; import { WatchlistProductionIdsHalSchema, WatchlistItemHalListSchema, WatchlistItemHalSchema, } from '../../types'; import type { AsyncResult } from '../../utilities'; import { errorResult, successResult, UnsupportedError } from '../../utilities'; import { BaseUsersResourceRepository } from './BaseUsersResourceRepository'; import type { ResourceQuery } from 'modules/BaseResourceRepository'; export class WatchlistResourceRepository extends BaseUsersResourceRepository< WatchlistItemHalSchemaType, WatchlistItemHalListSchemaType > { constructor(axios: AxiosInstance) { super(axios, '/watchlist', { get: WatchlistItemHalSchema, listForUser: WatchlistItemHalListSchema, }); } public async add( userId: WatchlistItem['userId'], productionId: WatchlistItem['productionId'], query?: ResourceQuery ): AsyncResult { if (!this._schemas.get) throw new UnsupportedError(); try { const response = await this._axios.post( `/users/${userId}${this._baseUrl}`, { productionId }, this.getRequestConfig(query) ); const responseData = this._schemas.get.parse(response.data); return successResult(responseData); } catch (error) { return errorResult(error as Error); } } public async remove( userId: WatchlistItem['userId'], watchlistItemId: WatchlistItem['id'], query?: ResourceQuery ): AsyncResult { try { const response = await this._axios.delete( `/users/${userId}${this._baseUrl}/${watchlistItemId}`, this.getRequestConfig(query) ); return successResult(response.data); } catch (error) { return errorResult(error as Error); } } public async getByProductionId( userId: string, productionId: string, query?: ResourceQuery ): AsyncResult { if (!this._schemas.get) throw new UnsupportedError(); try { const response = await this._axios.get( `/users/${userId}${this._baseUrl}/by-production/${productionId}`, this.getRequestConfig(query) ); if (response.status === 204) return successResult(null); const responseData = this._schemas.get.parse(response.data); return successResult(responseData); } catch (error) { return errorResult(error as Error); } } public async getProductionIds( userId: string ): AsyncResult { try { const response = await this._axios.get( `/users/${userId}${this._baseUrl}/production-ids` ); const responseData = WatchlistProductionIdsHalSchema.parse(response.data); return successResult(responseData); } catch (error) { return errorResult(error as Error); } } }