Merged in feat/sw-2863-move-contentstack-router-to-trpc-package (pull request #2389)
feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
import { mergeRouters } from "../../.."
|
||||
import { loyaltyLevelQueryRouter } from "./query"
|
||||
|
||||
export const loyaltyLevelRouter = mergeRouters(loyaltyLevelQueryRouter)
|
||||
@@ -0,0 +1,9 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@scandic-hotels/common/constants/language"
|
||||
import { MembershipLevelEnum } from "@scandic-hotels/common/constants/membershipLevels"
|
||||
|
||||
export const loyaltyLevelInput = z.object({
|
||||
level: z.nativeEnum(MembershipLevelEnum),
|
||||
lang: z.nativeEnum(Lang).optional(),
|
||||
})
|
||||
@@ -0,0 +1,28 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { MembershipLevelEnum } from "@scandic-hotels/common/constants/membershipLevels"
|
||||
|
||||
import type { CMSReward } from "../../../types/reward"
|
||||
|
||||
export const validateLoyaltyLevelsSchema = z
|
||||
.object({
|
||||
all_loyalty_level: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
level_id: z.nativeEnum(MembershipLevelEnum),
|
||||
name: z.string(),
|
||||
user_facing_tag: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
required_nights: z.number().optional().nullable(),
|
||||
required_points: z.number(),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
.transform((data) => data.all_loyalty_level.items)
|
||||
|
||||
export type LoyaltyLevelsResponse = z.input<typeof validateLoyaltyLevelsSchema>
|
||||
|
||||
export type LoyaltyLevel = z.output<typeof validateLoyaltyLevelsSchema>[number]
|
||||
|
||||
export type LevelWithRewards = LoyaltyLevel & { rewards: CMSReward[] }
|
||||
@@ -0,0 +1,124 @@
|
||||
import { cache } from "react"
|
||||
|
||||
import {
|
||||
type MembershipLevel,
|
||||
MembershipLevelEnum,
|
||||
} from "@scandic-hotels/common/constants/membershipLevels"
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { router } from "../../.."
|
||||
import { notFound } from "../../../errors"
|
||||
import {
|
||||
GetAllLoyaltyLevels,
|
||||
GetLoyaltyLevel,
|
||||
} from "../../../graphql/Query/LoyaltyLevels.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { contentstackBaseProcedure } from "../../../procedures"
|
||||
import { generateLoyaltyConfigTag } from "../../../utils/generateTag"
|
||||
import { loyaltyLevelInput } from "./input"
|
||||
import {
|
||||
type LoyaltyLevel,
|
||||
type LoyaltyLevelsResponse,
|
||||
validateLoyaltyLevelsSchema,
|
||||
} from "./output"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
export const getAllLoyaltyLevels = cache(async (lang: Lang) => {
|
||||
const getLoyaltyLevelAllCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"loyaltyLevel.all"
|
||||
)
|
||||
const metricsGetLoyaltyLevelAll = getLoyaltyLevelAllCounter.init()
|
||||
|
||||
metricsGetLoyaltyLevelAll.start()
|
||||
|
||||
// Ideally we should fetch all available tiers from API, but since they
|
||||
// are static, we can just use the enum values. We want to know which
|
||||
// levels we are fetching so that we can use tags to cache them
|
||||
const allLevelIds = Object.values(MembershipLevelEnum)
|
||||
|
||||
const tags = allLevelIds.map((levelId) =>
|
||||
generateLoyaltyConfigTag(lang, "loyalty_level", levelId)
|
||||
)
|
||||
|
||||
const loyaltyLevelsConfigResponse = await request<LoyaltyLevelsResponse>(
|
||||
GetAllLoyaltyLevels,
|
||||
{ lang, level_ids: allLevelIds },
|
||||
{ key: tags, ttl: "max" }
|
||||
)
|
||||
|
||||
if (!loyaltyLevelsConfigResponse.data) {
|
||||
const notFoundError = notFound(loyaltyLevelsConfigResponse)
|
||||
metricsGetLoyaltyLevelAll.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedLoyaltyLevels = validateLoyaltyLevelsSchema.safeParse(
|
||||
loyaltyLevelsConfigResponse.data
|
||||
)
|
||||
if (!validatedLoyaltyLevels.success) {
|
||||
metricsGetLoyaltyLevelAll.validationError(validatedLoyaltyLevels.error)
|
||||
return []
|
||||
}
|
||||
|
||||
metricsGetLoyaltyLevelAll.success()
|
||||
|
||||
return validatedLoyaltyLevels.data
|
||||
})
|
||||
|
||||
export const getLoyaltyLevel = cache(
|
||||
async (lang: Lang, level_id: MembershipLevel) => {
|
||||
const getLoyaltyLevelCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"loyaltyLevel.get"
|
||||
)
|
||||
const metricsGetLoyaltyLevel = getLoyaltyLevelCounter.init({
|
||||
lang,
|
||||
level_id,
|
||||
})
|
||||
|
||||
metricsGetLoyaltyLevel.start()
|
||||
|
||||
const loyaltyLevelsConfigResponse = await request<LoyaltyLevelsResponse>(
|
||||
GetLoyaltyLevel,
|
||||
{ lang, level_id },
|
||||
{
|
||||
key: generateLoyaltyConfigTag(lang, "loyalty_level", level_id),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
if (
|
||||
!loyaltyLevelsConfigResponse.data ||
|
||||
!loyaltyLevelsConfigResponse.data.all_loyalty_level.items.length
|
||||
) {
|
||||
const notFoundError = notFound(loyaltyLevelsConfigResponse)
|
||||
metricsGetLoyaltyLevel.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedLoyaltyLevels = validateLoyaltyLevelsSchema.safeParse(
|
||||
loyaltyLevelsConfigResponse.data
|
||||
)
|
||||
if (!validatedLoyaltyLevels.success) {
|
||||
metricsGetLoyaltyLevel.validationError(validatedLoyaltyLevels.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetLoyaltyLevel.success()
|
||||
|
||||
const result: LoyaltyLevel = validatedLoyaltyLevels.data[0]
|
||||
return result
|
||||
}
|
||||
)
|
||||
|
||||
export const loyaltyLevelQueryRouter = router({
|
||||
byLevel: contentstackBaseProcedure
|
||||
.input(loyaltyLevelInput)
|
||||
.query(async function ({ ctx, input }) {
|
||||
return getLoyaltyLevel(ctx.lang, input.level)
|
||||
}),
|
||||
all: contentstackBaseProcedure.query(async function ({ ctx }) {
|
||||
return getAllLoyaltyLevels(ctx.lang)
|
||||
}),
|
||||
})
|
||||
Reference in New Issue
Block a user