first commit
This commit is contained in:
53
app/api/user/addresses/[id]/default/route.ts
Normal file
53
app/api/user/addresses/[id]/default/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id: addressId } = await params
|
||||
|
||||
// Check if address belongs to user
|
||||
const existingAddress = await prisma.address.findFirst({
|
||||
where: {
|
||||
id: addressId,
|
||||
userId: session.user.id
|
||||
}
|
||||
})
|
||||
|
||||
if (!existingAddress) {
|
||||
return NextResponse.json({ error: 'Address not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
// Unset all other default addresses for this user
|
||||
await prisma.address.updateMany({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
isDefault: true
|
||||
},
|
||||
data: { isDefault: false }
|
||||
})
|
||||
|
||||
// Set this address as default
|
||||
const address = await prisma.address.update({
|
||||
where: { id: addressId },
|
||||
data: { isDefault: true }
|
||||
})
|
||||
|
||||
return NextResponse.json({ address })
|
||||
} catch (error) {
|
||||
console.error('Error setting default address:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to set default address' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
122
app/api/user/addresses/[id]/route.ts
Normal file
122
app/api/user/addresses/[id]/route.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id: addressId } = await params
|
||||
const data = await request.json()
|
||||
const {
|
||||
firstName,
|
||||
lastName,
|
||||
company,
|
||||
address1,
|
||||
address2,
|
||||
city,
|
||||
state,
|
||||
zipCode,
|
||||
country,
|
||||
phone,
|
||||
isDefault,
|
||||
type
|
||||
} = data
|
||||
|
||||
// Check if address belongs to user
|
||||
const existingAddress = await prisma.address.findFirst({
|
||||
where: {
|
||||
id: addressId,
|
||||
userId: session.user.id
|
||||
}
|
||||
})
|
||||
|
||||
if (!existingAddress) {
|
||||
return NextResponse.json({ error: 'Address not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
// If setting as default, unset other default addresses
|
||||
if (isDefault) {
|
||||
await prisma.address.updateMany({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
isDefault: true,
|
||||
id: { not: addressId }
|
||||
},
|
||||
data: { isDefault: false }
|
||||
})
|
||||
}
|
||||
|
||||
const address = await prisma.address.update({
|
||||
where: { id: addressId },
|
||||
data: {
|
||||
firstName,
|
||||
lastName,
|
||||
company,
|
||||
address1,
|
||||
address2,
|
||||
city,
|
||||
state,
|
||||
zipCode,
|
||||
country: country || 'India',
|
||||
phone,
|
||||
isDefault,
|
||||
type
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ address })
|
||||
} catch (error) {
|
||||
console.error('Error updating address:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update address' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id: addressId } = await params
|
||||
|
||||
// Check if address belongs to user
|
||||
const existingAddress = await prisma.address.findFirst({
|
||||
where: {
|
||||
id: addressId,
|
||||
userId: session.user.id
|
||||
}
|
||||
})
|
||||
|
||||
if (!existingAddress) {
|
||||
return NextResponse.json({ error: 'Address not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
await prisma.address.delete({
|
||||
where: { id: addressId }
|
||||
})
|
||||
|
||||
return NextResponse.json({ message: 'Address deleted successfully' })
|
||||
} catch (error) {
|
||||
console.error('Error deleting address:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete address' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
94
app/api/user/addresses/route.ts
Normal file
94
app/api/user/addresses/route.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const addresses = await prisma.address.findMany({
|
||||
where: { userId: session.user.id },
|
||||
orderBy: [
|
||||
{ isDefault: 'desc' },
|
||||
{ createdAt: 'desc' }
|
||||
]
|
||||
})
|
||||
|
||||
return NextResponse.json({ addresses })
|
||||
} catch (error) {
|
||||
console.error('Error fetching addresses:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch addresses' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const data = await request.json()
|
||||
const {
|
||||
firstName,
|
||||
lastName,
|
||||
company,
|
||||
address1,
|
||||
address2,
|
||||
city,
|
||||
state,
|
||||
zipCode,
|
||||
country,
|
||||
phone,
|
||||
isDefault,
|
||||
type
|
||||
} = data
|
||||
|
||||
// If setting as default, unset other default addresses
|
||||
if (isDefault) {
|
||||
await prisma.address.updateMany({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
isDefault: true
|
||||
},
|
||||
data: { isDefault: false }
|
||||
})
|
||||
}
|
||||
|
||||
const address = await prisma.address.create({
|
||||
data: {
|
||||
userId: session.user.id,
|
||||
firstName,
|
||||
lastName,
|
||||
company,
|
||||
address1,
|
||||
address2,
|
||||
city,
|
||||
state,
|
||||
zipCode,
|
||||
country: country || 'India',
|
||||
phone,
|
||||
isDefault,
|
||||
type
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ address })
|
||||
} catch (error) {
|
||||
console.error('Error creating address:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create address' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user