hata düzeltme 1

This commit is contained in:
2026-01-13 22:37:50 +03:00
parent 32009b4886
commit dc1b6f1359
11 changed files with 658 additions and 30 deletions

130
lib/sms/actions.ts Normal file
View File

@@ -0,0 +1,130 @@
"use server"
import { createClient } from "@/lib/supabase-server"
import { createClient as createSupabaseClient } from "@supabase/supabase-js"
import { revalidatePath } from "next/cache"
import { NetGsmService } from "./netgsm"
// Admin client for privileged operations (accessing sms_settings)
const supabaseAdmin = createSupabaseClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!,
{
auth: {
autoRefreshToken: false,
persistSession: false
}
}
)
async function assertAdmin() {
const supabase = createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) throw new Error("Oturum açmanız gerekiyor.")
const { data: profile } = await supabase.from('profiles').select('role').eq('id', user.id).single()
if (profile?.role !== 'admin') throw new Error("Yetkisiz işlem.")
return user
}
export async function getSmsSettings() {
try {
await assertAdmin()
const { data, error } = await supabaseAdmin
.from('sms_settings')
.select('*')
.single()
if (error && error.code !== 'PGRST116') { // PGRST116 is 'not found', which is fine initially
throw error
}
return { data }
} catch (error) {
return { error: (error as Error).message }
}
}
export async function updateSmsSettings(data: {
username: string
password?: string // Optional if not changing
header: string
}) {
try {
await assertAdmin()
// Check if exists
const { data: existing } = await supabaseAdmin.from('sms_settings').select('id').single()
const updates: any = {
username: data.username,
header: data.header,
updated_at: new Date().toISOString()
}
// Only update password if provided
if (data.password && data.password.trim() !== '') {
updates.password = data.password
}
if (existing) {
const { error } = await supabaseAdmin
.from('sms_settings')
.update(updates)
.eq('id', existing.id)
if (error) throw error
} else {
// First time setup, password is mandatory if not exists, but we can't easily check 'locally'
// We assume if new, password must be in updates.
if (!data.password) throw new Error("Yeni kurulum için şifre gereklidir.")
const { error } = await supabaseAdmin
.from('sms_settings')
.insert({ ...updates, password: data.password })
if (error) throw error
}
revalidatePath("/dashboard/settings")
return { success: true }
} catch (error) {
return { error: (error as Error).message }
}
}
export async function sendTestSms(phone: string) {
try {
await assertAdmin()
// Fetch credentials
const { data: settings } = await supabaseAdmin.from('sms_settings').select('*').single()
if (!settings) throw new Error("SMS ayarları yapılmamış.")
const mobileService = new NetGsmService({
username: settings.username,
password: settings.password,
header: settings.header,
apiUrl: settings.api_url
})
const result = await mobileService.sendSms(phone, "ParaKasa Test SMS: Entegrasyon basarili.")
// Log the result
await supabaseAdmin.from('sms_logs').insert({
phone,
message: "ParaKasa Test SMS: Entegrasyon basarili.",
status: result.success ? 'success' : 'error',
response_code: result.code || result.error
})
if (!result.success) {
throw new Error(result.error || "SMS gönderilemedi.")
}
return { success: true, jobId: result.jobId }
} catch (error) {
return { error: (error as Error).message }
}
}