Added get all eras endpoint
This commit is contained in:
parent
4b6b67c093
commit
26b4798761
|
@ -9,12 +9,12 @@
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fontsource/vollkorn": "^5.0.12",
|
"@fontsource/vollkorn": "^5.0.14",
|
||||||
"clean-deep": "^3.4.0",
|
"clean-deep": "^3.4.0",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"language-tags": "^1.0.9",
|
"language-tags": "^1.0.9",
|
||||||
"luxon": "^3.4.3",
|
"luxon": "^3.4.3",
|
||||||
"payload": "^1.15.6",
|
"payload": "^1.15.7",
|
||||||
"styled-components": "^6.0.8"
|
"styled-components": "^6.0.8"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -3687,9 +3687,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@fontsource/vollkorn": {
|
"node_modules/@fontsource/vollkorn": {
|
||||||
"version": "5.0.12",
|
"version": "5.0.14",
|
||||||
"resolved": "https://registry.npmjs.org/@fontsource/vollkorn/-/vollkorn-5.0.12.tgz",
|
"resolved": "https://registry.npmjs.org/@fontsource/vollkorn/-/vollkorn-5.0.14.tgz",
|
||||||
"integrity": "sha512-KjWzn/iQfN/PpgAYSX6Hzxqfgpt79f87aFVG0QazSVuSn3Lia6e9nDI46+nev2dD2UySwL9yUSx175IMvBt7FA=="
|
"integrity": "sha512-+7081lPksAtO3DMnZZ0lp+134XwA2w7GUBqFVZsVE5YljGwxK2tVhY48r3I24vghsWUE+qd6vNRWVqvLMSTkEQ=="
|
||||||
},
|
},
|
||||||
"node_modules/@hapi/hoek": {
|
"node_modules/@hapi/hoek": {
|
||||||
"version": "9.3.0",
|
"version": "9.3.0",
|
||||||
|
@ -9862,9 +9862,9 @@
|
||||||
"integrity": "sha512-KG8UEiEVkR3wGEb4m5yZkVCzigAD+cVEJck2CzYZO37ZGJfctvVptVO192MwrtPhzONn6go8ylnOdMhKqi4nfg=="
|
"integrity": "sha512-KG8UEiEVkR3wGEb4m5yZkVCzigAD+cVEJck2CzYZO37ZGJfctvVptVO192MwrtPhzONn6go8ylnOdMhKqi4nfg=="
|
||||||
},
|
},
|
||||||
"node_modules/payload": {
|
"node_modules/payload": {
|
||||||
"version": "1.15.6",
|
"version": "1.15.7",
|
||||||
"resolved": "https://registry.npmjs.org/payload/-/payload-1.15.6.tgz",
|
"resolved": "https://registry.npmjs.org/payload/-/payload-1.15.7.tgz",
|
||||||
"integrity": "sha512-rK73rUbEcvnSr6q69vD/U5MiH0uXcEL9KIsTnSjX4hGaG3VLXyp/xJ9oq8FF+M15XG4LEGzNyHbqqG2R8yBLtQ==",
|
"integrity": "sha512-Lco+L0lacWEC+zS2qQ2XJexZH9PWMDlwZIiyzi3NRq9DWs8nAY9ZuA4g4DAayiBww6yvPR6M+mpi3N6/wmJfgQ==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@date-io/date-fns": "^2.16.0",
|
"@date-io/date-fns": "^2.16.0",
|
||||||
"@dnd-kit/core": "^6.0.7",
|
"@dnd-kit/core": "^6.0.7",
|
||||||
|
|
|
@ -21,12 +21,12 @@
|
||||||
"start": "sudo docker compose up"
|
"start": "sudo docker compose up"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fontsource/vollkorn": "^5.0.12",
|
"@fontsource/vollkorn": "^5.0.14",
|
||||||
"clean-deep": "^3.4.0",
|
"clean-deep": "^3.4.0",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"language-tags": "^1.0.9",
|
"language-tags": "^1.0.9",
|
||||||
"luxon": "^3.4.3",
|
"luxon": "^3.4.3",
|
||||||
"payload": "^1.15.6",
|
"payload": "^1.15.7",
|
||||||
"styled-components": "^6.0.8"
|
"styled-components": "^6.0.8"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { backPropagationField } from "../../fields/backPropagationField/backProp
|
||||||
import { slugField } from "../../fields/slugField/slugField";
|
import { slugField } from "../../fields/slugField/slugField";
|
||||||
import { translatedFields } from "../../fields/translatedFields/translatedFields";
|
import { translatedFields } from "../../fields/translatedFields/translatedFields";
|
||||||
import { buildCollectionConfig } from "../../utils/collectionConfig";
|
import { buildCollectionConfig } from "../../utils/collectionConfig";
|
||||||
|
import { getAllEndpoint } from "./endpoints/getAllEndpoint";
|
||||||
import { importFromStrapi } from "./endpoints/importFromStrapi";
|
import { importFromStrapi } from "./endpoints/importFromStrapi";
|
||||||
import { beforeValidateEndingGreaterThanStarting } from "./hooks/beforeValidateEndingGreaterThanStarting";
|
import { beforeValidateEndingGreaterThanStarting } from "./hooks/beforeValidateEndingGreaterThanStarting";
|
||||||
import { beforeValidateNoIntersection } from "./hooks/beforeValidateNoIntersection";
|
import { beforeValidateNoIntersection } from "./hooks/beforeValidateNoIntersection";
|
||||||
|
@ -38,7 +39,7 @@ export const ChronologyEras: CollectionConfig = buildCollectionConfig({
|
||||||
hooks: {
|
hooks: {
|
||||||
beforeValidate: [beforeValidateEndingGreaterThanStarting, beforeValidateNoIntersection],
|
beforeValidate: [beforeValidateEndingGreaterThanStarting, beforeValidateNoIntersection],
|
||||||
},
|
},
|
||||||
endpoints: [importFromStrapi],
|
endpoints: [importFromStrapi, getAllEndpoint],
|
||||||
fields: [
|
fields: [
|
||||||
slugField({ name: fields.slug }),
|
slugField({ name: fields.slug }),
|
||||||
{
|
{
|
||||||
|
|
|
@ -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);
|
||||||
|
},
|
||||||
|
};
|
35
src/sdk.ts
35
src/sdk.ts
|
@ -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 = {
|
export type PayloadImage = {
|
||||||
url: string;
|
url: string;
|
||||||
width: number;
|
width: number;
|
||||||
|
@ -138,4 +171,6 @@ export const payload = {
|
||||||
await (await request(payloadApiUrl(Collections.Weapons, "slugs"))).json(),
|
await (await request(payloadApiUrl(Collections.Weapons, "slugs"))).json(),
|
||||||
getWeapon: async (slug: string): Promise<EndpointWeapon> =>
|
getWeapon: async (slug: string): Promise<EndpointWeapon> =>
|
||||||
await (await request(payloadApiUrl(Collections.Weapons, `slug/${slug}`))).json(),
|
await (await request(payloadApiUrl(Collections.Weapons, `slug/${slug}`))).json(),
|
||||||
|
getEras: async (): Promise<EndpointEra[]> =>
|
||||||
|
await (await request(payloadApiUrl(Collections.ChronologyEras, `all`))).json(),
|
||||||
};
|
};
|
||||||
|
|
|
@ -38,3 +38,12 @@ export const isValidPayloadImage = (
|
||||||
if (isUndefined(image.height)) return false;
|
if (isUndefined(image.height)) return false;
|
||||||
return true;
|
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>);
|
||||||
|
|
Loading…
Reference in New Issue