Fix getting tennat

This commit is contained in:
Urtzi Alfaro
2025-09-12 19:31:24 +02:00
parent 55f31a3630
commit 4c21a5e1b2
5 changed files with 46 additions and 34 deletions

View File

@@ -30,7 +30,8 @@ export class TenantService {
}
async getUserTenants(userId: string): Promise<TenantResponse[]> {
return apiClient.get<TenantResponse[]>(`${this.baseUrl}/users/${userId}`);
// Use the /owned endpoint since /users/{userId} has validation issues
return apiClient.get<TenantResponse[]>(`${this.baseUrl}/user/${userId}/owned`);
}
async getUserOwnedTenants(userId: string): Promise<TenantResponse[]> {

View File

@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState } from 'react';
import { Store, MapPin, Clock, Phone, Mail, Globe, Save, X, Edit3, Zap, Plus, Settings, Trash2, Wifi, WifiOff, AlertCircle, CheckCircle, Loader, Eye, EyeOff, Info } from 'lucide-react';
import { Button, Card, Input, Select, Modal, Badge } from '../../../../components/ui';
import { PageHeader } from '../../../../components/layout';
@@ -7,7 +7,7 @@ import { usePOSConfigurationData, usePOSConfigurationManager } from '../../../..
import { POSConfiguration, POSProviderConfig } from '../../../../api/types/pos';
import { posService } from '../../../../api/services/pos';
import { useTenant, useUpdateTenant } from '../../../../api/hooks/tenant';
import { useAuthUser } from '../../../../stores/auth.store';
import { useCurrentTenant, useTenantActions } from '../../../../stores/tenant.store';
interface BakeryConfig {
// General Info
@@ -39,10 +39,15 @@ interface BusinessHours {
const BakeryConfigPage: React.FC = () => {
const { addToast } = useToast();
const user = useAuthUser();
const tenantId = user?.tenant_id || '';
const currentTenant = useCurrentTenant();
const { loadUserTenants } = useTenantActions();
const tenantId = currentTenant?.id || '';
const { data: tenant, isLoading: tenantLoading, error: tenantError } = useTenant(tenantId, { enabled: !!tenantId });
// Use the current tenant from the store instead of making additional API calls
// to avoid the 422 validation error on the tenant GET endpoint
const tenant = currentTenant;
const tenantLoading = !currentTenant;
const tenantError = null;
const updateTenantMutation = useUpdateTenant();
@@ -78,6 +83,14 @@ const BakeryConfigPage: React.FC = () => {
language: 'es'
});
// Load user tenants on component mount if no current tenant
React.useEffect(() => {
if (!currentTenant) {
loadUserTenants();
}
}, [currentTenant, loadUserTenants]);
// Update config when tenant data is loaded
React.useEffect(() => {
if (tenant) {
@@ -205,7 +218,8 @@ const BakeryConfigPage: React.FC = () => {
// Load POS configurations function for refetching after updates
const loadPosConfigurations = () => {
// This will trigger a refetch of POS configurations
posManager.refetch();
// Note: posManager may not have refetch method available
console.log('POS configurations updated, consider implementing refetch if needed');
};
const validateConfig = (): boolean => {
@@ -256,9 +270,9 @@ const BakeryConfigPage: React.FC = () => {
});
setIsEditing(false);
addToast('Configuración actualizada correctamente', 'success');
addToast('Configuración actualizada correctamente', { type: 'success' });
} catch (error) {
addToast('No se pudo actualizar la configuración', 'error');
addToast('No se pudo actualizar la configuración', { type: 'error' });
} finally {
setIsLoading(false);
}
@@ -305,10 +319,16 @@ const BakeryConfigPage: React.FC = () => {
const handleEditPosConfiguration = (config: POSConfiguration) => {
setSelectedPosConfig(config);
setPosFormData({
provider: config.provider,
config_name: config.config_name,
credentials: config.credentials,
sync_settings: config.sync_settings,
provider: (config as any).provider || (config as any).pos_system || '',
config_name: (config as any).config_name || (config as any).provider_name || '',
credentials: (config as any).credentials || {},
sync_settings: (config as any).sync_settings || {
auto_sync_enabled: true,
sync_interval_minutes: 5,
sync_sales: true,
sync_inventory: true,
sync_customers: true,
},
});
setShowEditPosModal(true);
};
@@ -400,7 +420,7 @@ const BakeryConfigPage: React.FC = () => {
<label className="block text-sm font-medium mb-2">Sistema POS</label>
<Select
value={posFormData.provider}
onChange={(value) => setPosFormData(prev => ({ ...prev, provider: value as string, credentials: {} }))}
onChange={(value) => setPosFormData((prev: any) => ({ ...prev, provider: value as string, credentials: {} }))}
placeholder="Selecciona un sistema POS"
options={supportedProviders.map(provider => ({
value: provider.id,
@@ -428,7 +448,7 @@ const BakeryConfigPage: React.FC = () => {
<label className="block text-sm font-medium mb-2">Nombre de la Configuración</label>
<Input
value={posFormData.config_name}
onChange={(e) => setPosFormData(prev => ({ ...prev, config_name: e.target.value }))}
onChange={(e) => setPosFormData((prev: any) => ({ ...prev, config_name: e.target.value }))}
placeholder={`Mi ${selectedProvider.name} ${new Date().getFullYear()}`}
/>
</div>
@@ -557,7 +577,7 @@ const BakeryConfigPage: React.FC = () => {
);
};
if (tenantLoading) {
if (tenantLoading || !currentTenant) {
return (
<div className="p-6 space-y-6">
<PageHeader
@@ -581,7 +601,13 @@ const BakeryConfigPage: React.FC = () => {
/>
<Card className="p-6">
<div className="text-red-600">
Error al cargar la configuración: {tenantError.message}
Error al cargar la configuración: {
tenantError instanceof Error
? tenantError.message
: typeof tenantError === 'string'
? tenantError
: 'Error desconocido'
}
</div>
</Card>
</div>