Posts
A post is one editorial page — a blog article or a travel guide. Its unique identity is canonicalUrl; its slug is only unique inside a country/city/category namespace.
Model
model Post {
id String @id @default(cuid())
title String?
slug String
content String
coverImage String?
status String @default("draft")
scheduledAt DateTime?
publishedAt DateTime?
views Int @default(0)
metaTitle String?
metaDescription String?
meta Json?
toc Json?
jsonLdSchema Json?
tags String?
canonicalUrl String @unique
urlHistory String[] @default([])
cityHandle String?
blogCategoryId String?
subCategoryId String?
authorId String?
cityId String?
countryId String?
@@unique([countryId, cityId, blogCategoryId, slug])
}| Field | Notes |
|---|---|
status | draft, scheduled, published, trash |
scheduledAt | When the every-minute cron should publish it |
publishedAt | Set on publish, cleared when moved out of published |
toc | Extracted from the sanitised HTML on write |
meta | Free-form JSON for word count, reading time, focus keyphrase |
tags | Comma-separated string, not a relation |
cityHandle | Stored directly so old posts without a City row still resolve |
urlHistory | Every previous canonical URL |
Creating and editing
The admin form collects title, slug, Tiptap HTML content, cover image, category and optional subcategory, author, tags, status/schedule, SEO fields, JSON-LD, and country/city.
On create:
slugis slugified. If it is missing the request is rejected.- The canonical URL is assembled from country handle, city handle, category slug and slug, skipping
default. - A duplicate slug in the same
(countryId, cityId, blogCategoryId)namespace returns409. - A duplicate
canonicalUrlreturns409. - Content is sanitised, TOC is extracted, and
publishedAtis set only when the initial status ispublished.
On edit:
- The post is looked up by
idwhen supplied, otherwise by the pathslug. - An author cannot edit another author's post; a super admin can edit any.
- Changing the slug, city, country or category recomputes
canonicalUrland pushes the old value ontourlHistory. - Content is sanitised again, absolute image URLs are stripped, internal links not in the link bank are removed, and the TOC is rebuilt.
- Publishing revalidates the frontend. Every edit appends an audit entry.
Category "default"
The sentinel subcategory and country named/slugged default are removed from a post on save. They exist only so the schema has a fallback row.
Status lifecycle
draft ──▶ scheduled ──(cron, every minute)──▶ published
│ │ │
└──────────────┴──────────────▶ trash ◀─────────┘
│
restore (→ draft) or delete permanently| Action | Endpoint | Effect |
|---|---|---|
| Trash | DELETE /api/posts/delete/:postId | Sets status = "trash" |
| Delete permanently | DELETE /api/posts/delete/:postId?permanent=true | Removes the row |
| Restore | PATCH /api/posts/restore/:id | Sets status = "draft" |
Authors can only trash, restore or permanently delete their own posts. A super admin can act on any post.
Listing and filters
GET /api/posts (admin) accepts:
| Param | Behaviour |
|---|---|
status | draft, scheduled, published, or all (default) |
trash | true lists only trashed posts |
category | Category id, or all |
search | Case-insensitive title contains |
page, limit | Pagination |
A non-super-admin only sees their own posts. The default view excludes trash and orders by updatedAt desc, then id desc. The list selects summary columns only, not the full content.
Public reads
| Endpoint | Behaviour |
|---|---|
GET /api/posts/published | Published only. Filters search (title or meta description), category (slug), country (handle), city (handle). Ordered by publishedAt desc. |
GET /api/posts/public | Published only, max 50 per page, same category/country/city filters, no search. |
GET /api/posts/category/:slug | Published posts in one category. |
GET /api/posts/author/:id | Published posts by author, ordered by updatedAt desc. No pagination. |
GET /api/posts/:slug | Single post. Also returns related (same category, 3) and youMayAlsoLike (tag overlap, 5). |
GET /api/posts/resolve/:slug | { exists, canonicalPath, slug } for any current or historical path. |
The single-post endpoint accepts a full path in the URL segment (/api/posts/anything), or query parameters path, country, category, city. A full path always wins over a bare slug because the slug alone may be ambiguous. A 25-character value is also tried as a cuid.
Responses normalise canonicalUrl, removing default segments. Related posts include their excerpt, category and subcategory; youMayAlsoLike is built from tag overlap.
URL rules
/{countryHandle}/{cityHandle}/{categorySlug}/{slug}Every segment except the slug is optional. Examples:
| Data | Canonical URL |
|---|---|
| Country Nepal, city Pokhara, category travel-guide, slug best-time | /nepal/pokhara/travel-guide/best-time |
| Country Nepal, category blog, slug permits | /nepal/blog/permits |
| No relations, slug about-us | /about-us |
Lookups always try the exact canonicalUrl, then the normalised value, then urlHistory. A bare slug that matches more than one row is treated as ambiguous and returns no post rather than guessing.
Categories and subcategories
PostCategory and SubCategory are independent two-level taxonomies: a post has one optional category and one optional subcategory, but the subcategory is not scoped to the parent category.
Both models carry featureImage, secondaryImage, subtitle, content, metaTitle and metaDescription, so a category page can be a full landing page. GET /api/post-categories supports q, page and limit and returns a post count per category.
Writes require the categories or subcategories permission. Slugs are slugified; a duplicate slug is rejected by the unique constraint.
Caching
Every published post is cached by the frontend under the tag post-<slug>. Publishing, updating a published post, or the scheduler promotes a scheduled post triggers the revalidation webhook. A post edited while still a draft sends no revalidation because no public page exists yet.