feat(core-cms): compose @repo/blog/cms into payload config
This commit is contained in:
@@ -12,7 +12,7 @@ function generateSlug(title: string): string {
|
||||
|
||||
export async function createArticleUseCase(input: {
|
||||
title: string;
|
||||
content: unknown;
|
||||
content?: unknown;
|
||||
authorId: string;
|
||||
slug?: string;
|
||||
}): Promise<Article> {
|
||||
|
||||
@@ -5,11 +5,9 @@ vi.mock("payload", () => ({
|
||||
getPayload: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@repo/core-cms", () => ({
|
||||
default: {} as never,
|
||||
}));
|
||||
|
||||
describe("PayloadArticlesRepository", () => {
|
||||
const mockConfig = {} as never;
|
||||
|
||||
it("maps a Payload doc to a domain Article on getArticleBySlug", async () => {
|
||||
const { getPayload } = await import("payload");
|
||||
const findMock = vi.fn().mockResolvedValue({
|
||||
@@ -30,7 +28,7 @@ describe("PayloadArticlesRepository", () => {
|
||||
find: findMock,
|
||||
});
|
||||
|
||||
const repo = new PayloadArticlesRepository();
|
||||
const repo = new PayloadArticlesRepository(mockConfig);
|
||||
const result = await repo.getArticleBySlug("hello");
|
||||
|
||||
expect(findMock).toHaveBeenCalledWith({
|
||||
@@ -52,7 +50,7 @@ describe("PayloadArticlesRepository", () => {
|
||||
find: vi.fn().mockResolvedValue({ docs: [] }),
|
||||
});
|
||||
|
||||
const repo = new PayloadArticlesRepository();
|
||||
const repo = new PayloadArticlesRepository(mockConfig);
|
||||
const result = await repo.getArticleBySlug("missing");
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import "reflect-metadata";
|
||||
import { injectable } from "inversify";
|
||||
import { getPayload } from "payload";
|
||||
import type { SanitizedConfig } from "payload";
|
||||
|
||||
import config from "@repo/core-cms";
|
||||
import type { IArticlesRepository } from "@/application/repositories/articles-repository.interface";
|
||||
import type { Article } from "@/entities/article";
|
||||
|
||||
@@ -38,8 +38,14 @@ function mapDoc(doc: PayloadArticleDoc): Article {
|
||||
|
||||
@injectable()
|
||||
export class PayloadArticlesRepository implements IArticlesRepository {
|
||||
private config: SanitizedConfig;
|
||||
|
||||
constructor(config: SanitizedConfig) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
async getArticle(id: string): Promise<Article | undefined> {
|
||||
const payload = await getPayload({ config });
|
||||
const payload = await getPayload({ config: this.config });
|
||||
try {
|
||||
const doc = await payload.findByID({
|
||||
collection: "articles",
|
||||
@@ -53,7 +59,7 @@ export class PayloadArticlesRepository implements IArticlesRepository {
|
||||
}
|
||||
|
||||
async getArticleBySlug(slug: string): Promise<Article | undefined> {
|
||||
const payload = await getPayload({ config });
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const result = await payload.find({
|
||||
collection: "articles",
|
||||
where: { slug: { equals: slug } },
|
||||
@@ -70,7 +76,7 @@ export class PayloadArticlesRepository implements IArticlesRepository {
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}): Promise<Article[]> {
|
||||
const payload = await getPayload({ config });
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const where: Record<string, { equals: string }> = {};
|
||||
if (options?.status) where.status = { equals: options.status };
|
||||
if (options?.authorId) where.author = { equals: options.authorId };
|
||||
@@ -88,7 +94,7 @@ export class PayloadArticlesRepository implements IArticlesRepository {
|
||||
}
|
||||
|
||||
async createArticle(input: Article): Promise<Article> {
|
||||
const payload = await getPayload({ config });
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const created = await payload.create({
|
||||
collection: "articles",
|
||||
data: {
|
||||
@@ -107,7 +113,7 @@ export class PayloadArticlesRepository implements IArticlesRepository {
|
||||
id: string,
|
||||
input: Partial<Article>,
|
||||
): Promise<Article | undefined> {
|
||||
const payload = await getPayload({ config });
|
||||
const payload = await getPayload({ config: this.config });
|
||||
try {
|
||||
const updated = await payload.update({
|
||||
collection: "articles",
|
||||
|
||||
@@ -10,7 +10,7 @@ import { createArticleUseCase } from "@/application/use-cases/create-article.use
|
||||
|
||||
const createInputSchema = z.object({
|
||||
title: z.string().min(1).max(255),
|
||||
content: z.unknown(),
|
||||
content: z.unknown().optional(),
|
||||
authorId: z.string(),
|
||||
slug: z.string().optional(),
|
||||
});
|
||||
@@ -35,7 +35,12 @@ export async function createArticleController(
|
||||
cause: parsed.error,
|
||||
});
|
||||
}
|
||||
return createArticleUseCase(parsed.data);
|
||||
return createArticleUseCase({
|
||||
title: parsed.data.title,
|
||||
content: parsed.data.content ?? null,
|
||||
authorId: parsed.data.authorId,
|
||||
slug: parsed.data.slug,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getArticlesController(
|
||||
|
||||
Reference in New Issue
Block a user