exports.publishSession = async (req, res) => {
try {
const userId = req.user._id
const {
id,
title,
tags = [],
video_url = null,
description = '',
difficulty = 'Beginner'
} = req.body
if (!title || typeof title !== 'string') {
return res.status(400).json({ error: 'Title is required' })
}
const payload = {
user_id: userId,
title,
tags,
video_url,
description,
difficulty,
status: 'published'
}
let query
if (id) {
query = supabase
.from('sessions')
.update(payload)
.eq('id', id)
.eq('user_id', userId)
.select()
.single()
} else {
query = supabase
.from('sessions')
.insert(payload)
.select()
.single()
}
const { data: session, error } = await query
if (error) throw error
const profiles = await fetchProfilesForSessions([session])
res.status(id ? 200 : 201).json({ data: { session: mapRow(session, profiles) } })
} catch (err) {
console.error('publishSession:', err)
res.status(500).json({ error: 'Failed to publish session' })
}
}
exports.publishSession = async (req, res) => {
try {
const userId = req.user._id
const {
id,
title,
tags = [],
video_url = null,
description = '',
difficulty = 'Beginner'
} = req.body
if (!title || typeof title !== 'string') {
return res.status(400).json({ error: 'Title is required' })
}
const payload = {
user_id: userId,
title,
tags,
video_url,
description,
difficulty,
status: 'published'
}
let query
if (id) {
query = supabase
.from('sessions')
.update(payload)
.eq('id', id)
.eq('user_id', userId)
.select()
.single()
} else {
query = supabase
.from('sessions')
.insert(payload)
.select()
.single()
}
const { data: session, error } = await query
if (error) throw error
const profiles = await fetchProfilesForSessions([session])
res.status(id ? 200 : 201).json({ data: { session: mapRow(session, profiles) } })
} catch (err) {
console.error('publishSession:', err)
res.status(500).json({ error: 'Failed to publish session' })
}
}