Compare commits

...

2 Commits

Author SHA1 Message Date
7bf24c8f91 Facturas de cliente 2025-06-11 17:13:44 +02:00
484f0119a7 . 2025-06-11 15:13:21 +02:00
168 changed files with 2427 additions and 2236 deletions

12
.vscode/launch.json vendored
View File

@ -1,12 +1,20 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "WEB: Run in Development Mode",
"type": "node",
"request": "launch",
"runtimeExecutable": "pnpm",
"runtimeArgs": ["run", "dev", "--filter", "web"],
"console": "integratedTerminal"
},
{
"name": "SERVER: Run in Development Mode",
"type": "node",
"request": "launch",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "dev"],
"runtimeExecutable": "pnpm",
"runtimeArgs": ["run", "dev", "--filter", "server"],
"console": "integratedTerminal"
},
{

View File

@ -42,6 +42,7 @@
"dependencies": {
"@erp/core": "workspace:*",
"@erp/auth": "workspace:*",
"@erp/customer-invoices": "workspace:*",
"bcrypt": "^5.1.1",
"cls-rtracer": "^2.6.3",
"cors": "^2.8.5",

View File

@ -5,9 +5,12 @@ import os from "node:os";
import { createApp } from "./app";
import { ENV } from "./config";
import { tryConnectToDatabase } from "./config/database";
import { listRoutes } from "./lib";
import { initModules } from "./lib/modules";
import { registerModules } from "./register-modules";
const API_BASE_PATH = "/api/v1";
// Guardamos información del estado del servidor
export const currentState = {
launchedAt: DateTime.now(),
@ -129,7 +132,9 @@ process.on("uncaughtException", (error: Error) => {
// initStructure(sequelizeConn.connection);
// insertUsers();
await initModules({ app, database, baseRoutePath: "/api/v1", logger });
await initModules({ app, database, baseRoutePath: API_BASE_PATH, logger });
console.log(listRoutes(app._router, API_BASE_PATH));
server.listen(currentState.port, () => {
server.emit("listening");

View File

@ -1,2 +1,3 @@
export * from "./list-routes";
export * from "./logger";
export * from "./modules";

View File

@ -0,0 +1,30 @@
// Función para listar rutas
export function listRoutes(appOrRouter, basePath = "") {
const routes = [];
appOrRouter.stack.forEach((middleware) => {
if (middleware.route) {
// Es una ruta directa
const methods = Object.keys(middleware.route.methods).map((m) => m.toUpperCase());
routes.push({
path: basePath + middleware.route.path,
methods,
});
} else if (middleware.name === "router" && middleware.handle.stack) {
// Es un router anidado
const newBasePath =
basePath +
(middleware.regexp?.source !== "^\\/?$"
? middleware.regexp
?.toString()
.replace(/^\/\^\\/, "")
.replace(/\\\/\?\(\?=\\\/\|\$\)\/i$/, "")
.replace(/\\\//g, "/")
: "");
const childRoutes = listRoutes(middleware.handle, basePath + (middleware?.path || ""));
routes.push(...childRoutes);
}
});
return routes;
}

View File

@ -7,10 +7,10 @@ const registeredModules: Map<string, IModuleServer> = new Map();
const initializedModules = new Set<string>();
export function registerModule(pkg: IModuleServer) {
if (registeredModules.has(pkg.metadata.name)) {
throw new Error(`❌ Paquete "${pkg.metadata.name}" ya registrado.`);
if (registeredModules.has(pkg.name)) {
throw new Error(`❌ Paquete "${pkg.name}" ya registrado.`);
}
registeredModules.set(pkg.metadata.name, pkg);
registeredModules.set(pkg.name, pkg);
}
export async function initModules(params: ModuleParams) {
@ -27,7 +27,7 @@ const loadModule = (name: string, params: ModuleParams) => {
if (!pkg) throw new Error(`❌ Paquete "${name}" no encontrado.`);
// Resolver dependencias primero
const deps = pkg.metadata.dependencies || [];
const deps = pkg.dependencies || [];
deps.forEach((dep) => loadModule(dep, params));
// Inicializar el module
@ -44,7 +44,7 @@ const loadModule = (name: string, params: ModuleParams) => {
if (pkgApi?.services) {
const services = pkgApi.services;
if (services && typeof services === "object") {
registerService(pkg.metadata.name, services);
registerService(pkg.name, services);
}
}

View File

@ -1,8 +1,8 @@
import { authAPIModule } from "@erp/auth/api";
import { invoicesAPIModule } from "@erp/customer-invoices/api";
import { registerModule } from "./lib";
//import { invoicesAPIModule } from "@erp/invoices/api";
export const registerModules = () => {
registerModule(authAPIModule);
//registerModule(invoicesAPIModule);
registerModule(invoicesAPIModule);
};

View File

@ -31,7 +31,7 @@
"dependencies": {
"@erp/auth": "workspace:*",
"@erp/core": "workspace:*",
"@erp/invoices": "workspace:*",
"@erp/customer-invoices": "workspace:*",
"@repo/rdx-criteria": "workspace:*",
"@repo/rdx-ui": "workspace:*",
"@repo/shadcn-ui": "workspace:*",

View File

@ -45,7 +45,7 @@ export const App = () => {
getAccessToken,
setAccessToken,
clearAccessToken,
authService: createAuthService(dataSource),
authService: createAuthService(),
}}
>
<TooltipProvider delayDuration={0}>

View File

@ -1,5 +1,5 @@
import { AuthModuleManifiest } from "@erp/auth/client";
import { IModuleClient } from "@erp/core/client";
//import InvoicesModule from "@erp/invoices/client";
import { CustomerInvoicesModuleManifiest } from "@erp/customer-invoices/client";
export const modules: IModuleClient[] = [AuthModuleManifiest];
export const modules: IModuleClient[] = [AuthModuleManifiest, CustomerInvoicesModuleManifiest];

View File

@ -32,6 +32,8 @@ export const AppRoutes = (): JSX.Element => {
const grouped = groupModulesByLayout(modules);
console.log(grouped);
return (
<Router>
<ScrollToTop />

View File

@ -1,11 +1,9 @@
import { IModuleServer, ModuleParams } from "@erp/core";
import { IModuleServer, ModuleParams } from "@erp/core/api";
export const authAPIModule: IModuleServer = {
metadata: {
name: "auth",
version: "1.0.0",
dependencies: [],
},
name: "auth",
version: "1.0.0",
dependencies: [],
init(params: ModuleParams) {
// const contacts = getService<ContactsService>("contacts");
const { logger } = params;

View File

@ -1,12 +1,12 @@
import { PropsWithChildren, createContext, useEffect, useState } from "react";
import { IAuthService } from "../services";
export interface AuthContextType {
export type AuthContextType = {
token: string | null;
isAuthenticated: boolean;
login: (email: string, password: string) => Promise<void>;
logout: () => void;
}
};
export interface AuthContextParams {
authService: IAuthService;
@ -20,7 +20,10 @@ export const AuthContext = createContext<AuthContextType | undefined>(undefined)
/**
* Proveedor de autenticación para toda la app.
*/
export const AuthProvider = ({ params, children }: PropsWithChildren<{ params: any }>) => {
export const AuthProvider = ({
params,
children,
}: PropsWithChildren<{ params: AuthContextParams }>) => {
const { getAccessToken, setAccessToken, clearAccessToken, authService } = params;
const [token, setToken] = useState<string | null>(getAccessToken());
@ -30,9 +33,9 @@ export const AuthProvider = ({ params, children }: PropsWithChildren<{ params: a
}, []);
const login = async (email: string, password: string) => {
const { access_token } = await authService.login({ email, password });
setAccessToken(access_token);
setToken(access_token);
const { token } = await authService.login({ email, password });
setAccessToken(token);
setToken(token);
};
const logout = () => {

View File

@ -7,7 +7,7 @@ import { AuthContext, AuthContextType } from "../context";
export const useAuth = (): AuthContextType => {
const context = useContext(AuthContext);
if (!context) {
throw new Error("useAuth debe usarse dentro de <AuthProvider>");
throw new Error("useAuth must be used within a AuthProvider");
}
return context;
};

View File

@ -1,13 +1,14 @@
import { IDataSource } from "@erp/core/client";
import { useDataSource } from "@erp/core/client";
import { ILoginRequestDTO, ILoginResponseDTO } from "../../common";
export interface IAuthService {
login: (credentials: ILoginRequestDTO) => Promise<ILoginResponseDTO>;
}
export const createAuthService = (dataSource: IDataSource): IAuthService => {
export const createAuthService = (): IAuthService => {
return {
login: async (credentials: ILoginRequestDTO) => {
const dataSource = useDataSource();
const data = await dataSource.custom<ILoginResponseDTO>({
path: "login",
method: "post",

View File

@ -1,5 +1,5 @@
{
"name": "@erp/invoices",
"name": "@erp/customer-invoices",
"version": "0.0.1",
"main": "src/index.ts",
"types": "src/index.ts",
@ -23,6 +23,7 @@
"@repo/rdx-utils": "workspace:*",
"@repo/rdx-ui": "workspace:*",
"@repo/shadcn-ui": "workspace:*",
"@tanstack/react-query": "^5.74.11",
"ag-grid-community": "^33.3.0",
"ag-grid-react": "^33.3.0",
"express": "^4.18.2",

View File

@ -0,0 +1,147 @@
import { UniqueID, UtcDate } from "@/core/common/domain";
import {
type ICustomerInvoiceProps,
type ICustomerInvoiceService,
type CustomerInvoice,
CustomerInvoiceNumber,
CustomerInvoiceSerie,
CustomerInvoiceStatus,
} from "@/contexts/customerCustomerInvoices/domain";
import { ITransactionManager } from "@/core/common/infrastructure/database";
import { logger } from "@/core/logger";
import { Result } from "@repo/rdx-utils";
import { ICreateCustomerInvoiceRequestDTO } from "../../common/dto";
export class CreateCustomerInvoiceUseCase {
constructor(
private readonly customerCustomerInvoiceService: ICustomerInvoiceService,
private readonly transactionManager: ITransactionManager
) {}
public execute(
customerCustomerInvoiceID: UniqueID,
dto: ICreateCustomerInvoiceRequestDTO
): Promise<Result<CustomerInvoice, Error>> {
return this.transactionManager.complete(async (transaction) => {
try {
const validOrErrors = this.validateCustomerInvoiceData(dto);
if (validOrErrors.isFailure) {
return Result.fail(validOrErrors.error);
}
const data = validOrErrors.data;
// Update customerCustomerInvoice with dto
return await this.customerCustomerInvoiceService.createCustomerInvoice(customerCustomerInvoiceID, data, transaction);
} catch (error: unknown) {
logger.error(error as Error);
return Result.fail(error as Error);
}
});
}
private validateCustomerInvoiceData(dto: ICreateCustomerInvoiceRequestDTO): Result<ICustomerInvoiceProps, Error> {
const errors: Error[] = [];
const customerCustomerInvoiceNumerOrError = CustomerInvoiceNumber.create(dto.customerCustomerInvoice_number);
const customerCustomerInvoiceSeriesOrError = CustomerInvoiceSerie.create(dto.customerCustomerInvoice_series);
const issueDateOrError = UtcDate.create(dto.issue_date);
const operationDateOrError = UtcDate.create(dto.operation_date);
const result = Result.combine([
customerCustomerInvoiceNumerOrError,
customerCustomerInvoiceSeriesOrError,
issueDateOrError,
operationDateOrError,
]);
if (result.isFailure) {
return Result.fail(result.error);
}
const validatedData: ICustomerInvoiceProps = {
status: CustomerInvoiceStatus.createDraft(),
customerCustomerInvoiceNumber: customerCustomerInvoiceNumerOrError.data,
customerCustomerInvoiceSeries: customerCustomerInvoiceSeriesOrError.data,
issueDate: issueDateOrError.data,
operationDate: operationDateOrError.data,
customerCustomerInvoiceCurrency: dto.currency,
};
/*if (errors.length > 0) {
const message = errors.map((err) => err.message).toString();
return Result.fail(new Error(message));
}*/
return Result.ok(validatedData);
/*let customerCustomerInvoice_status = CustomerInvoiceStatus.create(dto.status).object;
if (customerCustomerInvoice_status.isEmpty()) {
customerCustomerInvoice_status = CustomerInvoiceStatus.createDraft();
}
let customerCustomerInvoice_series = CustomerInvoiceSeries.create(dto.customerCustomerInvoice_series).object;
if (customerCustomerInvoice_series.isEmpty()) {
customerCustomerInvoice_series = CustomerInvoiceSeries.create(dto.customerCustomerInvoice_series).object;
}
let issue_date = CustomerInvoiceDate.create(dto.issue_date).object;
if (issue_date.isEmpty()) {
issue_date = CustomerInvoiceDate.createCurrentDate().object;
}
let operation_date = CustomerInvoiceDate.create(dto.operation_date).object;
if (operation_date.isEmpty()) {
operation_date = CustomerInvoiceDate.createCurrentDate().object;
}
let customerCustomerInvoiceCurrency = Currency.createFromCode(dto.currency).object;
if (customerCustomerInvoiceCurrency.isEmpty()) {
customerCustomerInvoiceCurrency = Currency.createDefaultCode().object;
}
let customerCustomerInvoiceLanguage = Language.createFromCode(dto.language_code).object;
if (customerCustomerInvoiceLanguage.isEmpty()) {
customerCustomerInvoiceLanguage = Language.createDefaultCode().object;
}
const items = new Collection<CustomerInvoiceItem>(
dto.items?.map(
(item) =>
CustomerInvoiceSimpleItem.create({
description: Description.create(item.description).object,
quantity: Quantity.create(item.quantity).object,
unitPrice: UnitPrice.create({
amount: item.unit_price.amount,
currencyCode: item.unit_price.currency,
precision: item.unit_price.precision,
}).object,
}).object
)
);
if (!customerCustomerInvoice_status.isDraft()) {
throw Error("Error al crear una factura que no es borrador");
}
return DraftCustomerInvoice.create(
{
customerCustomerInvoiceSeries: customerCustomerInvoice_series,
issueDate: issue_date,
operationDate: operation_date,
customerCustomerInvoiceCurrency,
language: customerCustomerInvoiceLanguage,
customerCustomerInvoiceNumber: CustomerInvoiceNumber.create(undefined).object,
//notes: Note.create(customerCustomerInvoiceDTO.notes).object,
//senderId: UniqueID.create(null).object,
recipient,
items,
},
customerCustomerInvoiceId
);*/
}
}

View File

@ -2,18 +2,18 @@ import { UniqueID } from "@/core/common/domain";
import { ITransactionManager } from "@/core/common/infrastructure/database";
import { logger } from "@/core/logger";
import { Result } from "@repo/rdx-utils";
import { IInvoiceService } from "../domain";
import { ICustomerInvoiceService } from "../domain";
export class DeleteInvoiceUseCase {
export class DeleteCustomerInvoiceUseCase {
constructor(
private readonly invoiceService: IInvoiceService,
private readonly customerCustomerInvoiceService: ICustomerInvoiceService,
private readonly transactionManager: ITransactionManager
) {}
public execute(invoiceID: UniqueID): Promise<Result<boolean, Error>> {
public execute(customerCustomerInvoiceID: UniqueID): Promise<Result<boolean, Error>> {
return this.transactionManager.complete(async (transaction) => {
try {
return await this.invoiceService.deleteInvoiceById(invoiceID, transaction);
return await this.customerCustomerInvoiceService.deleteCustomerInvoiceById(customerCustomerInvoiceID, transaction);
} catch (error: unknown) {
logger.error(error as Error);
return Result.fail(error as Error);

View File

@ -2,18 +2,18 @@ import { UniqueID } from "@/core/common/domain";
import { ITransactionManager } from "@/core/common/infrastructure/database";
import { logger } from "@/lib/logger";
import { Result } from "@repo/rdx-utils";
import { IInvoiceService, Invoice } from "../domain";
import { ICustomerInvoiceService, CustomerInvoice } from "../domain";
export class GetInvoiceUseCase {
export class GetCustomerInvoiceUseCase {
constructor(
private readonly invoiceService: IInvoiceService,
private readonly customerCustomerInvoiceService: ICustomerInvoiceService,
private readonly transactionManager: ITransactionManager
) {}
public execute(invoiceID: UniqueID): Promise<Result<Invoice, Error>> {
public execute(customerCustomerInvoiceID: UniqueID): Promise<Result<CustomerInvoice, Error>> {
return this.transactionManager.complete(async (transaction) => {
try {
return await this.invoiceService.findInvoiceById(invoiceID, transaction);
return await this.customerCustomerInvoiceService.findCustomerInvoiceById(customerCustomerInvoiceID, transaction);
} catch (error: unknown) {
logger.error(error as Error);
return Result.fail(error as Error);

View File

@ -0,0 +1,5 @@
//export * from "./create-customer-invoice.use-case";
//export * from "./delete-customer-invoice.use-case";
export * from "./get-customer-invoice.use-case";
export * from "./list-customer-invoices.use-case";
//export * from "./update-customer-invoice.use-case";

View File

@ -1,19 +1,19 @@
import { ITransactionManager } from "@erp/core";
import { ITransactionManager } from "@erp/core/api";
import { Criteria } from "@repo/rdx-criteria/server";
import { Collection, Result } from "@repo/rdx-utils";
import { Transaction } from "sequelize";
import { IInvoiceService, Invoice } from "../domain";
import { ICustomerInvoiceService, CustomerInvoice } from "../domain";
export class ListInvoicesUseCase {
export class ListCustomerInvoicesUseCase {
constructor(
private readonly invoiceService: IInvoiceService,
private readonly customerCustomerInvoiceService: ICustomerInvoiceService,
private readonly transactionManager: ITransactionManager
) {}
public execute(criteria: Criteria): Promise<Result<Collection<Invoice>, Error>> {
public execute(criteria: Criteria): Promise<Result<Collection<CustomerInvoice>, Error>> {
return this.transactionManager.complete(async (transaction: Transaction) => {
try {
return await this.invoiceService.findInvoices(criteria, transaction);
return await this.customerCustomerInvoiceService.findCustomerInvoices(criteria, transaction);
} catch (error: unknown) {
return Result.fail(error as Error);
}

View File

@ -5,12 +5,12 @@
import { IAdapter, RepositoryBuilder } from "@/contexts/common/domain";
import { Result, UniqueID } from "@shared/contexts";
import { NullOr } from "@shared/utilities";
import { IInvoiceParticipantAddress, IInvoiceParticipantAddressRepository } from "../../domain";
import { ICustomerInvoiceParticipantAddress, ICustomerInvoiceParticipantAddressRepository } from "../../domain";
export const participantAddressFinder = async (
addressId: UniqueID,
adapter: IAdapter,
repository: RepositoryBuilder<IInvoiceParticipantAddressRepository>
repository: RepositoryBuilder<ICustomerInvoiceParticipantAddressRepository>
) => {
if (addressId.isNull()) {
return Result.fail<IApplicationServiceError>(
@ -22,7 +22,7 @@ export const participantAddressFinder = async (
}
const transaction = adapter.startTransaction();
let address: NullOr<IInvoiceParticipantAddress> = null;
let address: NullOr<ICustomerInvoiceParticipantAddress> = null;
try {
await transaction.complete(async (t) => {
@ -38,7 +38,7 @@ export const participantAddressFinder = async (
);
}
return Result.ok<IInvoiceParticipantAddress>(address);
return Result.ok<ICustomerInvoiceParticipantAddress>(address);
} catch (error: unknown) {
const _error = error as Error;

View File

@ -1,13 +1,13 @@
/* import { IAdapter, RepositoryBuilder } from "@/contexts/common/domain";
import { UniqueID } from "@shared/contexts";
import { IInvoiceParticipantRepository } from "../../domain";
import { InvoiceCustomer } from "../../domain/entities/invoice-customer/invoice-customer";
import { ICustomerInvoiceParticipantRepository } from "../../domain";
import { CustomerInvoiceCustomer } from "../../domain/entities/customerCustomerInvoice-customer/customerCustomerInvoice-customer";
export const participantFinder = async (
participantId: UniqueID,
adapter: IAdapter,
repository: RepositoryBuilder<IInvoiceParticipantRepository>
): Promise<InvoiceCustomer | undefined> => {
repository: RepositoryBuilder<ICustomerInvoiceParticipantRepository>
): Promise<CustomerInvoiceCustomer | undefined> => {
if (!participantId || (participantId && participantId.isNull())) {
return Promise.resolve(undefined);
}

View File

@ -0,0 +1,401 @@
import { UniqueID } from "@/core/common/domain";
import { ITransactionManager } from "@/core/common/infrastructure/database";
import { Result } from "@repo/rdx-utils";
import { IUpdateCustomerInvoiceRequestDTO } from "../../common/dto";
import { ICustomerInvoiceService, CustomerInvoice } from "../domain";
export class CreateCustomerInvoiceUseCase {
constructor(
private readonly customerCustomerInvoiceService: ICustomerInvoiceService,
private readonly transactionManager: ITransactionManager
) {}
public execute(
customerCustomerInvoiceID: UniqueID,
dto: Partial<IUpdateCustomerInvoiceRequestDTO>
): Promise<Result<CustomerInvoice, Error>> {
return this.transactionManager.complete(async (transaction) => {
return Result.fail(new Error("No implementado"));
/*
try {
const validOrErrors = this.validateCustomerInvoiceData(dto);
if (validOrErrors.isFailure) {
return Result.fail(validOrErrors.error);
}
const data = validOrErrors.data;
// Update customerCustomerInvoice with dto
return await this.customerCustomerInvoiceService.updateCustomerInvoiceById(customerCustomerInvoiceID, data, transaction);
} catch (error: unknown) {
logger.error(error as Error);
return Result.fail(error as Error);
}
*/
});
}
/* private validateCustomerInvoiceData(
dto: Partial<IUpdateCustomerInvoiceRequestDTO>
): Result<Partial<ICustomerInvoiceProps>, Error> {
const errors: Error[] = [];
const validatedData: Partial<ICustomerInvoiceProps> = {};
// Create customerCustomerInvoice
let customerCustomerInvoice_status = CustomerInvoiceStatus.create(customerCustomerInvoiceDTO.status).object;
if (customerCustomerInvoice_status.isEmpty()) {
customerCustomerInvoice_status = CustomerInvoiceStatus.createDraft();
}
let customerCustomerInvoice_series = CustomerInvoiceSeries.create(customerCustomerInvoiceDTO.customerCustomerInvoice_series).object;
if (customerCustomerInvoice_series.isEmpty()) {
customerCustomerInvoice_series = CustomerInvoiceSeries.create(customerCustomerInvoiceDTO.customerCustomerInvoice_series).object;
}
let issue_date = CustomerInvoiceDate.create(customerCustomerInvoiceDTO.issue_date).object;
if (issue_date.isEmpty()) {
issue_date = CustomerInvoiceDate.createCurrentDate().object;
}
let operation_date = CustomerInvoiceDate.create(customerCustomerInvoiceDTO.operation_date).object;
if (operation_date.isEmpty()) {
operation_date = CustomerInvoiceDate.createCurrentDate().object;
}
let customerCustomerInvoiceCurrency = Currency.createFromCode(customerCustomerInvoiceDTO.currency).object;
if (customerCustomerInvoiceCurrency.isEmpty()) {
customerCustomerInvoiceCurrency = Currency.createDefaultCode().object;
}
let customerCustomerInvoiceLanguage = Language.createFromCode(customerCustomerInvoiceDTO.language_code).object;
if (customerCustomerInvoiceLanguage.isEmpty()) {
customerCustomerInvoiceLanguage = Language.createDefaultCode().object;
}
const items = new Collection<CustomerInvoiceItem>(
customerCustomerInvoiceDTO.items?.map(
(item) =>
CustomerInvoiceSimpleItem.create({
description: Description.create(item.description).object,
quantity: Quantity.create(item.quantity).object,
unitPrice: UnitPrice.create({
amount: item.unit_price.amount,
currencyCode: item.unit_price.currency,
precision: item.unit_price.precision,
}).object,
}).object
)
);
if (!customerCustomerInvoice_status.isDraft()) {
throw Error("Error al crear una factura que no es borrador");
}
return DraftCustomerInvoice.create(
{
customerCustomerInvoiceSeries: customerCustomerInvoice_series,
issueDate: issue_date,
operationDate: operation_date,
customerCustomerInvoiceCurrency,
language: customerCustomerInvoiceLanguage,
customerCustomerInvoiceNumber: CustomerInvoiceNumber.create(undefined).object,
//notes: Note.create(customerCustomerInvoiceDTO.notes).object,
//senderId: UniqueID.create(null).object,
recipient,
items,
},
customerCustomerInvoiceId
);
} */
}
/* export type UpdateCustomerInvoiceResponseOrError =
| Result<never, IUseCaseError> // Misc errors (value objects)
| Result<CustomerInvoice, never>; // Success!
export class UpdateCustomerInvoiceUseCase2
implements
IUseCase<{ id: UniqueID; data: IUpdateCustomerInvoice_DTO }, Promise<UpdateCustomerInvoiceResponseOrError>>
{
private _context: IInvoicingContext;
private _adapter: ISequelizeAdapter;
private _repositoryManager: IRepositoryManager;
constructor(context: IInvoicingContext) {
this._context = context;
this._adapter = context.adapter;
this._repositoryManager = context.repositoryManager;
}
private getRepository<T>(name: string) {
return this._repositoryManager.getRepository<T>(name);
}
private handleValidationFailure(
validationError: Error,
message?: string
): Result<never, IUseCaseError> {
return Result.fail<IUseCaseError>(
UseCaseError.create(
UseCaseError.INVALID_INPUT_DATA,
message ? message : validationError.message,
validationError
)
);
}
async execute(request: {
id: UniqueID;
data: IUpdateCustomerInvoice_DTO;
}): Promise<UpdateCustomerInvoiceResponseOrError> {
const { id, data: customerCustomerInvoiceDTO } = request;
// Validaciones
const customerCustomerInvoiceDTOOrError = ensureUpdateCustomerInvoice_DTOIsValid(customerCustomerInvoiceDTO);
if (customerCustomerInvoiceDTOOrError.isFailure) {
return this.handleValidationFailure(customerCustomerInvoiceDTOOrError.error);
}
const transaction = this._adapter.startTransaction();
const customerCustomerInvoiceRepoBuilder = this.getRepository<ICustomerInvoiceRepository>("CustomerInvoice");
let customerCustomerInvoice: CustomerInvoice | null = null;
try {
await transaction.complete(async (t) => {
customerCustomerInvoice = await customerCustomerInvoiceRepoBuilder({ transaction: t }).getById(id);
});
if (customerCustomerInvoice === null) {
return Result.fail<IUseCaseError>(
UseCaseError.create(UseCaseError.NOT_FOUND_ERROR, `CustomerInvoice not found`, {
id: request.id.toString(),
entity: "customerCustomerInvoice",
})
);
}
return Result.ok<CustomerInvoice>(customerCustomerInvoice);
} catch (error: unknown) {
const _error = error as Error;
if (customerCustomerInvoiceRepoBuilder().isRepositoryError(_error)) {
return this.handleRepositoryError(error as BaseError, customerCustomerInvoiceRepoBuilder());
} else {
return this.handleUnexceptedError(error);
}
}
// Recipient validations
const recipientIdOrError = ensureParticipantIdIsValid(
customerCustomerInvoiceDTO?.recipient?.id,
);
if (recipientIdOrError.isFailure) {
return this.handleValidationFailure(
recipientIdOrError.error,
"Recipient ID not valid",
);
}
const recipientId = recipientIdOrError.object;
const recipientBillingIdOrError = ensureParticipantAddressIdIsValid(
customerCustomerInvoiceDTO?.recipient?.billing_address_id,
);
if (recipientBillingIdOrError.isFailure) {
return this.handleValidationFailure(
recipientBillingIdOrError.error,
"Recipient billing address ID not valid",
);
}
const recipientBillingId = recipientBillingIdOrError.object;
const recipientShippingIdOrError = ensureParticipantAddressIdIsValid(
customerCustomerInvoiceDTO?.recipient?.shipping_address_id,
);
if (recipientShippingIdOrError.isFailure) {
return this.handleValidationFailure(
recipientShippingIdOrError.error,
"Recipient shipping address ID not valid",
);
}
const recipientShippingId = recipientShippingIdOrError.object;
const recipientContact = await this.findContact(
recipientId,
recipientBillingId,
recipientShippingId,
);
if (!recipientContact) {
return this.handleValidationFailure(
new Error(`Recipient with ID ${recipientId.toString()} does not exist`),
);
}
// Crear customerCustomerInvoice
const customerCustomerInvoiceOrError = await this.tryUpdateCustomerInvoiceInstance(
customerCustomerInvoiceDTO,
customerCustomerInvoiceIdOrError.object,
//senderId,
//senderBillingId,
//senderShippingId,
recipientContact,
);
if (customerCustomerInvoiceOrError.isFailure) {
const { error: domainError } = customerCustomerInvoiceOrError;
let errorCode = "";
let message = "";
switch (domainError.code) {
case CustomerInvoice.ERROR_CUSTOMER_WITHOUT_NAME:
errorCode = UseCaseError.INVALID_INPUT_DATA;
message =
"El cliente debe ser una compañía o tener nombre y apellidos.";
break;
default:
errorCode = UseCaseError.UNEXCEPTED_ERROR;
message = "";
break;
}
return Result.fail<IUseCaseError>(
UseCaseError.create(errorCode, message, domainError),
);
}
return this.saveCustomerInvoice(customerCustomerInvoiceOrError.object);
}
private async tryUpdateCustomerInvoiceInstance(customerCustomerInvoiceDTO, customerCustomerInvoiceId, recipient) {
// Create customerCustomerInvoice
let customerCustomerInvoice_status = CustomerInvoiceStatus.create(customerCustomerInvoiceDTO.status).object;
if (customerCustomerInvoice_status.isEmpty()) {
customerCustomerInvoice_status = CustomerInvoiceStatus.createDraft();
}
let customerCustomerInvoice_series = CustomerInvoiceSeries.create(customerCustomerInvoiceDTO.customerCustomerInvoice_series).object;
if (customerCustomerInvoice_series.isEmpty()) {
customerCustomerInvoice_series = CustomerInvoiceSeries.create(customerCustomerInvoiceDTO.customerCustomerInvoice_series).object;
}
let issue_date = CustomerInvoiceDate.create(customerCustomerInvoiceDTO.issue_date).object;
if (issue_date.isEmpty()) {
issue_date = CustomerInvoiceDate.createCurrentDate().object;
}
let operation_date = CustomerInvoiceDate.create(customerCustomerInvoiceDTO.operation_date).object;
if (operation_date.isEmpty()) {
operation_date = CustomerInvoiceDate.createCurrentDate().object;
}
let customerCustomerInvoiceCurrency = Currency.createFromCode(customerCustomerInvoiceDTO.currency).object;
if (customerCustomerInvoiceCurrency.isEmpty()) {
customerCustomerInvoiceCurrency = Currency.createDefaultCode().object;
}
let customerCustomerInvoiceLanguage = Language.createFromCode(customerCustomerInvoiceDTO.language_code).object;
if (customerCustomerInvoiceLanguage.isEmpty()) {
customerCustomerInvoiceLanguage = Language.createDefaultCode().object;
}
const items = new Collection<CustomerInvoiceItem>(
customerCustomerInvoiceDTO.items?.map(
(item) =>
CustomerInvoiceSimpleItem.create({
description: Description.create(item.description).object,
quantity: Quantity.create(item.quantity).object,
unitPrice: UnitPrice.create({
amount: item.unit_price.amount,
currencyCode: item.unit_price.currency,
precision: item.unit_price.precision,
}).object,
}).object
)
);
if (!customerCustomerInvoice_status.isDraft()) {
throw Error("Error al crear una factura que no es borrador");
}
return DraftCustomerInvoice.create(
{
customerCustomerInvoiceSeries: customerCustomerInvoice_series,
issueDate: issue_date,
operationDate: operation_date,
customerCustomerInvoiceCurrency,
language: customerCustomerInvoiceLanguage,
customerCustomerInvoiceNumber: CustomerInvoiceNumber.create(undefined).object,
//notes: Note.create(customerCustomerInvoiceDTO.notes).object,
//senderId: UniqueID.create(null).object,
recipient,
items,
},
customerCustomerInvoiceId
);
}
private async findContact(
contactId: UniqueID,
billingAddressId: UniqueID,
shippingAddressId: UniqueID
) {
const contactRepoBuilder = this.getRepository<IContactRepository>("Contact");
const contact = await contactRepoBuilder().getById2(
contactId,
billingAddressId,
shippingAddressId
);
return contact;
}
private async saveCustomerInvoice(customerCustomerInvoice: DraftCustomerInvoice) {
const transaction = this._adapter.startTransaction();
const customerCustomerInvoiceRepoBuilder = this.getRepository<ICustomerInvoiceRepository>("CustomerInvoice");
try {
await transaction.complete(async (t) => {
const customerCustomerInvoiceRepo = customerCustomerInvoiceRepoBuilder({ transaction: t });
await customerCustomerInvoiceRepo.save(customerCustomerInvoice);
});
return Result.ok<DraftCustomerInvoice>(customerCustomerInvoice);
} catch (error: unknown) {
const _error = error as Error;
if (customerCustomerInvoiceRepoBuilder().isRepositoryError(_error)) {
return this.handleRepositoryError(error as BaseError, customerCustomerInvoiceRepoBuilder());
} else {
return this.handleUnexceptedError(error);
}
}
}
private handleUnexceptedError(error): Result<never, IUseCaseError> {
return Result.fail<IUseCaseError>(
UseCaseError.create(UseCaseError.UNEXCEPTED_ERROR, error.message, error)
);
}
private handleRepositoryError(
error: BaseError,
repository: ICustomerInvoiceRepository
): Result<never, IUseCaseError> {
const { message, details } = repository.handleRepositoryError(error);
return Result.fail<IUseCaseError>(
UseCaseError.create(UseCaseError.REPOSITORY_ERROR, message, details)
);
}
}
*/

View File

@ -1,13 +1,13 @@
import { AggregateRoot, MoneyValue, UniqueID, UtcDate } from "@repo/rdx-ddd";
import { Collection, Result } from "@repo/rdx-utils";
import { InvoiceCustomer, InvoiceItem, InvoiceItems } from "../entities";
import { InvoiceNumber, InvoiceSerie, InvoiceStatus } from "../value-objects";
import { CustomerInvoiceCustomer, CustomerInvoiceItem, CustomerInvoiceItems } from "../entities";
import { CustomerInvoiceNumber, CustomerInvoiceSerie, CustomerInvoiceStatus } from "../value-objects";
export interface IInvoiceProps {
invoiceNumber: InvoiceNumber;
invoiceSeries: InvoiceSerie;
export interface ICustomerInvoiceProps {
customerCustomerInvoiceNumber: CustomerInvoiceNumber;
customerCustomerInvoiceSeries: CustomerInvoiceSerie;
status: InvoiceStatus;
status: CustomerInvoiceStatus;
issueDate: UtcDate;
operationDate: UtcDate;
@ -15,7 +15,7 @@ export interface IInvoiceProps {
//dueDate: UtcDate; // ? --> depende de la forma de pago
//tax: Tax; // ? --> detalles?
invoiceCurrency: string;
customerCustomerInvoiceCurrency: string;
//language: Language;
@ -27,29 +27,29 @@ export interface IInvoiceProps {
//paymentInstructions: Note;
//paymentTerms: string;
customer?: InvoiceCustomer;
items?: InvoiceItems;
customer?: CustomerInvoiceCustomer;
items?: CustomerInvoiceItems;
}
export interface IInvoice {
export interface ICustomerInvoice {
id: UniqueID;
invoiceNumber: InvoiceNumber;
invoiceSeries: InvoiceSerie;
customerCustomerInvoiceNumber: CustomerInvoiceNumber;
customerCustomerInvoiceSeries: CustomerInvoiceSerie;
status: InvoiceStatus;
status: CustomerInvoiceStatus;
issueDate: UtcDate;
operationDate: UtcDate;
//senderId: UniqueID;
customer?: InvoiceCustomer;
customer?: CustomerInvoiceCustomer;
//dueDate
//tax: Tax;
//language: Language;
invoiceCurrency: string;
customerCustomerInvoiceCurrency: string;
//purchareOrderNumber: string;
//notes: Note;
@ -57,43 +57,43 @@ export interface IInvoice {
//paymentInstructions: Note;
//paymentTerms: string;
items: InvoiceItems;
items: CustomerInvoiceItems;
calculateSubtotal: () => MoneyValue;
calculateTaxTotal: () => MoneyValue;
calculateTotal: () => MoneyValue;
}
export class Invoice extends AggregateRoot<IInvoiceProps> implements IInvoice {
private _items!: Collection<InvoiceItem>;
//protected _status: InvoiceStatus;
export class CustomerInvoice extends AggregateRoot<ICustomerInvoiceProps> implements ICustomerInvoice {
private _items!: Collection<CustomerInvoiceItem>;
//protected _status: CustomerInvoiceStatus;
protected constructor(props: IInvoiceProps, id?: UniqueID) {
protected constructor(props: ICustomerInvoiceProps, id?: UniqueID) {
super(props, id);
this._items = props.items || InvoiceItems.create();
this._items = props.items || CustomerInvoiceItems.create();
}
static create(props: IInvoiceProps, id?: UniqueID): Result<Invoice, Error> {
const invoice = new Invoice(props, id);
static create(props: ICustomerInvoiceProps, id?: UniqueID): Result<CustomerInvoice, Error> {
const customerCustomerInvoice = new CustomerInvoice(props, id);
// Reglas de negocio / validaciones
// ...
// ...
// 🔹 Disparar evento de dominio "InvoiceAuthenticatedEvent"
//const { invoice } = props;
//user.addDomainEvent(new InvoiceAuthenticatedEvent(id, invoice.toString()));
// 🔹 Disparar evento de dominio "CustomerInvoiceAuthenticatedEvent"
//const { customerCustomerInvoice } = props;
//user.addDomainEvent(new CustomerInvoiceAuthenticatedEvent(id, customerCustomerInvoice.toString()));
return Result.ok(invoice);
return Result.ok(customerCustomerInvoice);
}
get invoiceNumber() {
return this.props.invoiceNumber;
get customerCustomerInvoiceNumber() {
return this.props.customerCustomerInvoiceNumber;
}
get invoiceSeries() {
return this.props.invoiceSeries;
get customerCustomerInvoiceSeries() {
return this.props.customerCustomerInvoiceSeries;
}
get issueDate() {
@ -104,7 +104,7 @@ export class Invoice extends AggregateRoot<IInvoiceProps> implements IInvoice {
return this.props.senderId;
}*/
get customer(): InvoiceCustomer | undefined {
get customer(): CustomerInvoiceCustomer | undefined {
return this.props.customer;
}
@ -152,8 +152,8 @@ export class Invoice extends AggregateRoot<IInvoiceProps> implements IInvoice {
return this.props.shipTo;
}*/
get invoiceCurrency() {
return this.props.invoiceCurrency;
get customerCustomerInvoiceCurrency() {
return this.props.customerCustomerInvoiceCurrency;
}
/*get notes() {
@ -161,11 +161,11 @@ export class Invoice extends AggregateRoot<IInvoiceProps> implements IInvoice {
}*/
// Method to get the complete list of line items
/*get lineItems(): InvoiceLineItem[] {
/*get lineItems(): CustomerInvoiceLineItem[] {
return this._lineItems;
}
addLineItem(lineItem: InvoiceLineItem, position?: number): void {
addLineItem(lineItem: CustomerInvoiceLineItem, position?: number): void {
if (position === undefined) {
this._lineItems.push(lineItem);
} else {
@ -174,29 +174,29 @@ export class Invoice extends AggregateRoot<IInvoiceProps> implements IInvoice {
}*/
calculateSubtotal(): MoneyValue {
const invoiceSubtotal = MoneyValue.create({
const customerCustomerInvoiceSubtotal = MoneyValue.create({
amount: 0,
currency_code: this.props.invoiceCurrency,
currency_code: this.props.customerCustomerInvoiceCurrency,
scale: 2,
}).data;
return this._items.getAll().reduce((subtotal, item) => {
return subtotal.add(item.calculateTotal());
}, invoiceSubtotal);
}, customerCustomerInvoiceSubtotal);
}
// Method to calculate the total tax in the invoice
// Method to calculate the total tax in the customerCustomerInvoice
calculateTaxTotal(): MoneyValue {
const taxTotal = MoneyValue.create({
amount: 0,
currency_code: this.props.invoiceCurrency,
currency_code: this.props.customerCustomerInvoiceCurrency,
scale: 2,
}).data;
return taxTotal;
}
// Method to calculate the total invoice amount, including taxes
// Method to calculate the total customerCustomerInvoice amount, including taxes
calculateTotal(): MoneyValue {
return this.calculateSubtotal().add(this.calculateTaxTotal());
}

View File

@ -0,0 +1 @@
export * from "./customerCustomerInvoice";

View File

@ -0,0 +1,2 @@
export * from "./customerCustomerInvoice-customer";
export * from "./customerCustomerInvoice-items";

View File

@ -0,0 +1,2 @@
export * from "./customerCustomerInvoice-address";
export * from "./customerCustomerInvoice-customer";

View File

@ -1,43 +1,43 @@
import { EmailAddress, Name, PostalAddress, ValueObject } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import { PhoneNumber } from "libphonenumber-js";
import { InvoiceAddressType } from "../../value-objects";
import { CustomerInvoiceAddressType } from "../../value-objects";
export interface IInvoiceAddressProps {
type: InvoiceAddressType;
export interface ICustomerInvoiceAddressProps {
type: CustomerInvoiceAddressType;
title: Name;
address: PostalAddress;
email: EmailAddress;
phone: PhoneNumber;
}
export interface IInvoiceAddress {
type: InvoiceAddressType;
export interface ICustomerInvoiceAddress {
type: CustomerInvoiceAddressType;
title: Name;
address: PostalAddress;
email: EmailAddress;
phone: PhoneNumber;
}
export class InvoiceAddress extends ValueObject<IInvoiceAddressProps> implements IInvoiceAddress {
public static create(props: IInvoiceAddressProps) {
return Result.ok(new InvoiceAddress(props));
export class CustomerInvoiceAddress extends ValueObject<ICustomerInvoiceAddressProps> implements ICustomerInvoiceAddress {
public static create(props: ICustomerInvoiceAddressProps) {
return Result.ok(new CustomerInvoiceAddress(props));
}
public static createShippingAddress(props: IInvoiceAddressProps) {
public static createShippingAddress(props: ICustomerInvoiceAddressProps) {
return Result.ok(
new InvoiceAddress({
new CustomerInvoiceAddress({
...props,
type: InvoiceAddressType.create("shipping").data,
type: CustomerInvoiceAddressType.create("shipping").data,
})
);
}
public static createBillingAddress(props: IInvoiceAddressProps) {
public static createBillingAddress(props: ICustomerInvoiceAddressProps) {
return Result.ok(
new InvoiceAddress({
new CustomerInvoiceAddress({
...props,
type: InvoiceAddressType.create("billing").data,
type: CustomerInvoiceAddressType.create("billing").data,
})
);
}
@ -58,11 +58,11 @@ export class InvoiceAddress extends ValueObject<IInvoiceAddressProps> implements
return this.props.phone;
}
get type(): InvoiceAddressType {
get type(): CustomerInvoiceAddressType {
return this.props.type;
}
getValue(): IInvoiceAddressProps {
getValue(): ICustomerInvoiceAddressProps {
return this.props;
}

View File

@ -1,38 +1,38 @@
import { DomainEntity, Name, TINNumber, UniqueID } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import { InvoiceAddress } from "./invoice-address";
import { CustomerInvoiceAddress } from "./customerCustomerInvoice-address";
export interface IInvoiceCustomerProps {
export interface ICustomerInvoiceCustomerProps {
tin: TINNumber;
companyName: Name;
firstName: Name;
lastName: Name;
billingAddress?: InvoiceAddress;
shippingAddress?: InvoiceAddress;
billingAddress?: CustomerInvoiceAddress;
shippingAddress?: CustomerInvoiceAddress;
}
export interface IInvoiceCustomer {
export interface ICustomerInvoiceCustomer {
id: UniqueID;
tin: TINNumber;
companyName: Name;
firstName: Name;
lastName: Name;
billingAddress?: InvoiceAddress;
shippingAddress?: InvoiceAddress;
billingAddress?: CustomerInvoiceAddress;
shippingAddress?: CustomerInvoiceAddress;
}
export class InvoiceCustomer
extends DomainEntity<IInvoiceCustomerProps>
implements IInvoiceCustomer
export class CustomerInvoiceCustomer
extends DomainEntity<ICustomerInvoiceCustomerProps>
implements ICustomerInvoiceCustomer
{
public static create(
props: IInvoiceCustomerProps,
props: ICustomerInvoiceCustomerProps,
id?: UniqueID
): Result<InvoiceCustomer, Error> {
const participant = new InvoiceCustomer(props, id);
return Result.ok<InvoiceCustomer>(participant);
): Result<CustomerInvoiceCustomer, Error> {
const participant = new CustomerInvoiceCustomer(props, id);
return Result.ok<CustomerInvoiceCustomer>(participant);
}
get tin(): TINNumber {

View File

@ -0,0 +1,2 @@
export * from "./customerCustomerInvoice-item";
export * from "./customerCustomerInvoice-items";

View File

@ -0,0 +1,83 @@
import { MoneyValue, Percentage, Quantity } from "@/core/common/domain";
import { CustomerInvoiceItemDescription } from "../../value-objects";
import { CustomerInvoiceItem } from "./customerCustomerInvoice-item";
describe("CustomerInvoiceItem", () => {
it("debería calcular correctamente el subtotal (unitPrice * quantity)", () => {
const props = {
description: CustomerInvoiceItemDescription.create("Producto A"),
quantity: Quantity.create({ amount: 200, scale: 2 }),
unitPrice: MoneyValue.create(50),
discount: Percentage.create(0),
};
const result = CustomerInvoiceItem.create(props);
expect(result.isOk()).toBe(true);
const customerCustomerInvoiceItem = result.unwrap();
expect(customerCustomerInvoiceItem.subtotalPrice.value).toBe(100); // 50 * 2
});
it("debería calcular correctamente el total con descuento", () => {
const props = {
description: new CustomerInvoiceItemDescription("Producto B"),
quantity: new Quantity(3),
unitPrice: new MoneyValue(30),
discount: new Percentage(10), // 10%
};
const result = CustomerInvoiceItem.create(props);
expect(result.isOk()).toBe(true);
const customerCustomerInvoiceItem = result.unwrap();
expect(customerCustomerInvoiceItem.totalPrice.value).toBe(81); // (30 * 3) - 10% de (30 * 3)
});
it("debería devolver los valores correctos de las propiedades", () => {
const props = {
description: new CustomerInvoiceItemDescription("Producto C"),
quantity: new Quantity(1),
unitPrice: new MoneyValue(100),
discount: new Percentage(5),
};
const result = CustomerInvoiceItem.create(props);
expect(result.isOk()).toBe(true);
const customerCustomerInvoiceItem = result.unwrap();
expect(customerCustomerInvoiceItem.description.value).toBe("Producto C");
expect(customerCustomerInvoiceItem.quantity.value).toBe(1);
expect(customerCustomerInvoiceItem.unitPrice.value).toBe(100);
expect(customerCustomerInvoiceItem.discount.value).toBe(5);
});
it("debería manejar correctamente un descuento del 0%", () => {
const props = {
description: new CustomerInvoiceItemDescription("Producto D"),
quantity: new Quantity(4),
unitPrice: new MoneyValue(25),
discount: new Percentage(0),
};
const result = CustomerInvoiceItem.create(props);
expect(result.isOk()).toBe(true);
const customerCustomerInvoiceItem = result.unwrap();
expect(customerCustomerInvoiceItem.totalPrice.value).toBe(100); // 25 * 4
});
it("debería manejar correctamente un descuento del 100%", () => {
const props = {
description: new CustomerInvoiceItemDescription("Producto E"),
quantity: new Quantity(2),
unitPrice: new MoneyValue(50),
discount: new Percentage(100),
};
const result = CustomerInvoiceItem.create(props);
expect(result.isOk()).toBe(true);
const customerCustomerInvoiceItem = result.unwrap();
expect(customerCustomerInvoiceItem.totalPrice.value).toBe(0); // (50 * 2) - 100% de (50 * 2)
});
});

View File

@ -0,0 +1,101 @@
import { DomainEntity, UniqueID } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import {
CustomerInvoiceItemDescription,
CustomerInvoiceItemDiscount,
CustomerInvoiceItemQuantity,
CustomerInvoiceItemSubtotalPrice,
CustomerInvoiceItemTotalPrice,
CustomerInvoiceItemUnitPrice,
} from "../../value-objects";
export interface ICustomerInvoiceItemProps {
description: CustomerInvoiceItemDescription;
quantity: CustomerInvoiceItemQuantity; // Cantidad de unidades
unitPrice: CustomerInvoiceItemUnitPrice; // Precio unitario en la moneda de la factura
//subtotalPrice?: MoneyValue; // Precio unitario * Cantidad
discount: CustomerInvoiceItemDiscount; // % descuento
//totalPrice?: MoneyValue;
}
export interface ICustomerInvoiceItem {
id: UniqueID;
description: CustomerInvoiceItemDescription;
quantity: CustomerInvoiceItemQuantity;
unitPrice: CustomerInvoiceItemUnitPrice;
subtotalPrice: CustomerInvoiceItemSubtotalPrice;
discount: CustomerInvoiceItemDiscount;
totalPrice: CustomerInvoiceItemTotalPrice;
}
export class CustomerInvoiceItem extends DomainEntity<ICustomerInvoiceItemProps> implements ICustomerInvoiceItem {
private _subtotalPrice!: CustomerInvoiceItemSubtotalPrice;
private _totalPrice!: CustomerInvoiceItemTotalPrice;
public static create(props: ICustomerInvoiceItemProps, id?: UniqueID): Result<CustomerInvoiceItem, Error> {
const item = new CustomerInvoiceItem(props, id);
// Reglas de negocio / validaciones
// ...
// ...
// 🔹 Disparar evento de dominio "CustomerInvoiceItemCreatedEvent"
//const { customerCustomerInvoice } = props;
//user.addDomainEvent(new CustomerInvoiceAuthenticatedEvent(id, customerCustomerInvoice.toString()));
return Result.ok(item);
}
get description(): CustomerInvoiceItemDescription {
return this.props.description;
}
get quantity(): CustomerInvoiceItemQuantity {
return this.props.quantity;
}
get unitPrice(): CustomerInvoiceItemUnitPrice {
return this.props.unitPrice;
}
get subtotalPrice(): CustomerInvoiceItemSubtotalPrice {
if (!this._subtotalPrice) {
this._subtotalPrice = this.calculateSubtotal();
}
return this._subtotalPrice;
}
get discount(): CustomerInvoiceItemDiscount {
return this.props.discount;
}
get totalPrice(): CustomerInvoiceItemTotalPrice {
if (!this._totalPrice) {
this._totalPrice = this.calculateTotal();
}
return this._totalPrice;
}
getValue() {
return this.props;
}
toPrimitive() {
return {
description: this.description.toPrimitive(),
quantity: this.quantity.toPrimitive(),
unit_price: this.unitPrice.toPrimitive(),
subtotal_price: this.subtotalPrice.toPrimitive(),
discount: this.discount.toPrimitive(),
total_price: this.totalPrice.toPrimitive(),
};
}
calculateSubtotal(): CustomerInvoiceItemSubtotalPrice {
return this.unitPrice.multiply(this.quantity.toNumber()); // Precio unitario * Cantidad
}
calculateTotal(): CustomerInvoiceItemTotalPrice {
return this.subtotalPrice.subtract(this.subtotalPrice.percentage(this.discount.toNumber()));
}
}

View File

@ -0,0 +1,8 @@
import { Collection } from "@repo/rdx-utils";
import { CustomerInvoiceItem } from "./customerCustomerInvoice-item";
export class CustomerInvoiceItems extends Collection<CustomerInvoiceItem> {
public static create(items?: CustomerInvoiceItem[]): CustomerInvoiceItems {
return new CustomerInvoiceItems(items);
}
}

View File

@ -0,0 +1,13 @@
import { Criteria } from "@repo/rdx-criteria/server";
import { UniqueID } from "@repo/rdx-ddd";
import { Collection, Result } from "@repo/rdx-utils";
import { CustomerInvoice } from "../aggregates";
export interface ICustomerInvoiceRepository {
findAll(criteria: Criteria, transaction?: any): Promise<Result<Collection<CustomerInvoice>, Error>>;
getById(id: UniqueID, transaction?: any): Promise<Result<CustomerInvoice, Error>>;
deleteById(id: UniqueID, transaction?: any): Promise<Result<boolean, Error>>;
create(customerCustomerInvoice: CustomerInvoice, transaction?: any): Promise<void>;
update(customerCustomerInvoice: CustomerInvoice, transaction?: any): Promise<void>;
}

View File

@ -0,0 +1 @@
export * from "./customerCustomerInvoice-repository.interface";

View File

@ -0,0 +1,23 @@
import { Criteria } from "@repo/rdx-criteria/server";
import { UniqueID } from "@repo/rdx-ddd";
import { Collection, Result } from "@repo/rdx-utils";
import { ICustomerInvoiceProps, CustomerInvoice } from "../aggregates";
export interface ICustomerInvoiceService {
findCustomerInvoices(criteria: Criteria, transaction?: any): Promise<Result<Collection<CustomerInvoice>, Error>>;
findCustomerInvoiceById(customerCustomerInvoiceId: UniqueID, transaction?: any): Promise<Result<CustomerInvoice>>;
updateCustomerInvoiceById(
customerCustomerInvoiceId: UniqueID,
data: Partial<ICustomerInvoiceProps>,
transaction?: any
): Promise<Result<CustomerInvoice, Error>>;
createCustomerInvoice(
customerCustomerInvoiceId: UniqueID,
data: ICustomerInvoiceProps,
transaction?: any
): Promise<Result<CustomerInvoice, Error>>;
deleteCustomerInvoiceById(customerCustomerInvoiceId: UniqueID, transaction?: any): Promise<Result<boolean, Error>>;
}

View File

@ -0,0 +1,86 @@
import { Criteria } from "@repo/rdx-criteria/server";
import { UniqueID } from "@repo/rdx-ddd";
import { Collection, Result } from "@repo/rdx-utils";
import { Transaction } from "sequelize";
import { ICustomerInvoiceProps, CustomerInvoice } from "../aggregates";
import { ICustomerInvoiceRepository } from "../repositories";
import { ICustomerInvoiceService } from "./customerCustomerInvoice-service.interface";
export class CustomerInvoiceService implements ICustomerInvoiceService {
constructor(private readonly repo: ICustomerInvoiceRepository) {}
async findCustomerInvoices(
criteria: Criteria,
transaction?: Transaction
): Promise<Result<Collection<CustomerInvoice>, Error>> {
const customerCustomerInvoicesOrError = await this.repo.findAll(criteria, transaction);
if (customerCustomerInvoicesOrError.isFailure) {
return Result.fail(customerCustomerInvoicesOrError.error);
}
// Solo devolver usuarios activos
//const allCustomerInvoices = customerCustomerInvoicesOrError.data.filter((customerCustomerInvoice) => customerCustomerInvoice.isActive);
//return Result.ok(new Collection(allCustomerInvoices));
return customerCustomerInvoicesOrError;
}
async findCustomerInvoiceById(customerCustomerInvoiceId: UniqueID, transaction?: Transaction): Promise<Result<CustomerInvoice>> {
return await this.repo.getById(customerCustomerInvoiceId, transaction);
}
async updateCustomerInvoiceById(
customerCustomerInvoiceId: UniqueID,
data: Partial<ICustomerInvoiceProps>,
transaction?: Transaction
): Promise<Result<CustomerInvoice, Error>> {
// Verificar si la factura existe
const customerCustomerInvoiceOrError = await this.repo.getById(customerCustomerInvoiceId, transaction);
if (customerCustomerInvoiceOrError.isFailure) {
return Result.fail(new Error("CustomerInvoice not found"));
}
return Result.fail(new Error("No implementado"));
/*const updatedCustomerInvoiceOrError = CustomerInvoice.update(customerCustomerInvoiceOrError.data, data);
if (updatedCustomerInvoiceOrError.isFailure) {
return Result.fail(
new Error(`Error updating customerCustomerInvoice: ${updatedCustomerInvoiceOrError.error.message}`)
);
}
const updateCustomerInvoice = updatedCustomerInvoiceOrError.data;
await this.repo.update(updateCustomerInvoice, transaction);
return Result.ok(updateCustomerInvoice);*/
}
async createCustomerInvoice(
customerCustomerInvoiceId: UniqueID,
data: ICustomerInvoiceProps,
transaction?: Transaction
): Promise<Result<CustomerInvoice, Error>> {
// Verificar si la factura existe
const customerCustomerInvoiceOrError = await this.repo.getById(customerCustomerInvoiceId, transaction);
if (customerCustomerInvoiceOrError.isSuccess) {
return Result.fail(new Error("CustomerInvoice exists"));
}
const newCustomerInvoiceOrError = CustomerInvoice.create(data, customerCustomerInvoiceId);
if (newCustomerInvoiceOrError.isFailure) {
return Result.fail(new Error(`Error creating customerCustomerInvoice: ${newCustomerInvoiceOrError.error.message}`));
}
const newCustomerInvoice = newCustomerInvoiceOrError.data;
await this.repo.create(newCustomerInvoice, transaction);
return Result.ok(newCustomerInvoice);
}
async deleteCustomerInvoiceById(
customerCustomerInvoiceId: UniqueID,
transaction?: Transaction
): Promise<Result<boolean, Error>> {
return this.repo.deleteById(customerCustomerInvoiceId, transaction);
}
}

View File

@ -0,0 +1,2 @@
export * from "./customerCustomerInvoice-service.interface";
export * from "./customerCustomerInvoice.service";

View File

@ -1,7 +1,7 @@
import { ValueObject } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
interface IInvoiceAddressTypeProps {
interface ICustomerInvoiceAddressTypeProps {
value: string;
}
@ -10,10 +10,10 @@ export enum INVOICE_ADDRESS_TYPE {
BILLING = "billing",
}
export class InvoiceAddressType extends ValueObject<IInvoiceAddressTypeProps> {
export class CustomerInvoiceAddressType extends ValueObject<ICustomerInvoiceAddressTypeProps> {
private static readonly ALLOWED_TYPES = ["shipping", "billing"];
static create(value: string): Result<InvoiceAddressType, Error> {
static create(value: string): Result<CustomerInvoiceAddressType, Error> {
if (!this.ALLOWED_TYPES.includes(value)) {
return Result.fail(
new Error(
@ -21,7 +21,7 @@ export class InvoiceAddressType extends ValueObject<IInvoiceAddressTypeProps> {
)
);
}
return Result.ok(new InvoiceAddressType({ value }));
return Result.ok(new CustomerInvoiceAddressType({ value }));
}
getValue(): string {

View File

@ -0,0 +1,50 @@
import { ValueObject } from "@repo/rdx-ddd";
import { Maybe, Result } from "@repo/rdx-utils";
import { z } from "zod";
interface ICustomerInvoiceItemDescriptionProps {
value: string;
}
export class CustomerInvoiceItemDescription extends ValueObject<ICustomerInvoiceItemDescriptionProps> {
private static readonly MAX_LENGTH = 255;
protected static validate(value: string) {
const schema = z
.string()
.trim()
.max(CustomerInvoiceItemDescription.MAX_LENGTH, {
message: `Description must be at most ${CustomerInvoiceItemDescription.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const valueIsValid = CustomerInvoiceItemDescription.validate(value);
if (!valueIsValid.success) {
return Result.fail(new Error(valueIsValid.error.errors[0].message));
}
return Result.ok(new CustomerInvoiceItemDescription({ value }));
}
static createNullable(value?: string): Result<Maybe<CustomerInvoiceItemDescription>, Error> {
if (!value || value.trim() === "") {
return Result.ok(Maybe.none<CustomerInvoiceItemDescription>());
}
return CustomerInvoiceItemDescription.create(value).map((value) => Maybe.some(value));
}
getValue(): string {
return this.props.value;
}
toString(): string {
return this.getValue();
}
toPrimitive() {
return this.getValue();
}
}

View File

@ -0,0 +1,3 @@
import { Percentage } from "@repo/rdx-ddd";
export class CustomerInvoiceItemDiscount extends Percentage {}

View File

@ -0,0 +1,3 @@
import { Quantity } from "@repo/rdx-ddd";
export class CustomerInvoiceItemQuantity extends Quantity {}

View File

@ -1,6 +1,6 @@
import { IMoneyValueProps, MoneyValue } from "@repo/rdx-ddd";
export class InvoiceItemSubtotalPrice extends MoneyValue {
export class CustomerInvoiceItemSubtotalPrice extends MoneyValue {
public static DEFAULT_SCALE = 4;
static create({ amount, currency_code, scale }: IMoneyValueProps) {

View File

@ -1,6 +1,6 @@
import { IMoneyValueProps, MoneyValue } from "@repo/rdx-ddd";
export class InvoiceItemTotalPrice extends MoneyValue {
export class CustomerInvoiceItemTotalPrice extends MoneyValue {
public static DEFAULT_SCALE = 4;
static create({ amount, currency_code, scale }: IMoneyValueProps) {

View File

@ -1,6 +1,6 @@
import { IMoneyValueProps, MoneyValue } from "@repo/rdx-ddd";
export class InvoiceItemUnitPrice extends MoneyValue {
export class CustomerInvoiceItemUnitPrice extends MoneyValue {
public static DEFAULT_SCALE = 4;
static create({ amount, currency_code, scale }: IMoneyValueProps) {

View File

@ -2,30 +2,30 @@ import { ValueObject } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import { z } from "zod";
interface IInvoiceNumberProps {
interface ICustomerInvoiceNumberProps {
value: string;
}
export class InvoiceNumber extends ValueObject<IInvoiceNumberProps> {
export class CustomerInvoiceNumber extends ValueObject<ICustomerInvoiceNumberProps> {
private static readonly MAX_LENGTH = 255;
protected static validate(value: string) {
const schema = z
.string()
.trim()
.max(InvoiceNumber.MAX_LENGTH, {
message: `Name must be at most ${InvoiceNumber.MAX_LENGTH} characters long`,
.max(CustomerInvoiceNumber.MAX_LENGTH, {
message: `Name must be at most ${CustomerInvoiceNumber.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const valueIsValid = InvoiceNumber.validate(value);
const valueIsValid = CustomerInvoiceNumber.validate(value);
if (!valueIsValid.success) {
return Result.fail(new Error(valueIsValid.error.errors[0].message));
}
return Result.ok(new InvoiceNumber({ value }));
return Result.ok(new CustomerInvoiceNumber({ value }));
}
getValue(): string {

View File

@ -2,38 +2,38 @@ import { ValueObject } from "@repo/rdx-ddd";
import { Maybe, Result } from "@repo/rdx-utils";
import { z } from "zod";
interface IInvoiceSerieProps {
interface ICustomerInvoiceSerieProps {
value: string;
}
export class InvoiceSerie extends ValueObject<IInvoiceSerieProps> {
export class CustomerInvoiceSerie extends ValueObject<ICustomerInvoiceSerieProps> {
private static readonly MAX_LENGTH = 255;
protected static validate(value: string) {
const schema = z
.string()
.trim()
.max(InvoiceSerie.MAX_LENGTH, {
message: `Name must be at most ${InvoiceSerie.MAX_LENGTH} characters long`,
.max(CustomerInvoiceSerie.MAX_LENGTH, {
message: `Name must be at most ${CustomerInvoiceSerie.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const valueIsValid = InvoiceSerie.validate(value);
const valueIsValid = CustomerInvoiceSerie.validate(value);
if (!valueIsValid.success) {
return Result.fail(new Error(valueIsValid.error.errors[0].message));
}
return Result.ok(new InvoiceSerie({ value }));
return Result.ok(new CustomerInvoiceSerie({ value }));
}
static createNullable(value?: string): Result<Maybe<InvoiceSerie>, Error> {
static createNullable(value?: string): Result<Maybe<CustomerInvoiceSerie>, Error> {
if (!value || value.trim() === "") {
return Result.ok(Maybe.none<InvoiceSerie>());
return Result.ok(Maybe.none<CustomerInvoiceSerie>());
}
return InvoiceSerie.create(value).map((value) => Maybe.some(value));
return CustomerInvoiceSerie.create(value).map((value) => Maybe.some(value));
}
getValue(): string {

View File

@ -1,7 +1,7 @@
import { ValueObject } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
interface IInvoiceStatusProps {
interface ICustomerInvoiceStatusProps {
value: string;
}
@ -11,7 +11,7 @@ export enum INVOICE_STATUS {
SENT = "sent",
REJECTED = "rejected",
}
export class InvoiceStatus extends ValueObject<IInvoiceStatusProps> {
export class CustomerInvoiceStatus extends ValueObject<ICustomerInvoiceStatusProps> {
private static readonly ALLOWED_STATUSES = ["draft", "emitted", "sent", "rejected"];
private static readonly TRANSITIONS: Record<string, string[]> = {
@ -21,36 +21,36 @@ export class InvoiceStatus extends ValueObject<IInvoiceStatusProps> {
rejected: [],
};
static create(value: string): Result<InvoiceStatus, Error> {
if (!InvoiceStatus.ALLOWED_STATUSES.includes(value)) {
static create(value: string): Result<CustomerInvoiceStatus, Error> {
if (!CustomerInvoiceStatus.ALLOWED_STATUSES.includes(value)) {
return Result.fail(new Error(`Estado de la factura no válido: ${value}`));
}
return Result.ok(
value === "rejected"
? InvoiceStatus.createRejected()
? CustomerInvoiceStatus.createRejected()
: value === "sent"
? InvoiceStatus.createSent()
? CustomerInvoiceStatus.createSent()
: value === "emitted"
? InvoiceStatus.createSent()
: InvoiceStatus.createDraft()
? CustomerInvoiceStatus.createSent()
: CustomerInvoiceStatus.createDraft()
);
}
public static createDraft(): InvoiceStatus {
return new InvoiceStatus({ value: INVOICE_STATUS.DRAFT });
public static createDraft(): CustomerInvoiceStatus {
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.DRAFT });
}
public static createEmitted(): InvoiceStatus {
return new InvoiceStatus({ value: INVOICE_STATUS.EMITTED });
public static createEmitted(): CustomerInvoiceStatus {
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.EMITTED });
}
public static createSent(): InvoiceStatus {
return new InvoiceStatus({ value: INVOICE_STATUS.SENT });
public static createSent(): CustomerInvoiceStatus {
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.SENT });
}
public static createRejected(): InvoiceStatus {
return new InvoiceStatus({ value: INVOICE_STATUS.REJECTED });
public static createRejected(): CustomerInvoiceStatus {
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.REJECTED });
}
getValue(): string {
@ -62,16 +62,16 @@ export class InvoiceStatus extends ValueObject<IInvoiceStatusProps> {
}
canTransitionTo(nextStatus: string): boolean {
return InvoiceStatus.TRANSITIONS[this.props.value].includes(nextStatus);
return CustomerInvoiceStatus.TRANSITIONS[this.props.value].includes(nextStatus);
}
transitionTo(nextStatus: string): Result<InvoiceStatus, Error> {
transitionTo(nextStatus: string): Result<CustomerInvoiceStatus, Error> {
if (!this.canTransitionTo(nextStatus)) {
return Result.fail(
new Error(`Transición no permitida de ${this.props.value} a ${nextStatus}`)
);
}
return InvoiceStatus.create(nextStatus);
return CustomerInvoiceStatus.create(nextStatus);
}
toString(): string {

View File

@ -0,0 +1,10 @@
export * from "./customerCustomerInvoice-address-type";
export * from "./customerCustomerInvoice-item-description";
export * from "./customerCustomerInvoice-item-discount";
export * from "./customerCustomerInvoice-item-quantity";
export * from "./customerCustomerInvoice-item-subtotal-price";
export * from "./customerCustomerInvoice-item-total-price";
export * from "./customerCustomerInvoice-item-unit-price";
export * from "./customerCustomerInvoice-number";
export * from "./customerCustomerInvoice-serie";
export * from "./customerCustomerInvoice-status";

View File

@ -0,0 +1,26 @@
import { IModuleServer, ModuleParams } from "@erp/core/api";
import { customerCustomerInvoicesRouter, models } from "./infrastructure";
export const customerCustomerInvoicesAPIModule: IModuleServer = {
name: "customerCustomerInvoices",
version: "1.0.0",
dependencies: [],
init(params: ModuleParams) {
// const contacts = getService<ContactsService>("contacts");
const { logger } = params;
customerCustomerInvoicesRouter(params);
logger.info("🚀 CustomerInvoices module initialized", { label: "customerCustomerInvoices" });
},
registerDependencies(params) {
const { database, logger } = params;
logger.info("🚀 CustomerInvoices module dependencies registered", { label: "customerCustomerInvoices" });
return {
models,
services: {
getCustomerInvoice: () => {},
/*...*/
},
};
},
};

View File

@ -1,9 +1,9 @@
import { ModuleParams } from "@erp/core";
import { ModuleParams } from "@erp/core/api";
import { Application, NextFunction, Request, Response, Router } from "express";
import { Sequelize } from "sequelize";
import { buildListInvoicesController } from "../../presentation";
import { buildListCustomerInvoicesController } from "../../presentation";
export const invoicesRouter = (params: ModuleParams) => {
export const customerCustomerInvoicesRouter = (params: ModuleParams) => {
const { app, database, baseRoutePath } = params as {
app: Application;
database: Sequelize;
@ -17,48 +17,48 @@ export const invoicesRouter = (params: ModuleParams) => {
//checkTabContext,
//checkUser,
(req: Request, res: Response, next: NextFunction) => {
buildListInvoicesController(database).execute(req, res, next);
buildListCustomerInvoicesController(database).execute(req, res, next);
}
);
app.use(`${baseRoutePath}/customerCustomerInvoices`, routes);
/*routes.get(
"/:invoiceId",
"/:customerCustomerInvoiceId",
//checkTabContext,
//checkUser,
(req: Request, res: Response, next: NextFunction) => {
buildGetInvoiceController(database).execute(req, res, next);
buildGetCustomerInvoiceController(database).execute(req, res, next);
}
);*/
/*routes.post(
"/",
validateAndParseBody(ICreateInvoiceRequestSchema, { sanitize: false }),
validateAndParseBody(ICreateCustomerInvoiceRequestSchema, { sanitize: false }),
//checkTabContext,
//checkUser,
(req: Request, res: Response, next: NextFunction) => {
buildCreateInvoiceController(database).execute(req, res, next);
buildCreateCustomerInvoiceController(database).execute(req, res, next);
}
);
routes.put(
"/:invoiceId",
validateAndParseBody(IUpdateInvoiceRequestSchema),
"/:customerCustomerInvoiceId",
validateAndParseBody(IUpdateCustomerInvoiceRequestSchema),
checkTabContext,
//checkUser,
(req: Request, res: Response, next: NextFunction) => {
buildUpdateInvoiceController().execute(req, res, next);
buildUpdateCustomerInvoiceController().execute(req, res, next);
}
);
routes.delete(
"/:invoiceId",
validateAndParseBody(IDeleteInvoiceRequestSchema),
"/:customerCustomerInvoiceId",
validateAndParseBody(IDeleteCustomerInvoiceRequestSchema),
checkTabContext,
//checkUser,
(req: Request, res: Response, next: NextFunction) => {
buildDeleteInvoiceController().execute(req, res, next);
buildDeleteCustomerInvoiceController().execute(req, res, next);
}
);*/
app.use(`${baseRoutePath}/invoices`, routes);
};

View File

@ -0,0 +1 @@
export * from "./customerInvoices.routes";

View File

@ -1,29 +1,29 @@
import { ISequelizeMapper, MapperParamsType, SequelizeMapper } from "@erp/core";
import { ISequelizeMapper, MapperParamsType, SequelizeMapper } from "@erp/core/api";
import { UniqueID } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import { InferCreationAttributes } from "sequelize";
import {
Invoice,
InvoiceItem,
InvoiceItemDescription,
InvoiceItemDiscount,
InvoiceItemQuantity,
InvoiceItemUnitPrice,
CustomerInvoice,
CustomerInvoiceItem,
CustomerInvoiceItemDescription,
CustomerInvoiceItemDiscount,
CustomerInvoiceItemQuantity,
CustomerInvoiceItemUnitPrice,
} from "../../domain";
import { InvoiceItemCreationAttributes, InvoiceItemModel, InvoiceModel } from "../sequelize";
import { CustomerInvoiceItemCreationAttributes, CustomerInvoiceItemModel, CustomerInvoiceModel } from "../sequelize";
export interface IInvoiceItemMapper
extends ISequelizeMapper<InvoiceItemModel, InvoiceItemCreationAttributes, InvoiceItem> {}
export interface ICustomerInvoiceItemMapper
extends ISequelizeMapper<CustomerInvoiceItemModel, CustomerInvoiceItemCreationAttributes, CustomerInvoiceItem> {}
export class InvoiceItemMapper
extends SequelizeMapper<InvoiceItemModel, InvoiceItemCreationAttributes, InvoiceItem>
implements IInvoiceItemMapper
export class CustomerInvoiceItemMapper
extends SequelizeMapper<CustomerInvoiceItemModel, CustomerInvoiceItemCreationAttributes, CustomerInvoiceItem>
implements ICustomerInvoiceItemMapper
{
public mapToDomain(
source: InvoiceItemModel,
source: CustomerInvoiceItemModel,
params?: MapperParamsType
): Result<InvoiceItem, Error> {
const { sourceParent } = params as { sourceParent: InvoiceModel };
): Result<CustomerInvoiceItem, Error> {
const { sourceParent } = params as { sourceParent: CustomerInvoiceModel };
// Validación y creación de ID único
const idOrError = UniqueID.create(source.item_id);
@ -32,13 +32,13 @@ export class InvoiceItemMapper
}
// Validación y creación de descripción
const descriptionOrError = InvoiceItemDescription.create(source.description || "");
const descriptionOrError = CustomerInvoiceItemDescription.create(source.description || "");
if (descriptionOrError.isFailure) {
return Result.fail(descriptionOrError.error);
}
// Validación y creación de cantidad
const quantityOrError = InvoiceItemQuantity.create({
const quantityOrError = CustomerInvoiceItemQuantity.create({
amount: source.quantity_amount,
scale: source.quantity_scale,
});
@ -47,17 +47,17 @@ export class InvoiceItemMapper
}
// Validación y creación de precio unitario
const unitPriceOrError = InvoiceItemUnitPrice.create({
const unitPriceOrError = CustomerInvoiceItemUnitPrice.create({
amount: source.unit_price_amount,
scale: source.unit_price_scale,
currency_code: sourceParent.invoice_currency,
currency_code: sourceParent.customerCustomerInvoice_currency,
});
if (unitPriceOrError.isFailure) {
return Result.fail(unitPriceOrError.error);
}
// Validación y creación de descuento
const discountOrError = InvoiceItemDiscount.create({
const discountOrError = CustomerInvoiceItemDiscount.create({
amount: source.discount_amount || 0,
scale: source.discount_scale || 0,
});
@ -79,7 +79,7 @@ export class InvoiceItemMapper
}
// Creación del objeto de dominio
return InvoiceItem.create(
return CustomerInvoiceItem.create(
{
description: descriptionOrError.data,
quantity: quantityOrError.data,
@ -91,17 +91,17 @@ export class InvoiceItemMapper
}
public mapToPersistence(
source: InvoiceItem,
source: CustomerInvoiceItem,
params?: MapperParamsType
): InferCreationAttributes<InvoiceItemModel, {}> {
): InferCreationAttributes<CustomerInvoiceItemModel, {}> {
const { index, sourceParent } = params as {
index: number;
sourceParent: Invoice;
sourceParent: CustomerInvoice;
};
const lineData = {
parent_id: undefined,
invoice_id: sourceParent.id.toPrimitive(),
customerCustomerInvoice_id: sourceParent.id.toPrimitive(),
item_type: "simple",
position: index,

View File

@ -0,0 +1,97 @@
import { ISequelizeMapper, MapperParamsType, SequelizeMapper } from "@erp/core/api";
import { UniqueID, UtcDate } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import { CustomerInvoice, CustomerInvoiceNumber, CustomerInvoiceSerie, CustomerInvoiceStatus } from "../../domain";
import { CustomerInvoiceCreationAttributes, CustomerInvoiceModel } from "../sequelize";
import { CustomerInvoiceItemMapper } from "./customerCustomerInvoice-item.mapper";
export interface ICustomerInvoiceMapper
extends ISequelizeMapper<CustomerInvoiceModel, CustomerInvoiceCreationAttributes, CustomerInvoice> {}
export class CustomerInvoiceMapper
extends SequelizeMapper<CustomerInvoiceModel, CustomerInvoiceCreationAttributes, CustomerInvoice>
implements ICustomerInvoiceMapper
{
private customerCustomerInvoiceItemMapper: CustomerInvoiceItemMapper;
constructor() {
super();
this.customerCustomerInvoiceItemMapper = new CustomerInvoiceItemMapper(); // Instanciar el mapper de items
}
public mapToDomain(source: CustomerInvoiceModel, params?: MapperParamsType): Result<CustomerInvoice, Error> {
const idOrError = UniqueID.create(source.id);
const statusOrError = CustomerInvoiceStatus.create(source.customerCustomerInvoice_status);
const customerCustomerInvoiceSeriesOrError = CustomerInvoiceSerie.create(source.customerCustomerInvoice_series);
const customerCustomerInvoiceNumberOrError = CustomerInvoiceNumber.create(source.customerCustomerInvoice_number);
const issueDateOrError = UtcDate.create(source.issue_date);
const operationDateOrError = UtcDate.create(source.operation_date);
const result = Result.combine([
idOrError,
statusOrError,
customerCustomerInvoiceSeriesOrError,
customerCustomerInvoiceNumberOrError,
issueDateOrError,
operationDateOrError,
]);
if (result.isFailure) {
return Result.fail(result.error);
}
// Mapear los items de la factura
const itemsOrErrors = this.customerCustomerInvoiceItemMapper.mapArrayToDomain(source.items, {
sourceParent: source,
...params,
});
if (itemsOrErrors.isFailure) {
return Result.fail(itemsOrErrors.error);
}
const customerCustomerInvoiceCurrency = source.customerCustomerInvoice_currency || "EUR";
return CustomerInvoice.create(
{
status: statusOrError.data,
customerCustomerInvoiceSeries: customerCustomerInvoiceSeriesOrError.data,
customerCustomerInvoiceNumber: customerCustomerInvoiceNumberOrError.data,
issueDate: issueDateOrError.data,
operationDate: operationDateOrError.data,
customerCustomerInvoiceCurrency,
items: itemsOrErrors.data,
},
idOrError.data
);
}
public mapToPersistence(source: CustomerInvoice, params?: MapperParamsType): CustomerInvoiceCreationAttributes {
const subtotal = source.calculateSubtotal();
const total = source.calculateTotal();
const items = this.customerCustomerInvoiceItemMapper.mapCollectionToPersistence(source.items, params);
return {
id: source.id.toString(),
customerCustomerInvoice_status: source.status.toPrimitive(),
customerCustomerInvoice_series: source.customerCustomerInvoiceSeries.toPrimitive(),
customerCustomerInvoice_number: source.customerCustomerInvoiceNumber.toPrimitive(),
issue_date: source.issueDate.toPrimitive(),
operation_date: source.operationDate.toPrimitive(),
customerCustomerInvoice_language: "es",
customerCustomerInvoice_currency: source.customerCustomerInvoiceCurrency || "EUR",
subtotal_amount: subtotal.amount,
subtotal_scale: subtotal.scale,
total_amount: total.amount,
total_scale: total.scale,
items,
};
}
}
const customerCustomerInvoiceMapper: CustomerInvoiceMapper = new CustomerInvoiceMapper();
export { customerCustomerInvoiceMapper };

View File

@ -1,50 +1,50 @@
import { ISequelizeMapper, SequelizeMapper } from "@/contexts/common/infrastructure";
import { Name, TINNumber, UniqueID } from "@shared/contexts";
import {
IInvoiceCustomerProps,
Invoice,
InvoiceCustomer,
InvoiceParticipantBillingAddress,
InvoiceParticipantShippingAddress,
ICustomerInvoiceCustomerProps,
CustomerInvoice,
CustomerInvoiceCustomer,
CustomerInvoiceParticipantBillingAddress,
CustomerInvoiceParticipantShippingAddress,
} from "../../domain";
import { IInvoicingContext } from "../InvoicingContext";
import { InvoiceParticipant_Model, TCreationInvoiceParticipant_Model } from "../sequelize";
import { CustomerInvoiceParticipant_Model, TCreationCustomerInvoiceParticipant_Model } from "../sequelize";
import {
IInvoiceParticipantAddressMapper,
createInvoiceParticipantAddressMapper,
} from "./invoiceParticipantAddress.mapper";
ICustomerInvoiceParticipantAddressMapper,
createCustomerInvoiceParticipantAddressMapper,
} from "./customerCustomerInvoiceParticipantAddress.mapper";
export interface IInvoiceParticipantMapper
export interface ICustomerInvoiceParticipantMapper
extends ISequelizeMapper<
InvoiceParticipant_Model,
TCreationInvoiceParticipant_Model,
InvoiceCustomer
CustomerInvoiceParticipant_Model,
TCreationCustomerInvoiceParticipant_Model,
CustomerInvoiceCustomer
> {}
export const createInvoiceParticipantMapper = (
export const createCustomerInvoiceParticipantMapper = (
context: IInvoicingContext
): IInvoiceParticipantMapper =>
new InvoiceParticipantMapper({
): ICustomerInvoiceParticipantMapper =>
new CustomerInvoiceParticipantMapper({
context,
addressMapper: createInvoiceParticipantAddressMapper(context),
addressMapper: createCustomerInvoiceParticipantAddressMapper(context),
});
class InvoiceParticipantMapper
class CustomerInvoiceParticipantMapper
extends SequelizeMapper<
InvoiceParticipant_Model,
TCreationInvoiceParticipant_Model,
InvoiceCustomer
CustomerInvoiceParticipant_Model,
TCreationCustomerInvoiceParticipant_Model,
CustomerInvoiceCustomer
>
implements IInvoiceParticipantMapper
implements ICustomerInvoiceParticipantMapper
{
public constructor(props: {
addressMapper: IInvoiceParticipantAddressMapper;
addressMapper: ICustomerInvoiceParticipantAddressMapper;
context: IInvoicingContext;
}) {
super(props);
}
protected toDomainMappingImpl(source: InvoiceParticipant_Model, params: any) {
protected toDomainMappingImpl(source: CustomerInvoiceParticipant_Model, params: any) {
/*if (!source.billingAddress) {
this.handleRequiredFieldError(
"billingAddress",
@ -60,20 +60,20 @@ class InvoiceParticipantMapper
}
*/
const billingAddress = source.billingAddress
? ((this.props.addressMapper as IInvoiceParticipantAddressMapper).mapToDomain(
? ((this.props.addressMapper as ICustomerInvoiceParticipantAddressMapper).mapToDomain(
source.billingAddress,
params
) as InvoiceParticipantBillingAddress)
) as CustomerInvoiceParticipantBillingAddress)
: undefined;
const shippingAddress = source.shippingAddress
? ((this.props.addressMapper as IInvoiceParticipantAddressMapper).mapToDomain(
? ((this.props.addressMapper as ICustomerInvoiceParticipantAddressMapper).mapToDomain(
source.shippingAddress,
params
) as InvoiceParticipantShippingAddress)
) as CustomerInvoiceParticipantShippingAddress)
: undefined;
const props: IInvoiceCustomerProps = {
const props: ICustomerInvoiceCustomerProps = {
tin: this.mapsValue(source, "tin", TINNumber.create),
firstName: this.mapsValue(source, "first_name", Name.create),
lastName: this.mapsValue(source, "last_name", Name.create),
@ -83,7 +83,7 @@ class InvoiceParticipantMapper
};
const id = this.mapsValue(source, "participant_id", UniqueID.create);
const participantOrError = InvoiceCustomer.create(props, id);
const participantOrError = CustomerInvoiceCustomer.create(props, id);
if (participantOrError.isFailure) {
throw participantOrError.error;
@ -93,13 +93,13 @@ class InvoiceParticipantMapper
}
protected toPersistenceMappingImpl(
source: InvoiceCustomer,
params: { sourceParent: Invoice }
): TCreationInvoiceParticipant_Model {
source: CustomerInvoiceCustomer,
params: { sourceParent: CustomerInvoice }
): TCreationCustomerInvoiceParticipant_Model {
const { sourceParent } = params;
return {
invoice_id: sourceParent.id.toPrimitive(),
customerCustomerInvoice_id: sourceParent.id.toPrimitive(),
participant_id: source.id.toPrimitive(),
tin: source.tin.toPrimitive(),
@ -108,11 +108,11 @@ class InvoiceParticipantMapper
company_name: source.companyName.toPrimitive(),
billingAddress: (
this.props.addressMapper as IInvoiceParticipantAddressMapper
this.props.addressMapper as ICustomerInvoiceParticipantAddressMapper
).mapToPersistence(source.billingAddress!, { sourceParent: source }),
shippingAddress: (
this.props.addressMapper as IInvoiceParticipantAddressMapper
this.props.addressMapper as ICustomerInvoiceParticipantAddressMapper
).mapToPersistence(source.shippingAddress!, { sourceParent: source }),
};
}

View File

@ -11,39 +11,39 @@ import {
UniqueID,
} from "@shared/contexts";
import {
IInvoiceParticipantAddressProps,
InvoiceCustomer,
InvoiceParticipantAddress,
ICustomerInvoiceParticipantAddressProps,
CustomerInvoiceCustomer,
CustomerInvoiceParticipantAddress,
} from "../../domain";
import { IInvoicingContext } from "../InvoicingContext";
import {
InvoiceParticipantAddress_Model,
TCreationInvoiceParticipantAddress_Model,
CustomerInvoiceParticipantAddress_Model,
TCreationCustomerInvoiceParticipantAddress_Model,
} from "../sequelize";
export interface IInvoiceParticipantAddressMapper
export interface ICustomerInvoiceParticipantAddressMapper
extends ISequelizeMapper<
InvoiceParticipantAddress_Model,
TCreationInvoiceParticipantAddress_Model,
InvoiceParticipantAddress
CustomerInvoiceParticipantAddress_Model,
TCreationCustomerInvoiceParticipantAddress_Model,
CustomerInvoiceParticipantAddress
> {}
export const createInvoiceParticipantAddressMapper = (
export const createCustomerInvoiceParticipantAddressMapper = (
context: IInvoicingContext
): IInvoiceParticipantAddressMapper => new InvoiceParticipantAddressMapper({ context });
): ICustomerInvoiceParticipantAddressMapper => new CustomerInvoiceParticipantAddressMapper({ context });
class InvoiceParticipantAddressMapper
class CustomerInvoiceParticipantAddressMapper
extends SequelizeMapper<
InvoiceParticipantAddress_Model,
TCreationInvoiceParticipantAddress_Model,
InvoiceParticipantAddress
CustomerInvoiceParticipantAddress_Model,
TCreationCustomerInvoiceParticipantAddress_Model,
CustomerInvoiceParticipantAddress
>
implements IInvoiceParticipantAddressMapper
implements ICustomerInvoiceParticipantAddressMapper
{
protected toDomainMappingImpl(source: InvoiceParticipantAddress_Model, params: any) {
protected toDomainMappingImpl(source: CustomerInvoiceParticipantAddress_Model, params: any) {
const id = this.mapsValue(source, "address_id", UniqueID.create);
const props: IInvoiceParticipantAddressProps = {
const props: ICustomerInvoiceParticipantAddressProps = {
type: source.type,
street: this.mapsValue(source, "street", Street.create),
city: this.mapsValue(source, "city", City.create),
@ -55,7 +55,7 @@ class InvoiceParticipantAddressMapper
notes: this.mapsValue(source, "notes", Note.create),
};
const addressOrError = InvoiceParticipantAddress.create(props, id);
const addressOrError = CustomerInvoiceParticipantAddress.create(props, id);
if (addressOrError.isFailure) {
throw addressOrError.error;
@ -65,8 +65,8 @@ class InvoiceParticipantAddressMapper
}
protected toPersistenceMappingImpl(
source: InvoiceParticipantAddress,
params: { sourceParent: InvoiceCustomer }
source: CustomerInvoiceParticipantAddress,
params: { sourceParent: CustomerInvoiceCustomer }
) {
const { sourceParent } = params;

View File

@ -0,0 +1 @@
export * from "./customerCustomerInvoice.mapper";

View File

@ -7,19 +7,19 @@ import {
NonAttribute,
Sequelize,
} from "sequelize";
import { InvoiceModel } from "./invoice.model";
import { CustomerInvoiceModel } from "./customerCustomerInvoice.model";
export type InvoiceItemCreationAttributes = InferCreationAttributes<
InvoiceItemModel,
{ omit: "invoice" }
export type CustomerInvoiceItemCreationAttributes = InferCreationAttributes<
CustomerInvoiceItemModel,
{ omit: "customerCustomerInvoice" }
>;
export class InvoiceItemModel extends Model<
InferAttributes<InvoiceItemModel>,
InferCreationAttributes<InvoiceItemModel, { omit: "invoice" }>
export class CustomerInvoiceItemModel extends Model<
InferAttributes<CustomerInvoiceItemModel>,
InferCreationAttributes<CustomerInvoiceItemModel, { omit: "customerCustomerInvoice" }>
> {
declare item_id: string;
declare invoice_id: string;
declare customerCustomerInvoice_id: string;
declare parent_id: CreationOptional<string>;
declare position: number;
@ -42,27 +42,27 @@ export class InvoiceItemModel extends Model<
declare total_amount: CreationOptional<number>;
declare total_scale: CreationOptional<number>;
declare invoice: NonAttribute<InvoiceModel>;
declare customerCustomerInvoice: NonAttribute<CustomerInvoiceModel>;
static associate(database: Sequelize) {
/*const { Invoice_Model, InvoiceItem_Model } = connection.models;
/*const { CustomerInvoice_Model, CustomerInvoiceItem_Model } = connection.models;
InvoiceItem_Model.belongsTo(Invoice_Model, {
as: "invoice",
foreignKey: "invoice_id",
CustomerInvoiceItem_Model.belongsTo(CustomerInvoice_Model, {
as: "customerCustomerInvoice",
foreignKey: "customerCustomerInvoice_id",
onDelete: "CASCADE",
});*/
}
}
export default (database: Sequelize) => {
InvoiceItemModel.init(
CustomerInvoiceItemModel.init(
{
item_id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
invoice_id: {
customerCustomerInvoice_id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
@ -159,7 +159,7 @@ export default (database: Sequelize) => {
},
{
sequelize: database,
tableName: "invoice_items",
tableName: "customerCustomerInvoice_items",
defaultScope: {},
@ -167,5 +167,5 @@ export default (database: Sequelize) => {
}
);
return InvoiceItemModel;
return CustomerInvoiceItemModel;
};

View File

@ -7,25 +7,25 @@ import {
NonAttribute,
Sequelize,
} from "sequelize";
import { InvoiceItemCreationAttributes, InvoiceItemModel } from "./invoice-item.model";
import { CustomerInvoiceItemCreationAttributes, CustomerInvoiceItemModel } from "./customerCustomerInvoice-item.model";
export type InvoiceCreationAttributes = InferCreationAttributes<InvoiceModel, { omit: "items" }> & {
items?: InvoiceItemCreationAttributes[];
export type CustomerInvoiceCreationAttributes = InferCreationAttributes<CustomerInvoiceModel, { omit: "items" }> & {
items?: CustomerInvoiceItemCreationAttributes[];
};
export class InvoiceModel extends Model<
InferAttributes<InvoiceModel>,
InferCreationAttributes<InvoiceModel, { omit: "items" }>
export class CustomerInvoiceModel extends Model<
InferAttributes<CustomerInvoiceModel>,
InferCreationAttributes<CustomerInvoiceModel, { omit: "items" }>
> {
declare id: string;
declare invoice_status: string;
declare invoice_series: CreationOptional<string>;
declare invoice_number: CreationOptional<string>;
declare customerCustomerInvoice_status: string;
declare customerCustomerInvoice_series: CreationOptional<string>;
declare customerCustomerInvoice_number: CreationOptional<string>;
declare issue_date: CreationOptional<string>;
declare operation_date: CreationOptional<string>;
declare invoice_language: string;
declare invoice_currency: string;
declare customerCustomerInvoice_language: string;
declare customerCustomerInvoice_currency: string;
// Subtotal
declare subtotal_amount: CreationOptional<number>;
@ -36,40 +36,40 @@ export class InvoiceModel extends Model<
declare total_scale: CreationOptional<number>;
// Relaciones
declare items: NonAttribute<InvoiceItemModel[]>;
//declare customer: NonAttribute<InvoiceParticipant_Model[]>;
declare items: NonAttribute<CustomerInvoiceItemModel[]>;
//declare customer: NonAttribute<CustomerInvoiceParticipant_Model[]>;
static associate(database: Sequelize) {
const { InvoiceModel, InvoiceItemModel } = database.models;
const { CustomerInvoiceModel, CustomerInvoiceItemModel } = database.models;
InvoiceModel.hasMany(InvoiceItemModel, {
CustomerInvoiceModel.hasMany(CustomerInvoiceItemModel, {
as: "items",
foreignKey: "invoice_id",
foreignKey: "customerCustomerInvoice_id",
onDelete: "CASCADE",
});
}
}
export default (database: Sequelize) => {
InvoiceModel.init(
CustomerInvoiceModel.init(
{
id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
invoice_status: {
customerCustomerInvoice_status: {
type: new DataTypes.STRING(),
allowNull: false,
},
invoice_series: {
customerCustomerInvoice_series: {
type: new DataTypes.STRING(),
allowNull: true,
defaultValue: null,
},
invoice_number: {
customerCustomerInvoice_number: {
type: new DataTypes.STRING(),
allowNull: true,
defaultValue: null,
@ -87,12 +87,12 @@ export default (database: Sequelize) => {
defaultValue: null,
},
invoice_language: {
customerCustomerInvoice_language: {
type: new DataTypes.STRING(),
allowNull: false,
},
invoice_currency: {
customerCustomerInvoice_currency: {
type: new DataTypes.STRING(3), // ISO 4217
allowNull: false,
},
@ -121,7 +121,7 @@ export default (database: Sequelize) => {
},
{
sequelize: database,
tableName: "invoices",
tableName: "customerCustomerInvoices",
paranoid: true, // softs deletes
timestamps: true,
@ -130,7 +130,7 @@ export default (database: Sequelize) => {
updatedAt: "updated_at",
deletedAt: "deleted_at",
indexes: [{ unique: true, fields: ["invoice_number"] }],
indexes: [{ unique: true, fields: ["customerCustomerInvoice_number"] }],
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
@ -140,5 +140,5 @@ export default (database: Sequelize) => {
}
);
return InvoiceModel;
return CustomerInvoiceModel;
};

View File

@ -0,0 +1,107 @@
import { SequelizeRepository } from "@erp/core/api";
import { Criteria } from "@repo/rdx-criteria/server";
import { UniqueID } from "@repo/rdx-ddd";
import { Collection, Result } from "@repo/rdx-utils";
import { Sequelize, Transaction } from "sequelize";
import { ICustomerInvoiceRepository, CustomerInvoice } from "../../domain";
import { ICustomerInvoiceMapper } from "../mappers/customerCustomerInvoice.mapper";
import { CustomerInvoiceItemModel } from "./customerCustomerInvoice-item.model";
import { CustomerInvoiceModel } from "./customerCustomerInvoice.model";
export class CustomerInvoiceRepository extends SequelizeRepository<CustomerInvoice> implements ICustomerInvoiceRepository {
private readonly _mapper!: ICustomerInvoiceMapper;
/**
* 🔹 Función personalizada para mapear errores de unicidad en autenticación
*/
private _customErrorMapper(error: Error): string | null {
if (error.name === "SequelizeUniqueConstraintError") {
return "CustomerInvoice with this email already exists";
}
return null;
}
constructor(database: Sequelize, mapper: ICustomerInvoiceMapper) {
super(database);
this._mapper = mapper;
}
async customerCustomerInvoiceExists(id: UniqueID, transaction?: Transaction): Promise<Result<boolean, Error>> {
try {
const _customerCustomerInvoice = await this._getById(CustomerInvoiceModel, id, {}, transaction);
return Result.ok(Boolean(id.equals(_customerCustomerInvoice.id)));
} catch (error: any) {
return this._handleDatabaseError(error, this._customErrorMapper);
}
}
async findAll(
criteria: Criteria,
transaction?: Transaction
): Promise<Result<Collection<CustomerInvoice>, Error>> {
try {
const rawCustomerInvoices = await CustomerInvoiceModel.findAll({
include: [
{
model: CustomerInvoiceItemModel,
as: "items",
},
],
transaction,
...this.convertCriteria(criteria),
});
console.error("aqui");
return this._mapper.mapArrayToDomain(rawCustomerInvoices);
} catch (error: unknown) {
console.error("Error in findAll", error);
return this._handleDatabaseError(error, this._customErrorMapper);
}
}
async getById(id: UniqueID, transaction?: Transaction): Promise<Result<CustomerInvoice, Error>> {
try {
const rawCustomerInvoice: any = await this._getById(
CustomerInvoiceModel,
id,
{
include: [
{
model: CustomerInvoiceItemModel,
as: "items",
},
],
},
transaction
);
if (!rawCustomerInvoice === true) {
return Result.fail(new Error(`CustomerInvoice with id ${id.toString()} not exists`));
}
} catch (error: any) {
return this._handleDatabaseError(error, this._customErrorMapper);
}
}
async deleteById(id: UniqueID, transaction?: Transaction): Promise<Result<boolean, Error>> {
try {
this._deleteById(CustomerInvoiceModel, id, false, transaction);
return Result.ok<boolean>(true);
} catch (error: any) {
return this._handleDatabaseError(error, this._customErrorMapper);
}
}
async create(customerCustomerInvoice: CustomerInvoice, transaction?: Transaction): Promise<void> {
const customerCustomerInvoiceData = this._mapper.mapToPersistence(customerCustomerInvoice);
await this._save(CustomerInvoiceModel, customerCustomerInvoice.id, customerCustomerInvoiceData, {}, transaction);
}
async update(customerCustomerInvoice: CustomerInvoice, transaction?: Transaction): Promise<void> {
const customerCustomerInvoiceData = this._mapper.mapToPersistence(customerCustomerInvoice);
await this._save(CustomerInvoiceModel, customerCustomerInvoice.id, customerCustomerInvoiceData, {}, transaction);
}
}

View File

@ -0,0 +1,106 @@
import {
CreationOptional,
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
NonAttribute,
Sequelize,
} from "sequelize";
import { CustomerInvoiceModel } from "./customerCustomerInvoice.model";
import {
CustomerInvoiceParticipantAddress_Model,
TCreationCustomerInvoiceParticipantAddress_Model,
} from "./customerCustomerInvoiceParticipantAddress.mo.del.ts.bak";
export type TCreationCustomerInvoiceParticipant_Model = InferCreationAttributes<
CustomerInvoiceParticipant_Model,
{ omit: "shippingAddress" | "billingAddress" | "customerCustomerInvoice" }
> & {
billingAddress: TCreationCustomerInvoiceParticipantAddress_Model;
shippingAddress: TCreationCustomerInvoiceParticipantAddress_Model;
};
export class CustomerInvoiceParticipant_Model extends Model<
InferAttributes<
CustomerInvoiceParticipant_Model,
{ omit: "shippingAddress" | "billingAddress" | "customerCustomerInvoice" }
>,
InferCreationAttributes<
CustomerInvoiceParticipant_Model,
{ omit: "shippingAddress" | "billingAddress" | "customerCustomerInvoice" }
>
> {
static associate(connection: Sequelize) {
const { CustomerInvoice_Model, CustomerInvoiceParticipantAddress_Model, CustomerInvoiceParticipant_Model } =
connection.models;
CustomerInvoiceParticipant_Model.belongsTo(CustomerInvoice_Model, {
as: "customerCustomerInvoice",
foreignKey: "customerCustomerInvoice_id",
onDelete: "CASCADE",
});
CustomerInvoiceParticipant_Model.hasOne(CustomerInvoiceParticipantAddress_Model, {
as: "shippingAddress",
foreignKey: "participant_id",
onDelete: "CASCADE",
});
CustomerInvoiceParticipant_Model.hasOne(CustomerInvoiceParticipantAddress_Model, {
as: "billingAddress",
foreignKey: "participant_id",
onDelete: "CASCADE",
});
}
declare participant_id: string;
declare customerCustomerInvoice_id: string;
declare tin: CreationOptional<string>;
declare company_name: CreationOptional<string>;
declare first_name: CreationOptional<string>;
declare last_name: CreationOptional<string>;
declare shippingAddress?: NonAttribute<CustomerInvoiceParticipantAddress_Model>;
declare billingAddress?: NonAttribute<CustomerInvoiceParticipantAddress_Model>;
declare customerCustomerInvoice?: NonAttribute<CustomerInvoiceModel>;
}
export default (sequelize: Sequelize) => {
CustomerInvoiceParticipant_Model.init(
{
participant_id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
customerCustomerInvoice_id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
tin: {
type: new DataTypes.STRING(),
allowNull: true,
},
company_name: {
type: new DataTypes.STRING(),
allowNull: true,
},
first_name: {
type: new DataTypes.STRING(),
allowNull: true,
},
last_name: {
type: new DataTypes.STRING(),
allowNull: true,
},
},
{
sequelize,
tableName: "customerCustomerInvoice_participants",
timestamps: false,
}
);
return CustomerInvoiceParticipant_Model;
};

View File

@ -7,20 +7,20 @@ import {
NonAttribute,
Sequelize,
} from "sequelize";
import { InvoiceParticipant_Model } from "./invoiceParticipant.mo.del.ts.bak";
import { CustomerInvoiceParticipant_Model } from "./customerCustomerInvoiceParticipant.mo.del.ts.bak";
export type TCreationInvoiceParticipantAddress_Model = InferCreationAttributes<
InvoiceParticipantAddress_Model,
export type TCreationCustomerInvoiceParticipantAddress_Model = InferCreationAttributes<
CustomerInvoiceParticipantAddress_Model,
{ omit: "participant" }
>;
export class InvoiceParticipantAddress_Model extends Model<
InferAttributes<InvoiceParticipantAddress_Model, { omit: "participant" }>,
InferCreationAttributes<InvoiceParticipantAddress_Model, { omit: "participant" }>
export class CustomerInvoiceParticipantAddress_Model extends Model<
InferAttributes<CustomerInvoiceParticipantAddress_Model, { omit: "participant" }>,
InferCreationAttributes<CustomerInvoiceParticipantAddress_Model, { omit: "participant" }>
> {
static associate(connection: Sequelize) {
const { InvoiceParticipantAddress_Model, InvoiceParticipant_Model } = connection.models;
InvoiceParticipantAddress_Model.belongsTo(InvoiceParticipant_Model, {
const { CustomerInvoiceParticipantAddress_Model, CustomerInvoiceParticipant_Model } = connection.models;
CustomerInvoiceParticipantAddress_Model.belongsTo(CustomerInvoiceParticipant_Model, {
as: "participant",
foreignKey: "participant_id",
});
@ -37,11 +37,11 @@ export class InvoiceParticipantAddress_Model extends Model<
declare phone: CreationOptional<string>;
declare email: CreationOptional<string>;
declare participant?: NonAttribute<InvoiceParticipant_Model>;
declare participant?: NonAttribute<CustomerInvoiceParticipant_Model>;
}
export default (sequelize: Sequelize) => {
InvoiceParticipantAddress_Model.init(
CustomerInvoiceParticipantAddress_Model.init(
{
address_id: {
type: new DataTypes.UUID(),
@ -86,9 +86,9 @@ export default (sequelize: Sequelize) => {
},
{
sequelize,
tableName: "invoice_participant_addresses",
tableName: "customerCustomerInvoice_participant_addresses",
}
);
return InvoiceParticipantAddress_Model;
return CustomerInvoiceParticipantAddress_Model;
};

View File

@ -0,0 +1,9 @@
import customerCustomerInvoiceItemModelInit from "./customerCustomerInvoice-item.model";
import customerCustomerInvoiceModelInit from "./customerCustomerInvoice.model";
export * from "./customerCustomerInvoice-item.model"; // exporta las clases, tipos
export * from "./customerCustomerInvoice.model";
export * from "./customerCustomerInvoice.repository";
// Array de inicializadores para que registerModels() lo use
export const models = [customerCustomerInvoiceItemModelInit, customerCustomerInvoiceModelInit];

View File

@ -0,0 +1,12 @@
import { DeleteCustomerInvoiceUseCase } from "@/contexts/customerCustomerInvoices/application";
import { ExpressController } from "@/core/common/presentation";
export class DeleteCustomerInvoiceController extends ExpressController {
public constructor(private readonly deleteCustomerInvoice: DeleteCustomerInvoiceUseCase) {
super();
}
async executeImpl(): Promise<any> {
return this.noContent();
}
}

View File

@ -0,0 +1,14 @@
import { DeleteCustomerInvoiceUseCase } from "@/contexts/customerCustomerInvoices/application";
import { CustomerInvoiceService } from "@/contexts/customerCustomerInvoices/domain";
import { customerCustomerInvoiceRepository } from "@/contexts/customerCustomerInvoices/intrastructure";
import { SequelizeTransactionManager } from "@/core/common/infrastructure";
import { DeleteCustomerInvoiceController } from "./delete-customer-invoice.controller";
export const buildDeleteCustomerInvoiceController = () => {
const transactionManager = new SequelizeTransactionManager();
const customerCustomerInvoiceService = new CustomerInvoiceService(customerCustomerInvoiceRepository);
const useCase = new DeleteCustomerInvoiceUseCase(customerCustomerInvoiceService, transactionManager);
return new DeleteCustomerInvoiceController(useCase);
};

View File

@ -0,0 +1,44 @@
import { ExpressController } from "@erp/core/api";
import { UniqueID } from "@repo/rdx-ddd";
import { GetCustomerInvoiceUseCase } from "../../application";
import { IGetCustomerInvoicePresenter } from "./presenter";
export class GetCustomerInvoiceController extends ExpressController {
public constructor(
private readonly getCustomerInvoice: GetCustomerInvoiceUseCase,
private readonly presenter: IGetCustomerInvoicePresenter
) {
super();
}
protected async executeImpl() {
const { customerCustomerInvoiceId } = this.req.params;
// Validar ID
const customerCustomerInvoiceIdOrError = UniqueID.create(customerCustomerInvoiceId);
if (customerCustomerInvoiceIdOrError.isFailure) return this.invalidInputError("CustomerInvoice ID not valid");
const customerCustomerInvoiceOrError = await this.getCustomerInvoice.execute(customerCustomerInvoiceIdOrError.data);
if (customerCustomerInvoiceOrError.isFailure) {
return this.handleError(customerCustomerInvoiceOrError.error);
}
return this.ok(this.presenter.toDTO(customerCustomerInvoiceOrError.data));
}
private handleError(error: Error) {
const message = error.message;
if (
message.includes("Database connection lost") ||
message.includes("Database request timed out")
) {
return this.unavailableError(
"Database service is currently unavailable. Please try again later."
);
}
return this.conflictError(message);
}
}

View File

@ -0,0 +1,24 @@
import { SequelizeTransactionManager } from "@erp/core/api";
import { Sequelize } from "sequelize";
import { CustomerInvoiceService } from "../../domain";
import { CustomerInvoiceRepository, customerCustomerInvoiceMapper } from "../../infrastructure";
import { GetCustomerInvoiceUseCase } from "../../application";
import { GetCustomerInvoiceController } from "./get-customer-invoice.controller";
import { getCustomerInvoicePresenter } from "./presenter";
export const buildGetCustomerInvoiceController = (database: Sequelize) => {
const transactionManager = new SequelizeTransactionManager(database);
const customerCustomerInvoiceRepository = new CustomerInvoiceRepository(
database,
customerCustomerInvoiceMapper
);
const customerCustomerInvoiceService = new CustomerInvoiceService(
customerCustomerInvoiceRepository
);
const useCase = new GetCustomerInvoiceUseCase(customerCustomerInvoiceService, transactionManager);
const presenter = getCustomerInvoicePresenter;
return new GetCustomerInvoiceController(useCase, presenter);
};

View File

@ -1,10 +1,10 @@
import { InvoiceItem } from "#/server/domain";
import { CustomerInvoiceItem } from "#/server/domain";
import { IInvoicingContext } from "#/server/intrastructure";
import { Collection } from "@rdx/core";
export const invoiceItemPresenter = (items: Collection<InvoiceItem>, context: IInvoicingContext) =>
export const customerCustomerInvoiceItemPresenter = (items: Collection<CustomerInvoiceItem>, context: IInvoicingContext) =>
items.totalCount > 0
? items.items.map((item: InvoiceItem) => ({
? items.items.map((item: CustomerInvoiceItem) => ({
description: item.description.toString(),
quantity: item.quantity.toString(),
unit_measure: "",

View File

@ -0,0 +1,26 @@
import { ICustomerInvoiceParticipant } from "@/contexts/invoicing/domain";
import { IInvoicingContext } from "@/contexts/invoicing/intrastructure/InvoicingContext";
import { ICreateCustomerInvoice_Participant_Response_DTO } from "@shared/contexts";
import { CustomerInvoiceParticipantAddressPresenter } from "./CustomerInvoiceParticipantAddress.presenter";
export const CustomerInvoiceParticipantPresenter = async (
participant: ICustomerInvoiceParticipant,
context: IInvoicingContext,
): Promise<ICreateCustomerInvoice_Participant_Response_DTO | undefined> => {
return {
id: participant.id.toString(),
tin: participant.tin.toString(),
first_name: participant.firstName.toString(),
last_name: participant.lastName.toString(),
company_name: participant.companyName.toString(),
billing_address: await CustomerInvoiceParticipantAddressPresenter(
participant.billingAddress!,
context,
),
shipping_address: await CustomerInvoiceParticipantAddressPresenter(
participant.shippingAddress!,
context,
),
};
};

View File

@ -1,11 +1,11 @@
import { InvoiceParticipantAddress } from "@/contexts/invoicing/domain";
import { CustomerInvoiceParticipantAddress } from "@/contexts/invoicing/domain";
import { IInvoicingContext } from "@/contexts/invoicing/intrastructure/InvoicingContext";
import { ICreateInvoice_AddressParticipant_Response_DTO } from "@shared/contexts";
import { ICreateCustomerInvoice_AddressParticipant_Response_DTO } from "@shared/contexts";
export const InvoiceParticipantAddressPresenter = async (
address: InvoiceParticipantAddress,
export const CustomerInvoiceParticipantAddressPresenter = async (
address: CustomerInvoiceParticipantAddress,
context: IInvoicingContext,
): Promise<ICreateInvoice_AddressParticipant_Response_DTO> => {
): Promise<ICreateCustomerInvoice_AddressParticipant_Response_DTO> => {
return {
id: address.id.toString(),
street: address.street.toString(),

View File

@ -0,0 +1,59 @@
import { IGetCustomerInvoiceResponseDTO } from "../../../../../common/dto";
import { CustomerInvoice, CustomerInvoiceItem } from "../../../../domain";
export interface IGetCustomerInvoicePresenter {
toDTO: (customerCustomerInvoice: CustomerInvoice) => IGetCustomerInvoiceResponseDTO;
}
export const getCustomerInvoicePresenter: IGetCustomerInvoicePresenter = {
toDTO: (customerCustomerInvoice: CustomerInvoice): IGetCustomerInvoiceResponseDTO => ({
id: customerCustomerInvoice.id.toPrimitive(),
customerCustomerInvoice_status: customerCustomerInvoice.status.toString(),
customerCustomerInvoice_number: customerCustomerInvoice.customerCustomerInvoiceNumber.toString(),
customerCustomerInvoice_series: customerCustomerInvoice.customerCustomerInvoiceSeries.toString(),
issue_date: customerCustomerInvoice.issueDate.toDateString(),
operation_date: customerCustomerInvoice.operationDate.toDateString(),
language_code: "ES",
currency: customerCustomerInvoice.customerCustomerInvoiceCurrency.toString(),
subtotal: customerCustomerInvoice.calculateSubtotal().toPrimitive(),
total: customerCustomerInvoice.calculateTotal().toPrimitive(),
items:
customerCustomerInvoice.items.size() > 0
? customerCustomerInvoice.items.map((item: CustomerInvoiceItem) => ({
description: item.description.toString(),
quantity: item.quantity.toPrimitive(),
unit_measure: "",
unit_price: item.unitPrice.toPrimitive(),
subtotal: item.calculateSubtotal().toPrimitive(),
//tax_amount: item.calculateTaxAmount().toPrimitive(),
total: item.calculateTotal().toPrimitive(),
}))
: [],
//sender: {}, //await CustomerInvoiceParticipantPresenter(customerCustomerInvoice.senderId, context),
/*recipient: await CustomerInvoiceParticipantPresenter(customerCustomerInvoice.recipient, context),
items: customerCustomerInvoiceItemPresenter(customerCustomerInvoice.items, context),
payment_term: {
payment_type: "",
due_date: "",
},
due_amount: {
currency: customerCustomerInvoice.currency.toString(),
precision: 2,
amount: 0,
},
custom_fields: [],
metadata: {
create_time: "",
last_updated_time: "",
delete_time: "",
},*/
}),
};

View File

@ -0,0 +1 @@
export * from "./get-customer-invoice.presenter";

View File

@ -0,0 +1,5 @@
//export * from "./create-customer-invoice";
//export * from "./delete-customer-invoice";
export * from "./get-customer-invoice";
export * from "./list-customer-invoices";
///export * from "./update-customer-invoice";

View File

@ -0,0 +1,26 @@
import { SequelizeTransactionManager } from "@erp/core/api";
import { Sequelize } from "sequelize";
import { ListCustomerInvoicesUseCase } from "../../application";
import { CustomerInvoiceService } from "../../domain";
import { CustomerInvoiceRepository, customerCustomerInvoiceMapper } from "../../infrastructure";
import { ListCustomerInvoicesController } from "./list-customer-invoices.controller";
import { listCustomerInvoicesPresenter } from "./presenter";
export const buildListCustomerInvoicesController = (database: Sequelize) => {
const transactionManager = new SequelizeTransactionManager(database);
const customerCustomerInvoiceRepository = new CustomerInvoiceRepository(
database,
customerCustomerInvoiceMapper
);
const customerCustomerInvoiceService = new CustomerInvoiceService(
customerCustomerInvoiceRepository
);
const useCase = new ListCustomerInvoicesUseCase(
customerCustomerInvoiceService,
transactionManager
);
const presenter = listCustomerInvoicesPresenter;
return new ListCustomerInvoicesController(useCase, presenter);
};

View File

@ -0,0 +1,38 @@
import { ExpressController } from "@erp/core/api";
import { ListCustomerInvoicesUseCase } from "../../application";
import { IListCustomerInvoicesPresenter } from "./presenter";
export class ListCustomerInvoicesController extends ExpressController {
public constructor(
private readonly listCustomerInvoices: ListCustomerInvoicesUseCase,
private readonly presenter: IListCustomerInvoicesPresenter
) {
super();
}
protected async executeImpl() {
const criteria = this.criteria;
const customerCustomerInvoicesOrError = await this.listCustomerInvoices.execute(criteria);
if (customerCustomerInvoicesOrError.isFailure) {
return this.handleError(customerCustomerInvoicesOrError.error);
}
return this.ok(this.presenter.toDTO(customerCustomerInvoicesOrError.data, criteria));
}
private handleError(error: Error) {
const message = error.message;
if (
message.includes("Database connection lost") ||
message.includes("Database request timed out")
) {
return this.unavailableError(
"Database service is currently unavailable. Please try again later."
);
}
return this.conflictError(message);
}
}

View File

@ -0,0 +1,22 @@
import { ICustomerInvoiceParticipant } from "@/contexts/invoicing/domain";
import { IListCustomerInvoice_Participant_Response_DTO } from "@shared/contexts";
import { CustomerInvoiceParticipantAddressPresenter } from "./CustomerInvoiceParticipantAddress.presenter";
export const CustomerInvoiceParticipantPresenter = (
participant: ICustomerInvoiceParticipant,
): IListCustomerInvoice_Participant_Response_DTO => {
return {
participant_id: participant?.id?.toString(),
tin: participant?.tin?.toString(),
first_name: participant?.firstName?.toString(),
last_name: participant?.lastName?.toString(),
company_name: participant?.companyName?.toString(),
billing_address: CustomerInvoiceParticipantAddressPresenter(
participant?.billingAddress!,
),
shipping_address: CustomerInvoiceParticipantAddressPresenter(
participant?.shippingAddress!,
),
};
};

View File

@ -1,6 +1,6 @@
export const InvoiceParticipantAddressPresenter = (
address: InvoiceParticipantAddress
): IListInvoice_AddressParticipant_Response_DTO => {
export const CustomerInvoiceParticipantAddressPresenter = (
address: CustomerInvoiceParticipantAddress
): IListCustomerInvoice_AddressParticipant_Response_DTO => {
return {
address_id: address?.id.toString(),
street: address?.street.toString(),

View File

@ -0,0 +1 @@
export * from "./list-customer-invoices.presenter";

View File

@ -0,0 +1,54 @@
import { IListResponseDTO } from "@erp/core/api";
import { Criteria } from "@repo/rdx-criteria/server";
import { Collection } from "@repo/rdx-utils";
import { IListCustomerInvoicesResponseDTO } from "../../../../common/dto";
import { CustomerInvoice } from "../../../domain";
export interface IListCustomerInvoicesPresenter {
toDTO: (
customerCustomerInvoices: Collection<CustomerInvoice>,
criteria: Criteria
) => IListResponseDTO<IListCustomerInvoicesResponseDTO>;
}
export const listCustomerInvoicesPresenter: IListCustomerInvoicesPresenter = {
toDTO: (
customerCustomerInvoices: Collection<CustomerInvoice>,
criteria: Criteria
): IListResponseDTO<IListCustomerInvoicesResponseDTO> => {
const items = customerInvoices.map((customerCustomerInvoice) => {
return {
id: customerCustomerInvoice.id.toPrimitive(),
customerCustomerInvoice_status: customerCustomerInvoice.status.toString(),
customerCustomerInvoice_number:
customerCustomerInvoice.customerCustomerInvoiceNumber.toString(),
customerCustomerInvoice_series:
customerCustomerInvoice.customerCustomerInvoiceSeries.toString(),
issue_date: customerCustomerInvoice.issueDate.toISOString(),
operation_date: customerCustomerInvoice.operationDate.toISOString(),
language_code: "ES",
currency: customerCustomerInvoice.customerCustomerInvoiceCurrency.toString(),
subtotal: customerCustomerInvoice.calculateSubtotal().toPrimitive(),
total: customerCustomerInvoice.calculateTotal().toPrimitive(),
//recipient: CustomerInvoiceParticipantPresenter(customerCustomerInvoice.recipient),
metadata: {
entity: "customerCustomerInvoice",
},
} as IListCustomerInvoicesResponseDTO;
});
const totalItems = customerInvoices.total();
return {
page: criteria.pageNumber,
per_page: criteria.pageSize,
total_pages: Math.ceil(totalItems / criteria.pageSize),
total_items: totalItems,
items: items,
};
},
};

View File

@ -1,37 +1,37 @@
import { IInvoicingContext } from "#/server/intrastructure";
import { InvoiceRepository } from "#/server/intrastructure/Invoice.repository";
import { CustomerInvoiceRepository } from "#/server/intrastructure/CustomerInvoice.repository";
export const updateInvoiceController = (context: IInvoicingContext) => {
export const updateCustomerInvoiceController = (context: IInvoicingContext) => {
const adapter = context.adapter;
const repoManager = context.repositoryManager;
repoManager.registerRepository("Invoice", (params = { transaction: null }) => {
repoManager.registerRepository("CustomerInvoice", (params = { transaction: null }) => {
const { transaction } = params;
return new InvoiceRepository({
return new CustomerInvoiceRepository({
transaction,
adapter,
mapper: createInvoiceMapper(context),
mapper: createCustomerInvoiceMapper(context),
});
});
repoManager.registerRepository("Participant", (params = { transaction: null }) => {
const { transaction } = params;
return new InvoiceParticipantRepository({
return new CustomerInvoiceParticipantRepository({
transaction,
adapter,
mapper: createInvoiceParticipantMapper(context),
mapper: createCustomerInvoiceParticipantMapper(context),
});
});
repoManager.registerRepository("ParticipantAddress", (params = { transaction: null }) => {
const { transaction } = params;
return new InvoiceParticipantAddressRepository({
return new CustomerInvoiceParticipantAddressRepository({
transaction,
adapter,
mapper: createInvoiceParticipantAddressMapper(context),
mapper: createCustomerInvoiceParticipantAddressMapper(context),
});
});
@ -45,12 +45,12 @@ export const updateInvoiceController = (context: IInvoicingContext) => {
});
});
const updateInvoiceUseCase = new UpdateInvoiceUseCase(context);
const updateCustomerInvoiceUseCase = new UpdateCustomerInvoiceUseCase(context);
return new UpdateInvoiceController(
return new UpdateCustomerInvoiceController(
{
useCase: updateInvoiceUseCase,
presenter: updateInvoicePresenter,
useCase: updateCustomerInvoiceUseCase,
presenter: updateCustomerInvoicePresenter,
},
context
);

View File

@ -1,13 +1,13 @@
import { InvoiceItem } from "@/contexts/invoicing/domain/InvoiceItems";
import { CustomerInvoiceItem } from "@/contexts/invoicing/domain/CustomerInvoiceItems";
import { IInvoicingContext } from "@/contexts/invoicing/intrastructure/InvoicingContext";
import { ICollection, IMoney_Response_DTO } from "@shared/contexts";
export const invoiceItemPresenter = (
items: ICollection<InvoiceItem>,
export const customerCustomerInvoiceItemPresenter = (
items: ICollection<CustomerInvoiceItem>,
context: IInvoicingContext
) =>
items.totalCount > 0
? items.items.map((item: InvoiceItem) => ({
? items.items.map((item: CustomerInvoiceItem) => ({
description: item.description.toString(),
quantity: item.quantity.toString(),
unit_measure: "",

View File

@ -0,0 +1,26 @@
import { ICustomerInvoiceParticipant } from "@/contexts/invoicing/domain";
import { IInvoicingContext } from "@/contexts/invoicing/intrastructure/InvoicingContext";
import { IUpdateCustomerInvoice_Participant_Response_DTO } from "@shared/contexts";
import { CustomerInvoiceParticipantAddressPresenter } from "./CustomerInvoiceParticipantAddress.presenter";
export const CustomerInvoiceParticipantPresenter = (
participant: ICustomerInvoiceParticipant,
context: IInvoicingContext,
): IUpdateCustomerInvoice_Participant_Response_DTO | undefined => {
return {
id: participant.id.toString(),
tin: participant.tin.toString(),
first_name: participant.firstName.toString(),
last_name: participant.lastName.toString(),
company_name: participant.companyName.toString(),
billing_address: CustomerInvoiceParticipantAddressPresenter(
participant.billingAddress!,
context,
),
shipping_address: CustomerInvoiceParticipantAddressPresenter(
participant.shippingAddress!,
context,
),
};
};

View File

@ -1,11 +1,11 @@
import { InvoiceParticipantAddress } from "@/contexts/invoicing/domain";
import { CustomerInvoiceParticipantAddress } from "@/contexts/invoicing/domain";
import { IInvoicingContext } from "@/contexts/invoicing/intrastructure/InvoicingContext";
import { IUpdateInvoice_AddressParticipant_Response_DTO } from "@shared/contexts";
import { IUpdateCustomerInvoice_AddressParticipant_Response_DTO } from "@shared/contexts";
export const InvoiceParticipantAddressPresenter = (
address: InvoiceParticipantAddress,
export const CustomerInvoiceParticipantAddressPresenter = (
address: CustomerInvoiceParticipantAddress,
context: IInvoicingContext,
): IUpdateInvoice_AddressParticipant_Response_DTO => {
): IUpdateCustomerInvoice_AddressParticipant_Response_DTO => {
return {
id: address.id.toString(),
street: address.street.toString(),

View File

@ -0,0 +1,33 @@
import { CustomerInvoice } from "@/contexts/invoicing/domain";
import { IInvoicingContext } from "@/contexts/invoicing/intrastructure/InvoicingContext";
import { IUpdateCustomerInvoice_Response_DTO } from "@shared/contexts";
import { customerCustomerInvoiceItemPresenter } from "./CustomerInvoiceItem.presenter";
import { CustomerInvoiceParticipantPresenter } from "./CustomerInvoiceParticipant.presenter";
export interface IUpdateCustomerInvoicePresenter {
map: (customerCustomerInvoice: CustomerInvoice, context: IInvoicingContext) => IUpdateCustomerInvoice_Response_DTO;
}
export const updateCustomerInvoicePresenter: IUpdateCustomerInvoicePresenter = {
map: (customerCustomerInvoice: CustomerInvoice, context: IInvoicingContext): IUpdateCustomerInvoice_Response_DTO => {
return {
id: customerCustomerInvoice.id.toString(),
customerCustomerInvoice_status: customerCustomerInvoice.status.toString(),
customerCustomerInvoice_number: customerCustomerInvoice.customerCustomerInvoiceNumber.toString(),
customerCustomerInvoice_series: customerCustomerInvoice.customerCustomerInvoiceSeries.toString(),
issue_date: customerCustomerInvoice.issueDate.toISO8601(),
operation_date: customerCustomerInvoice.operationDate.toISO8601(),
language_code: customerCustomerInvoice.language.toString(),
currency: customerCustomerInvoice.currency.toString(),
subtotal: customerCustomerInvoice.calculateSubtotal().toPrimitive(),
total: customerCustomerInvoice.calculateTotal().toPrimitive(),
//sender: {}, //await CustomerInvoiceParticipantPresenter(customerCustomerInvoice.senderId, context),
recipient: CustomerInvoiceParticipantPresenter(customerCustomerInvoice.recipient, context),
items: customerCustomerInvoiceItemPresenter(customerCustomerInvoice.items, context),
};
},
};

View File

@ -0,0 +1 @@
export * from "./UpdateCustomerInvoice.presenter";

View File

@ -1,16 +1,16 @@
import { IInvoicingContext } from "#/server/intrastructure";
import { ExpressController } from "@rdx/core";
import { IUpdateInvoicePresenter } from "./presenter";
import { IUpdateCustomerInvoicePresenter } from "./presenter";
export class UpdateInvoiceController extends ExpressController {
private useCase: UpdateInvoiceUseCase2;
private presenter: IUpdateInvoicePresenter;
export class UpdateCustomerInvoiceController extends ExpressController {
private useCase: UpdateCustomerInvoiceUseCase2;
private presenter: IUpdateCustomerInvoicePresenter;
private context: IInvoicingContext;
constructor(
props: {
useCase: UpdateInvoiceUseCase;
presenter: IUpdateInvoicePresenter;
useCase: UpdateCustomerInvoiceUseCase;
presenter: IUpdateCustomerInvoicePresenter;
},
context: IInvoicingContext
) {
@ -23,16 +23,16 @@ export class UpdateInvoiceController extends ExpressController {
}
async executeImpl(): Promise<any> {
const { invoiceId } = this.req.params;
const request: IUpdateInvoice_DTO = this.req.body;
const { customerCustomerInvoiceId } = this.req.params;
const request: IUpdateCustomerInvoice_DTO = this.req.body;
if (RuleValidator.validate(RuleValidator.RULE_NOT_NULL_OR_UNDEFINED, invoiceId).isFailure) {
return this.invalidInputError("Invoice Id param is required!");
if (RuleValidator.validate(RuleValidator.RULE_NOT_NULL_OR_UNDEFINED, customerCustomerInvoiceId).isFailure) {
return this.invalidInputError("CustomerInvoice Id param is required!");
}
const idOrError = UniqueID.create(invoiceId);
const idOrError = UniqueID.create(customerCustomerInvoiceId);
if (idOrError.isFailure) {
return this.invalidInputError("Invalid invoice Id param!");
return this.invalidInputError("Invalid customerCustomerInvoice Id param!");
}
try {
@ -46,7 +46,7 @@ export class UpdateInvoiceController extends ExpressController {
switch (error.code) {
case UseCaseError.NOT_FOUND_ERROR:
return this.notFoundError("Invoice not found", error);
return this.notFoundError("CustomerInvoice not found", error);
case UseCaseError.INVALID_INPUT_DATA:
return this.invalidInputError(error.message);
@ -62,9 +62,9 @@ export class UpdateInvoiceController extends ExpressController {
}
}
const invoice = <Invoice>result.object;
const customerCustomerInvoice = <CustomerInvoice>result.object;
return this.ok<IUpdateInvoice_Response_DTO>(this.presenter.map(invoice, this.context));
return this.ok<IUpdateCustomerInvoice_Response_DTO>(this.presenter.map(customerCustomerInvoice, this.context));
} catch (e: unknown) {
return this.fail(e as IServerError);
}

View File

@ -1,17 +1,17 @@
export type IListInvoicesRequestDTO = {}
export type IListCustomerInvoicesRequestDTO = {}
export interface ICreateInvoiceRequestDTO {
export interface ICreateCustomerInvoiceRequestDTO {
id: string;
invoice_number: string;
invoice_series: string;
customerCustomerInvoice_number: string;
customerCustomerInvoice_series: string;
issue_date: string;
operation_date: string;
language_code: string;
currency: string;
}
export interface IUpdateInvoiceRequestDTO {
export interface IUpdateCustomerInvoiceRequestDTO {
is_freelancer: boolean;
name: string;
trade_name: string;

View File

@ -1,11 +1,11 @@
import { IMetadataDTO, IMoneyDTO, IQuantityDTO } from "@erp/core";
export interface IListInvoicesResponseDTO {
export interface IListCustomerInvoicesResponseDTO {
id: string;
invoice_status: string;
invoice_number: string;
invoice_series: string;
customerCustomerInvoice_status: string;
customerCustomerInvoice_number: string;
customerCustomerInvoice_series: string;
issue_date: string;
operation_date: string;
language_code: string;
@ -17,12 +17,12 @@ export interface IListInvoicesResponseDTO {
metadata?: IMetadataDTO;
}
export interface IGetInvoiceResponseDTO {
export interface IGetCustomerInvoiceResponseDTO {
id: string;
invoice_status: string;
invoice_number: string;
invoice_series: string;
customerCustomerInvoice_status: string;
customerCustomerInvoice_number: string;
customerCustomerInvoice_series: string;
issue_date: string;
operation_date: string;
language_code: string;
@ -46,12 +46,12 @@ export interface IGetInvoiceResponseDTO {
metadata?: IMetadataDTO;
}
export interface ICreateInvoiceResponseDTO {
export interface ICreateCustomerInvoiceResponseDTO {
id: string;
invoice_status: string;
invoice_number: string;
invoice_series: string;
customerCustomerInvoice_status: string;
customerCustomerInvoice_number: string;
customerCustomerInvoice_series: string;
issue_date: string;
operation_date: string;
language_code: string;
@ -64,12 +64,12 @@ export interface ICreateInvoiceResponseDTO {
// Inferir el tipo en TypeScript desde el esquema Zod
//export type IUpdateAcccountResponseDTO = z.infer<typeof IUpdateAcccountResponseDTOSchema>;
export interface IUpdateInvoiceResponseDTO {
export interface IUpdateCustomerInvoiceResponseDTO {
id: string;
invoice_status: string;
invoice_number: string;
invoice_series: string;
customerCustomerInvoice_status: string;
customerCustomerInvoice_number: string;
customerCustomerInvoice_series: string;
issue_date: string;
operation_date: string;
language_code: string;

View File

@ -1,9 +1,9 @@
import { z } from "zod";
export const ICreateInvoiceRequestSchema = z.object({
export const ICreateCustomerInvoiceRequestSchema = z.object({
id: z.string().uuid(),
invoice_number: z.string().min(1),
invoice_series: z.string().min(1),
customerCustomerInvoice_number: z.string().min(1),
customerCustomerInvoice_series: z.string().min(1),
issue_date: z.string().refine((date) => {
const dateStr = z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Invalid YYYY-MM-DD format");
return dateStr.safeParse(date).success;
@ -38,6 +38,6 @@ export const ICreateInvoiceRequestSchema = z.object({
.optional(),
});
export const IUpdateInvoiceRequestSchema = z.object({});
export const IUpdateCustomerInvoiceRequestSchema = z.object({});
export const IDeleteInvoiceRequestSchema = z.object({});
export const IDeleteCustomerInvoiceRequestSchema = z.object({});

View File

@ -0,0 +1,3 @@
export * from "./customer-invoices.request.dto";
export * from "./customer-invoices.response.dto";
export * from "./customer-invoices.schemas";

View File

@ -0,0 +1,26 @@
{
"customerInvoices": {
"title": "Customer invoices",
"description": "Manage your customer invoices",
"list": {
"title": "Customer invoice list",
"description": "List all customer invoices"
},
"create": {
"title": "Create customer invoice",
"description": "Create a new customer invoice"
},
"edit": {
"title": "Edit customer invoice",
"description": "Edit the selected customer invoice"
},
"delete": {
"title": "Delete customer invoice",
"description": "Delete the selected customer invoice"
},
"view": {
"title": "View customer invoice",
"description": "View the details of the selected customer invoice"
}
}
}

Some files were not shown because too many files have changed in this diff Show More