102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
import { db } from '~/drizzle/db'
|
|
import { songs, requestTimes } from '~/drizzle/schema'
|
|
import { eq, sql } from 'drizzle-orm'
|
|
import { cacheService } from '../../../services/cacheService'
|
|
import { createSongRejectedNotification } from '../../../services/notificationService'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = event.context.user
|
|
if (!user || !['SONG_ADMIN', 'ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
|
|
throw createError({
|
|
statusCode: 403,
|
|
message: '没有权限访问'
|
|
})
|
|
}
|
|
|
|
const body = await readBody(event)
|
|
|
|
if (!body.songId || !['approved', 'rejected'].includes(body.status)) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: '参数错误'
|
|
})
|
|
}
|
|
|
|
try {
|
|
let songForNotification = null
|
|
|
|
const result = await db.transaction(async (tx) => {
|
|
const songResult = await tx.select().from(songs).where(eq(songs.id, body.songId)).limit(1)
|
|
const song = songResult[0]
|
|
|
|
if (!song) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
message: '歌曲不存在'
|
|
})
|
|
}
|
|
|
|
songForNotification = song
|
|
|
|
if (body.status === 'rejected') {
|
|
// 如果有 hitRequestId,减少对应时段的已接纳数量
|
|
if (song.hitRequestId) {
|
|
try {
|
|
await tx
|
|
.update(requestTimes)
|
|
.set({
|
|
accepted: sql`GREATEST(0, accepted - 1)`
|
|
})
|
|
.where(eq(requestTimes.id, song.hitRequestId))
|
|
} catch (error) {
|
|
console.error(`减少投稿时段接纳数量失败: ${error.message}`)
|
|
}
|
|
}
|
|
|
|
const note = song.submissionNote ? `[审核未通过] ${body.reason || ''}\n${song.submissionNote}` : `[审核未通过] ${body.reason || ''}`
|
|
|
|
// 释放排期序号并更新备注
|
|
const updatedSong = await tx.update(songs).set({
|
|
expectedPlayDate: null,
|
|
expectedPlaySequence: null,
|
|
hitRequestId: null,
|
|
submissionNote: note
|
|
}).where(eq(songs.id, body.songId)).returning()
|
|
|
|
return { success: true, song: updatedSong[0] }
|
|
} else {
|
|
const note = song.submissionNote ? `[审核已通过]\n${song.submissionNote}` : `[审核已通过]`
|
|
const updatedSong = await tx.update(songs).set({
|
|
submissionNote: note
|
|
}).where(eq(songs.id, body.songId)).returning()
|
|
|
|
return { success: true, song: updatedSong[0] }
|
|
}
|
|
})
|
|
|
|
if (body.status === 'rejected' && songForNotification && songForNotification.requesterId) {
|
|
createSongRejectedNotification(
|
|
songForNotification.requesterId,
|
|
{ title: songForNotification.title, artist: songForNotification.artist },
|
|
body.reason?.trim() || '未过审释放排期',
|
|
getClientIP(event)
|
|
).catch(e => console.error(e))
|
|
}
|
|
|
|
await cacheService.clearSongsCache()
|
|
|
|
return {
|
|
success: true,
|
|
message: body.status === 'rejected' ? '已驳回并释放排期' : '审核已通过',
|
|
data: result.song
|
|
}
|
|
} catch (error) {
|
|
console.error('审核歌曲失败:', error)
|
|
if (error.statusCode) throw error
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: '审核失败: ' + error.message
|
|
})
|
|
}
|
|
})
|