fix(SW-190): added imageVaultAsset schema
This commit is contained in:
@@ -2,6 +2,8 @@ import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
import { imageVaultAssetSchema } from "../schemas/imageVault"
|
||||
|
||||
export const validateContentPageSchema = z.object({
|
||||
content_page: z.object({
|
||||
title: z.string(),
|
||||
@@ -9,7 +11,7 @@ export const validateContentPageSchema = z.object({
|
||||
heading: z.string(),
|
||||
preamble: z.string(),
|
||||
}),
|
||||
hero_image: z.any().nullable(),
|
||||
hero_image: imageVaultAssetSchema.nullable().optional(),
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
locale: z.nativeEnum(Lang),
|
||||
|
||||
@@ -45,7 +45,7 @@ export const contentPageQueryRouter = router({
|
||||
const contentPageData = validatedContentPage.data.content_page
|
||||
const contentPage: ContentPage = {
|
||||
...contentPageData,
|
||||
hero_image: makeImageVaultImage(contentPageData.hero_image),
|
||||
heroImage: makeImageVaultImage(contentPageData.hero_image),
|
||||
}
|
||||
|
||||
const tracking: TrackingSDKPageData = {
|
||||
|
||||
@@ -2,7 +2,9 @@ import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
import { ImageVaultAsset } from "@/types/components/imageVaultImage"
|
||||
import { imageVaultAssetSchema } from "../schemas/imageVault"
|
||||
|
||||
import { ImageVaultAsset } from "@/types/components/imageVault"
|
||||
import {
|
||||
JoinLoyaltyContactTypenameEnum,
|
||||
LoyaltyBlocksTypenameEnum,
|
||||
@@ -192,7 +194,7 @@ const loyaltyPageSidebarItem = z.discriminatedUnion("__typename", [
|
||||
export const validateLoyaltyPageSchema = z.object({
|
||||
heading: z.string().nullable(),
|
||||
preamble: z.string().nullable(),
|
||||
hero_image: z.any().nullable(),
|
||||
hero_image: imageVaultAssetSchema.nullable().optional(),
|
||||
blocks: z.array(loyaltyPageBlockItem).nullable(),
|
||||
sidebar: z.array(loyaltyPageSidebarItem).nullable(),
|
||||
system: z.object({
|
||||
@@ -264,7 +266,7 @@ export type LoyaltyPage = Omit<
|
||||
LoyaltyPageDataRaw,
|
||||
"blocks" | "sidebar" | "hero_image"
|
||||
> & {
|
||||
hero_image?: ImageVaultAsset
|
||||
heroImage?: ImageVaultAsset
|
||||
blocks: Block[]
|
||||
sidebar: Sidebar[]
|
||||
}
|
||||
|
||||
@@ -85,6 +85,18 @@ export const loyaltyPageQueryRouter = router({
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
const validatedLoyaltyPage = validateLoyaltyPageSchema.safeParse(
|
||||
response.data.loyalty_page
|
||||
)
|
||||
|
||||
if (!validatedLoyaltyPage.success) {
|
||||
console.error(
|
||||
`Failed to validate Loyaltypage Data - (lang: ${lang}, uid: ${uid})`
|
||||
)
|
||||
console.error(validatedLoyaltyPage.error)
|
||||
return null
|
||||
}
|
||||
|
||||
const blocks = response.data.loyalty_page.blocks
|
||||
? response.data.loyalty_page.blocks.map((block: any) => {
|
||||
switch (block.__typename) {
|
||||
@@ -178,26 +190,17 @@ export const loyaltyPageQueryRouter = router({
|
||||
})
|
||||
: null
|
||||
|
||||
const loyaltyPage = {
|
||||
console.log({ HERO_IMAGE: response.data.loyalty_page.hero_image })
|
||||
|
||||
const loyaltyPage: LoyaltyPage = {
|
||||
heading: response.data.loyalty_page.heading,
|
||||
preamble: response.data.loyalty_page.preamble,
|
||||
hero_image: makeImageVaultImage(response.data.loyalty_page.hero_image),
|
||||
heroImage: makeImageVaultImage(response.data.loyalty_page.hero_image),
|
||||
system: response.data.loyalty_page.system,
|
||||
blocks,
|
||||
sidebar,
|
||||
}
|
||||
|
||||
const validatedLoyaltyPage =
|
||||
validateLoyaltyPageSchema.safeParse(loyaltyPage)
|
||||
|
||||
if (!validatedLoyaltyPage.success) {
|
||||
console.error(
|
||||
`Failed to validate Loyaltypage Data - (lang: ${lang}, uid: ${uid})`
|
||||
)
|
||||
console.error(validatedLoyaltyPage.error)
|
||||
return null
|
||||
}
|
||||
|
||||
const loyaltyTrackingData: TrackingSDKPageData = {
|
||||
pageId: response.data.loyalty_page.system.uid,
|
||||
lang: response.data.loyalty_page.system.locale as Lang,
|
||||
@@ -209,7 +212,7 @@ export const loyaltyPageQueryRouter = router({
|
||||
|
||||
// Assert LoyaltyPage type to get correct typings for RTE fields
|
||||
return {
|
||||
loyaltyPage: validatedLoyaltyPage.data as LoyaltyPage,
|
||||
loyaltyPage,
|
||||
tracking: loyaltyTrackingData,
|
||||
}
|
||||
}),
|
||||
|
||||
95
server/routers/contentstack/schemas/imageVault.ts
Normal file
95
server/routers/contentstack/schemas/imageVault.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { z } from "zod"
|
||||
|
||||
const metaData = z.object({
|
||||
DefinitionType: z.number().nullable().optional(),
|
||||
Description: z.string().nullable(),
|
||||
LanguageId: z.number().nullable(),
|
||||
MetadataDefinitionId: z.number(),
|
||||
Name: z.string(),
|
||||
Value: z.string().nullable(),
|
||||
})
|
||||
|
||||
/**
|
||||
* Defines a media asset, original or conversion
|
||||
*/
|
||||
const mediaConversion = z.object({
|
||||
/**
|
||||
* Aspect ratio of the conversion
|
||||
*/
|
||||
AspectRatio: z.number(),
|
||||
/**
|
||||
* Content type of the conversion
|
||||
*/
|
||||
ContentType: z.string(),
|
||||
/**
|
||||
* Aspect ratio of the selected/requested format
|
||||
*/
|
||||
FormatAspectRatio: z.number(),
|
||||
/**
|
||||
* Height of the selected/requested format
|
||||
*/
|
||||
FormatHeight: z.number(),
|
||||
/**
|
||||
* Width of the selected/requested format
|
||||
*/
|
||||
FormatWidth: z.number(),
|
||||
/**
|
||||
* Height, in pixels, of the conversion
|
||||
*/
|
||||
Height: z.number(),
|
||||
/**
|
||||
* Html representing the conversion
|
||||
*/
|
||||
Html: z.string(),
|
||||
/**
|
||||
* Id of the selected media format
|
||||
*/
|
||||
MediaFormatId: z.number(),
|
||||
/**
|
||||
* Name of the media format
|
||||
*/
|
||||
MediaFormatName: z.string(),
|
||||
/**
|
||||
* Name of the conversion
|
||||
*/
|
||||
Name: z.string(),
|
||||
/**
|
||||
* The url to the conversion
|
||||
*/
|
||||
Url: z.string(),
|
||||
/**
|
||||
* Width, in pixels, of the conversion
|
||||
*/
|
||||
Width: z.number(),
|
||||
})
|
||||
|
||||
/**
|
||||
* The response from ImageVault when inserting an asset
|
||||
*/
|
||||
export const imageVaultAssetSchema = z.object({
|
||||
/**
|
||||
* The media item id of the asset
|
||||
*/
|
||||
Id: z.number(),
|
||||
/**
|
||||
* The id of the vault where the asset resides
|
||||
*/
|
||||
VaultId: z.number(),
|
||||
/**
|
||||
* The name of the asset
|
||||
*/
|
||||
Name: z.string(),
|
||||
/**
|
||||
* The conversion selected by the user. Is an array but will only contain one object
|
||||
*/
|
||||
MediaConversions: z.array(mediaConversion),
|
||||
Metadata: z.array(metaData),
|
||||
/**
|
||||
* Date when the asset was added to ImageVault
|
||||
*/
|
||||
DateAdded: z.string(),
|
||||
/**
|
||||
* Name of the user that added the asset to ImageVault
|
||||
*/
|
||||
AddedBy: z.string(),
|
||||
})
|
||||
Reference in New Issue
Block a user