obs-ss-plugin-webui/app/api/syncGroups/route.ts
Decobus 5789986bb6
Some checks failed
Lint and Build / build (22) (pull_request) Failing after 32s
Lint and Build / build (20) (pull_request) Failing after 34s
Add OBS group management feature and documentation
- Add group_name column to teams table for mapping teams to OBS groups
- Create API endpoints for group creation (/api/createGroup) and bulk sync (/api/syncGroups)
- Update teams UI with group status display and creation buttons
- Implement automatic group assignment when adding streams
- Add comprehensive OBS setup documentation (docs/OBS_SETUP.md)
- Fix team list spacing issue with explicit margins
- Update OBS client with group management functions
- Add database migration script for existing deployments

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-20 00:28:16 -04:00

81 lines
No EOL
2.3 KiB
TypeScript

import { NextResponse } from 'next/server';
import { open } from 'sqlite';
import sqlite3 from 'sqlite3';
import path from 'path';
import { getTableName, BASE_TABLE_NAMES } from '@/lib/constants';
const { createGroupIfNotExists } = require('@/lib/obsClient');
const FILE_DIRECTORY = path.resolve(process.env.FILE_DIRECTORY || './files');
export async function POST() {
try {
// Open database connection
const dbPath = path.join(FILE_DIRECTORY, 'sources.db');
const db = await open({
filename: dbPath,
driver: sqlite3.Database,
});
const teamsTableName = getTableName(BASE_TABLE_NAMES.TEAMS, {
year: 2025,
season: 'summer',
suffix: 'sat'
});
// Get all teams without groups
const teamsWithoutGroups = await db.all(
`SELECT team_id, team_name FROM ${teamsTableName} WHERE group_name IS NULL`
);
const syncResults = [];
for (const team of teamsWithoutGroups) {
try {
// Create group in OBS using team name
const obsResult = await createGroupIfNotExists(team.team_name);
// Update database with group name
await db.run(
`UPDATE ${teamsTableName} SET group_name = ? WHERE team_id = ?`,
[team.team_name, team.team_id]
);
syncResults.push({
teamId: team.team_id,
teamName: team.team_name,
groupName: team.team_name,
success: true,
obsResult
});
} catch (error) {
console.error(`Error syncing team ${team.team_id}:`, error);
syncResults.push({
teamId: team.team_id,
teamName: team.team_name,
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
});
}
}
await db.close();
const successCount = syncResults.filter(r => r.success).length;
const failureCount = syncResults.filter(r => !r.success).length;
return NextResponse.json({
success: true,
message: `Sync completed: ${successCount} successful, ${failureCount} failed`,
results: syncResults,
summary: {
total: syncResults.length,
successful: successCount,
failed: failureCount
}
});
} catch (error) {
console.error('Error syncing groups:', error);
return NextResponse.json({ error: 'Failed to sync groups' }, { status: 500 });
}
}