From 84240035928efcd9dca008c73009d3d3ef627944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Holl=C3=B3?= Date: Thu, 8 Feb 2024 11:20:03 +0100 Subject: [PATCH] feat: extend event data on api next (#1257) * fix: Unable to compile TypeScript Try `npm i --save-dev @types/matomo-tracker` if it exists or add a new declaration (.d.ts) file containing `declare module 'matomo-tracker'; * extend events with responsible orgs and counts --- common/api/src/events/events-controller.ts | 21 ++++++++ common/api/src/events/events-service.ts | 58 +++++++++++++++++++++- common/api/tsconfig.json | 6 ++- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/common/api/src/events/events-controller.ts b/common/api/src/events/events-controller.ts index 88914e4ed..28e5dfd07 100644 --- a/common/api/src/events/events-controller.ts +++ b/common/api/src/events/events-controller.ts @@ -33,6 +33,7 @@ export class EventsController extends Controller { { id: "a24004fa-aee8-40e2-aeb8-c6a40a1a2ee4", name: "Consensus in heterogeneous groups", + slug: "event-slug", url: "https://community.platform.org/url/to/event", background: "https://img.platform.org/public/url/of/event/background", description: @@ -150,10 +151,25 @@ export class EventsController extends Controller { stage: { title: "Hybrid", }, + _count: { + participants: 20, + waitingList: 3, + }, + responsibleOrganizations: [ + { + organization: { + name: "Platform", + slug: "platform", + logo: "https://img.platform.org/public/url/of/logo", + url: "https://community.platform.org/url/to/organization", + }, + }, + ], }, { id: "ffca2a3a-c0bf-4931-b65a-8d8ccf867096", name: "Smallest Event", + slug: "smallest-event", url: null, background: null, description: null, @@ -176,6 +192,11 @@ export class EventsController extends Controller { targetGroups: [], experienceLevel: null, stage: null, + _count: { + participants: 0, + waitingList: 0, + }, + responsibleOrganizations: [], }, ], skip: 0, diff --git a/common/api/src/events/events-service.ts b/common/api/src/events/events-service.ts index f9f7b11b8..928198aab 100644 --- a/common/api/src/events/events-service.ts +++ b/common/api/src/events/events-service.ts @@ -5,6 +5,7 @@ import type { Request } from "express"; import { decorate } from "../lib/matomoUrlDecorator"; import { getBaseURL } from "../../src/utils"; import { filterEventByVisibility } from "../public-fields-filtering.server"; +import { GravityType } from "imgproxy/dist/types"; type Events = Awaited>; @@ -83,6 +84,23 @@ async function getEvents(request: Request, skip: number, take: number) { title: true, }, }, + responsibleOrganizations: { + select: { + organization: { + select: { + name: true, + slug: true, + logo: true, + }, + }, + }, + }, + _count: { + select: { + participants: true, + waitingList: true, + }, + }, }, where: { published: true, @@ -110,7 +128,7 @@ async function getEvents(request: Request, skip: number, take: number) { const enhancedEvents = await Promise.all( events.map(async (event) => { - const { slug, background, ...rest } = event; + const { slug, background, responsibleOrganizations, ...rest } = event; let publicBackground: string | null = null; if (authClient !== undefined) { @@ -131,7 +149,43 @@ async function getEvents(request: Request, skip: number, take: number) { ? decorate(request, `${baseURL}/event/${slug}`) : null; - const enhancedEvent = { ...rest, background: publicBackground }; + const enhancedResponsibleOrganizations = responsibleOrganizations.map( + (relation) => { + const { slug, logo, ...rest } = relation.organization; + let publicLogo: string | null = null; + if (authClient !== undefined) { + if (logo !== null) { + const publicURL = getPublicURL(authClient, logo); + if (publicURL !== null) { + publicLogo = getImageURL(publicURL, { + resize: { type: "fill", width: 64, height: 64 }, + gravity: GravityType.center, + }); + } + } + } + let baseURL = getBaseURL(process.env.COMMUNITY_BASE_URL); + const url = + baseURL !== undefined + ? decorate(request, `${baseURL}/organization/${slug}`) + : null; + return { + organization: { + ...rest, + logo: publicLogo, + slug, + url, + }, + }; + } + ); + + const enhancedEvent = { + ...rest, + background: publicBackground, + slug, + responsibleOrganizations: enhancedResponsibleOrganizations, + }; const filteredEvent = await filterEventByVisibility(enhancedEvent); diff --git a/common/api/tsconfig.json b/common/api/tsconfig.json index 85bc366e4..7e28b504a 100644 --- a/common/api/tsconfig.json +++ b/common/api/tsconfig.json @@ -37,5 +37,9 @@ /* Additional Stuff */ "resolveJsonModule": true, "skipLibCheck": true - } + }, + "ts-node": { + "files": true + }, + "files": ["src/@types/matomo-tracker/index.d.ts"] }