Facturas de cliente

This commit is contained in:
David Arranz 2025-09-16 11:17:29 +02:00
parent d5c6079d26
commit 2df2ce7083
10 changed files with 185 additions and 155 deletions

View File

@ -61,9 +61,7 @@ export abstract class ExpressController {
await this.executeImpl(); await this.executeImpl();
} catch (error: unknown) { } catch (error: unknown) {
const err = error as Error; this.handleError(error as Error);
console.debug("❌ Unhandled error executing controller:", err.message);
this.handleError(new InternalApiError(err.message));
} }
} }

View File

@ -1,111 +0,0 @@
import { Collection, Result } from "@repo/rdx-utils";
import { Model } from "sequelize";
export type MapperParamsType = Record<string, unknown>;
interface IDomainMapper<TModel extends Model, TEntity> {
mapToDomain(source: TModel, params?: MapperParamsType): Result<TEntity, Error>;
mapArrayToDomain(source: TModel[], params?: MapperParamsType): Result<Collection<TEntity>, Error>;
mapArrayAndCountToDomain(
source: TModel[],
totalCount: number,
params?: MapperParamsType
): Result<Collection<TEntity>, Error>;
}
interface IPersistenceMapper<TModelAttributes, TEntity> {
mapToPersistence(source: TEntity, params?: MapperParamsType): TModelAttributes;
mapCollectionToPersistence(
source: Collection<TEntity>,
params?: MapperParamsType
): TModelAttributes[];
}
export interface ISequelizeMapper<TModel extends Model, TModelAttributes, TEntity>
extends IDomainMapper<TModel, TEntity>,
IPersistenceMapper<TModelAttributes, TEntity> {}
export abstract class SequelizeMapper<TModel extends Model, TModelAttributes, TEntity>
implements ISequelizeMapper<TModel, TModelAttributes, TEntity>
{
public abstract mapToDomain(source: TModel, params?: MapperParamsType): Result<TEntity, Error>;
public mapArrayToDomain(
source: TModel[],
params?: MapperParamsType
): Result<Collection<TEntity>, Error> {
const items = source ?? [];
return this.mapArrayAndCountToDomain(items, items.length, params);
}
public mapArrayAndCountToDomain(
source: TModel[],
totalCount: number,
params?: MapperParamsType
): Result<Collection<TEntity>, Error> {
const _source = source ?? [];
try {
if (_source.length === 0) {
return Result.ok(new Collection([], totalCount));
}
const items = _source.map(
(value, index) => this.mapToDomain(value, { index, ...params }).data
);
return Result.ok(new Collection(items, totalCount));
} catch (error) {
return Result.fail(error as Error);
}
}
public abstract mapToPersistence(source: TEntity, params?: MapperParamsType): TModelAttributes;
public mapCollectionToPersistence(
source: Collection<TEntity>,
params?: MapperParamsType
): TModelAttributes[] {
return source.map((value, index) => this.mapToPersistence(value, { index, ...params }));
}
protected safeMap<T>(operation: () => T, key: string): Result<T, Error> {
try {
return Result.ok(operation());
} catch (error: unknown) {
return Result.fail(error as Error);
}
}
protected mapsValue(
row: TModel,
key: string,
customMapFn: (value: any, params: MapperParamsType) => Result<any, Error>,
params: MapperParamsType = { defaultValue: null }
): Result<any, Error> {
return customMapFn(row?.dataValues[key] ?? params.defaultValue, params);
}
protected mapsAssociation(
row: TModel,
associationName: string,
customMapper: IDomainMapper<any, any>,
params: MapperParamsType = {}
): Result<any, Error> {
if (!customMapper) {
Result.fail(Error(`Custom mapper undefined for ${associationName}`));
}
const { filter, ...otherParams } = params;
let associationRows = row?.dataValues[associationName] ?? [];
if (filter) {
associationRows = Array.isArray(associationRows)
? associationRows.filter(filter)
: filter(associationRows);
}
return Array.isArray(associationRows)
? customMapper.mapArrayToDomain(associationRows, otherParams)
: customMapper.mapToDomain(associationRows, otherParams);
}
}

View File

