2021-04-12 10:45:41 +02:00
|
|
|
import apiClient from './clients/api-client';
|
2020-12-01 17:55:23 +01:00
|
|
|
import { AxiosResponse } from 'axios';
|
2021-05-21 18:25:18 +02:00
|
|
|
import { ShoppingCart } from '../models/payment';
|
2021-06-22 11:13:44 +02:00
|
|
|
import { ComputePriceResult, Price, PriceIndexFilter } from '../models/price';
|
2020-12-01 17:55:23 +01:00
|
|
|
|
|
|
|
export default class PriceAPI {
|
2021-05-21 18:25:18 +02:00
|
|
|
static async compute (cart: ShoppingCart): Promise<ComputePriceResult> {
|
2021-06-22 11:13:44 +02:00
|
|
|
const res: AxiosResponse<ComputePriceResult> = await apiClient.post(`/api/prices/compute`, cart);
|
2020-12-02 10:06:18 +01:00
|
|
|
return res?.data;
|
2020-12-01 17:55:23 +01:00
|
|
|
}
|
2021-06-22 11:13:44 +02:00
|
|
|
|
|
|
|
static async index (filters?: Array<PriceIndexFilter>): Promise<Array<Price>> {
|
|
|
|
const res: AxiosResponse = await apiClient.get(`/api/prices${this.filtersToQuery(filters)}`);
|
|
|
|
return res?.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async update (price: Price): Promise<Price> {
|
2021-06-22 17:56:13 +02:00
|
|
|
const res: AxiosResponse<Price> = await apiClient.patch(`/api/prices/${price.id}`, { price });
|
2021-06-22 11:13:44 +02:00
|
|
|
return res?.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static filtersToQuery(filters?: Array<PriceIndexFilter>): string {
|
|
|
|
if (!filters) return '';
|
|
|
|
|
|
|
|
return '?' + filters.map(f => `${f.key}=${f.value}`).join('&');
|
|
|
|
}
|
2020-12-01 17:55:23 +01:00
|
|
|
}
|
|
|
|
|