Commit f2abd603 authored by Grant's avatar Grant
Browse files

add demo data

parent 77c13ecb
Loading
Loading
Loading
Loading
Loading

data/events.json

deleted100644 → 0
+0 −47
Original line number Diff line number Diff line
[
  {
    "id": "CANVAS-2023",
    "name": "Canvas 2023",
    "start": "2023-08-04T16:00:00.000Z",
    "end": "2023-08-07T03:59:00.000Z",
    "logos": [],
    "endpoints": {},
    "social": [
      {
        "label": "Community",
        "icon": "lemmy",
        "url": "https://toast.ooo/c/canvas"
      }
    ]
  },
  {
    "id": "CANVAS-2024",
    "name": "Canvas 2024",
    "start": "2024-07-12T04:00:00.000Z",
    "end": "2024-07-16T04:00:00.000Z",
    "logos": [],
    "endpoints": {},
    "social": [
      {
        "label": "Community",
        "icon": "lemmy",
        "url": "https://toast.ooo/c/canvas"
      },
      {
        "label": "Announcements",
        "icon": "mastodon",
        "url": "https://social.fediverse.events/@canvas"
      },
      {
        "label": "Matrix Space",
        "icon": "matrix",
        "url": "https://matrix.to/#/#canvas:aftermath.gg?via=matrix.org"
      },
      {
        "label": "Discord",
        "icon": "discord",
        "url": "https://discord.gg/mEUqXZw8kR"
      }
    ]
  }
]
+20 −0
Original line number Diff line number Diff line
import { DemoDataManager } from "../demo-data/DemoDataManager";
import type {
  Prisma,
  Event as DBEvent,
@@ -20,6 +21,14 @@ export class Event {
  }

  static async find(where: Prisma.EventWhereUniqueInput) {
    if (DemoDataManager.IS_DEMO) {
      const event = DemoDataManager.get()
        .getEvents()
        .find((e) => e.id === where.id);
      if (!event) throw new Error("Event not found");
      return this.load(event);
    }

    return this.load(
      await prisma.event.findFirstOrThrow({
        where,
@@ -32,6 +41,17 @@ export class Event {
  }

  static async query(opts: Prisma.EventFindManyArgs) {
    if (DemoDataManager.IS_DEMO) {
      const gte: Date = (opts.where!.start! as any).gte;
      const lte: Date = (opts.where!.start! as any).lte;

      const events = DemoDataManager.get()
        .getEvents()
        .filter((e) => e.start >= gte && e.start <= lte);

      return events.map((e) => this.load(e));
    }

    const events = await prisma.event.findMany({
      ...opts,
      include: {
+71 −0
Original line number Diff line number Diff line
import type { components } from "../types/openapi";
import fs from "fs";
import path from "path";
import type { Event, EventLogo, EventSocial } from "../generated/prisma";

export class DemoDataManager {
  private static instance: DemoDataManager;
  static readonly IS_DEMO = !!process.env.DEMO_MODE;

  static get() {
    if (!this.instance) this.instance = new DemoDataManager();
    return this.instance;
  }

  private constructor() {}

  private events: any[] = [];

  initialize() {
    this.events = JSON.parse(
      fs.readFileSync(path.join(__dirname, "events.json"), "utf8")
    );
  }

  loadPlaceholders(input: string): string {
    const placeholders = {
      $NOW: new Date().toISOString(),
      $TOMORROW: (() => {
        const date = new Date();
        date.setDate(date.getDate() + 1);
        return date.toISOString();
      })(),
      $WEEK1: (() => {
        const date = new Date();
        date.setDate(date.getDate() + 8);
        return date.toISOString();
      })(),
      $WEEK: (() => {
        const date = new Date();
        date.setDate(date.getDate() + 7);
        return date.toISOString();
      })(),
    };

    for (const [k, v] of Object.entries(placeholders)) {
      input = input.replace(new RegExp(k.replace(/\$/g, "\\$"), "g"), v);
    }
    return input;
  }

  getEvents() {
    const events: (Event & {
      EventLogo: EventLogo[];
      EventSocial: EventSocial[];
    })[] = JSON.parse(this.loadPlaceholders(JSON.stringify(this.events)));

    for (const event of events) {
      event.start = new Date(event.start);
      event.end = new Date(event.end);
      event.updatedAt = new Date(event.updatedAt);
      event.createdAt = new Date(event.createdAt);

      for (const social of event.EventSocial) {
        social.updatedAt = new Date(social.updatedAt);
        social.createdAt = new Date(social.createdAt);
      }
    }

    return events;
  }
}
+95 −0
Original line number Diff line number Diff line
[
  {
    "id": "CANVAS-2023",
    "name": "Canvas 2023",
    "start": "2023-08-04T16:00:00.000Z",
    "end": "2023-08-07T03:59:00.000Z",
    "endpoints": {},
    "EventLogo": [],
    "EventSocial": [
      {
        "id": "CANVAS-2023-SOCIAL1",
        "label": "Community",
        "icon": "lemmy",
        "url": "https://toast.ooo/c/canvas",
        "order": 0,
        "createdAt": "2023-08-04T16:00:00.000Z",
        "updatedAt": "2023-08-04T16:00:00.000Z"
      }
    ],
    "createdAt": "2023-08-04T16:00:00.000Z",
    "updatedAt": "2023-08-04T16:00:00.000Z"
  },
  {
    "id": "CANVAS-NOW",
    "name": "Canvas Now",
    "start": "$NOW",
    "end": "$TOMORROW",
    "endpoints": {},
    "EventLogo": [],
    "EventSocial": [
      {
        "id": "CANVAS-2024-SOCIAL1",
        "label": "Community",
        "icon": "lemmy",
        "url": "https://toast.ooo/c/canvas",
        "order": 0,
        "createdAt": "2024-07-12T04:00:00.000Z",
        "updatedAt": "2024-07-12T04:00:00.000Z"
      },
      {
        "id": "CANVAS-2024-SOCIAL2",
        "label": "Announcements",
        "icon": "mastodon",
        "url": "https://social.fediverse.events/@canvas",
        "order": 0,
        "createdAt": "2024-07-12T04:00:00.000Z",
        "updatedAt": "2024-07-12T04:00:00.000Z"
      },
      {
        "id": "CANVAS-2024-SOCIAL3",
        "label": "Matrix Space",
        "icon": "matrix",
        "url": "https://matrix.to/#/#canvas:aftermath.gg?via=matrix.org",
        "order": 0,
        "createdAt": "2024-07-12T04:00:00.000Z",
        "updatedAt": "2024-07-12T04:00:00.000Z"
      },
      {
        "id": "CANVAS-2024-SOCIAL4",
        "label": "Discord",
        "icon": "discord",
        "url": "https://discord.gg/mEUqXZw8kR",
        "order": 0,
        "createdAt": "2024-07-12T04:00:00.000Z",
        "updatedAt": "2024-07-12T04:00:00.000Z"
      }
    ],
    "createdAt": "2024-07-12T04:00:00.000Z",
    "updatedAt": "2024-07-12T04:00:00.000Z"
  },
  {
    "id": "IN-ONE-WEEK",
    "name": "A Cool Event",
    "start": "$WEEK",
    "end": "$WEEK1",
    "endpoints": {
      "open": "https://open-demo-event.fediverse.events",
      "auth_open": "https://open-demo-event.fediverse.events/go/auth-login"
    },
    "EventLogo": [],
    "EventSocial": [
      {
        "id": "IN-ONE-WEEK-SOCIAL1",
        "label": "Community",
        "icon": "lemmy",
        "url": "https://toast.ooo/c/canvas",
        "order": 0,
        "createdAt": "2024-07-12T04:00:00.000Z",
        "updatedAt": "2024-07-12T04:00:00.000Z"
      }
    ],
    "createdAt": "2024-07-12T04:00:00.000Z",
    "updatedAt": "2024-07-12T04:00:00.000Z"
  }
]
+5 −1
Original line number Diff line number Diff line
@@ -3,13 +3,17 @@ import type { components, paths } from "./types/openapi";
import { openapiRouter } from "./types/express";
import { HandoffProvider } from "./providers/HandoffProvider";
import { Event } from "./controller/Event";
import { DemoDataManager } from "./demo-data/DemoDataManager";

if (process.env.DEMO_MODE) {
  DemoDataManager.get().initialize();
}

HandoffProvider.get()
  .initialize()
  .then(() => {
    console.log("handoff initialized");
  });

const app = express();

app.get("/", (req, res) => {
Loading