Fix Novu migration utility query

This commit is contained in:
gpt-engineer-app[bot]
2025-10-01 13:22:08 +00:00
parent 88d5ad44c9
commit 549b964b60
2 changed files with 179 additions and 68 deletions

View File

@@ -1,6 +1,5 @@
import { useState } from 'react';
import { supabase } from '@/integrations/supabase/client';
import { notificationService } from '@/lib/notificationService';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Progress } from '@/components/ui/progress';
@@ -28,29 +27,31 @@ export function NovuMigrationUtility() {
setProgress(0);
try {
// First, fetch user IDs that already have Novu subscriber IDs
const { data: existingPrefs, error: prefsError } = await supabase
.from('user_notification_preferences')
.select('user_id')
.not('novu_subscriber_id', 'is', null);
// Call the server-side migration function
const { data: { session } } = await supabase.auth.getSession();
if (!session) {
throw new Error('You must be logged in to run the migration');
}
if (prefsError) throw prefsError;
const response = await fetch(
'https://ydvtmnrszybqnbcqbdcy.supabase.co/functions/v1/migrate-novu-users',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${session.access_token}`,
},
}
);
const existingUserIds = existingPrefs?.map(p => p.user_id) || [];
const data = await response.json();
// Fetch users without Novu subscriber IDs
const query = supabase
.from('profiles')
.select('user_id, users:user_id(email)');
if (!response.ok || !data.success) {
throw new Error(data.error || 'Migration failed');
}
// Only add the not filter if there are existing user IDs
const { data: users, error: fetchError } = existingUserIds.length > 0
? await query.not('user_id', 'in', `(${existingUserIds.join(',')})`)
: await query;
if (fetchError) throw fetchError;
if (!users || users.length === 0) {
if (!data.results || data.results.length === 0) {
toast({
title: "No users to migrate",
description: "All users are already registered with Novu.",
@@ -59,55 +60,12 @@ export function NovuMigrationUtility() {
return;
}
setTotalUsers(users.length);
const migrationResults: MigrationResult[] = [];
setTotalUsers(data.total);
setResults(data.results);
setProgress(100);
// Process users one by one
for (let i = 0; i < users.length; i++) {
const user = users[i];
const email = (user.users as any)?.email;
if (!email) {
migrationResults.push({
userId: user.user_id,
email: 'No email found',
success: false,
error: 'User email not found',
});
continue;
}
try {
const result = await notificationService.createSubscriber({
subscriberId: user.user_id,
email,
data: { userId: user.user_id },
});
migrationResults.push({
userId: user.user_id,
email,
success: result.success,
error: result.error,
});
// Small delay to avoid overwhelming the API
await new Promise(resolve => setTimeout(resolve, 100));
} catch (error: any) {
migrationResults.push({
userId: user.user_id,
email,
success: false,
error: error.message,
});
}
setProgress(((i + 1) / users.length) * 100);
setResults([...migrationResults]);
}
const successCount = migrationResults.filter(r => r.success).length;
const failureCount = migrationResults.filter(r => !r.success).length;
const successCount = data.results.filter((r: MigrationResult) => r.success).length;
const failureCount = data.results.filter((r: MigrationResult) => !r.success).length;
toast({
title: "Migration completed",