Skip to content

Commit

Permalink
feat: extend event data on api next (#1257)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
phollome committed Feb 8, 2024
1 parent 5171cd5 commit 8424003
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
21 changes: 21 additions & 0 deletions common/api/src/events/events-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -176,6 +192,11 @@ export class EventsController extends Controller {
targetGroups: [],
experienceLevel: null,
stage: null,
_count: {
participants: 0,
waitingList: 0,
},
responsibleOrganizations: [],
},
],
skip: 0,
Expand Down
58 changes: 56 additions & 2 deletions common/api/src/events/events-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof getEvents>>;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion common/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
/* Additional Stuff */
"resolveJsonModule": true,
"skipLibCheck": true
}
},
"ts-node": {
"files": true
},
"files": ["src/@types/matomo-tracker/index.d.ts"]
}

0 comments on commit 8424003

Please sign in to comment.