Skip to content

Commit

Permalink
big change for programmheft (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
leider authored Nov 19, 2024
1 parent 57f17c6 commit 72b3bfb
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 211 deletions.
4 changes: 3 additions & 1 deletion application/backend/rest/programmheft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ app.get("/programmheft/:year/:month", [checkOrgateam], (req: Request, res: Respo
yearMonthString = correctedDatum.fuerKalenderViews;
}

resToJson(res, store.getKalender(yearMonthString));
const kalender = store.getKalender(yearMonthString);
kalender?.sortEvents();
resToJson(res, kalender);
});

app.post("/programmheft", [checkOrgateam], (req: Request, res: Response) => {
Expand Down
56 changes: 28 additions & 28 deletions application/shared/programmheft/Event.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import DatumUhrzeit, { AdditionOptions } from "../commons/DatumUhrzeit.js";
import misc from "../commons/misc.js";

type MinFields = { start: string; title: string; farbe: string };
type AdditionalFields = { email?: string; emailOffset?: number; was?: string; wer?: string };

export class Event {
start: string;
title: string;
farbe: string;
email?: string;
emailOffset?: number;
was?: string;
wer?: string;

constructor({ start, title, farbe }: MinFields) {
constructor({
start,
farbe,
email,
emailOffset,
was,
wer,
}: {
start: string;
farbe: string;
email?: string;
emailOffset?: number;
was?: string;
wer?: string;
}) {
this.start = start;
this.title = title;
this.farbe = farbe;
this.email = email;
this.emailOffset = emailOffset;
this.was = was;
this.wer = wer;
}

static minimal(event: MinFields): Event {
return new Event(event);
get title(): string {
return `${(this.was ?? "").trim()} (${(this.wer ?? "").trim()})`;
}

static fromLine(line: string): Event | undefined {
Expand Down Expand Up @@ -51,33 +64,20 @@ export class Event {
const farbe = elements[2];
const start = dates(elements[3]);
if (was && start) {
const event = Event.minimal({
const email = elements[4] || "";
return new Event({
start,
title: `${was.trim()} (${wer.trim()})`,
farbe: farbe.trim(),
email: email.trim(),
emailOffset: misc.isNumber(elements[5]) ? Number.parseInt(elements[5]) : 7,
was: was.trim(),
wer: wer.trim(),
});
const email = elements[4] || "";
if (email) {
const emailOffset = misc.isNumber(elements[5]) ? Number.parseInt(elements[5]) : 7;
event.addMore({ email: email.trim(), emailOffset: emailOffset, was: was.trim(), wer: wer.trim() });
}
return event;
}
}

addMore({ email, emailOffset, was, wer }: AdditionalFields) {
this.email = email;
this.emailOffset = emailOffset;
this.was = was;
this.wer = wer;
}

get asTextLine(): string {
return `${this.was ?? ""}|${this.wer ?? ""}|${this.farbe}|${this.start}|${this.email ?? ""}|${this.emailOffset ?? 7}`;
}

moveBy(options: AdditionOptions) {
this.start = DatumUhrzeit.forISOString(this.start).plus(options).tagMonatJahrKompakt;
this.start = DatumUhrzeit.forISOString(this.start).plus(options).toISOString;
return this;
}
}
50 changes: 17 additions & 33 deletions application/shared/programmheft/kalender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import misc from "../commons/misc.js";
import DatumUhrzeit from "../commons/DatumUhrzeit.js";
import { Event } from "./Event.js";

function eventsToObject(contents?: string): Event[] {
if (!contents) {
return [];
}

const lines = contents.replaceAll("\\", "").split(/[\n\r]/);
function eventsToObject(contents: string): Event[] {
const lines = contents.split(/[\n\r]/);
return misc.compact(lines.map(Event.fromLine)) as Event[];
}

Expand Down Expand Up @@ -47,27 +43,24 @@ Danke & keep swingin'`;
}
}

function eventsToText(events: Event[]): string {
const strings = events.map((event) => event.asTextLine);
return [
`Was | Wer | Farbe | Wann | Email | Tage vorher
--- | --- | --- | --- | --- | ---`,
...strings,
].join("\n");
}

export default class Kalender {
id: string;
text = "";
events: Event[] = [];

constructor(object?: { id: string; text: string }) {
constructor(object?: { id: string; text: string; events?: Event[] }) {
if (object && object.id && object.id.split("/").length === 2) {
const splits = object.id.split("/");
if (misc.isNumber(splits[0]) && misc.isNumber(splits[1])) {
this.id = object.id;
if (object.text !== "") {
this.text = object.text;
}
if (!object.events) {
this.events = eventsToObject(this.text);
} else {
this.events = object.events.map((each) => new Event(each));
}
return;
}
}
Expand All @@ -78,32 +71,23 @@ export default class Kalender {
return this.id && this.id.split("/")[0];
}

textMovedTwoMonths() {
const oldEvents = eventsToObject(this.text);
const newEvents = oldEvents.map((each) => {
return each.moveBy({ monate: 2 });
});
return eventsToText(newEvents);
}

textMovedWithBase(otherKalId: string): string {
eventsMovedWithBase(otherKalId: string) {
const thisDatum = DatumUhrzeit.forYYYYslashMM(this.id);
const otherDatum = DatumUhrzeit.forYYYYslashMM(otherKalId);
const differenz = otherDatum.differenzInTagen(thisDatum);
const newEvents = eventsToObject(this.text).map((each) => {
return each.moveBy({ tage: differenz });
const result = this.events.map((each) => {
return new Event(each).moveBy({ tage: differenz });
});
return eventsToText(newEvents);
result.sort((a, b) => a.start.localeCompare(b.start));
return result;
}

asEvents(): Event[] {
return eventsToObject(this.text);
sortEvents() {
this.events.sort((a, b) => a.start.localeCompare(b.start));
}

eventsToSend(aDatumUhrzeit: DatumUhrzeit): EmailEvent[] {
const events = this.asEvents()
.filter((e) => !!e.email)
.map((e) => new EmailEvent(e));
const events = this.events.filter((e) => !!e.email).map((e) => new EmailEvent(e));
return events.filter((e) => e.shouldSendOn(aDatumUhrzeit));
}
}
76 changes: 38 additions & 38 deletions application/shared/test/programmheft/kalender_object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { describe, expect, it } from "vitest";

import Kalender, { EmailEvent } from "../../programmheft/kalender.js";
import { Event } from "../../programmheft/Event.js";
import DatumUhrzeit from "../../commons/DatumUhrzeit.js";

describe("Kalender", () => {
Expand All @@ -11,7 +12,7 @@ describe("Kalender", () => {
expect(kalender.id).to.eql("2018/01");
expect(kalender.year()).to.eql("2018");
expect(kalender.text).toBeDefined();
expect(kalender.asEvents()).to.eql([]);
expect(kalender.events).to.eql([]);
});

it("parses date correctly", () => {
Expand All @@ -32,7 +33,7 @@ describe("Kalender", () => {
text: "",
});
expect(kalender.text).to.eql("");
expect(kalender.asEvents()).to.eql([]);
expect(kalender.events).to.eql([]);
});

it("parses old style default text correctly", () => {
Expand All @@ -43,7 +44,7 @@ describe("Kalender", () => {
expect(kalender.text).to.eql(`Was | Wer | Farbe | Wann
--- | --- | --- | ---
`);
expect(kalender.asEvents()).to.eql([]);
expect(kalender.events).to.eql([]);
});
});

Expand All @@ -55,13 +56,10 @@ describe("Kalender", () => {
Irgendwas | Irgendwer | Green | 13.12.2020
`,
});
expect(kalender.asEvents()).to.eql([
{
start: "2020-12-12T23:00:00.000Z",
title: "Irgendwas (Irgendwer)",
farbe: "Green",
},
]);
const event = kalender.events[0];
expect(event.start).to.eql("2020-12-12T23:00:00.000Z");
expect(event.title).to.eql("Irgendwas (Irgendwer)");
expect(event.farbe).to.eql("Green");
});

it("ohne Jahr - parses old style filled as not existing", () => {
Expand All @@ -71,7 +69,7 @@ Irgendwas | Irgendwer | Green | 13.12.2020
Irgendwas | Irgendwer | Green | 13.12.
`,
});
expect(kalender.asEvents()).to.eql([]);
expect(kalender.events).to.eql([]);
});

it("parses broken date text correctly (e.g. empty)", () => {
Expand All @@ -81,7 +79,7 @@ Irgendwas | Irgendwer | Green | 13.12.
Irgendwas | Irgendwer | Green | 33.
`,
});
expect(kalender.asEvents()).to.eql([]);
expect(kalender.events).to.eql([]);
});
});

Expand All @@ -93,17 +91,12 @@ Irgendwas | Irgendwer | Green | 33.
Irgendwas | Irgendwer | Green | 13.12.2020 | andreas@andreas.as
`,
});
expect(kalender.asEvents()).to.eql([
{
start: "2020-12-12T23:00:00.000Z",
title: "Irgendwas (Irgendwer)",
farbe: "Green",
email: "andreas@andreas.as",
emailOffset: 7,
was: "Irgendwas",
wer: "Irgendwer",
},
]);
const event = kalender.events[0];
expect(event.start).to.eql("2020-12-12T23:00:00.000Z");
expect(event.title).to.eql("Irgendwas (Irgendwer)");
expect(event.farbe).to.eql("Green");
expect(event.email).to.eql("andreas@andreas.as");
expect(event.emailOffset).to.eql(7);
});

it("parses new style filled text correctly (email und offset)", () => {
Expand All @@ -113,18 +106,14 @@ Irgendwas | Irgendwer | Green | 13.12.2020 | andreas@andreas.as
Irgendwas | Irgendwer | Green | 13.12.2020 | andreas@andreas.as | 14
`,
});
const events = kalender.asEvents();
expect(events).to.eql([
{
start: "2020-12-12T23:00:00.000Z",
title: "Irgendwas (Irgendwer)",
farbe: "Green",
email: "andreas@andreas.as",
emailOffset: 14,
was: "Irgendwas",
wer: "Irgendwer",
},
]);
const events = kalender.events;
const event = kalender.events[0];
expect(event.start).to.eql("2020-12-12T23:00:00.000Z");
expect(event.title).to.eql("Irgendwas (Irgendwer)");
expect(event.farbe).to.eql("Green");
expect(event.email).to.eql("andreas@andreas.as");
expect(event.emailOffset).to.eql(14);

const nov29 = DatumUhrzeit.forGermanStringOrNow("29.11.20", "01:13");
expect(new EmailEvent(events[0]).shouldSendOn(nov29)).toBeTruthy();
});
Expand All @@ -147,7 +136,6 @@ Irgendwas | Irgendwer | Green | 13.12.2020 | andreas@andreas.as | 14
const emailEvent = emailEvents[0];
expect(emailEvent.event).to.eql({
start: "2020-12-12T23:00:00.000Z",
title: "Irgendwas (Irgendwer)",
farbe: "Green",
email: "andreas@andreas.as",
emailOffset: 14,
Expand Down Expand Up @@ -188,7 +176,7 @@ Danke & keep swingin'`,
});

it("und berücksichtigt den Offset", () => {
const emailEvents = kalender.asEvents();
const emailEvents = kalender.events;
expect(emailEvents).to.have.length(9);

const emailEvent = emailEvents[0];
Expand All @@ -198,10 +186,22 @@ Danke & keep swingin'`,
emailOffset: 7,
farbe: "DodgerBlue",
start: "2024-10-31T23:00:00.000Z",
title: "Booking fertig + Pressematerial liegt vor (Torsten, Andreas, Gernot, Amelie, Niklas)",
was: "Booking fertig + Pressematerial liegt vor",
wer: "Torsten, Andreas, Gernot, Amelie, Niklas",
});
});
});

describe("verschiebt und sortiert events", () => {
it("korrekt", () => {
const kalender = new Kalender({
id: "2020/12",
text: "",
events: [new Event({ start: "2020-12-12T23:00:00.000Z", farbe: "" }), new Event({ start: "2020-12-01T23:00:00.000Z", farbe: "" })],
});
const movedEvents = kalender.eventsMovedWithBase("2022/03");
expect(movedEvents[0].start).to.eql("2022-03-02T00:00:00+01:00");
expect(movedEvents[1].start).to.eql("2022-03-13T00:00:00+01:00");
});
});
});
1 change: 0 additions & 1 deletion application/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"react-dnd-touch-backend": "16.0.1",
"react-dom": "18.3.1",
"react-router-dom": "6.27.0",
"react-simplemde-editor": "5.2.0",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion application/vue/src/components/mails/MailingLists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default function MailingLists() {
onFinish={saveForm}
layout="vertical"
>
<JazzPageHeader title="Mailinglisten" buttons={[<SaveButton key="save" disabled={!dirty} />]}></JazzPageHeader>
<JazzPageHeader title="Mailinglisten" buttons={[<SaveButton key="save" disabled={!dirty} />]} />
<RowWrapper>
<Row gutter={12}>
<Col span={24}>
Expand Down
2 changes: 1 addition & 1 deletion application/vue/src/components/options/TerminePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function TerminePage() {
{
fieldName: ["startDate", "endDate"],
label: "Start und Ende",
type: "date",
type: "startEnd",
width: "s",
required: true,
},
Expand Down
Loading

0 comments on commit 72b3bfb

Please sign in to comment.