From 18781e701c0717c6c6edc1ab811fa6823dceea83 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Thu, 16 May 2024 14:13:45 +0200 Subject: [PATCH] Adapted Recorders' collection --- .../endpoints/getBySlugEndpoint.ts | 2 +- .../Pages/endpoints/getBySlugEndpoint.ts | 2 +- src/collections/Recorders/Recorders.ts | 24 ++++++-- .../Recorders/endpoints/getAllEndpoint.ts | 41 ------------- .../Recorders/endpoints/getByID.ts | 58 +++++++++++++++++++ .../Recorders/endpoints/getByUsername.ts | 25 -------- src/sdk.ts | 8 ++- src/types/collections.ts | 21 +++++++ src/utils/endpoints.ts | 2 +- 9 files changed, 106 insertions(+), 77 deletions(-) delete mode 100644 src/collections/Recorders/endpoints/getAllEndpoint.ts create mode 100644 src/collections/Recorders/endpoints/getByID.ts delete mode 100644 src/collections/Recorders/endpoints/getByUsername.ts diff --git a/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts b/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts index 4a42bde..4069d2c 100644 --- a/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts @@ -19,7 +19,7 @@ import { import { convertAudioToEndpointAudio } from "../../Audios/endpoints/getByID"; import { convertImageToEndpointImage } from "../../Images/endpoints/getByID"; import { convertPageToEndpointPage } from "../../Pages/endpoints/getBySlugEndpoint"; -import { convertRecorderToEndpointRecorder } from "../../Recorders/endpoints/getByUsername"; +import { convertRecorderToEndpointRecorder } from "../../Recorders/endpoints/getByID"; import { convertVideoToEndpointVideo } from "../../Videos/endpoints/getByID"; export const getBySlugEndpoint = createGetByEndpoint({ diff --git a/src/collections/Pages/endpoints/getBySlugEndpoint.ts b/src/collections/Pages/endpoints/getBySlugEndpoint.ts index 216ed4b..42b9cc1 100644 --- a/src/collections/Pages/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Pages/endpoints/getBySlugEndpoint.ts @@ -17,7 +17,7 @@ import { convertSourceToEndpointSource, } from "../../../utils/endpoints"; import { convertImageToEndpointImage } from "../../Images/endpoints/getByID"; -import { convertRecorderToEndpointRecorder } from "../../Recorders/endpoints/getByUsername"; +import { convertRecorderToEndpointRecorder } from "../../Recorders/endpoints/getByID"; export const getBySlugEndpoint = createGetByEndpoint({ collection: Collections.Pages, diff --git a/src/collections/Recorders/Recorders.ts b/src/collections/Recorders/Recorders.ts index 08010df..4649f5f 100644 --- a/src/collections/Recorders/Recorders.ts +++ b/src/collections/Recorders/Recorders.ts @@ -5,9 +5,10 @@ import { QuickFilters } from "../../components/QuickFilters"; import { CollectionGroups, Collections, RecordersRoles } from "../../constants"; import { imageField } from "../../fields/imageField/imageField"; import { rowField } from "../../fields/rowField/rowField"; +import { translatedFields } from "../../fields/translatedFields/translatedFields"; import { buildCollectionConfig } from "../../utils/collectionConfig"; -import { getAllEndpoint } from "./endpoints/getAllEndpoint"; -import { getByUsernameEndpoint } from "./endpoints/getByUsername"; +import { createEditor } from "../../utils/editor"; +import { getByID } from "./endpoints/getByID"; import { importFromStrapi } from "./endpoints/importFromStrapi"; import { beforeLoginMustHaveAtLeastOneRole } from "./hooks/beforeLoginMustHaveAtLeastOneRole"; @@ -15,8 +16,8 @@ const fields = { username: "username", anonymize: "anonymize", languages: "languages", - biographies: "biographies", - biography: "biography", + translations: "translations", + translationsBiography: "biography", avatar: "avatar", role: "role", } as const satisfies Record; @@ -37,7 +38,7 @@ export const Recorders = buildCollectionConfig({ fields.avatar, fields.username, fields.anonymize, - fields.biographies, + fields.translations, fields.languages, fields.role, ], @@ -79,7 +80,7 @@ export const Recorders = buildCollectionConfig({ hooks: { beforeLogin: [beforeLoginMustHaveAtLeastOneRole], }, - endpoints: [importFromStrapi, getAllEndpoint, getByUsernameEndpoint], + endpoints: [importFromStrapi, getByID], timestamps: false, fields: [ rowField([ @@ -105,6 +106,17 @@ export const Recorders = buildCollectionConfig({ description: "List of language(s) that this recorder is familiar with", }, }, + translatedFields({ + name: fields.translations, + fields: [ + { + name: fields.translationsBiography, + type: "richText", + editor: createEditor({ inlines: true, lists: true, links: true }), + required: true, + }, + ], + }), { name: fields.role, type: "select", diff --git a/src/collections/Recorders/endpoints/getAllEndpoint.ts b/src/collections/Recorders/endpoints/getAllEndpoint.ts deleted file mode 100644 index 8dc7b57..0000000 --- a/src/collections/Recorders/endpoints/getAllEndpoint.ts +++ /dev/null @@ -1,41 +0,0 @@ -import payload from "payload"; -import { Collections } from "../../../constants"; -import { EndpointRecorder } from "../../../sdk"; -import { CollectionEndpoint } from "../../../types/payload"; -import { isPayloadArrayType, isValidPayloadImage } from "../../../utils/asserts"; -import { convertImageToEndpointImage } from "../../Images/endpoints/getByID"; - -export const getAllEndpoint: CollectionEndpoint = { - method: "get", - path: "/all", - handler: async (req, res) => { - if (!req.user) { - return res.status(403).send({ - errors: [ - { - message: "You are not allowed to perform this action.", - }, - ], - }); - } - - const recorders = ( - await payload.find({ - collection: Collections.Recorders, - sort: "id", - pagination: false, - }) - ).docs; - - const result: EndpointRecorder[] = recorders.map( - ({ anonymize, id, username, avatar, languages }) => ({ - id, - username: anonymize ? `Recorder#${id.substring(0, 5)}` : username, - ...(isValidPayloadImage(avatar) ? { avatar: convertImageToEndpointImage(avatar) } : {}), - languages: isPayloadArrayType(languages) ? languages.map(({ id }) => id) : [], - }) - ); - - res.status(200).json(result); - }, -}; diff --git a/src/collections/Recorders/endpoints/getByID.ts b/src/collections/Recorders/endpoints/getByID.ts new file mode 100644 index 0000000..dff785c --- /dev/null +++ b/src/collections/Recorders/endpoints/getByID.ts @@ -0,0 +1,58 @@ +import payload from "payload"; +import { Collections } from "../../../constants"; +import { EndpointRecorder } from "../../../sdk"; +import { Recorder } from "../../../types/collections"; +import { CollectionEndpoint } from "../../../types/payload"; +import { isPayloadType, isValidPayloadImage } from "../../../utils/asserts"; +import { convertRTCToEndpointRTC } from "../../../utils/endpoints"; +import { convertImageToEndpointImage } from "../../Images/endpoints/getByID"; + +export const getByID: CollectionEndpoint = { + method: "get", + path: "/id/:id", + handler: async (req, res) => { + if (!req.user) { + return res.status(403).send({ + errors: [ + { + message: "You are not allowed to perform this action.", + }, + ], + }); + } + + if (!req.params.id) { + return res.status(400).send({ errors: [{ message: "Missing 'id' query params" }] }); + } + + try { + const result = await payload.findByID({ + collection: Collections.Recorders, + id: req.params.id, + }); + + return res.status(200).json(convertRecorderToEndpointRecorder(result)); + } catch { + return res.sendStatus(404); + } + }, +}; + +export const convertRecorderToEndpointRecorder = ({ + id, + languages, + username, + avatar, + anonymize, + translations, +}: Recorder): EndpointRecorder => ({ + id, + languages: languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [], + username: anonymize ? `Recorder#${id.substring(0, 5)}` : username, + ...(isValidPayloadImage(avatar) ? { avatar: convertImageToEndpointImage(avatar) } : {}), + translations: + translations?.map(({ language, biography }) => ({ + language: isPayloadType(language) ? language.id : language, + biography: convertRTCToEndpointRTC(biography), + })) ?? [], +}); diff --git a/src/collections/Recorders/endpoints/getByUsername.ts b/src/collections/Recorders/endpoints/getByUsername.ts deleted file mode 100644 index 5a3e586..0000000 --- a/src/collections/Recorders/endpoints/getByUsername.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Collections } from "../../../constants"; -import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint"; -import { EndpointRecorder } from "../../../sdk"; -import { Recorder } from "../../../types/collections"; -import { isPayloadType, isValidPayloadImage } from "../../../utils/asserts"; -import { convertImageToEndpointImage } from "../../Images/endpoints/getByID"; - -export const getByUsernameEndpoint = createGetByEndpoint({ - collection: Collections.Recorders, - attribute: "username", - handler: (recorder) => convertRecorderToEndpointRecorder(recorder), -}); - -export const convertRecorderToEndpointRecorder = ({ - id, - languages, - username, - avatar, - anonymize, -}: Recorder): EndpointRecorder => ({ - id, - languages: languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [], - username: anonymize ? `Recorder#${id.substring(0, 5)}` : username, - ...(isValidPayloadImage(avatar) ? { avatar: convertImageToEndpointImage(avatar) } : {}), -}); diff --git a/src/sdk.ts b/src/sdk.ts index 2a6b9a2..825662c 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -161,6 +161,10 @@ export type EndpointRecorder = { id: string; username: string; avatar?: EndpointImage; + translations: { + language: string; + biography: RichTextContent; + }[]; languages: string[]; }; @@ -544,8 +548,6 @@ export const payload = { await (await request(payloadApiUrl(Collections.Currencies, `all`))).json(), getWordings: async (): Promise => await (await request(payloadApiUrl(Collections.Wordings, `all`))).json(), - getRecorders: async (): Promise => - await (await request(payloadApiUrl(Collections.Recorders, `all`))).json(), getPage: async (slug: string): Promise => await (await request(payloadApiUrl(Collections.Pages, `slug/${slug}`))).json(), getCollectible: async (slug: string): Promise => @@ -578,4 +580,6 @@ export const payload = { await (await request(payloadApiUrl(Collections.Audios, `id/${id}`))).json(), getVideoByID: async (id: string): Promise => await (await request(payloadApiUrl(Collections.Videos, `id/${id}`))).json(), + getRecorderByID: async (id: string): Promise => + await (await request(payloadApiUrl(Collections.Recorders, `id/${id}`))).json(), }; diff --git a/src/types/collections.ts b/src/types/collections.ts index df37e48..61a0d75 100644 --- a/src/types/collections.ts +++ b/src/types/collections.ts @@ -262,6 +262,27 @@ export interface Recorder { username: string; avatar?: string | Image | null; languages?: (string | Language)[] | null; + translations?: + | { + language: string | Language; + biography: { + root: { + type: string; + children: { + type: string; + version: number; + [k: string]: unknown; + }[]; + direction: ("ltr" | "rtl") | null; + format: "left" | "start" | "center" | "right" | "end" | "justify" | ""; + indent: number; + version: number; + }; + [k: string]: unknown; + }; + id?: string | null; + }[] + | null; role?: ("Admin" | "Recorder" | "Api")[] | null; anonymize: boolean; email: string; diff --git a/src/utils/endpoints.ts b/src/utils/endpoints.ts index 8b6aeaa..d7825ef 100644 --- a/src/utils/endpoints.ts +++ b/src/utils/endpoints.ts @@ -3,7 +3,7 @@ import { convertCollectibleToEndpointCollectible } from "../collections/Collecti import { convertFolderToEndpointFolder } from "../collections/Folders/endpoints/getBySlugEndpoint"; import { convertImageToEndpointImage } from "../collections/Images/endpoints/getByID"; import { convertPageToEndpointPage } from "../collections/Pages/endpoints/getBySlugEndpoint"; -import { convertRecorderToEndpointRecorder } from "../collections/Recorders/endpoints/getByUsername"; +import { convertRecorderToEndpointRecorder } from "../collections/Recorders/endpoints/getByID"; import { convertVideoToEndpointVideo } from "../collections/Videos/endpoints/getByID"; import { AttributeTypes,