import type { SocialFeedDashboard, SocialFeedDashboardCreate, SocialFeedDashboardUpdate } from '@nexumi/shared'; import type { SocialFeed } from '@prisma/client'; import { prisma } from '../prisma'; function toDashboard(feed: SocialFeed): SocialFeedDashboard { return { id: feed.id, type: feed.type as SocialFeedDashboard['type'], sourceId: feed.sourceId, channelId: feed.channelId, rolePingId: feed.rolePingId ?? '', template: feed.template, enabled: feed.enabled }; } export async function listSocialFeeds(guildId: string): Promise { const feeds = await prisma.socialFeed.findMany({ where: { guildId }, orderBy: { createdAt: 'asc' } }); return feeds.map(toDashboard); } export async function createSocialFeed( guildId: string, input: SocialFeedDashboardCreate ): Promise { await prisma.guild.upsert({ where: { id: guildId }, update: {}, create: { id: guildId } }); const feed = await prisma.socialFeed.create({ data: { guildId, type: input.type, sourceId: input.sourceId, channelId: input.channelId, rolePingId: input.rolePingId ? input.rolePingId : null, template: input.template ?? null, enabled: input.enabled } }); return toDashboard(feed); } export async function updateSocialFeed( guildId: string, feedId: string, patch: SocialFeedDashboardUpdate ): Promise { const existing = await prisma.socialFeed.findFirst({ where: { id: feedId, guildId } }); if (!existing) { return null; } const { rolePingId, ...rest } = patch; const updated = await prisma.socialFeed.update({ where: { id: feedId }, data: { ...rest, ...(rolePingId !== undefined ? { rolePingId: rolePingId === '' ? null : rolePingId } : {}) } }); return toDashboard(updated); } export async function deleteSocialFeed(guildId: string, feedId: string): Promise { const result = await prisma.socialFeed.deleteMany({ where: { id: feedId, guildId } }); return result.count > 0; }