diff --git a/src/client.ts b/src/client.ts index 40202df..0d16a89 100644 --- a/src/client.ts +++ b/src/client.ts @@ -111,6 +111,10 @@ export default class TourManager { maps (): Modules.Maps.Api { return new Modules.Maps.Api(this.axios) } + + products (): Modules.Products.Api { + return new Modules.Products.Api(this.axios) + } } /** diff --git a/src/modules.ts b/src/modules.ts index 4671e90..e2f9a55 100644 --- a/src/modules.ts +++ b/src/modules.ts @@ -11,3 +11,4 @@ export * as Images from './modules/images.js' export * as Locations from './modules/locations.js' export * as Organizations from './modules/organizations.js' export * as Maps from './modules/maps.js' +export * as Products from './modules/products.js' \ No newline at end of file diff --git a/src/modules/products.ts b/src/modules/products.ts new file mode 100644 index 0000000..34d0f8f --- /dev/null +++ b/src/modules/products.ts @@ -0,0 +1,76 @@ +import type { + Paginated, + PaginatedQuery, + QueryBoolean, + Entity, + Fields +} from './common.js' +import type { AxiosInstance } from 'axios' +import { ApiGroup } from './common.js' +import timestamp from '../annotations/timestamp.js' +import type {Organization} from "./organizations"; + +type Price = { + currency: string + value: number +} + +type Provider = { + id: string + organization: Organization + name: string + descriptorUrl: string + enabled: boolean +} + +export interface IProduct extends Entity { + name: string + provider: Provider + groupName: string + description: string | null + category: string + prices: Price[] +} + +export class Product implements IProduct { + readonly id!: string + readonly provider!: Provider + readonly name!: string + readonly groupName!: string + readonly description!: string | null + readonly category!: string + readonly prices!: Price[] + + private readonly axios: AxiosInstance + @timestamp() readonly updated_at!: Date + @timestamp() readonly created_at!: Date + + constructor (data: IProduct, axios: AxiosInstance) { + this.axios = axios + Object.assign(this, data) + } + + async list (provider: Provider): Promise> { + const response = (await this.axios.get>(`/products`, provider.id)) + response.data = response.data.map(p => new Product(p, this.axios)) + return response as Paginated + } + + async reserve (id: string): Promise { + const response = await this.axios.post(`/products/reserve/${id}`) + return new Product(response, this.axios) + } +} + +export class Api extends ApiGroup { + async list (provider: Provider): Promise> { + const response = (await this.axios.get>(`/products`, provider.id)) + response.data = response.data.map(p => new Product(p, this.axios)) + return response as Paginated + } + + async reserve (id: string): Promise { + const response = await this.axios.post(`/products/reserve/${id}`) + return new Product(response, this.axios) + } +} \ No newline at end of file