import type { AxiosInstance } from 'axios'; import type { User, VisitHistoryItem, VisitHistoryItemHalListSchemaType, VisitHistoryItemHalSchemaType, VisitHistoryProductionIdsHal, } from '../../types'; import { VisitHistoryProductionIdsHalSchema, VisitHistoryItemHalListSchema, VisitHistoryItemHalSchema, } 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 VisitHistoryResourceRepository extends BaseUsersResourceRepository< VisitHistoryItemHalSchemaType, VisitHistoryItemHalListSchemaType > { constructor(axios: AxiosInstance) { super(axios, '/visit-history', { get: VisitHistoryItemHalSchema, listForUser: VisitHistoryItemHalListSchema, }); } public async add( userId: User['id'], productionId: VisitHistoryItem['productionId'], isAddedByUser: VisitHistoryItem['isAddedByUser'], eventId?: VisitHistoryItem['eventId'], query?: ResourceQuery ): AsyncResult { if (!this._schemas.get) throw new UnsupportedError(); try { const response = await this._axios.post( `/users/${userId}${this._baseUrl}`, { productionId, eventId, isAddedByUser }, 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: User['id'], visitHistoryItemId: VisitHistoryItem['id'], query?: ResourceQuery ): AsyncResult { try { const response = await this._axios.delete( `/users/${userId}${this._baseUrl}/${visitHistoryItemId}`, this.getRequestConfig(query) ); return successResult(response.data); } 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 = VisitHistoryProductionIdsHalSchema.parse( response.data ); return successResult(responseData); } catch (error) { return errorResult(error as Error); } } }