.
This commit is contained in:
parent
829c839f9b
commit
c8d4f4fa52
@ -1,4 +1,4 @@
|
||||
import { Result } from "@common/helpers";
|
||||
import { Collection, Result } from "@common/helpers";
|
||||
import { ITransactionManager } from "@common/infrastructure/database";
|
||||
import { User } from "@contexts/auth/domain";
|
||||
import { IUserService } from "@contexts/auth/domain/services";
|
||||
@ -9,9 +9,9 @@ export class ListUsersUseCase {
|
||||
private readonly transactionManager: ITransactionManager
|
||||
) {}
|
||||
|
||||
public async execute(): Promise<Result<User[], Error>> {
|
||||
return await this.transactionManager.complete(async (transaction) => {
|
||||
return await this.userService.findUsers(transaction);
|
||||
public execute(): Promise<Result<Collection<User>, Error>> {
|
||||
return this.transactionManager.complete((transaction) => {
|
||||
return this.userService.findUsers(transaction);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
import { UniqueID } from "@common/domain";
|
||||
import { Result } from "@common/helpers";
|
||||
import { Collection, Result } from "@common/helpers";
|
||||
import { User } from "../aggregates";
|
||||
|
||||
export interface IUserService {
|
||||
findUsers(transaction?: any): Promise<Result<User[], Error>>;
|
||||
findUsers(transaction?: any): Promise<Result<Collection<User>, Error>>;
|
||||
findUserById(userId: UniqueID, transaction?: any): Promise<Result<User>>;
|
||||
}
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
import { Collection, Result } from "@common/helpers";
|
||||
import { ITransactionManager } from "@common/infrastructure/database";
|
||||
import { Company } from "@contexts/companies/domain";
|
||||
import { ICompanyService } from "@contexts/companies/domain/services/company-service.interface";
|
||||
|
||||
export class ListCompaniesUseCase {
|
||||
constructor(
|
||||
private readonly companyService: ICompanyService,
|
||||
private readonly transactionManager: ITransactionManager
|
||||
) {}
|
||||
|
||||
public execute(): Promise<Result<Collection<Company>, Error>> {
|
||||
return this.transactionManager.complete((transaction) => {
|
||||
return this.companyService.findCompanies(transaction);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
import { Result } from "@common/domain";
|
||||
import { ITransactionManager } from "@common/infrastructure/database";
|
||||
import { User } from "@contexts/auth/domain";
|
||||
import { IUserService } from "@contexts/auth/domain/services";
|
||||
|
||||
export class ListCompaniesUseCase {
|
||||
constructor(
|
||||
private readonly userService: IUserService,
|
||||
private readonly transactionManager: ITransactionManager
|
||||
) {}
|
||||
|
||||
public async execute(): Promise<Result<User[], Error>> {
|
||||
return await this.transactionManager.complete(async (transaction) => {
|
||||
return await this.userService.findCompanies(transaction);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1 +1,9 @@
|
||||
export interface ICompanyRepository {}
|
||||
import { EmailAddress, UniqueID } from "@common/domain";
|
||||
import { Collection, Result } from "@common/helpers";
|
||||
import { Company } from "../aggregates";
|
||||
|
||||
export interface ICompanyRepository {
|
||||
findAll(transaction?: any): Promise<Result<Collection<Company>, Error>>;
|
||||
findById(id: UniqueID, transaction?: any): Promise<Result<Company, Error>>;
|
||||
findByEmail(email: EmailAddress, transaction?: any): Promise<Result<Company, Error>>;
|
||||
}
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import { Result, UniqueID } from "@common/domain";
|
||||
import { UniqueID } from "@common/domain";
|
||||
import { Collection, Result } from "@common/helpers";
|
||||
import { Company } from "../aggregates";
|
||||
|
||||
export interface ICompanyService {
|
||||
findCompanies(transaction?: any): Promise<Result<Company[], Error>>;
|
||||
findCompanies(transaction?: any): Promise<Result<Collection<Company>, Error>>;
|
||||
findCompanyById(userId: UniqueID, transaction?: any): Promise<Result<Company>>;
|
||||
}
|
||||
|
||||
@ -1,19 +1,20 @@
|
||||
import { Result, UniqueID } from "@common/domain";
|
||||
import { UniqueID } from "@common/domain";
|
||||
import { Collection, Result } from "@common/helpers";
|
||||
import { Company, ICompanyRepository } from "..";
|
||||
import { ICompanyService } from "./company-service.interface";
|
||||
|
||||
export class CompanyService implements ICompanyService {
|
||||
constructor(private readonly companyRepository: ICompanyRepository) {}
|
||||
|
||||
async findCompanies(transaction?: any): Promise<Result<Company[], Error>> {
|
||||
async findCompanies(transaction?: any): Promise<Result<Collection<Company>, Error>> {
|
||||
const companysOrError = await this.companyRepository.findAll(transaction);
|
||||
if (companysOrError.isFailure) {
|
||||
return Result.fail(companysOrError.error);
|
||||
}
|
||||
|
||||
// Solo devolver usuarios activos
|
||||
const activeCompanies = companysOrError.data.filter((company) => company /*.isActive*/);
|
||||
return Result.ok(activeCompanies);
|
||||
const activeCompanies = companysOrError.data.filter((company) => company.isActive);
|
||||
return Result.ok(new Collection(activeCompanies));
|
||||
}
|
||||
|
||||
async findCompanyById(companyId: UniqueID, transaction?: any): Promise<Result<Company>> {
|
||||
|
||||
@ -1,3 +1,2 @@
|
||||
export * from "./mappers";
|
||||
export * from "./passport";
|
||||
export * from "./sequelize";
|
||||
|
||||
@ -1,15 +1,16 @@
|
||||
import { SequelizeTransactionManager } from "@common/infrastructure";
|
||||
import { ListcompaniesUseCase } from "@contexts/auth/application/list-companies/list-companies.use-case";
|
||||
import { userRepository } from "@contexts/auth/infraestructure";
|
||||
import { ListcompaniesController } from "./list-companies.controller";
|
||||
import { listcompaniesPresenter } from "./list-companies.presenter";
|
||||
import { ListCompaniesUseCase } from "@contexts/companies/application/list-companies/list-companies.use-case";
|
||||
import { CompanyService } from "@contexts/companies/domain/services/company.service";
|
||||
import { companyRepository } from "@contexts/companies/infraestructure";
|
||||
import { ListCompaniesController } from "./list-companies.controller";
|
||||
import { listCompaniesPresenter } from "./list-companies.presenter";
|
||||
|
||||
export const listcompaniesController = () => {
|
||||
export const listCompaniesController = () => {
|
||||
const transactionManager = new SequelizeTransactionManager();
|
||||
const companieservice = new companieservice(userRepository);
|
||||
const companyService = new CompanyService(companyRepository);
|
||||
|
||||
const useCase = new ListcompaniesUseCase(companieservice, transactionManager);
|
||||
const presenter = listcompaniesPresenter;
|
||||
const useCase = new ListCompaniesUseCase(companyService, transactionManager);
|
||||
const presenter = listCompaniesPresenter;
|
||||
|
||||
return new ListcompaniesController(useCase, presenter);
|
||||
return new ListCompaniesController(useCase, presenter);
|
||||
};
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
import { ExpressController } from "@common/presentation";
|
||||
import { ListCompaniesUseCase } from "@contexts/companies/application/list-companies/list-users.use-case";
|
||||
import { ListCompaniesUseCase } from "@contexts/companies/application/list-companies/list-companies.use-case";
|
||||
import { IListCompaniesPresenter } from "./list-companies.presenter";
|
||||
|
||||
export class ListcompaniesController extends ExpressController {
|
||||
export class ListCompaniesController extends ExpressController {
|
||||
public constructor(
|
||||
private readonly listcompanies: ListCompaniesUseCase,
|
||||
private readonly listCompanies: ListCompaniesUseCase,
|
||||
private readonly presenter: IListCompaniesPresenter
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async executeImpl() {
|
||||
const companiesOrError = await this.listcompanies.execute();
|
||||
const companiesOrError = await this.listCompanies.execute();
|
||||
|
||||
if (companiesOrError.isFailure) {
|
||||
return this.handleError(companiesOrError.error);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { Collection, ensureString } from "@common/helpers";
|
||||
import { Collection, ensureBoolean, ensureNumber, ensureString } from "@common/helpers";
|
||||
import { Company } from "@contexts/companies/domain";
|
||||
import { IListCompaniesResponseDTO } from "../../dto";
|
||||
|
||||
|
||||
5
apps/server/src/contexts/sales/domain/index.ts
Normal file
5
apps/server/src/contexts/sales/domain/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export * from "./aggregates";
|
||||
export * from "./entities";
|
||||
export * from "./events";
|
||||
export * from "./repositories";
|
||||
export * from "./value-objects";
|
||||
2
apps/server/src/contexts/sales/infraestructure/index.ts
Normal file
2
apps/server/src/contexts/sales/infraestructure/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./mappers";
|
||||
export * from "./sequelize";
|
||||
2
apps/server/src/contexts/sales/presentation/index.ts
Normal file
2
apps/server/src/contexts/sales/presentation/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./controllers";
|
||||
export * from "./dto";
|
||||
20
apps/server/src/routes/companies.routes.ts
Normal file
20
apps/server/src/routes/companies.routes.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { validateRequestDTO } from "@common/presentation";
|
||||
import { checkTabContext } from "@contexts/auth/infraestructure";
|
||||
import { listCompaniesController, ListCompaniesSchema } from "@contexts/companies/presentation";
|
||||
import { NextFunction, Request, Response, Router } from "express";
|
||||
|
||||
export const companiesRouter = (appRouter: Router) => {
|
||||
const companiesRoutes: Router = Router({ mergeParams: true });
|
||||
|
||||
companiesRoutes.get(
|
||||
"/",
|
||||
validateRequestDTO(ListCompaniesSchema),
|
||||
checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
listCompaniesController().execute(req, res, next);
|
||||
}
|
||||
);
|
||||
|
||||
appRouter.use("/companies", companiesRoutes);
|
||||
};
|
||||
@ -1,20 +0,0 @@
|
||||
import { validateRequestDTO } from "@common/presentation";
|
||||
import { checkTabContext, checkUser } from "@contexts/auth/infraestructure";
|
||||
import { ListCompaniesSchema } from "@contexts/companies/presentation";
|
||||
import { NextFunction, Request, Response, Router } from "express";
|
||||
|
||||
export const companyRouter = (appRouter: Router) => {
|
||||
const companyRoutes: Router = Router({ mergeParams: true });
|
||||
|
||||
companyRoutes.get(
|
||||
"/",
|
||||
validateRequestDTO(ListCompaniesSchema),
|
||||
checkTabContext,
|
||||
checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
listCompaniesController().execute(req, res, next);
|
||||
}
|
||||
);
|
||||
|
||||
appRouter.use("/company", companyRoutes);
|
||||
};
|
||||
@ -16,5 +16,5 @@ export const userRouter = (appRouter: Router) => {
|
||||
}
|
||||
);
|
||||
|
||||
appRouter.use("/users", authRoutes);
|
||||
appRouter.use("/customers", authRoutes);
|
||||
};
|
||||
20
apps/server/src/routes/users.routes.ts
Normal file
20
apps/server/src/routes/users.routes.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { validateRequestDTO } from "@common/presentation";
|
||||
import { checkTabContext, checkUserIsAdmin } from "@contexts/auth/infraestructure";
|
||||
import { listUsersController, ListUsersSchema } from "@contexts/auth/presentation";
|
||||
import { NextFunction, Request, Response, Router } from "express";
|
||||
|
||||
export const usersRouter = (appRouter: Router) => {
|
||||
const usersRoutes: Router = Router({ mergeParams: true });
|
||||
|
||||
usersRoutes.get(
|
||||
"/",
|
||||
validateRequestDTO(ListUsersSchema),
|
||||
checkTabContext,
|
||||
checkUserIsAdmin,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
listUsersController().execute(req, res, next);
|
||||
}
|
||||
);
|
||||
|
||||
appRouter.use("/users", usersRoutes);
|
||||
};
|
||||
@ -1,6 +1,7 @@
|
||||
import { Router } from "express";
|
||||
import { authRouter } from "./auth.routes";
|
||||
import { userRouter } from "./user.routes";
|
||||
import { companiesRouter } from "./companies.routes";
|
||||
import { usersRouter } from "./users.routes";
|
||||
|
||||
export const v1Routes = () => {
|
||||
const routes = Router({ mergeParams: true });
|
||||
@ -10,7 +11,8 @@ export const v1Routes = () => {
|
||||
});
|
||||
|
||||
authRouter(routes);
|
||||
userRouter(routes);
|
||||
usersRouter(routes);
|
||||
companiesRouter(routes);
|
||||
|
||||
return routes;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user