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';
|
import { getAppConfig } from '../../../config';
|
||||||
|
|
||||||
@@ -22,4 +22,9 @@ export class UpdateProductDto {
|
|||||||
@IsArray()
|
@IsArray()
|
||||||
@IsUUID('4', { each: true })
|
@IsUUID('4', { each: true })
|
||||||
categoryIds: string[];
|
categoryIds: string[];
|
||||||
|
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsInt()
|
||||||
|
@Min(0)
|
||||||
|
sortOrder: number;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ export class Product {
|
|||||||
@Column({ type: 'text', default: '' })
|
@Column({ type: 'text', default: '' })
|
||||||
descriptionHtml: string;
|
descriptionHtml: string;
|
||||||
|
|
||||||
|
@Column({ type: 'integer', default: 0 })
|
||||||
|
sortOrder: number;
|
||||||
|
|
||||||
@OneToMany(() => ProductVariant, variant => variant.product)
|
@OneToMany(() => ProductVariant, variant => variant.product)
|
||||||
variants: ProductVariant[];
|
variants: ProductVariant[];
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ export class ProductsService {
|
|||||||
|
|
||||||
const [products, total] = await this.productRepo.findAndCount({
|
const [products, total] = await this.productRepo.findAndCount({
|
||||||
where: trimmedSearch ? { title: ILike(`%${trimmedSearch}%`) } : {},
|
where: trimmedSearch ? { title: ILike(`%${trimmedSearch}%`) } : {},
|
||||||
order: { createdAt: 'DESC' },
|
order: { sortOrder: 'ASC', createdAt: 'DESC' },
|
||||||
skip: (page - 1) * limit,
|
skip: (page - 1) * limit,
|
||||||
take: limit
|
take: limit
|
||||||
});
|
});
|
||||||
@@ -56,6 +56,7 @@ export class ProductsService {
|
|||||||
where: { id: In(productIds) },
|
where: { id: In(productIds) },
|
||||||
relations: ['categories', 'variants'],
|
relations: ['categories', 'variants'],
|
||||||
order: {
|
order: {
|
||||||
|
sortOrder: 'ASC',
|
||||||
createdAt: 'DESC',
|
createdAt: 'DESC',
|
||||||
variants: { sortOrder: 'ASC', createdAt: 'ASC' }
|
variants: { sortOrder: 'ASC', createdAt: 'ASC' }
|
||||||
}
|
}
|
||||||
@@ -125,6 +126,7 @@ export class ProductsService {
|
|||||||
where: { id: In(uniqueIds) },
|
where: { id: In(uniqueIds) },
|
||||||
relations: ['variants'],
|
relations: ['variants'],
|
||||||
order: {
|
order: {
|
||||||
|
sortOrder: 'ASC',
|
||||||
createdAt: 'DESC',
|
createdAt: 'DESC',
|
||||||
variants: { sortOrder: 'ASC', createdAt: 'ASC' }
|
variants: { sortOrder: 'ASC', createdAt: 'ASC' }
|
||||||
}
|
}
|
||||||
@@ -164,7 +166,7 @@ export class ProductsService {
|
|||||||
|
|
||||||
async update(
|
async update(
|
||||||
id: string,
|
id: string,
|
||||||
{ title, descriptionHtml, isDraft, categoryIds }: UpdateProductDto
|
{ title, descriptionHtml, isDraft, categoryIds, sortOrder }: UpdateProductDto
|
||||||
): Promise<ProductWithVariantsExtended> {
|
): Promise<ProductWithVariantsExtended> {
|
||||||
const existing = await this.productRepo.findOne({
|
const existing = await this.productRepo.findOne({
|
||||||
where: { id }
|
where: { id }
|
||||||
@@ -177,6 +179,7 @@ export class ProductsService {
|
|||||||
existing.title = title.trim();
|
existing.title = title.trim();
|
||||||
existing.isDraft = isDraft;
|
existing.isDraft = isDraft;
|
||||||
existing.descriptionHtml = descriptionHtml;
|
existing.descriptionHtml = descriptionHtml;
|
||||||
|
existing.sortOrder = sortOrder;
|
||||||
existing.categories = await this.categoriesService.findByIds(categoryIds);
|
existing.categories = await this.categoriesService.findByIds(categoryIds);
|
||||||
|
|
||||||
await this.productRepo.save(existing);
|
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 () => {
|
it('returns an empty map when no variant ids are requested', async () => {
|
||||||
await expect(service.getStorefrontVariantsByIds([], new Map())).resolves.toEqual(new Map());
|
await expect(service.getStorefrontVariantsByIds([], new Map())).resolves.toEqual(new Map());
|
||||||
expect(variantRepo.find).not.toHaveBeenCalled();
|
expect(variantRepo.find).not.toHaveBeenCalled();
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ export class StorefrontProductsService {
|
|||||||
where: categoryId ? { isDraft: false, categories: { id: categoryId } } : { isDraft: false },
|
where: categoryId ? { isDraft: false, categories: { id: categoryId } } : { isDraft: false },
|
||||||
relations: ['variants', 'variants.images'],
|
relations: ['variants', 'variants.images'],
|
||||||
order: {
|
order: {
|
||||||
|
sortOrder: 'ASC',
|
||||||
createdAt: 'DESC',
|
createdAt: 'DESC',
|
||||||
variants: {
|
variants: {
|
||||||
sortOrder: 'ASC',
|
sortOrder: 'ASC',
|
||||||
@@ -87,7 +88,6 @@ export class StorefrontProductsService {
|
|||||||
where: { id, isDraft: false },
|
where: { id, isDraft: false },
|
||||||
relations: ['variants', 'variants.images'],
|
relations: ['variants', 'variants.images'],
|
||||||
order: {
|
order: {
|
||||||
createdAt: 'DESC',
|
|
||||||
variants: {
|
variants: {
|
||||||
sortOrder: 'ASC',
|
sortOrder: 'ASC',
|
||||||
createdAt: 'ASC',
|
createdAt: 'ASC',
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export interface Product {
|
|||||||
title: string;
|
title: string;
|
||||||
deliveryMode: DeliveryMode;
|
deliveryMode: DeliveryMode;
|
||||||
isDraft: boolean;
|
isDraft: boolean;
|
||||||
|
sortOrder: number;
|
||||||
descriptionHtml: string;
|
descriptionHtml: string;
|
||||||
variants?: ProductVariantExtended[];
|
variants?: ProductVariantExtended[];
|
||||||
categories?: Category[];
|
categories?: Category[];
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
export interface UpdateProductPayload {
|
export interface UpdateProductPayload {
|
||||||
title: string;
|
title: string;
|
||||||
isDraft: boolean;
|
isDraft: boolean;
|
||||||
|
sortOrder: number;
|
||||||
descriptionHtml: string;
|
descriptionHtml: string;
|
||||||
categoryIds: string[];
|
categoryIds: string[];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,15 @@
|
|||||||
/>
|
/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
|
||||||
|
<el-form-item label="Sort order" prop="sortOrder">
|
||||||
|
<el-input-number
|
||||||
|
v-model="productForm.sortOrder"
|
||||||
|
:min="0"
|
||||||
|
:step="1"
|
||||||
|
@update:model-value="productFormRef?.clearValidate('sortOrder')"
|
||||||
|
/>
|
||||||
|
</el-form-item>
|
||||||
|
|
||||||
<el-form-item label="Categories">
|
<el-form-item label="Categories">
|
||||||
<el-select
|
<el-select
|
||||||
v-model="selectedCategoryIds"
|
v-model="selectedCategoryIds"
|
||||||
@@ -161,7 +170,8 @@ const productFormRef = ref<FormInstance>();
|
|||||||
const productForm = reactive({
|
const productForm = reactive({
|
||||||
title: '',
|
title: '',
|
||||||
descriptionHtml: '',
|
descriptionHtml: '',
|
||||||
isDraft: true
|
isDraft: true,
|
||||||
|
sortOrder: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
const productSaving = ref(false);
|
const productSaving = ref(false);
|
||||||
@@ -175,6 +185,10 @@ const productRules: FormRules = {
|
|||||||
message: `At most ${validationProductTitleMaxLength} characters`,
|
message: `At most ${validationProductTitleMaxLength} characters`,
|
||||||
trigger: 'blur'
|
trigger: 'blur'
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
sortOrder: [
|
||||||
|
{ required: true, message: 'Sort order is required', trigger: 'change' },
|
||||||
|
{ type: 'integer', message: 'Sort order must be a whole number', trigger: 'change' }
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -200,6 +214,7 @@ onBeforeMount(async () => {
|
|||||||
productForm.title = currentProduct.value.title;
|
productForm.title = currentProduct.value.title;
|
||||||
productForm.descriptionHtml = currentProduct.value.descriptionHtml;
|
productForm.descriptionHtml = currentProduct.value.descriptionHtml;
|
||||||
productForm.isDraft = currentProduct.value.isDraft;
|
productForm.isDraft = currentProduct.value.isDraft;
|
||||||
|
productForm.sortOrder = currentProduct.value.sortOrder;
|
||||||
selectedCategoryIds.value = (currentProduct.value.categories ?? []).map(c => c.id);
|
selectedCategoryIds.value = (currentProduct.value.categories ?? []).map(c => c.id);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -236,6 +251,7 @@ const submitProduct = async (): Promise<void> => {
|
|||||||
title: productForm.title.trim(),
|
title: productForm.title.trim(),
|
||||||
descriptionHtml: productForm.descriptionHtml,
|
descriptionHtml: productForm.descriptionHtml,
|
||||||
isDraft: productForm.isDraft,
|
isDraft: productForm.isDraft,
|
||||||
|
sortOrder: productForm.sortOrder,
|
||||||
categoryIds: selectedCategoryIds.value
|
categoryIds: selectedCategoryIds.value
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,8 @@
|
|||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
|
|
||||||
|
<el-table-column prop="sortOrder" label="Order" width="80" />
|
||||||
|
|
||||||
<el-table-column label="Categories" min-width="160" show-overflow-tooltip>
|
<el-table-column label="Categories" min-width="160" show-overflow-tooltip>
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
{{ formatProductCategories(row.categories) }}
|
{{ formatProductCategories(row.categories) }}
|
||||||
|
|||||||
Reference in New Issue
Block a user