feat(api): setting up

This commit is contained in:
2025-11-09 16:28:36 +02:00
parent d72f3871a4
commit 4b82a11207
35 changed files with 2280 additions and 1516 deletions
-547
View File
@@ -1,547 +0,0 @@
import type { AnyColumn, SQL } from "drizzle-orm";
import { and, asc, desc, eq, gt, lt, or, sql } from "drizzle-orm";
import type { Database } from "@/client";
import { articles, bookmarkArticles, bookmarks, comments, sources, users } from "@/schema";
import {
type PageRequest,
type PageState,
type PaginationMeta,
type SortDirection,
buildPaginationResult,
createPageState,
decodeCursor,
} from "@/utils/pagination";
export interface ArticleFilters {
search?: string | null;
category?: string | null;
dateRange?: { start: number; end: number } | null;
sortDirection?: SortDirection;
}
export interface ArticleOverviewRow {
article_id: string;
articleTitle: string;
articleLink: string;
articleCategories: string | null;
article_excerpt: string | null;
article_published_at: string;
article_image: string | null;
article_reading_time: number | null;
sourceId: string;
source_display_name: string | null;
source_image: string;
sourceUrl: string;
source_name: string;
source_created_at: string;
article_is_bookmarked: boolean;
}
export interface ArticleOverviewResult {
data: ArticleOverviewRow[];
pagination: PaginationMeta;
}
export interface ArticleDetailsRow {
article_id: string;
articleTitle: string;
articleLink: string;
articleCategories: string | null;
articleBody: string;
article_hash: string;
article_published_at: string;
article_crawled_at: string;
article_updated_at: string | null;
article_bias: string;
article_reliability: string;
article_transparency: string;
article_sentiment: string;
article_metadata: unknown;
article_reading_time: number | null;
sourceId: string;
source_name: string;
source_description: string | null;
sourceUrl: string;
source_updated_at: string | null;
source_display_name: string | null;
source_bias: string;
source_reliability: string;
source_transparency: string;
source_image: string;
article_is_bookmarked: boolean;
}
export interface ArticleCommentRow {
comment_id: string;
comment_content: string;
comment_sentiment: string;
comment_created_at: string;
user_id: string;
user_name: string;
}
interface NormalizedArticleFilters {
search?: string;
category?: string;
dateRange?: { start: number; end: number } | null;
sortDirection: SortDirection;
}
export interface ArticleExportRow {
articleId: string;
articleTitle: string;
articleLink: string;
articleCategories: string | null;
articleBody: string;
articleSource: string;
articleHash: string;
articlePublishedAt: string;
articleCrawledAt: string;
}
export interface ArticleExportParams {
source?: string | null;
dateRange?: { start: number; end: number } | null;
batchSize?: number;
}
export async function* getArticlesForExport(
db: Database,
params: ArticleExportParams = {},
): AsyncGenerator<ArticleExportRow> {
const batchSize = params.batchSize && params.batchSize > 0 ? params.batchSize : 1000;
const filters: SQL[] = [];
if (params.source) {
filters.push(eq(sources.name, params.source));
}
if (params.dateRange) {
filters.push(
sql`${articles.publishedAt} BETWEEN to_timestamp(
${params.dateRange.start}
)
AND
to_timestamp
(
${params.dateRange.end}
)`,
);
}
let query = db
.select({
articleBody: articles.body,
articleCategories: sql<string | null>`array_to_string
(${articles.categories}, ',')`,
articleCrawledAt: articles.crawledAt,
articleHash: articles.hash,
articleId: articles.id,
articleLink: articles.link,
articlePublishedAt: articles.publishedAt,
articleSource: sources.name,
articleTitle: articles.title,
})
.from(articles)
.innerJoin(sources, eq(articles.sourceId, sources.id));
if (filters.length === 1) {
query = query.where(filters[0]);
} else if (filters.length > 1) {
query = query.where(and(...filters));
}
query = query.orderBy(desc(articles.publishedAt), desc(articles.id));
let offset = 0;
while (true) {
const rows = await query.limit(batchSize).offset(offset);
if (rows.length === 0) {
break;
}
for (const row of rows) {
yield {
...row,
articleCategories: row.articleCategories ?? null,
};
}
offset += batchSize;
}
}
const SOURCE_IMAGE_BASE = "https://devscast.org/images/sources/";
function normalizeArticleFilters(filters?: ArticleFilters): NormalizedArticleFilters {
const trimmedSearch = filters?.search?.trim();
const trimmedCategory = filters?.category?.trim();
return {
category: trimmedCategory && trimmedCategory.length > 0 ? trimmedCategory : undefined,
dateRange: filters?.dateRange ?? null,
search: trimmedSearch && trimmedSearch.length > 0 ? trimmedSearch : undefined,
sortDirection: filters?.sortDirection ?? "desc",
};
}
function buildArticleFilterConditions(filters: NormalizedArticleFilters): {
conditions: SQL[];
searchQuery?: string;
} {
const conditions: SQL[] = [];
let searchQuery: string | undefined;
if (filters.category) {
conditions.push(sql`${filters.category} = ANY(
${articles.categories}
)`);
}
if (filters.search) {
const sanitized = filters.search.replace(/\s+/g, " & ");
if (sanitized.length > 0) {
searchQuery = sanitized;
conditions.push(
sql`${articles.tsv} @@ to_tsquery('french',
${sanitized}
)`,
);
}
}
if (filters.dateRange) {
conditions.push(
sql`${articles.publishedAt} BETWEEN to_timestamp(
${filters.dateRange.start}
)
AND
to_timestamp
(
${filters.dateRange.end}
)`,
);
}
return { conditions, searchQuery };
}
function buildBookmarkExistsExpression(userId: string): SQL<boolean> {
return sql`EXISTS
(SELECT 1
FROM ${bookmarkArticles} ba
INNER JOIN ${bookmarks} b ON ba.bookmark_id = b.id
WHERE ba.article_id = ${articles.id}
AND b.user_id = ${userId})`;
}
async function fetchArticleOverview(
db: Database,
options: {
userId: string;
page: PageState;
filters: NormalizedArticleFilters;
baseConditions?: SQL[];
},
): Promise<ArticleOverviewResult> {
const baseConditions = options.baseConditions ?? [];
const { conditions: filterConditions, searchQuery } = buildArticleFilterConditions(
options.filters,
);
const whereConditions = [...baseConditions, ...filterConditions];
const bookmarkExpression = buildBookmarkExistsExpression(options.userId);
const selectFields = {
article_excerpt: articles.excerpt,
article_id: articles.id,
article_image: articles.image,
article_is_bookmarked: bookmarkExpression,
article_published_at: articles.publishedAt,
article_reading_time: articles.readingTime,
articleCategories: sql<string | null>`array_to_string
(${articles.categories}, ',')`,
articleLink: articles.link,
articleTitle: articles.title,
source_created_at: sources.createdAt,
source_display_name: sources.displayName,
source_image: sql<string>`('${SOURCE_IMAGE_BASE}' || ${sources.name} || '.png')`,
source_name: sources.name,
sourceId: sources.id,
sourceUrl: sources.url,
} satisfies Record<string, SQL | AnyColumn>;
let query = db
.select(selectFields)
.from(articles)
.innerJoin(sources, eq(articles.sourceId, sources.id));
const cursor = decodeCursor(options.page.cursor);
if (cursor?.date) {
const comparison =
options.filters.sortDirection === "asc"
? or(
gt(articles.publishedAt, cursor.date),
and(eq(articles.publishedAt, cursor.date), gt(articles.id, cursor.id)),
)
: or(
lt(articles.publishedAt, cursor.date),
and(eq(articles.publishedAt, cursor.date), lt(articles.id, cursor.id)),
);
whereConditions.push(comparison);
}
if (whereConditions.length === 1) {
query = query.where(whereConditions[0]);
} else if (whereConditions.length > 1) {
query = query.where(and(...whereConditions));
}
const orderings: (SQL | AnyColumn)[] = [];
if (searchQuery) {
orderings.push(
options.filters.sortDirection === "asc"
? sql`ts_rank
(${articles.tsv}, to_tsquery('french', ${searchQuery}))
ASC`
: sql`ts_rank
(${articles.tsv}, to_tsquery('french', ${searchQuery}))
DESC`,
);
}
if (options.filters.sortDirection === "asc") {
orderings.push(asc(articles.publishedAt), asc(articles.id));
} else {
orderings.push(desc(articles.publishedAt), desc(articles.id));
}
const rows = await query.orderBy(...orderings).limit(options.page.limit + 1);
return buildPaginationResult(rows, options.page, {
date: "article_published_at",
id: "article_id",
});
}
export async function getArticleOverviewList(
db: Database,
params: {
userId: string;
page?: PageRequest;
filters?: ArticleFilters;
},
): Promise<ArticleOverviewResult> {
const page = createPageState(params.page);
const filters = normalizeArticleFilters(params.filters);
return fetchArticleOverview(db, {
filters,
page,
userId: params.userId,
});
}
export async function getSourceArticleOverviewList(
db: Database,
params: {
sourceId: string;
userId: string;
page?: PageRequest;
filters?: ArticleFilters;
},
): Promise<ArticleOverviewResult> {
const page = createPageState(params.page);
const filters = normalizeArticleFilters(params.filters);
return fetchArticleOverview(db, {
baseConditions: [eq(sources.id, params.sourceId)],
filters,
page,
userId: params.userId,
});
}
export async function getBookmarkedArticleList(
db: Database,
params: {
userId: string;
bookmarkId: string;
page?: PageRequest;
filters?: ArticleFilters;
},
): Promise<ArticleOverviewResult> {
const page = createPageState(params.page);
const filters = normalizeArticleFilters(params.filters);
const { conditions: filterConditions, searchQuery } = buildArticleFilterConditions(filters);
const whereConditions: SQL[] = [
eq(bookmarks.id, params.bookmarkId),
eq(bookmarks.userId, params.userId),
...filterConditions,
];
const selectFields = {
article_excerpt: articles.excerpt,
article_id: articles.id,
article_image: articles.image,
article_is_bookmarked: sql<boolean>`true`,
article_published_at: articles.publishedAt,
article_reading_time: articles.readingTime,
articleCategories: sql<string | null>`array_to_string
(${articles.categories}, ',')`,
articleLink: articles.link,
articleTitle: articles.title,
source_created_at: sources.createdAt,
source_display_name: sources.displayName,
source_image: sql<string>`('${SOURCE_IMAGE_BASE}' || ${sources.name} || '.png')`,
source_name: sources.name,
sourceId: sources.id,
sourceUrl: sources.url,
} satisfies Record<string, SQL | AnyColumn>;
let query = db
.select(selectFields)
.from(bookmarkArticles)
.innerJoin(articles, eq(bookmarkArticles.articleId, articles.id))
.innerJoin(bookmarks, eq(bookmarkArticles.bookmarkId, bookmarks.id))
.innerJoin(sources, eq(articles.sourceId, sources.id));
const cursor = decodeCursor(page.cursor);
if (cursor?.date) {
const comparison =
filters.sortDirection === "asc"
? or(
gt(articles.publishedAt, cursor.date),
and(eq(articles.publishedAt, cursor.date), gt(articles.id, cursor.id)),
)
: or(
lt(articles.publishedAt, cursor.date),
and(eq(articles.publishedAt, cursor.date), lt(articles.id, cursor.id)),
);
whereConditions.push(comparison);
}
if (whereConditions.length === 1) {
query = query.where(whereConditions[0]);
} else if (whereConditions.length > 1) {
query = query.where(and(...whereConditions));
}
const orderings: (SQL | AnyColumn)[] = [];
if (searchQuery) {
orderings.push(
filters.sortDirection === "asc"
? sql`ts_rank
(${articles.tsv}, to_tsquery('french', ${searchQuery}))
ASC`
: sql`ts_rank
(${articles.tsv}, to_tsquery('french', ${searchQuery}))
DESC`,
);
}
if (filters.sortDirection === "asc") {
orderings.push(asc(articles.publishedAt), asc(articles.id));
} else {
orderings.push(desc(articles.publishedAt), desc(articles.id));
}
const rows = await query.orderBy(...orderings).limit(page.limit + 1);
return buildPaginationResult(rows, page, {
date: "article_published_at",
id: "article_id",
});
}
export async function getArticleDetails(
db: Database,
params: { id: string; userId: string },
): Promise<ArticleDetailsRow | null> {
const bookmarkExpression = buildBookmarkExistsExpression(params.userId);
const [row] = await db
.select({
article_bias: articles.bias,
article_crawled_at: articles.crawledAt,
article_hash: articles.hash,
article_id: articles.id,
article_is_bookmarked: bookmarkExpression,
article_metadata: articles.metadata,
article_published_at: articles.publishedAt,
article_reading_time: articles.readingTime,
article_reliability: articles.reliability,
article_sentiment: articles.sentiment,
article_transparency: articles.transparency,
article_updated_at: articles.updatedAt,
articleBody: articles.body,
articleCategories: sql<string | null>`array_to_string
(${articles.categories}, ',')`,
articleLink: articles.link,
articleTitle: articles.title,
source_bias: sources.bias,
source_description: sources.description,
source_display_name: sources.displayName,
source_image: sql<string>`('${SOURCE_IMAGE_BASE}' || ${sources.name} || '.png')`,
source_name: sources.name,
source_reliability: sources.reliability,
source_transparency: sources.transparency,
source_updated_at: sources.updatedAt,
sourceId: sources.id,
sourceUrl: sources.url,
})
.from(articles)
.innerJoin(sources, eq(articles.sourceId, sources.id))
.where(eq(articles.id, params.id))
.limit(1);
return row ?? null;
}
export async function getArticleCommentList(
db: Database,
params: { articleId: string; page?: PageRequest },
): Promise<{ data: ArticleCommentRow[]; pagination: PaginationMeta }> {
const page = createPageState(params.page);
const whereConditions: SQL[] = [eq(comments.articleId, params.articleId)];
const cursor = decodeCursor(page.cursor);
if (cursor?.date) {
whereConditions.push(
or(
lt(comments.createdAt, cursor.date),
and(eq(comments.createdAt, cursor.date), lt(comments.id, cursor.id)),
),
);
}
let query = db
.select({
comment_content: comments.content,
comment_created_at: comments.createdAt,
comment_id: comments.id,
comment_sentiment: comments.sentiment,
user_id: users.id,
user_name: users.name,
})
.from(comments)
.innerJoin(users, eq(comments.userId, users.id));
if (whereConditions.length === 1) {
query = query.where(whereConditions[0]);
} else if (whereConditions.length > 1) {
query = query.where(and(...whereConditions));
}
const rows = await query
.orderBy(desc(comments.createdAt), desc(comments.id))
.limit(page.limit + 1);
return buildPaginationResult(rows, page, {
date: "comment_created_at",
id: "comment_id",
});
}
-66
View File
@@ -1,66 +0,0 @@
import type { SQL } from "drizzle-orm";
import { and, desc, eq, lt, sql } from "drizzle-orm";
import type { Database } from "@/client";
import { bookmarkArticles, bookmarks } from "@/schema";
import {
type PageRequest,
type PaginationMeta,
buildPaginationResult,
createPageState,
decodeCursor,
} from "@/utils/pagination";
export interface BookmarkRow {
bookmark_id: string;
bookmark_name: string;
bookmark_description: string | null;
bookmark_created_at: string;
bookmark_updated_at: string | null;
bookmark_articles_count: number;
bookmark_is_public: boolean;
}
export interface BookmarkListResult {
data: BookmarkRow[];
pagination: PaginationMeta;
}
export async function getBookmarkList(
db: Database,
params: { userId: string; page?: PageRequest },
): Promise<BookmarkListResult> {
const page = createPageState(params.page);
const whereConditions: SQL[] = [eq(bookmarks.userId, params.userId)];
const cursor = decodeCursor(page.cursor);
if (cursor?.id) {
whereConditions.push(lt(bookmarks.id, cursor.id));
}
let query = db
.select({
bookmark_articles_count: sql<number>`count(${bookmarkArticles.articleId})`,
bookmark_created_at: bookmarks.createdAt,
bookmark_description: bookmarks.description,
bookmark_id: bookmarks.id,
bookmark_is_public: bookmarks.isPublic,
bookmark_name: bookmarks.name,
bookmark_updated_at: bookmarks.updatedAt,
})
.from(bookmarks)
.leftJoin(bookmarkArticles, eq(bookmarkArticles.bookmarkId, bookmarks.id))
.groupBy(bookmarks.id);
if (whereConditions.length === 1) {
query = query.where(whereConditions[0]);
} else if (whereConditions.length > 1) {
query = query.where(and(...whereConditions));
}
const rows = await query
.orderBy(desc(bookmarks.createdAt), desc(bookmarks.id))
.limit(page.limit + 1);
return buildPaginationResult(rows, page, { id: "bookmark_id" });
}
-4
View File
@@ -1,4 +0,0 @@
export * from "./articles";
export * from "./bookmarks";
export * from "./sources";
export * from "./users";
-339
View File
@@ -1,339 +0,0 @@
import type { SQL } from "drizzle-orm";
import { and, desc, eq, lt, or, sql } from "drizzle-orm";
import type { Database } from "@/client";
import { PUBLICATION_GRAPH_DAYS, SOURCE_IMAGE_BASE } from "@/constant";
import { articles, followedSources, sources } from "@/schema";
import {
type PageRequest,
type PaginationMeta,
buildPaginationResult,
createPageState,
decodeCursor,
} from "@/utils/pagination";
export interface SourceOverviewRow {
sourceId: string;
source_display_name: string | null;
source_image: string;
sourceUrl: string;
source_name: string;
source_created_at: string;
source_is_followed: boolean;
}
export interface SourceOverviewResult {
data: SourceOverviewRow[];
pagination: PaginationMeta;
}
export interface PublicationEntry {
day: string;
count: number;
}
export interface CategoryShare {
category: string;
count: number;
percentage: number;
}
export interface SourceDetailsResult {
source: {
sourceId: string;
source_name: string;
source_description: string | null;
sourceUrl: string;
source_updated_at: string | null;
source_display_name: string | null;
source_bias: string;
source_reliability: string;
source_transparency: string;
source_image: string;
articles_count: number;
source_crawled_at: string | null;
articles_metadata_available: number;
source_is_followed: boolean;
};
publicationGraph: PublicationEntry[];
categoryShares: CategoryShare[];
}
export interface SourceStatisticsRow {
sourceId: string;
sourceName: string;
sourceCrawledAt: string | null;
articlesCount: number;
articleMetadataAvailable: number;
}
export async function getSourceStatisticsList(db: Database): Promise<SourceStatisticsRow[]> {
const rows = await db
.select({
articleMetadataAvailable: sql<number>`sum
(CASE WHEN ${articles.metadata} IS NOT NULL THEN 1 ELSE 0 END)`,
articlesCount: sql<number>`count
(${articles.id})`,
sourceCrawledAt: sql<string | null>`max
(${articles.crawledAt})`,
sourceId: sources.id,
sourceName: sources.name,
})
.from(sources)
.leftJoin(articles, eq(articles.sourceId, sources.id))
.groupBy(sources.id, sources.name)
.orderBy(sources.name.asc());
return rows.map((row) => ({
articleMetadataAvailable: Number(row.articleMetadataAvailable ?? 0),
articlesCount: Number(row.articlesCount ?? 0),
sourceCrawledAt: row.sourceCrawledAt,
sourceId: row.sourceId,
sourceName: row.sourceName,
}));
}
export interface PublicationDateParams {
source: string;
category?: string | null;
}
async function selectPublicationBoundary(
db: Database,
fn: "min" | "max",
params: PublicationDateParams,
): Promise<string> {
const conditions: SQL[] = [eq(sources.name, params.source)];
if (params.category) {
conditions.push(sql`${params.category} = ANY(${articles.categories})`);
}
const whereClause = conditions.length > 1 ? and(...conditions) : conditions[0];
const [result] = await db
.select({
boundary:
fn === "min"
? sql<string | null>`min
(${articles.publishedAt})`
: sql<string | null>`max
(${articles.publishedAt})`,
})
.from(articles)
.innerJoin(sources, eq(articles.sourceId, sources.id))
.where(whereClause);
return result?.boundary ?? new Date().toISOString();
}
export async function getEarliestPublicationDate(
db: Database,
params: PublicationDateParams,
): Promise<string> {
return selectPublicationBoundary(db, "min", params);
}
export async function getLatestPublicationDate(
db: Database,
params: PublicationDateParams,
): Promise<string> {
return selectPublicationBoundary(db, "max", params);
}
function buildFollowExistsExpression(userId: string): SQL<boolean> {
return sql`EXISTS
(SELECT 1
FROM ${followedSources} f
WHERE f.sourceId = ${sources.id}
AND f.follower_id = ${userId})`;
}
export async function getSourceOverviewList(
db: Database,
params: { userId: string; page?: PageRequest },
): Promise<SourceOverviewResult> {
const page = createPageState(params.page);
const followExpression = buildFollowExistsExpression(params.userId);
let query = db
.select({
source_created_at: sources.createdAt,
source_display_name: sources.displayName,
source_image: sql<string>`('${SOURCE_IMAGE_BASE}' || ${sources.name} || '.png')`,
source_is_followed: followExpression,
source_name: sources.name,
sourceId: sources.id,
sourceUrl: sources.url,
})
.from(sources);
const cursor = decodeCursor(page.cursor);
if (cursor?.date) {
query = query.where(
or(
lt(sources.createdAt, cursor.date),
and(eq(sources.createdAt, cursor.date), lt(sources.id, cursor.id)),
),
);
}
const rows = await query.orderBy(desc(sources.createdAt), desc(sources.id)).limit(page.limit + 1);
return buildPaginationResult(rows, page, {
date: "source_created_at",
id: "sourceId",
});
}
function createBackwardDateRange(days: number): { start: number; end: number } {
const now = new Date();
const end = Math.floor((now.getTime() + 86_400_000) / 1000);
const startDate = new Date(now.getTime() - days * 86_400_000);
const start = Math.floor(startDate.getTime() / 1000);
return { end, start };
}
async function fetchPublicationGraph(db: Database, sourceId: string): Promise<PublicationEntry[]> {
const range = createBackwardDateRange(PUBLICATION_GRAPH_DAYS);
const rows = await db
.select({
count: sql<number>`count
(${articles.id})`,
day: sql<string>`date
(${articles.publishedAt})`,
})
.from(articles)
.where(eq(articles.sourceId, sourceId))
.where(
sql`${articles.publishedAt} BETWEEN to_timestamp(
${range.start}
)
AND
to_timestamp
(
${range.end}
)`,
)
.groupBy(sql`date
(${articles.publishedAt})`)
.orderBy(sql`date
(${articles.publishedAt})`);
const counts = new Map<string, number>();
for (const row of rows) {
counts.set(row.day, Number(row.count ?? 0));
}
const entries: PublicationEntry[] = [];
const start = new Date(range.start * 1000);
const end = new Date(range.end * 1000);
for (let date = new Date(start.getTime()); date < end; date.setUTCDate(date.getUTCDate() + 1)) {
const day = date.toISOString().slice(0, 10);
entries.push({ count: counts.get(day) ?? 0, day });
}
return entries;
}
async function fetchCategoryShares(db: Database, sourceId: string): Promise<CategoryShare[]> {
const rows = await db
.select({
categories: sql<string | null>`array_to_string
(${articles.categories}, ',')`,
})
.from(articles)
.where(eq(articles.sourceId, sourceId));
const counts = new Map<string, number>();
for (const row of rows) {
if (!row.categories) continue;
for (const category of row.categories.split(",")) {
const normalized = category.trim();
if (normalized.length === 0) continue;
counts.set(normalized, (counts.get(normalized) ?? 0) + 1);
}
}
const total = Array.from(counts.values()).reduce((acc, value) => acc + value, 0);
const shares: CategoryShare[] = Array.from(counts.entries()).map(([category, count]) => ({
category,
count,
percentage: total > 0 ? Math.round((count / total) * 10000) / 100 : 0,
}));
shares.sort((a, b) => b.count - a.count);
return shares;
}
export async function getSourceDetails(
db: Database,
params: { sourceId: string; userId: string },
): Promise<SourceDetailsResult | null> {
const followExpression = buildFollowExistsExpression(params.userId);
const [row] = await db
.select({
articles_count: sql<number>`count
(${articles.id})`,
articles_metadata_available: sql<number>`count
(*)
FILTER (WHERE
${articles.metadata}
IS
NOT
NULL
)`,
source_bias: sources.bias,
source_crawled_at: sql<string | null>`max
(${articles.crawledAt})`,
source_description: sources.description,
source_display_name: sources.displayName,
source_image: sql<string>`('${SOURCE_IMAGE_BASE}' || ${sources.name} || '.png')`,
source_is_followed: followExpression,
source_name: sources.name,
source_reliability: sources.reliability,
source_transparency: sources.transparency,
source_updated_at: sources.updatedAt,
sourceId: sources.id,
sourceUrl: sources.url,
})
.from(sources)
.leftJoin(articles, eq(articles.sourceId, sources.id))
.where(eq(sources.id, params.sourceId))
.groupBy(
sources.id,
sources.name,
sources.description,
sources.url,
sources.updatedAt,
sources.displayName,
sources.bias,
sources.reliability,
sources.transparency,
)
.limit(1);
if (!row) {
return null;
}
const [publicationGraph, categoryShares] = await Promise.all([
fetchPublicationGraph(db, params.sourceId),
fetchCategoryShares(db, params.sourceId),
]);
return {
categoryShares,
publicationGraph,
source: {
...row,
articles_count: Number(row.articles_count ?? 0),
articles_metadata_available: Number(row.articles_metadata_available ?? 0),
},
};
}
-31
View File
@@ -1,31 +0,0 @@
import { eq } from "drizzle-orm";
import type { Database } from "@/client";
import { users } from "@/schema";
export interface UserProfileRow {
user_id: string;
user_name: string;
user_email: string;
user_created_at: string;
user_updated_at: string | null;
}
export async function getUserProfile(
db: Database,
params: { userId: string },
): Promise<UserProfileRow | null> {
const [row] = await db
.select({
user_created_at: users.createdAt,
user_email: users.email,
user_id: users.id,
user_name: users.name,
user_updated_at: users.updatedAt,
})
.from(users)
.where(eq(users.id, params.userId))
.limit(1);
return row ?? null;
}