feat: Add action and dialog to copy/transfer modules between products
All checks were successful
Staging Build / build (push) Successful in 2m39s
All checks were successful
Staging Build / build (push) Successful in 2m39s
This commit is contained in:
@@ -148,3 +148,61 @@ export async function deleteProduct(id: string) {
|
||||
revalidatePath('/admin/products')
|
||||
revalidatePath('/order')
|
||||
}
|
||||
|
||||
export async function transferModules(
|
||||
sourceProductId: string,
|
||||
targetProductId: string,
|
||||
mode: 'overwrite' | 'append'
|
||||
) {
|
||||
const supabase = await createClient()
|
||||
|
||||
// 1. Fetch modules of source product
|
||||
const { data: sourceModules, error: fetchError } = await supabase
|
||||
.from('product_modules')
|
||||
.select('*')
|
||||
.eq('product_id', sourceProductId)
|
||||
|
||||
if (fetchError) throw fetchError
|
||||
if (!sourceModules || sourceModules.length === 0) return
|
||||
|
||||
// 2. If overwrite, delete existing modules of target product
|
||||
if (mode === 'overwrite') {
|
||||
const { error: deleteError } = await supabase
|
||||
.from('product_modules')
|
||||
.delete()
|
||||
.eq('product_id', targetProductId)
|
||||
if (deleteError) throw deleteError
|
||||
}
|
||||
|
||||
// 3. Map IDs to new UUIDs and fix requirements/exclusions
|
||||
const idMap: Record<string, string> = {}
|
||||
sourceModules.forEach(m => {
|
||||
idMap[m.id] = randomUUID()
|
||||
})
|
||||
|
||||
const newModules = sourceModules.map(m => {
|
||||
const newId = idMap[m.id]
|
||||
const requirements = (m.requirements || []).map((id: string) => idMap[id] || id)
|
||||
const exclusions = (m.exclusions || []).map((id: string) => idMap[id] || id)
|
||||
return {
|
||||
id: newId,
|
||||
product_id: targetProductId,
|
||||
name: m.name,
|
||||
description: m.description,
|
||||
price: m.price,
|
||||
requirements,
|
||||
exclusions,
|
||||
has_quantity: m.has_quantity ?? false,
|
||||
}
|
||||
})
|
||||
|
||||
// 4. Insert into DB
|
||||
const { error: insertError } = await supabase
|
||||
.from('product_modules')
|
||||
.insert(newModules)
|
||||
|
||||
if (insertError) throw insertError
|
||||
|
||||
revalidatePath('/admin/products')
|
||||
revalidatePath('/order')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user