mirror of
https://git.meshkee.com/Meshkee/backend.git
synced 2026-08-11 22:30:59 +04:30
Allow null price and unlimited stock on store item create/sync.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
5cd762c65d
commit
b5a54e4174
@@ -8,6 +8,7 @@ import {
|
||||
IsString,
|
||||
Min,
|
||||
MinLength,
|
||||
ValidateIf,
|
||||
ValidateNested,
|
||||
} from 'class-validator';
|
||||
|
||||
@@ -21,21 +22,34 @@ export class StoreItemVariantSelectionDto {
|
||||
optionId!: string;
|
||||
}
|
||||
|
||||
/** Keep null as null; avoid `Number(null) === 0`. */
|
||||
function nullableNumber({ value }: { value: unknown }) {
|
||||
if (value === null || value === undefined || value === '') return null;
|
||||
const n = typeof value === 'number' ? value : Number(value);
|
||||
return Number.isFinite(n) ? n : value;
|
||||
}
|
||||
|
||||
export class CreateStoreItemVariantDto {
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => StoreItemVariantSelectionDto)
|
||||
selections!: StoreItemVariantSelectionDto[];
|
||||
|
||||
/** null / omitted = contact for price */
|
||||
@IsOptional()
|
||||
@Transform(nullableNumber)
|
||||
@ValidateIf((_, v) => v !== null && v !== undefined)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
price?: number;
|
||||
price?: number | null;
|
||||
|
||||
/** null / omitted = unlimited stock */
|
||||
@IsOptional()
|
||||
@Transform(nullableNumber)
|
||||
@ValidateIf((_, v) => v !== null && v !== undefined)
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
stockQuantity?: number;
|
||||
stockQuantity?: number | null;
|
||||
}
|
||||
|
||||
export class BatchCreateStoreItemsDto {
|
||||
@@ -117,14 +131,18 @@ export class ListPublicStoreItemsDto {
|
||||
|
||||
export class UpdateStoreItemVariantDto {
|
||||
@IsOptional()
|
||||
@Transform(nullableNumber)
|
||||
@ValidateIf((_, v) => v !== null && v !== undefined)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
price?: number;
|
||||
price?: number | null;
|
||||
|
||||
@IsOptional()
|
||||
@Transform(nullableNumber)
|
||||
@ValidateIf((_, v) => v !== null && v !== undefined)
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
stockQuantity?: number;
|
||||
stockQuantity?: number | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@@ -167,13 +185,19 @@ export class SyncStoreItemVariantRowDto {
|
||||
@Type(() => StoreItemVariantSelectionDto)
|
||||
selections!: StoreItemVariantSelectionDto[];
|
||||
|
||||
/** null = contact for price */
|
||||
@Transform(nullableNumber)
|
||||
@ValidateIf((_, v) => v !== null && v !== undefined)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
price!: number;
|
||||
price!: number | null;
|
||||
|
||||
/** null = unlimited stock */
|
||||
@Transform(nullableNumber)
|
||||
@ValidateIf((_, v) => v !== null && v !== undefined)
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
stockQuantity!: number;
|
||||
stockQuantity!: number | null;
|
||||
}
|
||||
|
||||
export class SyncProductStoreItemVariantsDto {
|
||||
|
||||
@@ -916,11 +916,11 @@ export class StoreItemsService {
|
||||
existingKeys.add(key);
|
||||
batchKeys.add(key);
|
||||
|
||||
if (item.price !== undefined && item.price < 0) {
|
||||
if (item.price != null && item.price < 0) {
|
||||
throw new BadRequestException('Price cannot be negative');
|
||||
}
|
||||
|
||||
if (item.stockQuantity !== undefined && item.stockQuantity < 0) {
|
||||
if (item.stockQuantity != null && item.stockQuantity < 0) {
|
||||
throw new BadRequestException('Stock cannot be negative');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user