feature: pope prayer intentions

This commit is contained in:
Benno Tielen 2025-01-28 13:28:18 +01:00
parent cb5fb6dc7a
commit 07e6b35916
8 changed files with 6496 additions and 2 deletions

View file

@ -0,0 +1,151 @@
import { Section } from '@/components/Section/Section'
import { fetchPopePrayerIntentions } from '@/fetch/popePrayerIntentions'
import { Container } from '@/components/Container/Container'
import { Title } from '@/components/Title/Title'
import { P } from '@/components/Text/Paragraph'
import { PageHeader } from '@/compositions/PageHeader/PageHeader'
import { NextPrevButtons } from '@/components/NextPrevButtons/NextPrevButtons'
const months: Record<string, string> = {
"01": "Januar",
"02": "Februar",
"03": "März",
"04": "April",
"05": "Mai",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "Oktober",
"11": "November",
"12": "Dezember",
}
const getMonthAsString = (month: string): string => {
if (months.hasOwnProperty(month)) {
return months[month];
}
return "Unknown";
}
/**
* Get next month
* eg: "12" => "01"
*
* @param month
*/
function nextMonth(month: string) {
let monthNumber = parseInt(month, 10);
monthNumber++;
if (monthNumber > 12) {
monthNumber = 1;
}
return monthNumber.toString().padStart(2, '0');
}
/**
* Get previous month
* eg: "03" => "02"
*
* @param month
*/
function prevMonth(month: string) {
let monthNumber = parseInt(month, 10);
monthNumber--;
if (monthNumber == 0) {
monthNumber = 12;
}
return monthNumber.toString().padStart(2, '0');
}
/**
* Get next url
*
* @param year
* @param month
*/
function nextUrl(year: number, month: string) {
const next = nextMonth(month);
if (next == "01") {
year++;
}
return `/gebetsanliegen-des-papstes/${year}/${next}`;
}
/**
* Get previous url
*
* @param year
* @param month
*/
function prevUrl(year: number, month: string) {
const prev = prevMonth(month);
if (prev == "12") {
year--;
}
return `/gebetsanliegen-des-papstes/${year}/${prev}`;
}
export default async function PrayerIntentionPage({ params }: { params: Promise<{year: string, month: string}>}) {
const {year, month} = await params;
const yearInt = parseInt(year, 10);
let prayer = await fetchPopePrayerIntentions(yearInt, month)
const monthString = getMonthAsString(month)
if(!prayer) {
prayer = {
id: "dummy",
createdAt: "",
updatedAt: "",
month: "01",
year: 2025,
title: "Nicht gefunden",
prayer: "Gebetsanliegen nicht gefunden"
}
}
return (
<>
<PageHeader
title={"Gebetsanliegen des Papstes"}
description={"Der Papst lädt uns ein, uns mit ihm im Gebet zu verbinden und gemeinsam die Welt vor Gott zu tragen."}
/>
<Section padding={"small"}>
<Container>
<Title
color={"contrast"}
title={`${monthString} ${year}: ${prayer.title}`}
size={"sm"}
/>
<P width={"3/4"}>
{ prayer.prayer}
</P>
</Container>
</Section>
<Section>
</Section>
<Section padding={"small"}>
<NextPrevButtons
prev={{
href: prevUrl(yearInt, month),
text: getMonthAsString(prevMonth(month))
}}
next={{
href: nextUrl(yearInt, month),
text: getMonthAsString(nextMonth(month))
}}
/>
</Section>
</>
)
}

View file

@ -0,0 +1,110 @@
import { CollectionConfig } from 'payload'
import { isAdminOrEmployee } from '@/collections/access/admin'
export const PopesPrayerIntentions: CollectionConfig = {
slug: 'popePrayerIntentions',
labels: {
singular: {
de: 'Gebetsanliegen des Papstes'
},
plural: {
de: 'Gebetsanliegen des Papstes'
}
},
fields: [
{
name: 'year',
type: 'number',
label: {
de: "Jahr"
},
required: true,
defaultValue: new Date().getFullYear(),
},
{
name: 'month',
type: 'select',
label: {
de: 'Monat'
},
options: [
{
label: 'Januar',
value: '01',
},
{
label: 'Februar',
value: '02',
},
{
label: 'März',
value: '03',
},
{
label: 'April',
value: '04',
},
{
label: 'Mai',
value: '05',
},
{
label: 'Juni',
value: '06'
},
{
label: 'Juli',
value: '07',
},
{
label: 'August',
value: '08',
},
{
label: 'September',
value: '09',
},
{
label: 'Oktober',
value: '10',
},
{
label: 'November',
value: '11',
},
{
label: 'Dezember',
value: '12'
}
],
required: true,
defaultValue: '01'
},
{
type: 'text',
name: 'title',
label: {
de: "Titel"
},
defaultValue: "Für ",
required: true
},
{
type: 'textarea',
name: 'prayer',
label: {
de: "Gebet"
},
required: true
}
],
admin: {
useAsTitle: 'title',
},
access: {
read: () => true,
create: isAdminOrEmployee(),
update: isAdminOrEmployee(),
delete: isAdminOrEmployee(),
},
}

View file

