import { PartialType } from '@nestjs/mapped-types'; import { StoreStatus } from '@prisma/client'; import { Transform } from 'class-transformer'; import { IsEmail, IsEnum, IsIn, IsInt, IsOptional, IsString, Length, Matches, Max, MaxLength, Min, Validate, ValidatorConstraint, ValidatorConstraintInterface, } from 'class-validator'; import { isValidSiret } from '../../common/siret.util'; const trim = ({ value }: { value: unknown }) => (typeof value === 'string' ? value.trim() : value); /** « 123 456 789 00018 » → « 12345678900018 » */ const digitsOnly = ({ value }: { value: unknown }) => (typeof value === 'string' ? value.replace(/\s/g, '') : value); @ValidatorConstraint({ name: 'siret' }) class SiretConstraint implements ValidatorConstraintInterface { validate(value: unknown) { return typeof value === 'string' && isValidSiret(value); } defaultMessage() { return 'SIRET invalide (14 chiffres, clé de contrôle incorrecte)'; } } export class CreateStoreDto { @Transform(trim) @IsString() @Length(2, 120) name!: string; @Transform(digitsOnly) @Validate(SiretConstraint) siret!: string; @Transform(trim) @IsString() @Length(3, 200) addressLine!: string; @Transform(digitsOnly) @Matches(/^\d{5}$/, { message: 'Code postal : 5 chiffres' }) postalCode!: string; @Transform(trim) @IsString() @Length(1, 120) city!: string; @Transform(trim) @IsString() @Length(2, 120) contactName!: string; @Transform(trim) @IsEmail() contactEmail!: string; @Transform(digitsOnly) @Matches(/^(\+33|0)[1-9]\d{8}$/, { message: 'Téléphone invalide' }) contactPhone!: string; /** Plafond d'une opération, en euros entiers (converti en centimes). */ @IsInt() @Min(1) @Max(10_000) maxAmountEuros!: number; /** Délai minimum entre deux crédits d'un même client, en minutes. */ @IsInt() @Min(0) @Max(24 * 60) minScanIntervalMin!: number; } export class UpdateStoreDto extends PartialType(CreateStoreDto) {} export class SetStoreStatusDto { @IsEnum(StoreStatus) status!: StoreStatus; } export class ListStoresQuery { @IsOptional() @IsString() @MaxLength(100) q?: string; @IsOptional() @IsIn(Object.values(StoreStatus)) status?: StoreStatus; }