Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/services/funnelStepsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ export const funnelStepsService = {
return data;
},

async createFunnelStep(step: Omit<FunnelStep, 'id' | 'created_at' | 'updated_at'>) {
// Get current tenant ID for the insert
const { data: currentTenantId } = await tenantAwareClient.rpc('get_current_tenant_id');
if (!currentTenantId) throw new Error('Tenant context not available');
async createFunnelStep(step: Omit<FunnelStep, 'id' | 'created_at' | 'updated_at'>, explicitTenantId?: string) {
// Use explicit tenant ID if provided, otherwise try RPC call with fallback to storage
let tenantId = explicitTenantId;

if (!tenantId) {
const { data: currentTenantId } = await tenantAwareClient.rpc('get_current_tenant_id');
tenantId = currentTenantId;
}

if (!tenantId && typeof window !== 'undefined') {
tenantId = sessionStorage.getItem('selected_tenant_id') ||
localStorage.getItem('preferred_tenant_id') ||
undefined;
}

if (!tenantId) throw new Error('Tenant context not available');

const { data, error } = await tenantAwareClient
.from('funnel_steps')
Expand All @@ -35,7 +47,7 @@ export const funnelStepsService = {
custom_css: step.custom_css || '',
custom_js: step.custom_js || '',
version: 1,
tenant_id: currentTenantId
tenant_id: tenantId
})
.select()
.single();
Expand Down
11 changes: 4 additions & 7 deletions src/services/funnelTemplatesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,10 @@ export const funnelTemplatesService = {

log.debug(`Step 3.${i + 1}: Inserting step data`, { stepData });

// Get current tenant ID for the insert
const { data: currentTenantId } = await tenantAwareClient.rpc('get_current_tenant_id');
if (!currentTenantId) throw new Error('Tenant context not available');

// Use the tenantId that was already validated at the start of createFunnelFromTemplate
const { data: createdStep, error: stepError } = await tenantAwareClient
.from('funnel_steps')
.insert({ ...stepData, tenant_id: currentTenantId })
.insert({ ...stepData, tenant_id: tenantId })
.select()
.single();

Expand Down Expand Up @@ -238,10 +235,10 @@ export const funnelTemplatesService = {

log.debug(`Step 3.${i + 1}.5: Inserting optin template data`, { optinTemplateData });

// Note: tenant_id for optin_templates is inherited from the funnel/step context
// Use the tenantId that was already validated at the start of createFunnelFromTemplate
const { data: createdOptinTemplate, error: optinError } = await tenantAwareClient
.from('optin_templates')
.insert({ ...optinTemplateData, tenant_id: currentTenantId })
.insert({ ...optinTemplateData, tenant_id: tenantId })
.select()
.single();

Expand Down
Loading