feat: citizenship applications
This commit is contained in:
@@ -5,6 +5,7 @@ import { Block, PageMeta, PageStatus, PageType } from "./types";
|
||||
import { archivePage, createPage, deletePage, updatePage } from "./repository";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { deleteNavLinkByPageId } from "../navlinks/repository";
|
||||
import { validatePage } from "./validation";
|
||||
|
||||
export async function savePageAction(params: {
|
||||
id: string | null,
|
||||
@@ -15,6 +16,10 @@ export async function savePageAction(params: {
|
||||
blocks: Block[],
|
||||
meta?: PageMeta
|
||||
}) {
|
||||
const parsed = validatePage(params);
|
||||
if (!parsed.success) {
|
||||
throw new Error(parsed.errors.join("; "));
|
||||
}
|
||||
|
||||
if (params.id) {
|
||||
await updatePage(params.id, params.title, params.type, params.status, params.blocks, params.meta);
|
||||
|
||||
+11
-7
@@ -1,18 +1,19 @@
|
||||
import { getCurrentUser } from "../user";
|
||||
import { Block, PageMeta, PageStatus, PageType } from "./types";
|
||||
import type { Block, Page, PageMeta, PageStatus, PageType } from "./types";
|
||||
import { prisma } from "../prisma";
|
||||
|
||||
export async function getPages(type: PageType, status: PageStatus, limit?: number) {
|
||||
// return await prisma.page.findMany({ where: { type, status }, take: limit, orderBy: { publishedAt: "desc" }, });
|
||||
return []
|
||||
return await prisma.page.findMany({ where: { type, status }, take: limit, orderBy: { publishedAt: "desc" }, }) as Page[];
|
||||
}
|
||||
|
||||
export async function getPageById(id: string) {
|
||||
return await prisma.page.findUnique({ where: { id: id } });
|
||||
}
|
||||
|
||||
export async function getPageBySlug(slug: string) {
|
||||
return await prisma.page.findUnique({ where: { slug } });
|
||||
export async function getPageBySlug(slug: string, type?: PageType) {
|
||||
return await prisma.page.findFirst({
|
||||
where: { slug, ...(type ? { type } : {}) },
|
||||
}) as Page | null;
|
||||
}
|
||||
|
||||
export async function createPage(slug: string, title: string, type: PageType, status: PageStatus, blocks: Block[], meta?: PageMeta) {
|
||||
@@ -68,6 +69,9 @@ export async function updatePage(
|
||||
const user = await getCurrentUser();
|
||||
if (!user?.isAdmin) throw new Error("Access denied");
|
||||
|
||||
const currentPage = await prisma.page.findUnique({ where: { id }, select: { status: true } });
|
||||
const isNewlyPublished = status === "PUBLISHED" && currentPage?.status !== "PUBLISHED";
|
||||
|
||||
const page = await prisma.page.update({
|
||||
where: { id },
|
||||
data: {
|
||||
@@ -75,7 +79,7 @@ export async function updatePage(
|
||||
type,
|
||||
status,
|
||||
meta: meta ?? {},
|
||||
publishedAt: status === "PUBLISHED" ? new Date() : undefined,
|
||||
publishedAt: isNewlyPublished ? new Date() : undefined,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: user.id
|
||||
},
|
||||
@@ -115,5 +119,5 @@ export async function getBlocksByPageId(pageId: string) {
|
||||
return prisma.block.findMany({
|
||||
where: { pageId },
|
||||
orderBy: { position: "asc" },
|
||||
});
|
||||
}) as Promise<Block[]>;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const pageSchema = z.object({
|
||||
id: z.string().nullable(),
|
||||
title: z.string().min(1, "Title is required"),
|
||||
slug: z.string().min(1, "Slug is required"),
|
||||
type: z.enum(["NEWS", "PAGE"]),
|
||||
status: z.enum(["DRAFT", "PUBLISHED", "ARCHIVED"]),
|
||||
blocks: z.array(z.any()),
|
||||
meta: z.object({
|
||||
location: z.string().optional(),
|
||||
tags: z.array(z.string()).optional(),
|
||||
}).optional(),
|
||||
});
|
||||
|
||||
export type PageFormData = z.infer<typeof pageSchema>;
|
||||
|
||||
export function validatePage(data: unknown): { success: true; data: PageFormData } | { success: false; errors: string[] } {
|
||||
const result = pageSchema.safeParse(data);
|
||||
if (!result.success) {
|
||||
const fieldErrors = result.error.flatten().fieldErrors ?? {};
|
||||
const messages = Object.values(fieldErrors).flat().filter(Boolean);
|
||||
return { success: false, errors: messages };
|
||||
}
|
||||
return { success: true, data: result.data };
|
||||
}
|
||||
Reference in New Issue
Block a user