fix: add team model in prisma middleware (#3947)

* fix: add team model in prisma middleware

* refactor: move to getParsedResults function

* fix: add eventtype

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/3814/head
Udit Takkar 2022-08-27 00:29:58 +05:30 committed by GitHub
parent 974ff03127
commit 2d3a6fb4ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import type { PrismaClient } from "@prisma/client";
import type { PrismaClient, EventType } from "@prisma/client";
import MarkdownIt from "markdown-it";
const md = new MarkdownIt("zero").enable([
@ -14,22 +14,32 @@ function parseAndSanitize(description: string) {
return parsedMarkdown;
}
function getParsedResults(eventTypes: EventType | EventType[]) {
const results = Array.isArray(eventTypes) ? eventTypes : [eventTypes];
const parsedResults = results.map((record) => ({
...record,
descriptionAsSafeHTML: record.description ? parseAndSanitize(record.description) : null,
}));
/* If the original result was an array, return the parsed array, otherwise is a single record */
return Array.isArray(eventTypes) ? parsedResults : parsedResults[0];
}
/**
* Parses event type descriptions and treat them as markdown,
* then sanitizes the resulting HTML and adds a new `descriptionAsSafeHTML` property.
*/
function eventTypeDescriptionParseAndSanitize(prisma: PrismaClient) {
prisma.$use(async (params, next) => {
if (params.model === "EventType") {
if (params.model === "Team") {
const result = await next(params);
if (result?.eventTypes) {
result.eventTypes = getParsedResults(result.eventTypes);
}
return result;
} else if (params.model === "EventType") {
const result = await next(params);
if (result) {
const results = Array.isArray(result) ? result : [result];
const parsedResults = results.map((record) => ({
...record,
descriptionAsSafeHTML: record.description ? parseAndSanitize(record.description) : null,
}));
/* If the original result was an array, return the parsed array, otherwise is a single record */
return Array.isArray(result) ? parsedResults : parsedResults[0];
return getParsedResults(result);
}
return result;
}