@ -0,0 +1,36 @@
import { stringify } from 'qs-esm'
import { PopePrayerIntention } from '@/payload-types'
/**
* Fetch pope prayer intentions
*
* @param year
* @param month - in the form of '01' for january, '02' for february ...
*/
export const fetchPopePrayerIntentions = async (year: number, month: string): Promise<PopePrayerIntention | undefined> => {
const query: any = {
and: [
{
month: {
equals: month,
},
year: {
equals: year
}
}
],
}
const stringifiedQuery = stringify(
{
where: query,
limit: 1
},
{ addQueryPrefix: true },
)
const response = await fetch(`http://localhost:3000/api/popePrayerIntentions${stringifiedQuery}`)
if (!response.ok) return undefined
let data = await response.json()
return data.docs[0]
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,37 @@
import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'
export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
await payload.db.drizzle.execute(sql`
CREATE TYPE "public"."enum_pope_prayer_intentions_month" AS ENUM('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
CREATE TABLE IF NOT EXISTS "pope_prayer_intentions" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"year" numeric DEFAULT 2025 NOT NULL,
"month" "enum_pope_prayer_intentions_month" DEFAULT '01' NOT NULL,
"title" varchar DEFAULT 'Für ' NOT NULL,
"prayer" varchar NOT NULL,
"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
);
ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "pope_prayer_intentions_id" uuid;
CREATE INDEX IF NOT EXISTS "pope_prayer_intentions_updated_at_idx" ON "pope_prayer_intentions" USING btree ("updated_at");
CREATE INDEX IF NOT EXISTS "pope_prayer_intentions_created_at_idx" ON "pope_prayer_intentions" USING btree ("created_at");
DO $$ BEGIN
ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_pope_prayer_intentions_fk" FOREIGN KEY ("pope_prayer_intentions_id") REFERENCES "public"."pope_prayer_intentions"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_pope_prayer_intentions_id_idx" ON "payload_locked_documents_rels" USING btree ("pope_prayer_intentions_id");`)
}
export async function down({ payload, req }: MigrateDownArgs): Promise<void> {
await payload.db.drizzle.execute(sql`
ALTER TABLE "pope_prayer_intentions" DISABLE ROW LEVEL SECURITY;
DROP TABLE "pope_prayer_intentions" CASCADE;
ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_pope_prayer_intentions_fk";
DROP INDEX IF EXISTS "payload_locked_documents_rels_pope_prayer_intentions_id_idx";
ALTER TABLE "payload_locked_documents_rels" DROP COLUMN IF EXISTS "pope_prayer_intentions_id";
DROP TYPE "public"."enum_pope_prayer_intentions_month";`)
}

View file

@ -1,5 +1,6 @@
import * as migration_20241205_121237 from './20241205_121237';
import * as migration_20241217_135114_contact_person_photo from './20241217_135114_contact_person_photo';
import * as migration_20250128_100809_pope_prayer_intentions from './20250128_100809_pope_prayer_intentions';
export const migrations = [
{
@ -10,6 +11,11 @@ export const migrations = [
{
up: migration_20241217_135114_contact_person_photo.up,
down: migration_20241217_135114_contact_person_photo.down,
name: '20241217_135114_contact_person_photo'
name: '20241217_135114_contact_person_photo',
},
{
up: migration_20250128_100809_pope_prayer_intentions.up,
down: migration_20250128_100809_pope_prayer_intentions.down,
name: '20250128_100809_pope_prayer_intentions'
},
];

View file

@ -14,6 +14,7 @@ export interface Config {
parish: Parish;
church: Church;
worship: Worship;
popePrayerIntentions: PopePrayerIntention;
announcement: Announcement;
blog: Blog;
highlight: Highlight;
@ -36,6 +37,7 @@ export interface Config {
parish: ParishSelect<false> | ParishSelect<true>;
church: ChurchSelect<false> | ChurchSelect<true>;
worship: WorshipSelect<false> | WorshipSelect<true>;
popePrayerIntentions: PopePrayerIntentionsSelect<false> | PopePrayerIntentionsSelect<true>;
announcement: AnnouncementSelect<false> | AnnouncementSelect<true>;
blog: BlogSelect<false> | BlogSelect<true>;
highlight: HighlightSelect<false> | HighlightSelect<true>;
@ -212,6 +214,19 @@ export interface Worship {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "popePrayerIntentions".
*/
export interface PopePrayerIntention {
id: string;
year: number;
month: '01' | '02' | '03' | '04' | '05' | '06' | '07' | '08' | '09' | '10' | '11' | '12';
title: string;
prayer: string;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "announcement".
@ -559,6 +574,10 @@ export interface PayloadLockedDocument {
relationTo: 'worship';
value: string | Worship;
} | null)
| ({
relationTo: 'popePrayerIntentions';
value: string | PopePrayerIntention;
} | null)
| ({
relationTo: 'announcement';
value: string | Announcement;
@ -708,6 +727,18 @@ export interface WorshipSelect<T extends boolean = true> {
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "popePrayerIntentions_select".
*/
export interface PopePrayerIntentionsSelect<T extends boolean = true> {
year?: T;
month?: T;
title?: T;
prayer?: T;
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "announcement_select".

View file

@ -36,6 +36,7 @@ import { Locations } from '@/collections/Locations'
import { ContactPerson } from '@/collections/ContactPerson'
import { postgresAdapter } from '@payloadcms/db-postgres'
import { gcsStorage } from '@payloadcms/storage-gcs'
import { PopesPrayerIntentions } from '@/collections/PopesPrayerIntentions'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
@ -48,6 +49,7 @@ export default buildConfig({
Parish,
Churches,
Worship,
PopesPrayerIntentions,
Announcements,
Blog,
Highlight,
@ -87,7 +89,7 @@ export default buildConfig({
},
db: postgresAdapter({
idType: "uuid",
push: false,
push: true,
pool: {
connectionString: process.env.DATABASE_URI,
}