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