push
This commit is contained in:
@@ -7,6 +7,16 @@ const NewsInput = z.object({
|
||||
body: z.string().min(10),
|
||||
kategorie: z.enum(['Wichtig', 'Pruefung', 'Foerderung', 'Veranstaltung', 'Allgemein']),
|
||||
publishedAt: z.string().datetime().optional().nullable(),
|
||||
attachments: z
|
||||
.array(
|
||||
z.object({
|
||||
name: z.string(),
|
||||
storagePath: z.string(),
|
||||
sizeBytes: z.number().int().optional().nullable(),
|
||||
mimeType: z.string().optional().nullable(),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
})
|
||||
|
||||
export const newsRouter = router({
|
||||
@@ -104,6 +114,16 @@ export const newsRouter = router({
|
||||
body: input.body,
|
||||
kategorie: input.kategorie,
|
||||
publishedAt: input.publishedAt ? new Date(input.publishedAt) : null,
|
||||
...(input.attachments && input.attachments.length > 0 && {
|
||||
attachments: {
|
||||
create: input.attachments.map((a) => ({
|
||||
name: a.name,
|
||||
storagePath: a.storagePath,
|
||||
sizeBytes: a.sizeBytes,
|
||||
mimeType: a.mimeType,
|
||||
})),
|
||||
},
|
||||
}),
|
||||
},
|
||||
})
|
||||
|
||||
@@ -121,23 +141,44 @@ export const newsRouter = router({
|
||||
update: adminProcedure
|
||||
.input(z.object({ id: z.string(), data: NewsInput.partial() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const wasUnpublished = await ctx.prisma.news.findFirst({
|
||||
where: { id: input.id, orgId: ctx.orgId, publishedAt: null },
|
||||
const existing = await ctx.prisma.news.findFirst({
|
||||
where: { id: input.id, orgId: ctx.orgId },
|
||||
})
|
||||
|
||||
const news = await ctx.prisma.news.updateMany({
|
||||
where: { id: input.id, orgId: ctx.orgId },
|
||||
if (!existing) {
|
||||
throw new Error('News not found or access denied')
|
||||
}
|
||||
|
||||
const wasUnpublished = !existing.publishedAt
|
||||
|
||||
const { attachments, ...restData } = input.data
|
||||
|
||||
const news = await ctx.prisma.news.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
...input.data,
|
||||
publishedAt: input.data.publishedAt
|
||||
? new Date(input.data.publishedAt)
|
||||
: undefined,
|
||||
...restData,
|
||||
publishedAt: restData.publishedAt
|
||||
? new Date(restData.publishedAt)
|
||||
: restData.publishedAt === null
|
||||
? null
|
||||
: undefined,
|
||||
...(attachments && {
|
||||
attachments: {
|
||||
deleteMany: {},
|
||||
create: attachments.map((a) => ({
|
||||
name: a.name,
|
||||
storagePath: a.storagePath,
|
||||
sizeBytes: a.sizeBytes,
|
||||
mimeType: a.mimeType,
|
||||
})),
|
||||
},
|
||||
}),
|
||||
},
|
||||
})
|
||||
|
||||
// Trigger push if just published
|
||||
if (wasUnpublished && input.data.publishedAt && input.data.title) {
|
||||
sendPushNotifications(ctx.orgId, input.data.title).catch(console.error)
|
||||
if (wasUnpublished && news.publishedAt && news.title) {
|
||||
sendPushNotifications(ctx.orgId, news.title).catch(console.error)
|
||||
}
|
||||
|
||||
return news
|
||||
|
||||
Reference in New Issue
Block a user