Skip to content

Commit

Permalink
fix: undoing schema and type hijiinx
Browse files Browse the repository at this point in the history
  • Loading branch information
kimon-satan committed Dec 9, 2024
1 parent 46e3d4b commit 1f903c4
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const lessonListingFixture = (
worksheetCount: 1,
hasCopyrightMaterial: false,
orderInUnit: 1,
actions: null,
},
{
expired: false,
Expand All @@ -46,6 +47,7 @@ const lessonListingFixture = (
worksheetCount: 1,
hasCopyrightMaterial: false,
orderInUnit: 2,
actions: null,
},
{
expired: false,
Expand All @@ -59,6 +61,7 @@ const lessonListingFixture = (
worksheetCount: 1,
hasCopyrightMaterial: false,
orderInUnit: 3,
actions: null,
},
{
expired: false,
Expand All @@ -72,6 +75,7 @@ const lessonListingFixture = (
worksheetCount: 1,
hasCopyrightMaterial: false,
orderInUnit: 4,
actions: null,
},
{
expired: false,
Expand All @@ -85,6 +89,7 @@ const lessonListingFixture = (
worksheetCount: 1,
hasCopyrightMaterial: false,
orderInUnit: 5,
actions: null,
},
],
...partial,
Expand Down
14 changes: 9 additions & 5 deletions src/node-lib/curriculum-api-2023/helpers/zodToCamelCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import { z } from "zod";
import { convertKey } from "@/utils/snakeCaseConverter";

// Recursively transform Zod schema keys from snake_case to camelCase
export const zodToCamelCase = <T extends z.ZodTypeAny>(
export const zodToCamelCase = <T extends z.ZodObject<z.ZodRawShape>>(
schema: T,
): z.ZodTypeAny => {
if (!(schema._def.typeName === "ZodObject")) {
throw new Error("zodToCamelCase only works with ZodObject schemas");
}

const transformedShape: Record<string, z.ZodTypeAny> = {};

Object.keys(schema.shape).forEach((key) => {
const camelKey = convertKey(key);
const value = schema.shape[key];

// Recursively transform nested schemas
transformedShape[camelKey] =
value instanceof z.ZodObject ? zodToCamelCase(value) : value;
if (value !== undefined) {
// Recursively transform nested schemas
transformedShape[camelKey] =
value?._def.typeName === "ZodObject"
? zodToCamelCase(value as z.ZodObject<z.ZodRawShape>)
: value;
}
});

return z.object(transformedShape);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import lessonListing, {
getTransformedLessons,
getPackagedUnit,
} from "./lessonListing.query";
import lessonListingSchema from "./lessonListing.schema";
import { lessonListingPageDataSchema } from "./lessonListing.schema";

describe("lessonListing()", () => {
describe("lessonListing query", () => {
Expand Down Expand Up @@ -35,7 +35,7 @@ describe("lessonListing()", () => {
unitSlug: "unit-slug",
});

expect(lessonListingSchema.parse(res)).toEqual({
expect(lessonListingPageDataSchema.parse(res)).toEqual({
programmeSlug: "programme-slug",
keyStageSlug: "ks1",
keyStageTitle: "Key Stage 1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { ProgrammeFields } from "@oaknational/oak-curriculum-schema";

import lessonListingSchema, {
Actions,
import {
lessonListingPageDataSchema,
LessonListingPageData,
partialSyntheticUnitvariantLessonsArraySchema,
partialSyntheticUnitvariantLessonsSchema,
} from "./lessonListing.schema";

import { Sdk } from "@/node-lib/curriculum-api-2023/sdk";
import OakError from "@/errors/OakError";
import { LessonListSchema } from "@/node-lib/curriculum-api-2023/shared.schema";
import {
LessonListSchema,
Actions,
} from "@/node-lib/curriculum-api-2023/shared.schema";
import { LessonListingQuery } from "@/node-lib/curriculum-api-2023/generated/sdk";
import { applyGenericOverridesAndExceptions } from "@/node-lib/curriculum-api-2023/helpers/overridesAndExceptions";
import { getCorrectYear } from "@/node-lib/curriculum-api-2023/helpers/getCorrectYear";
Expand Down Expand Up @@ -136,7 +139,7 @@ const lessonListingQuery =
}, {} as PackagedUnitData);

const packagedUnit = getPackagedUnit(packagedUnitData, unitLessons);
return lessonListingSchema.parse(packagedUnit);
return lessonListingPageDataSchema.parse(packagedUnit);
};

export default lessonListingQuery;
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ import { z } from "zod";
import {
programmeFieldsSchema,
syntheticUnitvariantLessonsSchema,
actionsSchema,
} from "@oaknational/oak-curriculum-schema";

import { lessonListSchema } from "../../shared.schema";
import { zodToCamelCase } from "../../helpers/zodToCamelCase";

const camelActionSchema = zodToCamelCase(actionsSchema);

export type Actions = z.infer<typeof camelActionSchema>;
import {
lessonListItemSchema,
lessonListSchema,
} from "@/node-lib/curriculum-api-2023/shared.schema";

const lessonListingSchema = z.object({
export const lessonListingPageDataSchema = z.object({
programmeSlug: z.string(),
unitSlug: z.string(),
unitTitle: z.string(),
Expand All @@ -30,14 +27,10 @@ const lessonListingSchema = z.object({
pathwayTitle: programmeFieldsSchema.shape.pathway,
pathwayDisplayOrder: programmeFieldsSchema.shape.pathway_display_order,
lessons: lessonListSchema,
actions: camelActionSchema,
actions: lessonListItemSchema.shape.actions.nullable(),
});

export type lessonListingSchema = z.infer<typeof lessonListingSchema> & {
actions: Actions | null;
};

export type LessonListingPageData = z.infer<typeof lessonListingSchema>;
export type LessonListingPageData = z.infer<typeof lessonListingPageDataSchema>;

export const partialSyntheticUnitvariantLessonsSchema = z.object({
...syntheticUnitvariantLessonsSchema.omit({
Expand All @@ -53,5 +46,3 @@ export const partialSyntheticUnitvariantLessonsArraySchema = z.array(
export type PartialSyntheticUnitvariantLessons = z.infer<
typeof partialSyntheticUnitvariantLessonsSchema
>;

export default lessonListingSchema;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
ProgrammeFieldsCamel,
rawQuerySchema,
UnitListingData,
UnitsCamel,
} from "./unitListing.schema";

import { NEW_COHORT } from "@/config/cohort";
Expand All @@ -20,7 +19,6 @@ import {
Sdk,
} from "@/node-lib/curriculum-api-2023/generated/sdk";
import OakError from "@/errors/OakError";
import { getIntersection } from "@/utils/getIntersection";

const getTierData = (programmeSlug: string): UnitListingData["tiers"] => [
{
Expand Down Expand Up @@ -80,10 +78,6 @@ const unitListingQuery =
reshapedUnits,
);

const actions = getIntersection<UnitsCamel[number]["actions"]>(
unitsCamel.map((unit) => unit.actions),
);

const tiers = programmeFields.tierSlug
? getTierData(args.programmeSlug)
: [];
Expand All @@ -109,7 +103,6 @@ const unitListingQuery =
hasNewContent,
subjectCategories,
yearGroups,
actions,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ const unitListingData = z.object({
z.object({ year: yearSlugs, yearTitle: yearDescriptions }),
),
subjectCategories: z.array(subjectCategorySchema),
actions: z.array(z.object({})),
});

export type UnitListingData = z.infer<typeof unitListingData>;
41 changes: 24 additions & 17 deletions src/node-lib/curriculum-api-2023/shared.schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { z } from "zod";
import { actionsSchema } from "@oaknational/oak-curriculum-schema";

import { zodToCamelCase } from "./helpers/zodToCamelCase";

export const contentGuidanceSchema = z.object({
contentGuidanceLabel: z.string(),
Expand Down Expand Up @@ -236,23 +239,27 @@ export const baseLessonDownloadsSchema = z.object({
loginRequired: z.boolean().nullable(),
});

export const lessonListSchema = z.array(
z.object({
lessonSlug: z.string(),
lessonTitle: z.string(),
description: z.string(),
pupilLessonOutcome: z.string().nullish(),
expired: z.boolean().nullable(),
quizCount: z.number().nullish(),
videoCount: z.number().nullish(),
presentationCount: z.number().nullish(),
worksheetCount: z.number().nullish(),
hasCopyrightMaterial: z.boolean().nullish(),
orderInUnit: z.number().nullish(),
lessonCohort: z.string().nullish(),
actions: z.object({}).nullable(),
}),
);
const camelActionSchema = zodToCamelCase(actionsSchema);

export type Actions = z.infer<typeof camelActionSchema>;

export const lessonListItemSchema = z.object({
lessonSlug: z.string(),
lessonTitle: z.string(),
description: z.string(),
pupilLessonOutcome: z.string().nullish(),
expired: z.boolean().nullable(),
quizCount: z.number().nullish(),
videoCount: z.number().nullish(),
presentationCount: z.number().nullish(),
worksheetCount: z.number().nullish(),
hasCopyrightMaterial: z.boolean().nullish(),
orderInUnit: z.number().nullish(),
lessonCohort: z.string().nullish(),
actions: camelActionSchema.nullish(),
});

export const lessonListSchema = z.array(lessonListItemSchema);

export type LessonListSchema = z.infer<typeof lessonListSchema>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ import {
} from "@/node-lib/isr";
import { RESULTS_PER_PAGE } from "@/utils/resultsPerPage";
import curriculumApi2023 from "@/node-lib/curriculum-api-2023";
import {
LessonListingPageData,
lessonListingSchema,
} from "@/node-lib/curriculum-api-2023/queries/lessonListing/lessonListing.schema";
import { LessonListingPageData } from "@/node-lib/curriculum-api-2023/queries/lessonListing/lessonListing.schema";
import getPageProps from "@/node-lib/getPageProps";
import HeaderListing from "@/components/TeacherComponents/HeaderListing";
import isSlugLegacy from "@/utils/slugModifiers/isSlugLegacy";
Expand Down Expand Up @@ -58,7 +55,7 @@ export type LessonListingPageProps = {
* This data gets stored in the browser and is used to render the lesson list,
* so it's important to keep it as small as possible.
*/
function getHydratedLessonsFromUnit(unit: lessonListingSchema) {
function getHydratedLessonsFromUnit(unit: LessonListingPageData) {
const { lessons, ...rest } = unit;
return lessons.map((lesson) => ({
...lesson,
Expand Down

0 comments on commit 1f903c4

Please sign in to comment.