hata düzeltme 1
This commit is contained in:
@@ -21,3 +21,8 @@
|
|||||||
- [x] Temel Site Ayarları (Başlık, İletişim).
|
- [x] Temel Site Ayarları (Başlık, İletişim).
|
||||||
- [x] Ürün Yönetimi (Temel CRUD).
|
- [x] Ürün Yönetimi (Temel CRUD).
|
||||||
- [x] Kategori Yönetimi (Arayüz hazır, veritabanı bekleniyor).
|
- [x] Kategori Yönetimi (Arayüz hazır, veritabanı bekleniyor).
|
||||||
|
|
||||||
|
|
||||||
|
## 3. NetGSm Entegrasyonu
|
||||||
|
- Login için Sms doğrulama entegrasyonu yapılacak.
|
||||||
|
-
|
||||||
@@ -129,7 +129,7 @@ export default async function DashboardPage() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function PlusIcon(props: any) {
|
function PlusIcon(props: React.SVGProps<SVGSVGElement>) {
|
||||||
return (
|
return (
|
||||||
<svg
|
<svg
|
||||||
{...props}
|
{...props}
|
||||||
|
|||||||
@@ -1,44 +1,28 @@
|
|||||||
import { createClient } from "@/lib/supabase-server"
|
import { createClient } from "@/lib/supabase-server"
|
||||||
import { SiteSettingsForm } from "@/components/dashboard/site-settings-form"
|
import { SettingsTabs } from "@/components/dashboard/settings-tabs"
|
||||||
import { AppearanceForm } from "@/components/dashboard/appearance-form"
|
import { getSmsSettings } from "@/lib/sms/actions"
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
|
||||||
|
|
||||||
import { Button } from "@/components/ui/button"
|
|
||||||
|
|
||||||
export default async function SettingsPage() {
|
export default async function SettingsPage() {
|
||||||
const supabase = createClient()
|
const supabase = createClient()
|
||||||
|
|
||||||
// Fetch site settings
|
// Fetch site settings
|
||||||
const { data: settings } = await supabase
|
const { data: siteSettings } = await supabase
|
||||||
.from('site_settings')
|
.from('site_settings')
|
||||||
.select('*')
|
.select('*')
|
||||||
.single()
|
.single()
|
||||||
|
|
||||||
|
// Fetch SMS settings (server-side call to our action/db)
|
||||||
|
// Note: getSmsSettings is an action that checks for admin, which is fine.
|
||||||
|
// However, since we are in a server component with a Supabase client, we could also fetch directly if we had admin client.
|
||||||
|
// But let's use the clean action or direct logic.
|
||||||
|
// Actually, calling server action from server component is fine, but we need to handle the return structure.
|
||||||
|
const smsResponse = await getSmsSettings()
|
||||||
|
const smsSettings = smsResponse.data || null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex-1 space-y-4 p-8 pt-6">
|
<div className="flex-1 space-y-4 p-8 pt-6">
|
||||||
<h2 className="text-3xl font-bold tracking-tight">Ayarlar</h2>
|
<h2 className="text-3xl font-bold tracking-tight">Ayarlar</h2>
|
||||||
|
<SettingsTabs siteSettings={siteSettings} smsSettings={smsSettings} />
|
||||||
{/* Site General Settings */}
|
|
||||||
<div className="grid gap-4">
|
|
||||||
<SiteSettingsForm initialData={settings} />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="grid gap-4 grid-cols-1 md:grid-cols-2">
|
|
||||||
<AppearanceForm />
|
|
||||||
|
|
||||||
<Card>
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle>Hesap Güvenliği</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Şifre ve oturum yönetimi.
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
<Button variant="outline" className="w-full">Şifre Değiştir</Button>
|
|
||||||
<Button variant="destructive" className="w-full">Hesabı Sil</Button>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
53
components/dashboard/settings-tabs.tsx
Normal file
53
components/dashboard/settings-tabs.tsx
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
|
||||||
|
import { SiteSettingsForm } from "@/components/dashboard/site-settings-form"
|
||||||
|
import { SmsSettingsForm } from "@/components/dashboard/sms-settings-form"
|
||||||
|
import { AppearanceForm } from "@/components/dashboard/appearance-form"
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||||
|
import { Button } from "@/components/ui/button"
|
||||||
|
|
||||||
|
interface SettingsTabsProps {
|
||||||
|
siteSettings: Record<string, any> | null
|
||||||
|
smsSettings: Record<string, any> | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SettingsTabs({ siteSettings, smsSettings }: SettingsTabsProps) {
|
||||||
|
return (
|
||||||
|
<Tabs defaultValue="general" className="space-y-4">
|
||||||
|
<TabsList>
|
||||||
|
<TabsTrigger value="general">Genel</TabsTrigger>
|
||||||
|
<TabsTrigger value="sms">SMS / Bildirimler</TabsTrigger>
|
||||||
|
<TabsTrigger value="appearance">Görünüm</TabsTrigger>
|
||||||
|
<TabsTrigger value="security">Güvenlik</TabsTrigger>
|
||||||
|
</TabsList>
|
||||||
|
|
||||||
|
<TabsContent value="general" className="space-y-4">
|
||||||
|
<SiteSettingsForm initialData={siteSettings} />
|
||||||
|
</TabsContent>
|
||||||
|
|
||||||
|
<TabsContent value="sms" className="space-y-4">
|
||||||
|
<SmsSettingsForm initialData={smsSettings} />
|
||||||
|
</TabsContent>
|
||||||
|
|
||||||
|
<TabsContent value="appearance" className="space-y-4">
|
||||||
|
<AppearanceForm />
|
||||||
|
</TabsContent>
|
||||||
|
|
||||||
|
<TabsContent value="security" className="space-y-4">
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Hesap Güvenliği</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Şifre ve oturum yönetimi.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
<Button variant="outline" className="w-full">Şifre Değiştir</Button>
|
||||||
|
<Button variant="destructive" className="w-full">Hesabı Sil</Button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</TabsContent>
|
||||||
|
</Tabs>
|
||||||
|
)
|
||||||
|
}
|
||||||
198
components/dashboard/sms-settings-form.tsx
Normal file
198
components/dashboard/sms-settings-form.tsx
Normal file
@@ -0,0 +1,198 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { useState } from "react"
|
||||||
|
import { useForm } from "react-hook-form"
|
||||||
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
|
import * as z from "zod"
|
||||||
|
import { Button } from "@/components/ui/button"
|
||||||
|
import { Input } from "@/components/ui/input"
|
||||||
|
import {
|
||||||
|
Form,
|
||||||
|
FormControl,
|
||||||
|
FormDescription,
|
||||||
|
FormField,
|
||||||
|
FormItem,
|
||||||
|
FormLabel,
|
||||||
|
FormMessage,
|
||||||
|
} from "@/components/ui/form"
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle, CardFooter } from "@/components/ui/card"
|
||||||
|
import { toast } from "sonner"
|
||||||
|
import { Loader2, Smartphone, Send } from "lucide-react"
|
||||||
|
import { updateSmsSettings, sendTestSms } from "@/lib/sms/actions"
|
||||||
|
|
||||||
|
const smsSettingsSchema = z.object({
|
||||||
|
username: z.string().min(1, "Kullanıcı adı gereklidir."),
|
||||||
|
password: z.string().optional(),
|
||||||
|
header: z.string().min(1, "Başlık (Gönderici Adı) gereklidir."),
|
||||||
|
})
|
||||||
|
|
||||||
|
type SmsSettingsValues = z.infer<typeof smsSettingsSchema>
|
||||||
|
|
||||||
|
interface SmsSettingsFormProps {
|
||||||
|
initialData: {
|
||||||
|
username: string
|
||||||
|
header: string
|
||||||
|
} | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SmsSettingsForm({ initialData }: SmsSettingsFormProps) {
|
||||||
|
const [loading, setLoading] = useState(false)
|
||||||
|
const [testLoading, setTestLoading] = useState(false)
|
||||||
|
const [testPhone, setTestPhone] = useState("")
|
||||||
|
|
||||||
|
const form = useForm<SmsSettingsValues>({
|
||||||
|
resolver: zodResolver(smsSettingsSchema),
|
||||||
|
defaultValues: {
|
||||||
|
username: initialData?.username || "",
|
||||||
|
header: initialData?.header || "",
|
||||||
|
password: "",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const onSubmit = async (data: SmsSettingsValues) => {
|
||||||
|
setLoading(true)
|
||||||
|
try {
|
||||||
|
const result = await updateSmsSettings({
|
||||||
|
username: data.username,
|
||||||
|
password: data.password,
|
||||||
|
header: data.header,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (result.error) {
|
||||||
|
toast.error(result.error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.success("SMS ayarları güncellendi.")
|
||||||
|
// Don't reset form fully, keeps values visible except password
|
||||||
|
form.setValue("password", "")
|
||||||
|
} catch {
|
||||||
|
toast.error("Bir sorun oluştu.")
|
||||||
|
} finally {
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const onTestSms = async () => {
|
||||||
|
if (!testPhone) {
|
||||||
|
toast.error("Lütfen bir test numarası girin.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setTestLoading(true)
|
||||||
|
try {
|
||||||
|
const result = await sendTestSms(testPhone)
|
||||||
|
if (result.error) {
|
||||||
|
toast.error("Test başarısız: " + result.error)
|
||||||
|
} else {
|
||||||
|
toast.success("Test SMS başarıyla gönderildi!")
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
toast.error("Test sırasında bir hata oluştu.")
|
||||||
|
} finally {
|
||||||
|
setTestLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="grid gap-6">
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>NetGSM Konfigürasyonu</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
NetGSM API bilgilerinizi buradan yönetebilirsiniz. Şifre alanı sadece değiştirmek istediğinizde gereklidir.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<Form {...form}>
|
||||||
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="username"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>NetGSM Kullanıcı Adı (850...)</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="850xxxxxxx" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="password"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Şifre</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input type="password" placeholder="••••••••" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>
|
||||||
|
Mevcut şifreyi korumak için boş bırakın.
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="header"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Mesaj Başlığı (Gönderici Adı)</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="PARAKASA" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>
|
||||||
|
NetGSM panelinde tanımlı gönderici adınız.
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Button type="submit" disabled={loading}>
|
||||||
|
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||||
|
Ayarları Kaydet
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Bağlantı Testi</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Ayarların doğru çalıştığını doğrulamak için test SMS gönderin.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="flex gap-4">
|
||||||
|
<div className="grid gap-2 flex-1">
|
||||||
|
<Input
|
||||||
|
placeholder="5551234567"
|
||||||
|
value={testPhone}
|
||||||
|
onChange={(e) => setTestPhone(e.target.value)}
|
||||||
|
/>
|
||||||
|
<p className="text-[0.8rem] text-muted-foreground">
|
||||||
|
Başında 0 olmadan 10 hane giriniz.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<Button variant="secondary" onClick={onTestSms} disabled={testLoading || !testPhone}>
|
||||||
|
{testLoading ? (
|
||||||
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||||
|
) : (
|
||||||
|
<Send className="mr-2 h-4 w-4" />
|
||||||
|
)}
|
||||||
|
Test Gönder
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
55
components/ui/tabs.tsx
Normal file
55
components/ui/tabs.tsx
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import * as React from "react"
|
||||||
|
import * as TabsPrimitive from "@radix-ui/react-tabs"
|
||||||
|
|
||||||
|
import { cn } from "@/lib/utils"
|
||||||
|
|
||||||
|
const Tabs = TabsPrimitive.Root
|
||||||
|
|
||||||
|
const TabsList = React.forwardRef<
|
||||||
|
React.ElementRef<typeof TabsPrimitive.List>,
|
||||||
|
React.ComponentPropsWithoutRef<typeof TabsPrimitive.List>
|
||||||
|
>(({ className, ...props }, ref) => (
|
||||||
|
<TabsPrimitive.List
|
||||||
|
ref={ref}
|
||||||
|
className={cn(
|
||||||
|
"inline-flex h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground",
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
))
|
||||||
|
TabsList.displayName = TabsPrimitive.List.displayName
|
||||||
|
|
||||||
|
const TabsTrigger = React.forwardRef<
|
||||||
|
React.ElementRef<typeof TabsPrimitive.Trigger>,
|
||||||
|
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger>
|
||||||
|
>(({ className, ...props }, ref) => (
|
||||||
|
<TabsPrimitive.Trigger
|
||||||
|
ref={ref}
|
||||||
|
className={cn(
|
||||||
|
"inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-background data-[state=active]:text-foreground data-[state=active]:shadow-sm",
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
))
|
||||||
|
TabsTrigger.displayName = TabsPrimitive.Trigger.displayName
|
||||||
|
|
||||||
|
const TabsContent = React.forwardRef<
|
||||||
|
React.ElementRef<typeof TabsPrimitive.Content>,
|
||||||
|
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Content>
|
||||||
|
>(({ className, ...props }, ref) => (
|
||||||
|
<TabsPrimitive.Content
|
||||||
|
ref={ref}
|
||||||
|
className={cn(
|
||||||
|
"mt-2 ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
))
|
||||||
|
TabsContent.displayName = TabsPrimitive.Content.displayName
|
||||||
|
|
||||||
|
export { Tabs, TabsList, TabsTrigger, TabsContent }
|
||||||
130
lib/sms/actions.ts
Normal file
130
lib/sms/actions.ts
Normal 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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
88
lib/sms/netgsm.ts
Normal file
88
lib/sms/netgsm.ts
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
export interface NetGsmConfig {
|
||||||
|
username?: string;
|
||||||
|
password?: string;
|
||||||
|
header?: string;
|
||||||
|
apiUrl?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SmsResult {
|
||||||
|
success: boolean;
|
||||||
|
jobId?: string;
|
||||||
|
error?: string;
|
||||||
|
code?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NetGsmService {
|
||||||
|
private config: NetGsmConfig;
|
||||||
|
|
||||||
|
constructor(config: NetGsmConfig) {
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send SMS using NetGSM GET API
|
||||||
|
* Refer: https://www.netgsm.com.tr/dokuman/#http-get-servisi
|
||||||
|
*/
|
||||||
|
async sendSms(phone: string, message: string): Promise<SmsResult> {
|
||||||
|
if (!this.config.username || !this.config.password || !this.config.header) {
|
||||||
|
return { success: false, error: "NetGSM konfigürasyonu eksik." };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean phone number (remove spaces, parentheses, etc)
|
||||||
|
// NetGSM expects 905xxxxxxxxx or just 5xxxxxxxxx, we'll ensure format
|
||||||
|
let cleanPhone = phone.replace(/\D/g, '');
|
||||||
|
if (cleanPhone.startsWith('90')) {
|
||||||
|
cleanPhone = cleanPhone.substring(0); // keep it
|
||||||
|
} else if (cleanPhone.startsWith('0')) {
|
||||||
|
cleanPhone = '9' + cleanPhone;
|
||||||
|
} else if (cleanPhone.length === 10) {
|
||||||
|
cleanPhone = '90' + cleanPhone;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Encode parameters
|
||||||
|
const params = new URLSearchParams({
|
||||||
|
usercode: this.config.username,
|
||||||
|
password: this.config.password,
|
||||||
|
gsmno: cleanPhone,
|
||||||
|
message: message,
|
||||||
|
msgheader: this.config.header,
|
||||||
|
dil: 'TR' // Turkish characters support
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = `${this.config.apiUrl || 'https://api.netgsm.com.tr/sms/send/get'}?${params.toString()}`;
|
||||||
|
|
||||||
|
const response = await fetch(url);
|
||||||
|
const textResponse = await response.text();
|
||||||
|
|
||||||
|
// NetGSM returns a code (e.g. 00 123456789) or error code (e.g. 20)
|
||||||
|
// Codes starting with 00, 01, 02 indicate success
|
||||||
|
const code = textResponse.split(' ')[0];
|
||||||
|
|
||||||
|
if (['00', '01', '02'].includes(code)) {
|
||||||
|
return { success: true, jobId: textResponse.split(' ')[1] || code, code };
|
||||||
|
} else {
|
||||||
|
const errorMap: Record<string, string> = {
|
||||||
|
'20': 'Mesaj metni ya da karakter sınırını (1.000) aştı veya mesaj boş.',
|
||||||
|
'30': 'Geçersiz kullanıcı adı , şifre veya kullanıcınızın API erişim izni yok.',
|
||||||
|
'40': 'Gönderici adı (Başlık) sistemde tanımlı değil.',
|
||||||
|
'70': 'Hatalı sorgu.',
|
||||||
|
'50': 'Kendi numaranıza veya Rehberden SMS gönderiyorsanız; Abone kendi numarasını veya rehberindeki bir numarayı gönderici kimliği (MsgHeader) olarak kullanamaz.',
|
||||||
|
'51': 'Aboneliğinizin süresi dolmuş.',
|
||||||
|
'52': 'Aboneliğiniz bulunmamaktadır.',
|
||||||
|
'60': 'Bakiyeniz yetersiz.',
|
||||||
|
'71': 'Gönderim yapmak istediğiniz gsm numarası/numaraları hatalı.'
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
code,
|
||||||
|
error: errorMap[code] || `Bilinmeyen hata kodu: ${code} - Yanıt: ${textResponse}`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
return { success: false, error: (error as Error).message };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
package-lock.json
generated
31
package-lock.json
generated
@@ -18,6 +18,7 @@
|
|||||||
"@radix-ui/react-separator": "^1.1.8",
|
"@radix-ui/react-separator": "^1.1.8",
|
||||||
"@radix-ui/react-slot": "^1.2.4",
|
"@radix-ui/react-slot": "^1.2.4",
|
||||||
"@radix-ui/react-switch": "^1.2.6",
|
"@radix-ui/react-switch": "^1.2.6",
|
||||||
|
"@radix-ui/react-tabs": "^1.1.13",
|
||||||
"@supabase/ssr": "^0.8.0",
|
"@supabase/ssr": "^0.8.0",
|
||||||
"@supabase/supabase-js": "^2.89.0",
|
"@supabase/supabase-js": "^2.89.0",
|
||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
@@ -1368,6 +1369,36 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@radix-ui/react-tabs": {
|
||||||
|
"version": "1.1.13",
|
||||||
|
"resolved": "https://registry.npmjs.org/@radix-ui/react-tabs/-/react-tabs-1.1.13.tgz",
|
||||||
|
"integrity": "sha512-7xdcatg7/U+7+Udyoj2zodtI9H/IIopqo+YOIcZOq1nJwXWBZ9p8xiu5llXlekDbZkca79a/fozEYQXIA4sW6A==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@radix-ui/primitive": "1.1.3",
|
||||||
|
"@radix-ui/react-context": "1.1.2",
|
||||||
|
"@radix-ui/react-direction": "1.1.1",
|
||||||
|
"@radix-ui/react-id": "1.1.1",
|
||||||
|
"@radix-ui/react-presence": "1.1.5",
|
||||||
|
"@radix-ui/react-primitive": "2.1.3",
|
||||||
|
"@radix-ui/react-roving-focus": "1.1.11",
|
||||||
|
"@radix-ui/react-use-controllable-state": "1.2.2"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@types/react": "*",
|
||||||
|
"@types/react-dom": "*",
|
||||||
|
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||||
|
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"@types/react": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"@types/react-dom": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@radix-ui/react-use-callback-ref": {
|
"node_modules/@radix-ui/react-use-callback-ref": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.1.1.tgz",
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
"@radix-ui/react-separator": "^1.1.8",
|
"@radix-ui/react-separator": "^1.1.8",
|
||||||
"@radix-ui/react-slot": "^1.2.4",
|
"@radix-ui/react-slot": "^1.2.4",
|
||||||
"@radix-ui/react-switch": "^1.2.6",
|
"@radix-ui/react-switch": "^1.2.6",
|
||||||
|
"@radix-ui/react-tabs": "^1.1.13",
|
||||||
"@supabase/ssr": "^0.8.0",
|
"@supabase/ssr": "^0.8.0",
|
||||||
"@supabase/supabase-js": "^2.89.0",
|
"@supabase/supabase-js": "^2.89.0",
|
||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
|
|||||||
83
supabase_schema_netgsm.sql
Normal file
83
supabase_schema_netgsm.sql
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
-- Create sms_settings table
|
||||||
|
CREATE TABLE IF NOT EXISTS public.sms_settings (
|
||||||
|
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||||
|
provider TEXT DEFAULT 'netgsm',
|
||||||
|
api_url TEXT DEFAULT 'https://api.netgsm.com.tr/sms/send/get',
|
||||||
|
username TEXT,
|
||||||
|
password TEXT,
|
||||||
|
header TEXT,
|
||||||
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now())
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create sms_logs table
|
||||||
|
CREATE TABLE IF NOT EXISTS public.sms_logs (
|
||||||
|
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||||
|
phone TEXT NOT NULL,
|
||||||
|
message TEXT NOT NULL,
|
||||||
|
status TEXT, -- 'success' or 'error'
|
||||||
|
response_code TEXT,
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now())
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Enable RLS
|
||||||
|
ALTER TABLE public.sms_settings ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE public.sms_logs ENABLE ROW LEVEL SECURITY;
|
||||||
|
|
||||||
|
-- RLS Policies for sms_settings
|
||||||
|
-- Only admins can view settings
|
||||||
|
CREATE POLICY "Admins can view sms settings" ON public.sms_settings
|
||||||
|
FOR SELECT
|
||||||
|
USING (
|
||||||
|
exists (
|
||||||
|
select 1 from public.profiles
|
||||||
|
where profiles.id = auth.uid()
|
||||||
|
and profiles.role = 'admin'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Only admins can update settings
|
||||||
|
CREATE POLICY "Admins can update sms settings" ON public.sms_settings
|
||||||
|
FOR UPDATE
|
||||||
|
USING (
|
||||||
|
exists (
|
||||||
|
select 1 from public.profiles
|
||||||
|
where profiles.id = auth.uid()
|
||||||
|
and profiles.role = 'admin'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Only admins can insert settings (though usually init script does this)
|
||||||
|
CREATE POLICY "Admins can insert sms settings" ON public.sms_settings
|
||||||
|
FOR INSERT
|
||||||
|
WITH CHECK (
|
||||||
|
exists (
|
||||||
|
select 1 from public.profiles
|
||||||
|
where profiles.id = auth.uid()
|
||||||
|
and profiles.role = 'admin'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- RLS Policies for sms_logs
|
||||||
|
-- Only admins can view logs
|
||||||
|
CREATE POLICY "Admins can view sms logs" ON public.sms_logs
|
||||||
|
FOR SELECT
|
||||||
|
USING (
|
||||||
|
exists (
|
||||||
|
select 1 from public.profiles
|
||||||
|
where profiles.id = auth.uid()
|
||||||
|
and profiles.role = 'admin'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- System functionality (via Service Role) will bypass RLS, so we don't strictly need INSERT policies for user logic
|
||||||
|
-- unless we want admins to manually insert logs (unlikely).
|
||||||
|
-- But for good measure, allow admins to delete logs if needed
|
||||||
|
CREATE POLICY "Admins can delete sms logs" ON public.sms_logs
|
||||||
|
FOR DELETE
|
||||||
|
USING (
|
||||||
|
exists (
|
||||||
|
select 1 from public.profiles
|
||||||
|
where profiles.id = auth.uid()
|
||||||
|
and profiles.role = 'admin'
|
||||||
|
)
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user