Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions apps/api/src/routes/content/collections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ describe("Collections Routes Tests", () => {
vi.mocked(db.query.collections.findMany).mockResolvedValue([
{
slug: "harsh-leadership-truths",
data: [
{
title: "Harsh Leadership Truths",
description:
"Exploring the often overlooked realities of engineering leadership.",
coverImage: "content/cover.png",
},
],
title: "Harsh Leadership Truths",
description:
"Exploring the often overlooked realities of engineering leadership.",
coverImage: "content/cover.png",
authors: [
{
slug: "crutchcorn",
Expand Down Expand Up @@ -91,14 +87,10 @@ describe("Collections Routes Tests", () => {
vi.mocked(db.query.collections.findMany).mockResolvedValue([
{
slug: "harsh-leadership-truths",
data: [
{
title: "Harsh Leadership Truths",
description:
"Exploring the often overlooked realities of engineering leadership.",
coverImage: null,
},
],
title: "Harsh Leadership Truths",
description:
"Exploring the often overlooked realities of engineering leadership.",
coverImage: null,
authors: [
{
slug: "crutchcorn",
Expand Down
26 changes: 12 additions & 14 deletions apps/api/src/routes/content/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createImageUrl } from "../../utils.ts";

const CollectionsQueryParamsSchema = Type.Object({
locale: Type.String({ default: "en" }),
branch: Type.String({ default: "main" }),
page: Type.Number({ minimum: 0 }),
limit: Type.Number({ minimum: 1 }),
author: Type.Optional(Type.String()),
Expand Down Expand Up @@ -97,14 +98,14 @@ const collectionsRoutes: FastifyPluginAsync = async (fastify) => {
const queryParams = request.query;

const collections = await db.query.collections.findMany({
where: queryParams.author
? { authors: { slug: queryParams.author } }
: undefined,
where: {
locale: queryParams.locale,
branch: queryParams.branch,
authors: queryParams.author
? { slug: queryParams.author }
: undefined,
},
with: {
data: {
columns: { coverImage: true, title: true, description: true },
where: { locale: queryParams.locale },
},
authors: { columns: { slug: true, name: true, profileImage: true } },
Comment thread
fennifith marked this conversation as resolved.
},
extras: {
Expand All @@ -117,16 +118,13 @@ const collectionsRoutes: FastifyPluginAsync = async (fastify) => {

const collectionsResponse: CollectionsResponse = [];
for (const collection of collections) {
const collectionData = collection.data[0];
if (!collectionData) continue;

const formattedCollection: CollectionsResponse[number] = {
slug: collection.slug,
coverUrl: collectionData.coverImage
? createImageUrl(collectionData.coverImage)
coverUrl: collection.coverImage
? createImageUrl(collection.coverImage)
: undefined,
title: collectionData.title,
description: collectionData.description,
title: collection.title,
description: collection.description,
chapterCount: collection.chapterCount,
authors: [],
};
Expand Down
113 changes: 21 additions & 92 deletions apps/api/src/routes/content/post.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("Post Routes Tests", () => {
});

describe("/content/post/:slug", () => {
test("returns a post with its authors and a chapter list sorted by collectionOrder", async () => {
test("returns a post with its authors and a chapter list", async () => {
vi.mocked(db.query.posts.findFirst).mockResolvedValue({
slug: "chapter-two",
title: "Chapter Two",
Expand All @@ -30,22 +30,22 @@ describe("Post Routes Tests", () => {
profileImage: "content/profile.png",
},
],
collection: {
slug: "example-collection",
data: [{ title: "Example Collection" }],
posts: [
{
slug: "chapter-two",
collectionOrder: 1,
title: "Chapter Two",
},
{
slug: "chapter-one",
collectionOrder: 0,
title: "Chapter One",
},
],
},
collections: [
{
slug: "example-collection",
title: "Example Collection",
posts: [
{
slug: "chapter-one",
title: "Chapter One",
},
{
slug: "chapter-two",
title: "Chapter Two",
},
],
},
],
versions: [],
} as never);

Expand Down Expand Up @@ -103,7 +103,7 @@ describe("Post Routes Tests", () => {
authors: [
{ slug: "crutchcorn", name: "Corbin Crutchley", profileImage: null },
],
collection: null,
collections: [],
versions: [],
} as never);

Expand Down Expand Up @@ -142,7 +142,7 @@ describe("Post Routes Tests", () => {
wordCount: 100,
publishedAt: null,
authors: [],
collection: null,
collections: [],
} as never);

const response = await app.inject({
Expand All @@ -159,77 +159,6 @@ describe("Post Routes Tests", () => {
`);
});

test("excludes unpublished sibling chapters from the chapter list", async () => {
vi.mocked(db.query.posts.findFirst).mockResolvedValue({
slug: "chapter-one",
title: "Chapter One",
description: "The first chapter",
bannerImage: null,
socialImage: null,
wordCount: 300,
publishedAt: new Date("2024-01-15T00:00:00Z"),
authors: [],
collection: {
slug: "example-collection",
data: [{ title: "Example Collection" }],
posts: [
{
slug: "chapter-one",
collectionOrder: 0,
title: "Chapter One",
publishedAt: new Date("2024-01-15T00:00:00Z"),
},
{
slug: "chapter-two-draft",
collectionOrder: 1,
title: "Chapter Two (Draft)",
publishedAt: null,
},
{
slug: "chapter-three",
collectionOrder: 2,
title: "Chapter Three",
publishedAt: new Date("2024-01-20T00:00:00Z"),
},
],
},
versions: [],
} as never);

const response = await app.inject({
method: "GET",
url: "/content/post/chapter-one",
query: { locale: "en" },
});

expect(response.statusCode).toBe(200);
expect(response.json()).toMatchInlineSnapshot(`
{
"authors": [],
"collection": {
"chapters": [
{
"slug": "chapter-one",
"title": "Chapter One",
},
{
"slug": "chapter-three",
"title": "Chapter Three",
},
],
"slug": "example-collection",
"title": "Example Collection",
},
"description": "The first chapter",
"publishedAt": "2024-01-15T00:00:00.000Z",
"slug": "chapter-one",
"title": "Chapter One",
"versions": [],
"wordCount": 300,
}
`);
});

test("includes the current post in the versions list, in the order returned by the query", async () => {
vi.mocked(db.query.posts.findFirst).mockResolvedValue({
slug: "example-post",
Expand All @@ -240,7 +169,7 @@ describe("Post Routes Tests", () => {
wordCount: 400,
publishedAt: new Date("2024-01-15T00:00:00Z"),
authors: [],
collection: null,
collections: [],
versions: [
{
slug: "example-post",
Expand Down Expand Up @@ -308,7 +237,7 @@ describe("Post Routes Tests", () => {
wordCount: 200,
publishedAt: new Date("2024-01-15T00:00:00Z"),
authors: [],
collection: null,
collections: [],
versions: [],
} as never);

Expand Down
39 changes: 21 additions & 18 deletions apps/api/src/routes/content/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,26 @@ const postRoutes: FastifyPluginAsync = async (fastify) => {
},
with: {
authors: { columns: { slug: true, name: true, profileImage: true } },
collection: {
collections: {
columns: { slug: true, title: true },
where: { locale, branch },
with: {
data: {
columns: { title: true },
where: { locale },
},
posts: {
columns: {
slug: true,
collectionOrder: true,
title: true,
publishedAt: true,
},
where: { locale, branch },
where: {
locale,
branch,
publishedAt: {
isNotNull: true,
},
},
orderBy: {
collectionOrder: "asc",
publishedAt: "asc",
},
},
},
},
Expand Down Expand Up @@ -178,20 +184,17 @@ const postRoutes: FastifyPluginAsync = async (fastify) => {
return;
}

const collectionData = post.collection?.data[0];
const collectionData = post.collections[0];

const collection: PostResponse["collection"] =
post.collection && collectionData
post.collections && collectionData
? {
slug: post.collection.slug,
slug: collectionData.slug,
title: collectionData.title,
chapters: post.collection.posts
.filter((chapter) => chapter.publishedAt !== null)
.sort((a, b) => a.collectionOrder - b.collectionOrder)
.map((chapter) => ({
slug: chapter.slug,
title: chapter.title,
})),
chapters: collectionData.posts.map((chapter) => ({
slug: chapter.slug,
title: chapter.title,
})),
}
: undefined;

Expand Down
11 changes: 6 additions & 5 deletions apps/worker/src/tasks/grant-author-achievements/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
authorAchievements,
postAuthors,
collectionAuthors,
collectionData,
collections,
posts,
} from "@playfulprogramming/db";
import { createInstallationClient } from "@playfulprogramming/github-api";
Expand Down Expand Up @@ -91,16 +91,17 @@ export default createProcessor(Tasks.GRANT_AUTHOR_ACHIEVEMENTS, async (job) => {
.select({ value: count() })
.from(collectionAuthors)
.innerJoin(
collectionData,
collections,
and(
eq(collectionData.slug, collectionAuthors.collectionSlug),
eq(collectionData.locale, "en"),
eq(collections.id, collectionAuthors.collectionId),
eq(collections.branch, "main"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to lock in main for the "GRANT_AUTHOR_ACHIVEMENTS" task? Wouldn't this mean that PRs that webhook into this would incorrectly point at main?

eq(collections.locale, "en"),
),
)
.where(
and(
eq(collectionAuthors.authorSlug, authorSlug),
isNotNull(collectionData.publishedAt),
isNotNull(collections.publishedAt),
),
);
const collectionCount = collectionCountResult[0]?.value ?? 0;
Expand Down
Loading