Handle draft/published status in endpoints
This commit is contained in:
parent
1a9d4f2dd0
commit
64bc76febb
|
@ -1,4 +1,4 @@
|
||||||
import { CollectibleNature, CollectionStatus, Collections } from "../../../constants";
|
import { CollectibleNature, Collections } from "../../../constants";
|
||||||
import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint";
|
import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint";
|
||||||
import { EndpointCollectible, EndpointCollectiblePreview, PayloadImage } from "../../../sdk";
|
import { EndpointCollectible, EndpointCollectiblePreview, PayloadImage } from "../../../sdk";
|
||||||
import { Collectible } from "../../../types/collections";
|
import { Collectible } from "../../../types/collections";
|
||||||
|
@ -6,15 +6,17 @@ import {
|
||||||
isDefined,
|
isDefined,
|
||||||
isPayloadArrayType,
|
isPayloadArrayType,
|
||||||
isPayloadType,
|
isPayloadType,
|
||||||
|
isPublished,
|
||||||
isValidPayloadImage,
|
isValidPayloadImage,
|
||||||
} from "../../../utils/asserts";
|
} from "../../../utils/asserts";
|
||||||
import { convertTagsToGroups, getDomainFromUrl, handleParentPages } from "../../../utils/endpoints";
|
import { convertTagsToGroups, getDomainFromUrl, handleParentPages } from "../../../utils/endpoints";
|
||||||
import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint";
|
import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint";
|
||||||
|
|
||||||
export const getBySlugEndpoint = createGetByEndpoint(
|
export const getBySlugEndpoint = createGetByEndpoint({
|
||||||
Collections.Collectibles,
|
collection: Collections.Collectibles,
|
||||||
"slug",
|
attribute: "slug",
|
||||||
(collectible: Collectible): EndpointCollectible => {
|
depth: 3,
|
||||||
|
handler: (collectible: Collectible): EndpointCollectible => {
|
||||||
const {
|
const {
|
||||||
nature,
|
nature,
|
||||||
urls,
|
urls,
|
||||||
|
@ -42,7 +44,9 @@ export const getBySlugEndpoint = createGetByEndpoint(
|
||||||
scans: handleScans(collectible.scans),
|
scans: handleScans(collectible.scans),
|
||||||
nature: nature === "Physical" ? CollectibleNature.Physical : CollectibleNature.Digital,
|
nature: nature === "Physical" ? CollectibleNature.Physical : CollectibleNature.Digital,
|
||||||
parentPages: handleParentPages({ collectibles: parentItems, folders }),
|
parentPages: handleParentPages({ collectibles: parentItems, folders }),
|
||||||
subitems: isPayloadArrayType(subitems) ? subitems.map(convertCollectibleToPreview) : [],
|
subitems: isPayloadArrayType(subitems)
|
||||||
|
? subitems.filter(isPublished).map(convertCollectibleToPreview)
|
||||||
|
: [],
|
||||||
urls: urls?.map(({ url }) => ({ url, label: getDomainFromUrl(url) })) ?? [],
|
urls: urls?.map(({ url }) => ({ url, label: getDomainFromUrl(url) })) ?? [],
|
||||||
...(weightEnabled && weight ? { weight: weight.amount } : {}),
|
...(weightEnabled && weight ? { weight: weight.amount } : {}),
|
||||||
...handleSize(size, sizeEnabled),
|
...handleSize(size, sizeEnabled),
|
||||||
|
@ -50,8 +54,7 @@ export const getBySlugEndpoint = createGetByEndpoint(
|
||||||
...handlePrice(price, priceEnabled),
|
...handlePrice(price, priceEnabled),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
3
|
});
|
||||||
);
|
|
||||||
|
|
||||||
const handlePrice = (
|
const handlePrice = (
|
||||||
price: Collectible["price"],
|
price: Collectible["price"],
|
||||||
|
@ -183,7 +186,7 @@ const handleContents = (contents: Collectible["contents"]): EndpointCollectible[
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
case "pages":
|
case "pages":
|
||||||
return isPayloadType(content.value)
|
return isPayloadType(content.value) && isPublished(content.value)
|
||||||
? { relationTo: "pages", value: convertPageToPreview(content.value) }
|
? { relationTo: "pages", value: convertPageToPreview(content.value) }
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
|
@ -202,7 +205,6 @@ const handleContents = (contents: Collectible["contents"]): EndpointCollectible[
|
||||||
|
|
||||||
export const convertCollectibleToPreview = ({
|
export const convertCollectibleToPreview = ({
|
||||||
slug,
|
slug,
|
||||||
_status,
|
|
||||||
thumbnail,
|
thumbnail,
|
||||||
translations,
|
translations,
|
||||||
releaseDate,
|
releaseDate,
|
||||||
|
@ -213,7 +215,6 @@ export const convertCollectibleToPreview = ({
|
||||||
slug,
|
slug,
|
||||||
languages:
|
languages:
|
||||||
languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [],
|
languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [],
|
||||||
status: _status === "draft" ? CollectionStatus.Draft : CollectionStatus.Published,
|
|
||||||
...(releaseDate ? { releaseDate } : {}),
|
...(releaseDate ? { releaseDate } : {}),
|
||||||
...(isValidPayloadImage(thumbnail) ? { thumbnail } : {}),
|
...(isValidPayloadImage(thumbnail) ? { thumbnail } : {}),
|
||||||
tagGroups: convertTagsToGroups(tags),
|
tagGroups: convertTagsToGroups(tags),
|
||||||
|
|
|
@ -2,15 +2,16 @@ import { Collections } from "../../../constants";
|
||||||
import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint";
|
import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint";
|
||||||
import { EndpointFolder, EndpointFolderPreview } from "../../../sdk";
|
import { EndpointFolder, EndpointFolderPreview } from "../../../sdk";
|
||||||
import { Folder, Language } from "../../../types/collections";
|
import { Folder, Language } from "../../../types/collections";
|
||||||
import { isDefined, isPayloadType } from "../../../utils/asserts";
|
import { isDefined, isPayloadType, isPublished } from "../../../utils/asserts";
|
||||||
import { handleParentPages } from "../../../utils/endpoints";
|
import { handleParentPages } from "../../../utils/endpoints";
|
||||||
import { convertCollectibleToPreview } from "../../Collectibles/endpoints/getBySlugEndpoint";
|
import { convertCollectibleToPreview } from "../../Collectibles/endpoints/getBySlugEndpoint";
|
||||||
import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint";
|
import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint";
|
||||||
|
|
||||||
export const getBySlugEndpoint = createGetByEndpoint(
|
export const getBySlugEndpoint = createGetByEndpoint({
|
||||||
Collections.Folders,
|
collection: Collections.Folders,
|
||||||
"slug",
|
attribute: "slug",
|
||||||
(folder: Folder): EndpointFolder => {
|
depth: 3,
|
||||||
|
handler: (folder: Folder): EndpointFolder => {
|
||||||
const { sections, files, parentFolders } = folder;
|
const { sections, files, parentFolders } = folder;
|
||||||
return {
|
return {
|
||||||
...convertFolderToPreview(folder),
|
...convertFolderToPreview(folder),
|
||||||
|
@ -34,9 +35,10 @@ export const getBySlugEndpoint = createGetByEndpoint(
|
||||||
},
|
},
|
||||||
files:
|
files:
|
||||||
files?.flatMap<EndpointFolder["files"][number]>(({ relationTo, value }) => {
|
files?.flatMap<EndpointFolder["files"][number]>(({ relationTo, value }) => {
|
||||||
if (!isPayloadType(value)) {
|
if (!isPayloadType(value) || !isPublished(value)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (relationTo) {
|
switch (relationTo) {
|
||||||
case "collectibles":
|
case "collectibles":
|
||||||
return [{ relationTo, value: convertCollectibleToPreview(value) }];
|
return [{ relationTo, value: convertCollectibleToPreview(value) }];
|
||||||
|
@ -47,8 +49,7 @@ export const getBySlugEndpoint = createGetByEndpoint(
|
||||||
parentPages: handleParentPages({ folders: parentFolders }),
|
parentPages: handleParentPages({ folders: parentFolders }),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
3
|
});
|
||||||
);
|
|
||||||
|
|
||||||
export const convertFolderToPreview = ({
|
export const convertFolderToPreview = ({
|
||||||
slug,
|
slug,
|
||||||
|
|
|
@ -15,10 +15,10 @@ import { Page } from "../../../types/collections";
|
||||||
import { isPayloadArrayType, isPayloadType, isValidPayloadImage } from "../../../utils/asserts";
|
import { isPayloadArrayType, isPayloadType, isValidPayloadImage } from "../../../utils/asserts";
|
||||||
import { convertTagsToGroups, handleParentPages, handleRecorder } from "../../../utils/endpoints";
|
import { convertTagsToGroups, handleParentPages, handleRecorder } from "../../../utils/endpoints";
|
||||||
|
|
||||||
export const getBySlugEndpoint = createGetByEndpoint(
|
export const getBySlugEndpoint = createGetByEndpoint({
|
||||||
Collections.Pages,
|
collection: Collections.Pages,
|
||||||
"slug",
|
attribute: "slug",
|
||||||
(page: Page): EndpointPage => {
|
handler: (page: Page): EndpointPage => {
|
||||||
const { translations, collectibles, folders, backgroundImage } = page;
|
const { translations, collectibles, folders, backgroundImage } = page;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -52,8 +52,8 @@ export const getBySlugEndpoint = createGetByEndpoint(
|
||||||
),
|
),
|
||||||
parentPages: handleParentPages({ collectibles, folders }),
|
parentPages: handleParentPages({ collectibles, folders }),
|
||||||
};
|
};
|
||||||
}
|
},
|
||||||
);
|
});
|
||||||
|
|
||||||
const handleContent = (
|
const handleContent = (
|
||||||
{ root: { children, ...others } }: RichTextContent,
|
{ root: { children, ...others } }: RichTextContent,
|
||||||
|
@ -152,7 +152,6 @@ export const convertPageToPreview = ({
|
||||||
translations,
|
translations,
|
||||||
tags,
|
tags,
|
||||||
thumbnail,
|
thumbnail,
|
||||||
_status,
|
|
||||||
type,
|
type,
|
||||||
}: Page): EndpointPagePreview => ({
|
}: Page): EndpointPagePreview => ({
|
||||||
slug,
|
slug,
|
||||||
|
@ -166,5 +165,4 @@ export const convertPageToPreview = ({
|
||||||
...(subtitle ? { subtitle } : {}),
|
...(subtitle ? { subtitle } : {}),
|
||||||
})),
|
})),
|
||||||
authors: isPayloadArrayType(authors) ? authors.map(handleRecorder) : [],
|
authors: isPayloadArrayType(authors) ? authors.map(handleRecorder) : [],
|
||||||
status: _status === "published" ? "published" : "draft",
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,12 +1,20 @@
|
||||||
import payload, { GeneratedTypes } from "payload";
|
import payload, { GeneratedTypes } from "payload";
|
||||||
import { CollectionEndpoint } from "../types/payload";
|
import { CollectionEndpoint } from "../types/payload";
|
||||||
|
import { isPublished } from "../utils/asserts";
|
||||||
|
|
||||||
export const createGetByEndpoint = <C extends keyof GeneratedTypes["collections"], R>(
|
interface Params<C extends keyof GeneratedTypes["collections"], R> {
|
||||||
collection: C,
|
collection: C;
|
||||||
attribute: string,
|
attribute: string;
|
||||||
handler: (doc: GeneratedTypes["collections"][C]) => Promise<R> | R,
|
handler: (doc: GeneratedTypes["collections"][C]) => Promise<R> | R;
|
||||||
depth?: number
|
depth?: number;
|
||||||
): CollectionEndpoint => ({
|
}
|
||||||
|
|
||||||
|
export const createGetByEndpoint = <C extends keyof GeneratedTypes["collections"], R>({
|
||||||
|
attribute,
|
||||||
|
collection,
|
||||||
|
handler,
|
||||||
|
depth,
|
||||||
|
}: Params<C, R>): CollectionEndpoint => ({
|
||||||
path: `/${attribute}/:${attribute}`,
|
path: `/${attribute}/:${attribute}`,
|
||||||
method: "get",
|
method: "get",
|
||||||
handler: async (req, res) => {
|
handler: async (req, res) => {
|
||||||
|
@ -26,10 +34,16 @@ export const createGetByEndpoint = <C extends keyof GeneratedTypes["collections"
|
||||||
where: { [attribute]: { equals: req.params[attribute] } },
|
where: { [attribute]: { equals: req.params[attribute] } },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.docs[0]) {
|
const doc = result.docs[0];
|
||||||
|
|
||||||
|
if (!doc) {
|
||||||
return res.sendStatus(404);
|
return res.sendStatus(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).send(await handler(result.docs[0]));
|
if ("_status" in doc && !isPublished(doc)) {
|
||||||
|
return res.sendStatus(404);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).send(await handler(doc));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -213,7 +213,6 @@ export type EndpointPagePreview = {
|
||||||
title: string;
|
title: string;
|
||||||
subtitle?: string;
|
subtitle?: string;
|
||||||
}[];
|
}[];
|
||||||
status: "draft" | "published";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type EndpointPage = EndpointPagePreview & {
|
export type EndpointPage = EndpointPagePreview & {
|
||||||
|
@ -247,7 +246,6 @@ export type EndpointCollectiblePreview = {
|
||||||
description?: RichTextContent;
|
description?: RichTextContent;
|
||||||
}[];
|
}[];
|
||||||
tagGroups: EndpointTagsGroup[];
|
tagGroups: EndpointTagsGroup[];
|
||||||
status: "draft" | "published";
|
|
||||||
releaseDate?: string;
|
releaseDate?: string;
|
||||||
languages: string[];
|
languages: string[];
|
||||||
};
|
};
|
||||||
|
|
|
@ -43,3 +43,7 @@ export const isPayloadType = <T extends Object>(value: string | T): value is T =
|
||||||
export const isPayloadArrayType = <T extends Object>(
|
export const isPayloadArrayType = <T extends Object>(
|
||||||
value: (string | T)[] | null | undefined
|
value: (string | T)[] | null | undefined
|
||||||
): value is T[] => isDefined(value) && value.every(isPayloadType<T>);
|
): value is T[] => isDefined(value) && value.every(isPayloadType<T>);
|
||||||
|
|
||||||
|
export const isPublished = <T extends { _status?: ("draft" | "published") | null }>(
|
||||||
|
object: T
|
||||||
|
): boolean => object._status === "published";
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Collections } from "../constants";
|
import { Collections } from "../constants";
|
||||||
import { EndpointRecorder, EndpointTag, EndpointTagsGroup, ParentPage } from "../sdk";
|
import { EndpointRecorder, EndpointTag, EndpointTagsGroup, ParentPage } from "../sdk";
|
||||||
import { Collectible, Folder, Recorder, Tag } from "../types/collections";
|
import { Collectible, Folder, Recorder, Tag } from "../types/collections";
|
||||||
import { isPayloadArrayType, isPayloadType, isValidPayloadImage } from "./asserts";
|
import { isPayloadArrayType, isPayloadType, isPublished, isValidPayloadImage } from "./asserts";
|
||||||
|
|
||||||
export const convertTagsToGroups = (
|
export const convertTagsToGroups = (
|
||||||
tags: (string | Tag)[] | null | undefined
|
tags: (string | Tag)[] | null | undefined
|
||||||
|
@ -53,7 +53,7 @@ export const handleParentPages = ({
|
||||||
const result: ParentPage[] = [];
|
const result: ParentPage[] = [];
|
||||||
|
|
||||||
if (collectibles && isPayloadArrayType(collectibles)) {
|
if (collectibles && isPayloadArrayType(collectibles)) {
|
||||||
collectibles.forEach(({ slug, translations }) => {
|
collectibles.filter(isPublished).forEach(({ slug, translations }) => {
|
||||||
result.push({
|
result.push({
|
||||||
collection: Collections.Collectibles,
|
collection: Collections.Collectibles,
|
||||||
slug,
|
slug,
|
||||||
|
|
Loading…
Reference in New Issue