Added get all eras endpoint

This commit is contained in:
DrMint 2023-10-09 13:52:10 +02:00
parent 4b6b67c093
commit 26b4798761
7 changed files with 173 additions and 11 deletions

BIN
bun.lockb Executable file

Binary file not shown.

16
package-lock.json generated
View File

@ -9,12 +9,12 @@
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"@fontsource/vollkorn": "^5.0.12",
"@fontsource/vollkorn": "^5.0.14",
"clean-deep": "^3.4.0",
"cross-env": "^7.0.3",
"language-tags": "^1.0.9",
"luxon": "^3.4.3",
"payload": "^1.15.6",
"payload": "^1.15.7",
"styled-components": "^6.0.8"
},
"devDependencies": {
@ -3687,9 +3687,9 @@
}
},
"node_modules/@fontsource/vollkorn": {
"version": "5.0.12",
"resolved": "https://registry.npmjs.org/@fontsource/vollkorn/-/vollkorn-5.0.12.tgz",
"integrity": "sha512-KjWzn/iQfN/PpgAYSX6Hzxqfgpt79f87aFVG0QazSVuSn3Lia6e9nDI46+nev2dD2UySwL9yUSx175IMvBt7FA=="
"version": "5.0.14",
"resolved": "https://registry.npmjs.org/@fontsource/vollkorn/-/vollkorn-5.0.14.tgz",
"integrity": "sha512-+7081lPksAtO3DMnZZ0lp+134XwA2w7GUBqFVZsVE5YljGwxK2tVhY48r3I24vghsWUE+qd6vNRWVqvLMSTkEQ=="
},
"node_modules/@hapi/hoek": {
"version": "9.3.0",
@ -9862,9 +9862,9 @@
"integrity": "sha512-KG8UEiEVkR3wGEb4m5yZkVCzigAD+cVEJck2CzYZO37ZGJfctvVptVO192MwrtPhzONn6go8ylnOdMhKqi4nfg=="
},
"node_modules/payload": {
"version": "1.15.6",
"resolved": "https://registry.npmjs.org/payload/-/payload-1.15.6.tgz",
"integrity": "sha512-rK73rUbEcvnSr6q69vD/U5MiH0uXcEL9KIsTnSjX4hGaG3VLXyp/xJ9oq8FF+M15XG4LEGzNyHbqqG2R8yBLtQ==",
"version": "1.15.7",
"resolved": "https://registry.npmjs.org/payload/-/payload-1.15.7.tgz",
"integrity": "sha512-Lco+L0lacWEC+zS2qQ2XJexZH9PWMDlwZIiyzi3NRq9DWs8nAY9ZuA4g4DAayiBww6yvPR6M+mpi3N6/wmJfgQ==",
"dependencies": {
"@date-io/date-fns": "^2.16.0",
"@dnd-kit/core": "^6.0.7",

View File

@ -21,12 +21,12 @@
"start": "sudo docker compose up"
},
"dependencies": {
"@fontsource/vollkorn": "^5.0.12",
"@fontsource/vollkorn": "^5.0.14",
"clean-deep": "^3.4.0",
"cross-env": "^7.0.3",
"language-tags": "^1.0.9",
"luxon": "^3.4.3",
"payload": "^1.15.6",
"payload": "^1.15.7",
"styled-components": "^6.0.8"
},
"devDependencies": {

View File

@ -5,6 +5,7 @@ import { backPropagationField } from "../../fields/backPropagationField/backProp
import { slugField } from "../../fields/slugField/slugField";
import { translatedFields } from "../../fields/translatedFields/translatedFields";
import { buildCollectionConfig } from "../../utils/collectionConfig";
import { getAllEndpoint } from "./endpoints/getAllEndpoint";
import { importFromStrapi } from "./endpoints/importFromStrapi";
import { beforeValidateEndingGreaterThanStarting } from "./hooks/beforeValidateEndingGreaterThanStarting";
import { beforeValidateNoIntersection } from "./hooks/beforeValidateNoIntersection";
@ -38,7 +39,7 @@ export const ChronologyEras: CollectionConfig = buildCollectionConfig({
hooks: {
beforeValidate: [beforeValidateEndingGreaterThanStarting, beforeValidateNoIntersection],
},
endpoints: [importFromStrapi],
endpoints: [importFromStrapi, getAllEndpoint],
fields: [
slugField({ name: fields.slug }),
{

View File

@ -0,0 +1,117 @@
import payload from "payload";
import { Collections } from "../../../constants";
import { EndpointEra } from "../../../sdk";
import { ChronologyEra, ChronologyItem, Recorder } from "../../../types/collections";
import { CollectionEndpoint } from "../../../types/payload";
import {
isEmpty,
isPayloadArrayType,
isPayloadType,
isString,
isUndefined,
} from "../../../utils/asserts";
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 eras: ChronologyEra[] = (
await payload.find({
collection: Collections.ChronologyEras,
limit: 100,
})
).docs;
const result = eras.map<EndpointEra>(
({ endingYear, startingYear, slug, translations, events: items }) => ({
slug,
startingYear,
endingYear,
translations:
translations?.flatMap(({ language, title, description }) => {
if (isString(language)) return [];
const translation = {
language: language.id,
title,
description,
};
if (isEmpty(translation.description)) delete translation.description;
return translation;
}) ?? [],
items:
items
?.filter(isPayloadType<ChronologyItem>)
.sort((a, b) => {
const aYear = a.date.year;
const bYear = b.date.year;
if (aYear !== bYear) return aYear - bYear;
const aMonth = a.date.month ?? 0;
const bMonth = b.date.month ?? 0;
if (aMonth !== bMonth) return aMonth - bMonth;
const aDay = a.date.day ?? 0;
const bDay = b.date.day ?? 0;
if (aDay !== bDay) return aDay - bDay;
return 0;
})
.flatMap(({ date, events, createdAt, updatedAt, updatedBy }) => {
if (isString(updatedBy)) return [];
const item = {
date,
events: events.map(({ translations }) => ({
translations: translations.flatMap(
({
language,
sourceLanguage,
description,
notes,
proofreaders = [],
transcribers = [],
translators = [],
title,
}) => {
if (isString(language)) return [];
if (isString(sourceLanguage)) return [];
if (!isPayloadArrayType<Recorder>(proofreaders)) return [];
if (!isPayloadArrayType<Recorder>(transcribers)) return [];
if (!isPayloadArrayType<Recorder>(translators)) return [];
const event = {
language: language.id,
sourceLanguage: sourceLanguage.id,
title,
description,
notes,
proofreaders: proofreaders.map(({ id }) => id),
transcribers: transcribers.map(({ id }) => id),
translators: translators.map(({ id }) => id),
};
if (isEmpty(title)) delete event.title;
if (isEmpty(description)) delete event.description;
if (isEmpty(notes)) delete event.notes;
return event;
}
),
})),
createdAt: new Date(createdAt),
updatedAt: new Date(updatedAt),
updatedBy: updatedBy.id,
};
if (isUndefined(item.date.month)) delete item.date.month;
if (isUndefined(item.date.day)) delete item.date.day;
return item;
}) ?? [],
})
);
res.status(200).json(result);
},
};

View File

@ -125,6 +125,39 @@ export type EndpointBasicWeapon = {
};
};
export type EndpointEra = {
slug: string;
startingYear: number;
endingYear: number;
translations: {
language: string;
title: string;
description?: string;
}[];
items: {
date: {
year: number;
month?: number;
day?: number;
};
events: {
translations: {
language: string;
sourceLanguage: string;
title?: string;
description?: string;
notes?: string;
transcribers: string[];
translators: string[];
proofreaders: string[];
}[];
}[];
createdAt: Date;
updatedAt: Date;
updatedBy: string;
}[];
};
export type PayloadImage = {
url: string;
width: number;
@ -138,4 +171,6 @@ export const payload = {
await (await request(payloadApiUrl(Collections.Weapons, "slugs"))).json(),
getWeapon: async (slug: string): Promise<EndpointWeapon> =>
await (await request(payloadApiUrl(Collections.Weapons, `slug/${slug}`))).json(),
getEras: async (): Promise<EndpointEra[]> =>
await (await request(payloadApiUrl(Collections.ChronologyEras, `all`))).json(),
};

View File

@ -38,3 +38,12 @@ export const isValidPayloadImage = (
if (isUndefined(image.height)) return false;
return true;
};
export const isString = <T extends Object>(value: string | T): value is string =>
typeof value === "string";
export const isPayloadType = <T extends Object>(value: string | T): value is T =>
typeof value === "object";
export const isPayloadArrayType = <T extends Object>(value: string[] | T[]): value is T[] =>
value.every(isPayloadType<T>);