feat(content): add announcements and public news flows
This commit is contained in:
74
packages/db/src/announcements.ts
Normal file
74
packages/db/src/announcements.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import {
|
||||
type AnnouncementPlacement,
|
||||
createAnnouncementInputSchema,
|
||||
updateAnnouncementInputSchema,
|
||||
} from "@cms/content"
|
||||
|
||||
import { db } from "./client"
|
||||
|
||||
export type PublicAnnouncement = {
|
||||
id: string
|
||||
title: string
|
||||
message: string
|
||||
ctaLabel: string | null
|
||||
ctaHref: string | null
|
||||
placement: string
|
||||
priority: number
|
||||
}
|
||||
|
||||
export async function listAnnouncements(limit = 200) {
|
||||
return db.announcement.findMany({
|
||||
orderBy: [{ priority: "asc" }, { updatedAt: "desc" }],
|
||||
take: limit,
|
||||
})
|
||||
}
|
||||
|
||||
export async function createAnnouncement(input: unknown) {
|
||||
const payload = createAnnouncementInputSchema.parse(input)
|
||||
|
||||
return db.announcement.create({
|
||||
data: payload,
|
||||
})
|
||||
}
|
||||
|
||||
export async function updateAnnouncement(input: unknown) {
|
||||
const payload = updateAnnouncementInputSchema.parse(input)
|
||||
const { id, ...data } = payload
|
||||
|
||||
return db.announcement.update({
|
||||
where: { id },
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteAnnouncement(id: string) {
|
||||
return db.announcement.delete({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
|
||||
export async function listActiveAnnouncements(
|
||||
placement: AnnouncementPlacement,
|
||||
now = new Date(),
|
||||
): Promise<PublicAnnouncement[]> {
|
||||
const announcements = await db.announcement.findMany({
|
||||
where: {
|
||||
placement,
|
||||
isVisible: true,
|
||||
OR: [{ startsAt: null }, { startsAt: { lte: now } }],
|
||||
AND: [{ OR: [{ endsAt: null }, { endsAt: { gte: now } }] }],
|
||||
},
|
||||
orderBy: [{ priority: "asc" }, { createdAt: "desc" }],
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
message: true,
|
||||
ctaLabel: true,
|
||||
ctaHref: true,
|
||||
placement: true,
|
||||
priority: true,
|
||||
},
|
||||
})
|
||||
|
||||
return announcements
|
||||
}
|
||||
Reference in New Issue
Block a user