@ -20,9 +20,13 @@ export abstract class SequelizeQueryMapper<TModel extends Model, TEntity>
return Result.ok(new Collection([], totalCount)); return Result.ok(new Collection([], totalCount));
} }
const items = _source.map( const items = _source.map((value, index) => {
(value, index) => this.mapToDTO(value as TModel, { index, ...params }).data const result = this.mapToDTO(value as TModel, { index, ...params });
); if (result.isFailure) {
throw result.error;
}
return result.data;
});
return Result.ok(new Collection(items, totalCount)); return Result.ok(new Collection(items, totalCount));
} catch (error) { } catch (error) {
return Result.fail(error as Error); return Result.fail(error as Error);

View File

@ -27,8 +27,13 @@ export class CustomerFullPresenter extends Presenter<Customer, GetCustomerByIdRe
postal_code: toEmptyString(address.postalCode, (value) => value.toPrimitive()), postal_code: toEmptyString(address.postalCode, (value) => value.toPrimitive()),
country: toEmptyString(address.country, (value) => value.toPrimitive()), country: toEmptyString(address.country, (value) => value.toPrimitive()),
email: toEmptyString(customer.email, (value) => value.toPrimitive()), email_primary: toEmptyString(customer.emailPrimary, (value) => value.toPrimitive()),
phone: toEmptyString(customer.phone, (value) => value.toPrimitive()), email_secondary: toEmptyString(customer.emailSecondary, (value) => value.toPrimitive()),
phone_primary: toEmptyString(customer.phonePrimary, (value) => value.toPrimitive()),
phone_secondary: toEmptyString(customer.phoneSecondary, (value) => value.toPrimitive()),
mobile_primary: toEmptyString(customer.mobilePrimary, (value) => value.toPrimitive()),
mobile_secondary: toEmptyString(customer.mobileSecondary, (value) => value.toPrimitive()),
fax: toEmptyString(customer.fax, (value) => value.toPrimitive()), fax: toEmptyString(customer.fax, (value) => value.toPrimitive()),
website: toEmptyString(customer.website, (value) => value.toPrimitive()), website: toEmptyString(customer.website, (value) => value.toPrimitive()),

View File

@ -29,8 +29,13 @@ export class ListCustomersPresenter extends Presenter {
postal_code: toEmptyString(address.postalCode, (value) => value.toPrimitive()), postal_code: toEmptyString(address.postalCode, (value) => value.toPrimitive()),
country: toEmptyString(address.country, (value) => value.toPrimitive()), country: toEmptyString(address.country, (value) => value.toPrimitive()),
email: toEmptyString(customer.email, (value) => value.toPrimitive()), email_primary: toEmptyString(customer.emailPrimary, (value) => value.toPrimitive()),
phone: toEmptyString(customer.phone, (value) => value.toPrimitive()), email_secondary: toEmptyString(customer.emailSecondary, (value) => value.toPrimitive()),
phone_primary: toEmptyString(customer.phonePrimary, (value) => value.toPrimitive()),
phone_secondary: toEmptyString(customer.phoneSecondary, (value) => value.toPrimitive()),
mobile_primary: toEmptyString(customer.mobilePrimary, (value) => value.toPrimitive()),
mobile_secondary: toEmptyString(customer.mobileSecondary, (value) => value.toPrimitive()),
fax: toEmptyString(customer.fax, (value) => value.toPrimitive()), fax: toEmptyString(customer.fax, (value) => value.toPrimitive()),
website: toEmptyString(customer.website, (value) => value.toPrimitive()), website: toEmptyString(customer.website, (value) => value.toPrimitive()),

View File

@ -28,8 +28,12 @@ export interface CustomerProps {
address: PostalAddress; address: PostalAddress;
email: Maybe<EmailAddress>; emailPrimary: Maybe<EmailAddress>;
phone: Maybe<PhoneNumber>; emailSecondary: Maybe<EmailAddress>;
phonePrimary: Maybe<PhoneNumber>;
phoneSecondary: Maybe<PhoneNumber>;
mobilePrimary: Maybe<PhoneNumber>;
mobileSecondary: Maybe<PhoneNumber>;
fax: Maybe<PhoneNumber>; fax: Maybe<PhoneNumber>;
website: Maybe<URLAddress>; website: Maybe<URLAddress>;
@ -114,12 +118,28 @@ export class Customer extends AggregateRoot<CustomerProps> {
return this.props.address; return this.props.address;
} }
public get email(): Maybe<EmailAddress> { public get emailPrimary(): Maybe<EmailAddress> {
return this.props.email; return this.props.emailPrimary;
} }
public get phone(): Maybe<PhoneNumber> { public get emailSecondary(): Maybe<EmailAddress> {
return this.props.phone; return this.props.emailSecondary;
}
public get phonePrimary(): Maybe<PhoneNumber> {
return this.props.phonePrimary;
}
public get phoneSecondary(): Maybe<PhoneNumber> {
return this.props.phoneSecondary;
}
public get mobilePrimary(): Maybe<PhoneNumber> {
return this.props.mobilePrimary;
}
public get mobileSecondary(): Maybe<PhoneNumber> {
return this.props.mobileSecondary;
} }
public get fax(): Maybe<PhoneNumber> { public get fax(): Maybe<PhoneNumber> {

View File

@ -106,15 +106,39 @@ export class CustomerDomainMapper
errors errors
); );
const emailAddress = extractOrPushError( const emailPrimaryAddress = extractOrPushError(
maybeFromNullableVO(source.email, (value) => EmailAddress.create(value)), maybeFromNullableVO(source.email_primary, (value) => EmailAddress.create(value)),
"email", "email_primary",
errors errors
); );
const phoneNumber = extractOrPushError( const emailSecondaryAddress = extractOrPushError(
maybeFromNullableVO(source.phone, (value) => PhoneNumber.create(value)), maybeFromNullableVO(source.email_secondary, (value) => EmailAddress.create(value)),
"phone", "email_secondary",
errors
);
const phonePrimaryNumber = extractOrPushError(
maybeFromNullableVO(source.phone_primary, (value) => PhoneNumber.create(value)),
"phone_primary",
errors
);
const phoneSecondaryNumber = extractOrPushError(
maybeFromNullableVO(source.phone_secondary, (value) => PhoneNumber.create(value)),
"phone_secondary",
errors
);
const mobilePrimaryNumber = extractOrPushError(
maybeFromNullableVO(source.mobile_primary, (value) => PhoneNumber.create(value)),
"mobile_primary",
errors
);
const mobileSecondaryNumber = extractOrPushError(
maybeFromNullableVO(source.mobile_secondary, (value) => PhoneNumber.create(value)),
"mobile_secondary",
errors errors
); );
@ -192,8 +216,12 @@ export class CustomerDomainMapper
address: postalAddress!, address: postalAddress!,
email: emailAddress!, emailPrimary: emailPrimaryAddress!,
phone: phoneNumber!, emailSecondary: emailSecondaryAddress!,
phonePrimary: phonePrimaryNumber!,
phoneSecondary: phoneSecondaryNumber!,
mobilePrimary: mobilePrimaryNumber!,
mobileSecondary: mobileSecondaryNumber!,
fax: faxNumber!, fax: faxNumber!,
website: website!, website: website!,
@ -209,7 +237,10 @@ export class CustomerDomainMapper
} }
} }
public mapToPersistence(source: Customer, params?: MapperParamsType): CustomerCreationAttributes { public mapToPersistence(
source: Customer,
params?: MapperParamsType
): Result<CustomerCreationAttributes, Error> {
const customerValues: Partial<CustomerCreationAttributes> = { const customerValues: Partial<CustomerCreationAttributes> = {
id: source.id.toPrimitive(), id: source.id.toPrimitive(),
company_id: source.companyId.toPrimitive(), company_id: source.companyId.toPrimitive(),
@ -220,8 +251,12 @@ export class CustomerDomainMapper
trade_name: toNullable(source.tradeName, (tradeName) => tradeName.toPrimitive()), trade_name: toNullable(source.tradeName, (tradeName) => tradeName.toPrimitive()),
tin: toNullable(source.tin, (tin) => tin.toPrimitive()), tin: toNullable(source.tin, (tin) => tin.toPrimitive()),
email: toNullable(source.email, (email) => email.toPrimitive()), email_primary: toNullable(source.emailPrimary, (email) => email.toPrimitive()),
phone: toNullable(source.phone, (phone) => phone.toPrimitive()), email_secondary: toNullable(source.emailSecondary, (email) => email.toPrimitive()),
phone_primary: toNullable(source.phonePrimary, (phone) => phone.toPrimitive()),
phone_secondary: toNullable(source.phoneSecondary, (phone) => phone.toPrimitive()),
mobile_primary: toNullable(source.mobilePrimary, (mobile) => mobile.toPrimitive()),
mobile_secondary: toNullable(source.mobileSecondary, (mobile) => mobile.toPrimitive()),
fax: toNullable(source.fax, (fax) => fax.toPrimitive()), fax: toNullable(source.fax, (fax) => fax.toPrimitive()),
website: toNullable(source.website, (website) => website.toPrimitive()), website: toNullable(source.website, (website) => website.toPrimitive()),
@ -246,6 +281,6 @@ export class CustomerDomainMapper
}); });
} }
return customerValues as CustomerCreationAttributes; return Result.ok<CustomerCreationAttributes>(customerValues as CustomerCreationAttributes);
} }
} }

