feat(hiboutik): add Hiboutik API client, repositories and DTOs

This commit is contained in:
MastersGame
2026-11-05 23:00:47 +01:00
parent e6e39b4f80
commit 9cfcd12351
13 changed files with 334 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
import { Injectable } from '@nestjs/common';
import { HiboutikClient } from './hiboutik.client';
import { BrandHiboutikDto } from '../models/hiboutikDTO/brand.hiboutik.dto';
/** Reads brands from the Hiboutik API. */
@Injectable()
export class BrandHiboutikRepository {
constructor(private readonly hiboutikClient: HiboutikClient) {}
async getBrands(): Promise<BrandHiboutikDto[]> {
return this.hiboutikClient.getAllPages<BrandHiboutikDto>('/brands', {
order_by: 'brand_id',
sort: 'ASC',
});
}
}

View File

@@ -0,0 +1,17 @@
import { Injectable } from '@nestjs/common';
import { CategoryHiboutikDto } from '../models/hiboutikDTO/category.hiboutik.dto';
import { HiboutikClient } from './hiboutik.client';
/** Reads categories from the Hiboutik API. */
@Injectable()
export class CategoryHiboutikRepository {
constructor(private readonly hiboutikClient: HiboutikClient) {}
/** Sorted by parent id, which does not guarantee that a parent comes before its children. */
async getCategories(): Promise<CategoryHiboutikDto[]> {
return this.hiboutikClient.getAllPages<CategoryHiboutikDto>('/categories', {
order_by: 'category_id_parent',
sort: 'ASC',
});
}
}

View File

@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common';
import { HiboutikClient } from './hiboutik.client';
import { CustomerHiboutikDto, FullCustomerHiboutikDto } from '../models/hiboutikDTO/customer.hiboutik.dto';
/** Reads customers from the Hiboutik API. */
@Injectable()
export class CustomerHiboutikRepository {
constructor(private readonly hiboutikClient: HiboutikClient) {}
/** Customer summaries only: use {@link getCustomerById} for addresses and sales. */
async getCustomers(): Promise<CustomerHiboutikDto[]> {
return this.hiboutikClient.getAllPages<CustomerHiboutikDto>('/customers');
}
/**
* Full customer, with its addresses and sales (Hiboutik answers with a one-element array).
* @throws if the customer does not exist.
*/
async getCustomerById(id: number): Promise<FullCustomerHiboutikDto> {
const data = await this.hiboutikClient.get<FullCustomerHiboutikDto[]>('/customer/' + String(id));
const customer = data[0];
if (!customer) throw new Error(`Hiboutik customer ${id} not found`);
return customer;
}
}

View File

@@ -0,0 +1,55 @@
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { firstValueFrom } from 'rxjs';
/** Results per page of every paginated Hiboutik endpoint (see "Results per page" in the API docs). */
const HIBOUTIK_PAGE_SIZE = 250;
/** Low-level HTTP client for the Hiboutik REST API: base URL and Basic auth credentials come from the environment. */
@Injectable()
export class HiboutikClient {
private readonly baseUrl: string;
private readonly authHeader: string;
constructor(
private readonly httpService: HttpService,
private readonly configService: ConfigService,
) {
const username = this.configService.get<string>('HIBOUTIK_USERNAME') ?? '';
const password = this.configService.get<string>('HIBOUTIK_PASSWORD') ?? '';
this.authHeader = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
this.baseUrl = this.configService.get<string>('HIBOUTIK_BASE_URL') ?? '';
}
/** GET on a Hiboutik path (relative to `HIBOUTIK_BASE_URL`), returns the response body. */
async get<T>(path: string, params?: Record<string, unknown>): Promise<T> {
const response = await firstValueFrom(
this.httpService.get<T>(`${this.baseUrl}${path}`, {
headers: { Authorization: this.authHeader },
params,
}),
);
return response.data;
}
/**
* Fetches every page of a paginated endpoint (`p` param, starting at 1) and concatenates the results.
* Only for paginated endpoints: on a non-paginated one, a full first page would loop forever on the same results.
*/
async getAllPages<T>(path: string, baseParams: Record<string, unknown> = {}): Promise<T[]> {
const results: T[] = [];
let page = 1;
while (true) {
const pageData = await this.get<T[]>(path, { ...baseParams, p: page });
if (!pageData || pageData.length === 0) break;
results.push(...pageData);
// A page smaller than the page size is the last one: no need to request an empty page
if (pageData.length < HIBOUTIK_PAGE_SIZE) break;
page++;
}
return results;
}
}

View File

@@ -0,0 +1,17 @@
import { Injectable } from '@nestjs/common';
import { HiboutikClient } from './hiboutik.client';
import { ProductHiboutikDto } from '../models/hiboutikDTO/product.hiboutik.dto';
/** Reads products from the Hiboutik API. */
@Injectable()
export class ProductHiboutikRepository {
constructor(private readonly hiboutikClient: HiboutikClient) {}
/** All products, archived ones included, with their sizes and per-size rules. */
async getProducts(): Promise<ProductHiboutikDto[]> {
return this.hiboutikClient.getAllPages<ProductHiboutikDto>('/products', {
order_by: 'product_id',
sort: 'ASC',
});
}
}

View File

@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common';
import { HiboutikClient } from './hiboutik.client';
import { StockHiboutikDto } from '../models/hiboutikDTO/stock.hiboutik.dto';
/** Reads available stocks from the Hiboutik API. */
@Injectable()
export class StockHiboutikRepository {
constructor(private readonly hiboutikClient: HiboutikClient) {}
/** Stock of every product and size, all warehouses combined. Not paginated. */
async getStocks(): Promise<StockHiboutikDto[]> {
const stocks = await this.hiboutikClient.get<StockHiboutikDto[]>('/stock_available/all_wh', {});
// Hiboutik may return ids as strings: normalize them so strict comparisons work downstream
return stocks.map((stock) => ({
...stock,
product_id: Number(stock.product_id),
product_size: Number(stock.product_size),
}));
}
}

View File

@@ -0,0 +1,17 @@
import { Injectable } from '@nestjs/common';
import { HiboutikClient } from './hiboutik.client';
import { SupplierHiboutikDto } from '../models/hiboutikDTO/supplier.hiboutik.dto';
/** Reads suppliers from the Hiboutik API. */
@Injectable()
export class SupplierHiboutikRepository {
constructor(private readonly hiboutikClient: HiboutikClient) {}
/** `/suppliers` is not paginated: everything comes in one call. */
async getSuppliers(): Promise<SupplierHiboutikDto[]> {
return this.hiboutikClient.get<SupplierHiboutikDto[]>('/suppliers', {
order_by: 'supplier_id',
sort: 'ASC',
});
}
}