Show newest portfolios first on dashboard and website lists.

Order by sort_order descending and assign new items the next highest sort value so create and move-up stay consistent.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Alireza Hassani
2026-08-23 15:01:52 +03:30
co-authored by Cursor
parent ea40ae40d8
commit af0573c089
3 changed files with 23 additions and 13 deletions
+21 -11
View File
@@ -41,6 +41,13 @@ const portfolioInclude = {
media: true,
} satisfies Prisma.portfoliosInclude;
/** Higher `sort_order` first (newest / moved-up), then newest dates. */
const portfolioListOrderBy = [
{ sort_order: 'desc' },
{ published_at: 'desc' },
{ created_at: 'desc' },
] as const;
@Injectable()
export class PortfoliosService {
constructor(
@@ -64,11 +71,7 @@ export class PortfoliosService {
const [items, total] = await Promise.all([
this.prisma.portfolios.findMany({
where,
orderBy: [
{ sort_order: 'asc' },
{ published_at: 'desc' },
{ created_at: 'desc' },
],
orderBy: [...portfolioListOrderBy],
skip,
take: pageSize,
include: portfolioInclude,
@@ -143,6 +146,9 @@ export class PortfoliosService {
);
}
const sortOrder =
dto.sortOrder ?? (await this.nextSortOrder(businessId));
const created = await this.prisma.$transaction(async (tx) => {
const portfolio = await tx.portfolios.create({
data: {
@@ -155,7 +161,7 @@ export class PortfoliosService {
content: this.buildContent(dto.mainTextHtml) as Prisma.InputJsonValue,
status,
featured_media_id: featuredMediaId,
sort_order: dto.sortOrder ?? 0,
sort_order: sortOrder,
published_at: status === ContentStatus.published ? new Date() : null,
metadata: this.buildMetadata(dto.tags) as Prisma.InputJsonValue,
},
@@ -419,11 +425,7 @@ export class PortfoliosService {
const [items, total] = await Promise.all([
this.prisma.portfolios.findMany({
where,
orderBy: [
{ sort_order: 'asc' },
{ published_at: 'desc' },
{ created_at: 'desc' },
],
orderBy: [...portfolioListOrderBy],
skip,
take: pageSize,
include: portfolioInclude,
@@ -827,6 +829,14 @@ export class PortfoliosService {
};
}
private async nextSortOrder(businessId: bigint): Promise<number> {
const result = await this.prisma.portfolios.aggregate({
where: { business_id: businessId },
_max: { sort_order: true },
});
return (result._max.sort_order ?? -1) + 1;
}
private asRecord(value: Prisma.JsonValue): Record<string, unknown> {
if (value && typeof value === 'object' && !Array.isArray(value)) {
return value as Record<string, unknown>;