View File

@ -43,8 +43,13 @@ export type CustomerListDTO = {
address: PostalAddress; address: PostalAddress;
email: Maybe<EmailAddress>; email_primary: Maybe<EmailAddress>;
phone: Maybe<PhoneNumber>; email_secondary: Maybe<EmailAddress>;
phone_primary: Maybe<PhoneNumber>;
phone_secondary: Maybe<PhoneNumber>;
mobile_primary: Maybe<PhoneNumber>;
mobile_secondary: Maybe<PhoneNumber>;
fax: Maybe<PhoneNumber>; fax: Maybe<PhoneNumber>;
website: Maybe<URLAddress>; website: Maybe<URLAddress>;
@ -124,15 +129,39 @@ export class CustomerListMapper
errors errors
); );
const emailAddress = extractOrPushError( const emailPrimaryAddress = extractOrPushError(
maybeFromNullableVO(raw.email, (value) => EmailAddress.create(value)), maybeFromNullableVO(raw.email_primary, (value) => EmailAddress.create(value)),
"email", "email_primary",
errors errors
); );
const phoneNumber = extractOrPushError( const emailSecondaryAddress = extractOrPushError(
maybeFromNullableVO(raw.phone, (value) => PhoneNumber.create(value)), maybeFromNullableVO(raw.email_secondary, (value) => EmailAddress.create(value)),
"phone", "email_secondary",
errors
);
const phonePrimaryNumber = extractOrPushError(
maybeFromNullableVO(raw.phone_primary, (value) => PhoneNumber.create(value)),
"phone_primary",
errors
);
const phoneSecondaryNumber = extractOrPushError(
maybeFromNullableVO(raw.phone_secondary, (value) => PhoneNumber.create(value)),
"phone_secondary",
errors
);
const mobilePrimaryNumber = extractOrPushError(
maybeFromNullableVO(raw.mobile_primary, (value) => PhoneNumber.create(value)),
"mobile_primary",
errors
);
const mobileSecondaryNumber = extractOrPushError(
maybeFromNullableVO(raw.mobile_secondary, (value) => PhoneNumber.create(value)),
"mobile_secondary",
errors errors
); );
@ -200,8 +229,12 @@ export class CustomerListMapper
address: postalAddress!, address: postalAddress!,
email: emailAddress!, email_primary: emailPrimaryAddress!,
phone: phoneNumber!, email_secondary: emailSecondaryAddress!,
phone_primary: phonePrimaryNumber!,
phone_secondary: phoneSecondaryNumber!,
mobile_primary: mobilePrimaryNumber!,
mobile_secondary: mobileSecondaryNumber!,
fax: faxNumber!, fax: faxNumber!,
website: website!, website: website!,

