Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
demanguillaume committed Jun 24, 2024
1 parent aa601a2 commit 9cb8c18
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 134 deletions.
146 changes: 73 additions & 73 deletions prisma/seed.ts

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/app/api/journeys/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import {
getAllJourneys,
registerOrModifyJourney,
} from "@/services/journeyService";
import { HttpOperation } from "@/types/enums/httpOperation";
import { JourneyWithoutDates } from "@/types/journey";
import { StepWithoutDates } from "@/types/step";
import { journeyBodySchema } from "@/validators/api/journeySchema";
import { createJourneyBodySchema } from "@/validators/api/journeySchema";
import { Prisma } from "@prisma/client";
import { NextRequest, NextResponse } from "next/server";

Expand All @@ -34,6 +35,7 @@ export async function POST(request: NextRequest) {
try {
const body = await request.json();
// Parse the body with zod to get the journey and steps
const journeyBodySchema = createJourneyBodySchema(HttpOperation.POST);
const parsedBody = journeyBodySchema.parse(body);
const journey: JourneyWithoutDates = parsedBody.journey;
const steps: StepWithoutDates[] = parsedBody.steps;
Expand Down
1 change: 1 addition & 0 deletions src/app/apiClient/registerUser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const registerUserApi = async (data: {
email: string;
password: string;
confirmPassword: string;
username: string;
name: string;
lastName: string;
Expand Down
6 changes: 6 additions & 0 deletions src/types/enums/httpOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum HttpOperation {
GET,
POST,
PUT,
DELETE,
}
2 changes: 1 addition & 1 deletion src/types/step.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type StepWithoutDates = {
id?: number;
journeyId: number;
journeyId: number | undefined;
puzzle: string;
answer: string;
hint: string;
Expand Down
32 changes: 17 additions & 15 deletions src/validators/api/journeySchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "zod";
import { baseStepSchema } from "./stepSchema";
import { createStepSchema } from "./stepSchema";
import { HttpOperation } from "@/types/enums/httpOperation";

// Journey schema with custom error messages
const baseJourneySchema = z.object({
Expand Down Expand Up @@ -49,19 +50,20 @@ const baseJourneySchema = z.object({
});

// Schéma combiné pour le corps de la requête avec transformation des steps pour inclure journeyId
export const journeyBodySchema = z
.object({
journey: baseJourneySchema,
steps: z.array(baseStepSchema),
})
.transform((data) => {
const { journey, steps } = data;
export const createJourneyBodySchema = (httpOperation: HttpOperation) =>
z
.object({
journey: baseJourneySchema,
steps: z.array(createStepSchema(httpOperation)),
})
.transform((data) => {
const { journey, steps } = data;

// Ajouter journeyId à chaque étape avant validation
const stepsWithJourneyId = steps.map((step) => ({
...step,
journeyId: journey.authorId,
}));
// Ajouter journeyId à chaque étape avant validation
const stepsWithJourneyId = steps.map((step) => ({
...step,
journeyId: journey.authorId,
}));

return { journey, steps: stepsWithJourneyId };
});
return { journey, steps: stepsWithJourneyId };
});
98 changes: 54 additions & 44 deletions src/validators/api/stepSchema.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
import { z } from "zod";
import { HttpOperation } from "@/types/enums/httpOperation";

// Step schema with custom error messages
export const baseStepSchema = z.object({
id: z
.string()
.optional()
.transform((val) => (val ? Number(val) : undefined)),
puzzle: z
.string({ required_error: "Required field" })
.max(1000, { message: "Please enter less than 1000 characters" }),
answer: z
.string({ required_error: "Required field" })
.max(255, { message: "Please enter less than 255 characters" }),
hint: z
.string({ required_error: "Required field" })
.max(1000, { message: "Please enter less than 1000 characters" }),
picturePuzzle: z
.string({ required_error: "Required field" })
.url({ message: "Please provide a valid URL" }),
pictureHint: z
.string({ required_error: "Required field" })
.url({ message: "Please provide a valid URL" }),
latitude: z.number({ required_error: "Required field" }),
longitude: z.number({ required_error: "Required field" }),
address: z
.string()
.max(255, { message: "Please enter less than 255 characters" })
.optional(),
city: z
.string()
.max(50, { message: "Please enter less than 50 characters" })
.optional(),
postalCode: z
.string()
.regex(/^\d{5}$/, { message: "Postal code must be exactly 5 digits" })
.optional(),
country: z
.string()
.max(50, { message: "Please enter less than 50 characters" })
.optional(),
stepNumber: z.number({ required_error: "Required field" }).int(),
journeyId: z.number({ required_error: "Required field" }).int(),
});
export const createStepSchema = (httpOperation: HttpOperation) => {
const baseStepSchema = z.object({
id: z
.string()
.optional()
.transform((val) => (val ? Number(val) : undefined)),
puzzle: z
.string({ required_error: "Required field" })
.max(1000, { message: "Please enter less than 1000 characters" }),
answer: z
.string({ required_error: "Required field" })
.max(255, { message: "Please enter less than 255 characters" }),
hint: z
.string({ required_error: "Required field" })
.max(1000, { message: "Please enter less than 1000 characters" }),
picturePuzzle: z
.string({ required_error: "Required field" })
.url({ message: "Please provide a valid URL" }),
pictureHint: z
.string({ required_error: "Required field" })
.url({ message: "Please provide a valid URL" }),
latitude: z.number({ required_error: "Required field" }),
longitude: z.number({ required_error: "Required field" }),
address: z
.string()
.max(255, { message: "Please enter less than 255 characters" })
.optional(),
city: z
.string()
.max(50, { message: "Please enter less than 50 characters" })
.optional(),
postalCode: z
.string()
.regex(/^\d{5}$/, { message: "Postal code must be exactly 5 digits" })
.optional(),
country: z
.string()
.max(50, { message: "Please enter less than 50 characters" })
.optional(),
stepNumber: z.number({ required_error: "Required field" }).int(),
journeyId:
httpOperation === HttpOperation.POST
? z.number({ required_error: "Required field" }).int()
: z.number().int().optional(),
});

return z.object({
step: baseStepSchema,
});
};

// Combined schema for the body
export const stepBodySchema = z.object({
step: baseStepSchema,
});
// export const stepBodySchema = z.object({
// step: baseStepSchema,
// });

0 comments on commit 9cb8c18

Please sign in to comment.