Add product sortOrder for admin-controlled storefront list order.
Storefront and CMS lists sort by sortOrder ascending with createdAt descending as tie-break; existing rows default to 0.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class AddProductSortOrder1785000000000 implements MigrationInterface {
|
||||
name = 'AddProductSortOrder1785000000000';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`ALTER TABLE "products" ADD "sortOrder" integer NOT NULL DEFAULT '0'`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`ALTER TABLE "products" DROP COLUMN "sortOrder"`);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IsArray, IsBoolean, IsNotEmpty, IsString, IsUUID, MaxLength } from 'class-validator';
|
||||
import { IsArray, IsBoolean, IsInt, IsNotEmpty, IsString, IsUUID, MaxLength, Min } from 'class-validator';
|
||||
|
||||
import { getAppConfig } from '../../../config';
|
||||
|
||||
@@ -22,4 +22,9 @@ export class UpdateProductDto {
|
||||
@IsArray()
|
||||
@IsUUID('4', { each: true })
|
||||
categoryIds: string[];
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
sortOrder: number;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@ export class Product {
|
||||
@Column({ type: 'text', default: '' })
|
||||
descriptionHtml: string;
|
||||
|
||||
@Column({ type: 'integer', default: 0 })
|
||||
sortOrder: number;
|
||||
|
||||
@OneToMany(() => ProductVariant, variant => variant.product)
|
||||
variants: ProductVariant[];
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ export class ProductsService {
|
||||
|
||||
const [products, total] = await this.productRepo.findAndCount({
|
||||
where: trimmedSearch ? { title: ILike(`%${trimmedSearch}%`) } : {},
|
||||
order: { createdAt: 'DESC' },
|
||||
order: { sortOrder: 'ASC', createdAt: 'DESC' },
|
||||
skip: (page - 1) * limit,
|
||||
take: limit
|
||||
});
|
||||
@@ -56,6 +56,7 @@ export class ProductsService {
|
||||
where: { id: In(productIds) },
|
||||
relations: ['categories', 'variants'],
|
||||
order: {
|
||||
sortOrder: 'ASC',
|
||||
createdAt: 'DESC',
|
||||
variants: { sortOrder: 'ASC', createdAt: 'ASC' }
|
||||
}
|
||||
@@ -125,6 +126,7 @@ export class ProductsService {
|
||||
where: { id: In(uniqueIds) },
|
||||
relations: ['variants'],
|
||||
order: {
|
||||
sortOrder: 'ASC',
|
||||
createdAt: 'DESC',
|
||||
variants: { sortOrder: 'ASC', createdAt: 'ASC' }
|
||||
}
|
||||
@@ -164,7 +166,7 @@ export class ProductsService {
|
||||
|
||||
async update(
|
||||
id: string,
|
||||
{ title, descriptionHtml, isDraft, categoryIds }: UpdateProductDto
|
||||
{ title, descriptionHtml, isDraft, categoryIds, sortOrder }: UpdateProductDto
|
||||
): Promise<ProductWithVariantsExtended> {
|
||||
const existing = await this.productRepo.findOne({
|
||||
where: { id }
|
||||
@@ -177,6 +179,7 @@ export class ProductsService {
|
||||
existing.title = title.trim();
|
||||
existing.isDraft = isDraft;
|
||||
existing.descriptionHtml = descriptionHtml;
|
||||
existing.sortOrder = sortOrder;
|
||||
existing.categories = await this.categoriesService.findByIds(categoryIds);
|
||||
|
||||
await this.productRepo.save(existing);
|
||||
|
||||
@@ -42,6 +42,19 @@ describe('StorefrontProductsService', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('orders storefront products by sortOrder then createdAt desc', async () => {
|
||||
await service.listStorefrontProducts(new Map(), undefined);
|
||||
|
||||
expect(productRepo.find).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
order: expect.objectContaining({
|
||||
sortOrder: 'ASC',
|
||||
createdAt: 'DESC'
|
||||
})
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('returns an empty map when no variant ids are requested', async () => {
|
||||
await expect(service.getStorefrontVariantsByIds([], new Map())).resolves.toEqual(new Map());
|
||||
expect(variantRepo.find).not.toHaveBeenCalled();
|
||||
|
||||
@@ -63,6 +63,7 @@ export class StorefrontProductsService {
|
||||
where: categoryId ? { isDraft: false, categories: { id: categoryId } } : { isDraft: false },
|
||||
relations: ['variants', 'variants.images'],
|
||||
order: {
|
||||
sortOrder: 'ASC',
|
||||
createdAt: 'DESC',
|
||||
variants: {
|
||||
sortOrder: 'ASC',
|
||||
@@ -87,7 +88,6 @@ export class StorefrontProductsService {
|
||||
where: { id, isDraft: false },
|
||||
relations: ['variants', 'variants.images'],
|
||||
order: {
|
||||
createdAt: 'DESC',
|
||||
variants: {
|
||||
sortOrder: 'ASC',
|
||||
createdAt: 'ASC',
|
||||
|
||||
Reference in New Issue
Block a user