feat(F-091): completed feature

This commit is contained in:
chattie
2026-08-20 21:47:31 +02:00
parent d29c9b71c9
commit 4d0970df5d
12 changed files with 204 additions and 61 deletions

View File

@@ -19,9 +19,11 @@ export class ProductBrandNotFoundError extends Error {
}
}
export type ProductVariantCode = 'sku' | 'ean';
export class ProductVariantCodeAlreadyExistsError extends Error {
constructor() {
super('Product variant SKU or EAN already exists');
constructor(public readonly field?: ProductVariantCode) {
super(field ? `Product variant ${field.toUpperCase()} already exists` : 'Product variant SKU or EAN already exists');
this.name = 'ProductVariantCodeAlreadyExistsError';
}
}

View File

@@ -1,5 +1,8 @@
import type pg from 'pg';
import { ProductVariantCodeAlreadyExistsError } from '../domain/errors.js';
import {
ProductVariantCodeAlreadyExistsError,
type ProductVariantCode,
} from '../domain/errors.js';
import type { ProductVariantRepository } from '../domain/ports.js';
import type { NewProductVariant, ProductVariant, ProductVariantPatch } from '../domain/variant.js';
@@ -47,7 +50,7 @@ export class PgProductVariantRepository implements ProductVariantRepository {
return toVariant(row);
} catch (error) {
if (isPgError(error, UNIQUE_VIOLATION)) {
throw new ProductVariantCodeAlreadyExistsError();
throw new ProductVariantCodeAlreadyExistsError(uniqueConstraintField(error));
}
throw error;
}
@@ -82,7 +85,7 @@ export class PgProductVariantRepository implements ProductVariantRepository {
return row ? toVariant(row) : undefined;
} catch (error) {
if (isPgError(error, UNIQUE_VIOLATION)) {
throw new ProductVariantCodeAlreadyExistsError();
throw new ProductVariantCodeAlreadyExistsError(uniqueConstraintField(error));
}
throw error;
}
@@ -116,3 +119,14 @@ function toVariant(row: VariantRow): ProductVariant {
function isPgError(error: unknown, code: string): boolean {
return typeof error === 'object' && error !== null && 'code' in error && error.code === code;
}
function uniqueConstraintField(error: unknown): ProductVariantCode | undefined {
if (typeof error !== 'object' || error === null || !('constraint' in error)) {
return undefined;
}
const constraint = error.constraint;
if (typeof constraint !== 'string') return undefined;
if (constraint.endsWith('_sku_key')) return 'sku';
if (constraint.endsWith('_ean_key')) return 'ean';
return undefined;
}