feat: implement multi-tenant catalog service with SSR support and data interfaces
This commit is contained in:
3
src/app/core/services/api-response.interface.ts
Normal file
3
src/app/core/services/api-response.interface.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface ApiResponse<T> {
|
||||
data: T;
|
||||
}
|
||||
11
src/app/core/services/catalog/catalog.interface.ts
Normal file
11
src/app/core/services/catalog/catalog.interface.ts
Normal 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;
|
||||
}
|
||||
32
src/app/core/services/catalog/catalog.service.ts
Normal file
32
src/app/core/services/catalog/catalog.service.ts
Normal 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));
|
||||
}
|
||||
}
|
||||
@@ -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>;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user