Adapted Recorders' collection

This commit is contained in:
DrMint 2024-05-16 14:13:45 +02:00
parent 46613c8500
commit 18781e701c
9 changed files with 106 additions and 77 deletions

View File

@ -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({

View File

@ -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,

View File

@ -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<string, string>;
@ -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",

View File

@ -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);
},
};

View File

@ -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),
})) ?? [],
});

View File

@ -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) } : {}),
});

View File

@ -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<EndpointWording[]> =>
await (await request(payloadApiUrl(Collections.Wordings, `all`))).json(),
getRecorders: async (): Promise<EndpointRecorder[]> =>
await (await request(payloadApiUrl(Collections.Recorders, `all`))).json(),
getPage: async (slug: string): Promise<EndpointPage> =>
await (await request(payloadApiUrl(Collections.Pages, `slug/${slug}`))).json(),
getCollectible: async (slug: string): Promise<EndpointCollectible> =>
@ -578,4 +580,6 @@ export const payload = {
await (await request(payloadApiUrl(Collections.Audios, `id/${id}`))).json(),
getVideoByID: async (id: string): Promise<EndpointVideo> =>
await (await request(payloadApiUrl(Collections.Videos, `id/${id}`))).json(),
getRecorderByID: async (id: string): Promise<EndpointRecorder> =>
await (await request(payloadApiUrl(Collections.Recorders, `id/${id}`))).json(),
};

View File

@ -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;

View File

@ -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,