feat: implement multi-tenant catalog service with SSR support and data interfaces

This commit is contained in:
2026-06-29 08:50:00 -03:00
parent 77e4a22740
commit 9f32588828
5 changed files with 59 additions and 3 deletions

View File

@@ -0,0 +1,3 @@
export interface ApiResponse<T> {
data: T;
}

View File

@@ -0,0 +1,11 @@
export interface Product {
id: number;
tenant_codigo: string;
categoria_id: number;
slug: string;
nombre: string;
descripcion?: string;
precio: string;
created_at?: string;
updated_at?: string;
}

View File

@@ -0,0 +1,32 @@
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map, Observable } from 'rxjs';
import { ApiResponse } from '../api-response.interface';
import { TenantService } from '../tenant.service';
import { Product } from './catalog.interface';
@Injectable({
providedIn: 'root'
})
export class CatalogService {
private readonly http = inject(HttpClient);
private readonly tenantService = inject(TenantService);
private get tenantApiUrl(): string {
return this.tenantService.getTenantApiUrl();
}
getProductos(): Observable<Product[]> {
return this.http
.get<ApiResponse<Product[]>>(`${this.tenantApiUrl}/productos`)
.pipe(map((response) => response.data));
}
getProducto(id: number): Observable<Product> {
return this.http
.get<ApiResponse<Product>>(`${this.tenantApiUrl}/productos/${id}`)
.pipe(map((response) => response.data));
}
}

View File

@@ -1,3 +1,5 @@
import { ApiResponse } from './api-response.interface';
export interface Tenant {
id: number;
codigo: string;
@@ -11,6 +13,5 @@ export interface Tenant {
footer_logo: string;
}
export interface TenantBootstrapResponse {
data: Tenant;
}
export type TenantBootstrapResponse = ApiResponse<Tenant>;

View File

@@ -40,6 +40,15 @@ export class TenantService {
return this.tenantState();
}
getTenantApiUrl(): string {
const tenant = this.tenantState();
if (!tenant) {
throw new Error('No se pudo resolver el tenant activo.');
}
return `${environment.url}tenants/${tenant.codigo}`;
}
async bootstrap(): Promise<void> {
if (this.statusState() !== 'idle') {
return;