View File

@ -27,8 +27,18 @@ export class CustomerModel extends Model<
declare postal_code: string; declare postal_code: string;
declare country: string; declare country: string;
declare email: string; // Correos electrónicos
declare phone: string; declare email_primary: string;
declare email_secondary: string;
// Teléfonos fijos
declare phone_primary: string;
declare phone_secondary: string;
// Móviles
declare mobile_primary: string;
declare mobile_secondary: string;
declare fax: string; declare fax: string;
declare website: string; declare website: string;
@ -113,7 +123,7 @@ export default (database: Sequelize) => {
defaultValue: null, defaultValue: null,
}, },
email: { email_primary: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
@ -121,11 +131,37 @@ export default (database: Sequelize) => {
isEmail: true, isEmail: true,
}, },
}, },
phone: { email_secondary: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
validate: {
isEmail: true,
},
},
phone_primary: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
}, },
phone_secondary: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
mobile_primary: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
mobile_secondary: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
fax: { fax: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: true, allowNull: true,

View File

@ -18,8 +18,13 @@ export const GetCustomerByIdResponseSchema = z.object({
postal_code: z.string(), postal_code: z.string(),
country: z.string(), country: z.string(),
email: z.string(), email_primary: z.string(),
phone: z.string(), email_secondary: z.string(),
phone_primary: z.string(),
phone_secondary: z.string(),
mobile_primary: z.string(),
mobile_secondary: z.string(),
fax: z.string(), fax: z.string(),
website: z.string(), website: z.string(),