import type { AxiosInstance } from 'axios'; import type { z } from 'zod'; import type { ProductionHalListSchemaType, ProductionListQuery, } from '../../types'; import { AssetRelationListHalSchema, ProductionCreateInputSchema, ProductionHalListSchema, ProductionHalSchema, ProductionUpdateInputSchema, } from '../../types'; import type { AsyncListResult } from '../../utilities'; import { errorResult, successListResult } from '../../utilities'; import type { AssetsResourceRepository } from '../AssetsResourceRepository'; import type { ResourceQuery } from '../BaseResourceRepository'; import { ScheduleResourceRepository } from './ScheduleResourceRepository'; export class ProductionsResourceRepository extends ScheduleResourceRepository< z.infer, z.infer, z.infer, z.infer, undefined, z.infer > { constructor(axios: AxiosInstance, assetRepository: AssetsResourceRepository) { super( axios, '/productions', { get: ProductionHalSchema, list: ProductionHalListSchema, createInput: ProductionCreateInputSchema, updateInput: ProductionUpdateInputSchema, assetList: AssetRelationListHalSchema, }, assetRepository ); } /** * The `search` method adds support for production list requests with queries that would normally be too long to fit in a URL, for example filtering by a big amount of IDs. * * @param bodyQuery The query parameters that will be passed to the request body. * @param query The query parameters that will be passed to the URL. The API merges these, giving priority to the `bodyQuery`. * @param url */ public async search( bodyQuery: ProductionListQuery = {}, query: ResourceQuery = {}, url: string = `${this._baseUrl}/search` ): AsyncListResult { try { const request = await this._axios.post( url, bodyQuery, this.getRequestConfig(query) ); const data = ProductionHalListSchema.parse(request.data); const nextFn = (() => { if (!data._links.next?.href) { return undefined; } return () => this.search(bodyQuery, {}, data._links.next!.href); })(); const previousFn = (() => { if (!data._links.previous?.href) { return undefined; } return () => this.search(bodyQuery, {}, data._links.previous!.href); })(); return successListResult(data, previousFn, nextFn); } catch (error) { return errorResult(error as Error); } } public async listByTitle( title: string, query: ResourceQuery = {}, url: string = `${this._baseUrl}/by-title/${title}` ): AsyncListResult { try { const request = await this._axios.get(url, this.getRequestConfig(query)); const data = ProductionHalListSchema.parse(request.data); const nextFn = (() => { if (!data._links.next?.href) { return undefined; } return () => this.listByTitle(title, {}, data._links.next!.href); })(); const previousFn = (() => { if (!data._links.previous?.href) { return undefined; } return () => this.listByTitle(title, {}, data._links.previous!.href); })(); return successListResult(data, previousFn, nextFn); } catch (error) { return errorResult(error as Error); } } }