.
This commit is contained in:
parent
27717cc9b8
commit
8e13fa18ae
@ -1,9 +1,7 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
jwt: {
|
jwt: {
|
||||||
secret_key:
|
secret_key: "9d6c903873c341816995a8be0355c6f0d6d471fc6aedacf50790e9b1e49c45b3",
|
||||||
"9d6c903873c341816995a8be0355c6f0d6d471fc6aedacf50790e9b1e49c45b3",
|
refresh_secret_key: "3972dc40c69327b65352ed097419213b0b75561169dba562410b85660bb1f305",
|
||||||
refresh_secret_key:
|
|
||||||
"3972dc40c69327b65352ed097419213b0b75561169dba562410b85660bb1f305",
|
|
||||||
token_expiration: "7d",
|
token_expiration: "7d",
|
||||||
refresh_token_expiration: "30d",
|
refresh_token_expiration: "30d",
|
||||||
},
|
},
|
||||||
@ -38,12 +36,14 @@ module.exports = {
|
|||||||
public_url: "",
|
public_url: "",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
admin: {
|
||||||
|
name: "Administrador",
|
||||||
|
email: "darranz@rodax-software.com",
|
||||||
|
password: "123456",
|
||||||
|
},
|
||||||
|
|
||||||
uploads: {
|
uploads: {
|
||||||
imports:
|
imports: process.env.UPLOAD_PATH || "/home/rodax/Documentos/BBDD/server/uploads/imports",
|
||||||
process.env.UPLOAD_PATH ||
|
documents: process.env.UPLOAD_PATH || "/home/rodax/Documentos/BBDD/server/uploads/documents",
|
||||||
"/home/rodax/Documentos/BBDD/server/uploads/imports",
|
|
||||||
documents:
|
|
||||||
process.env.UPLOAD_PATH ||
|
|
||||||
"/home/rodax/Documentos/BBDD/server/uploads/documents",
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
import { IAdapter, RepositoryBuilder } from "@/contexts/common/domain";
|
import { config } from "@/config";
|
||||||
|
import { IAdapter, Password, RepositoryBuilder } from "@/contexts/common/domain";
|
||||||
import { Email, Name, UniqueID } from "@shared/contexts";
|
import { Email, Name, UniqueID } from "@shared/contexts";
|
||||||
import { IUserRepository, User } from "../domain";
|
import { IUserRepository, User, UserRole } from "../domain";
|
||||||
|
|
||||||
export const existsUserByID = async (
|
export const existsUserByID = async (
|
||||||
id: UniqueID,
|
id: UniqueID,
|
||||||
adapter: IAdapter,
|
adapter: IAdapter,
|
||||||
repository: RepositoryBuilder<IUserRepository>,
|
repository: RepositoryBuilder<IUserRepository>
|
||||||
): Promise<boolean> => {
|
): Promise<boolean> => {
|
||||||
return await adapter
|
return await adapter
|
||||||
.startTransaction()
|
.startTransaction()
|
||||||
@ -15,19 +16,17 @@ export const existsUserByID = async (
|
|||||||
export const existsUserByName = async (
|
export const existsUserByName = async (
|
||||||
name: Name,
|
name: Name,
|
||||||
adapter: IAdapter,
|
adapter: IAdapter,
|
||||||
repository: RepositoryBuilder<IUserRepository>,
|
repository: RepositoryBuilder<IUserRepository>
|
||||||
): Promise<boolean> => {
|
): Promise<boolean> => {
|
||||||
return await adapter
|
return await adapter
|
||||||
.startTransaction()
|
.startTransaction()
|
||||||
.complete(async (t) =>
|
.complete(async (t) => repository({ transaction: t }).existsUserWithName(name));
|
||||||
repository({ transaction: t }).existsUserWithName(name),
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const findUserByID = async (
|
export const findUserByID = async (
|
||||||
id: UniqueID,
|
id: UniqueID,
|
||||||
adapter: IAdapter,
|
adapter: IAdapter,
|
||||||
repository: RepositoryBuilder<IUserRepository>,
|
repository: RepositoryBuilder<IUserRepository>
|
||||||
): Promise<User | null> => {
|
): Promise<User | null> => {
|
||||||
return await adapter
|
return await adapter
|
||||||
.startTransaction()
|
.startTransaction()
|
||||||
@ -37,23 +36,42 @@ export const findUserByID = async (
|
|||||||
export const existsUserByEmail = async (
|
export const existsUserByEmail = async (
|
||||||
email: Email,
|
email: Email,
|
||||||
adapter: IAdapter,
|
adapter: IAdapter,
|
||||||
repository: RepositoryBuilder<IUserRepository>,
|
repository: RepositoryBuilder<IUserRepository>
|
||||||
): Promise<boolean> => {
|
): Promise<boolean> => {
|
||||||
return await adapter
|
return await adapter
|
||||||
.startTransaction()
|
.startTransaction()
|
||||||
.complete(async (t) =>
|
.complete(async (t) => repository({ transaction: t }).existsUserWithEmail(email));
|
||||||
repository({ transaction: t }).existsUserWithEmail(email),
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const findUserByEmail = async (
|
export const findUserByEmail = async (
|
||||||
email: Email,
|
email: Email,
|
||||||
adapter: IAdapter,
|
adapter: IAdapter,
|
||||||
repository: RepositoryBuilder<IUserRepository>,
|
repository: RepositoryBuilder<IUserRepository>
|
||||||
): Promise<User | null> => {
|
): Promise<User | null> => {
|
||||||
return await adapter
|
return await adapter
|
||||||
.startTransaction()
|
.startTransaction()
|
||||||
.complete(async (t) =>
|
.complete(async (t) => repository({ transaction: t }).findUserByEmail(email));
|
||||||
repository({ transaction: t }).findUserByEmail(email),
|
};
|
||||||
);
|
|
||||||
|
export const initializeAdmin = async (
|
||||||
|
adapter: IAdapter,
|
||||||
|
repository: RepositoryBuilder<IUserRepository>
|
||||||
|
) => {
|
||||||
|
return await adapter.startTransaction().complete(async (t) => {
|
||||||
|
const adminExists = await repository({ transaction: t }).existsAdminUser();
|
||||||
|
|
||||||
|
if (!adminExists) {
|
||||||
|
const admin = User.create(
|
||||||
|
{
|
||||||
|
name: Name.create(config.admin.name).object,
|
||||||
|
email: Email.create(config.admin.email).object,
|
||||||
|
password: Password.createFromPlainTextPassword(config.admin.password).object,
|
||||||
|
roles: [UserRole.ROLE_ADMIN],
|
||||||
|
},
|
||||||
|
UniqueID.generateNewID().object
|
||||||
|
).object;
|
||||||
|
await repository({ transaction: t }).create(admin);
|
||||||
|
console.log("Usuario administrador creado");
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,5 @@
|
|||||||
import { IRepository } from "@/contexts/common/domain";
|
import { IRepository } from "@/contexts/common/domain";
|
||||||
import {
|
import { Email, ICollection, IQueryCriteria, Name, UniqueID } from "@shared/contexts";
|
||||||
Email,
|
|
||||||
ICollection,
|
|
||||||
IQueryCriteria,
|
|
||||||
Name,
|
|
||||||
UniqueID,
|
|
||||||
} from "@shared/contexts";
|
|
||||||
import { User } from "../entities";
|
import { User } from "../entities";
|
||||||
|
|
||||||
export interface IUserRepository extends IRepository<any> {
|
export interface IUserRepository extends IRepository<any> {
|
||||||
@ -18,6 +12,7 @@ export interface IUserRepository extends IRepository<any> {
|
|||||||
|
|
||||||
removeById(id: UniqueID): Promise<void>;
|
removeById(id: UniqueID): Promise<void>;
|
||||||
|
|
||||||
|
existsAdminUser(): Promise<boolean>;
|
||||||
existsUserWithId(id: UniqueID): Promise<boolean>;
|
existsUserWithId(id: UniqueID): Promise<boolean>;
|
||||||
existsUserWithName(name: Name): Promise<boolean>;
|
existsUserWithName(name: Name): Promise<boolean>;
|
||||||
existsUserWithEmail(email: Email): Promise<boolean>;
|
existsUserWithEmail(email: Email): Promise<boolean>;
|
||||||
|
|||||||
@ -1,15 +1,6 @@
|
|||||||
import {
|
import { ISequelizeAdapter, SequelizeRepository } from "@/contexts/common/infrastructure/sequelize";
|
||||||
ISequelizeAdapter,
|
import { Email, ICollection, IQueryCriteria, Name, UniqueID } from "@shared/contexts";
|
||||||
SequelizeRepository,
|
import { Op, Transaction } from "sequelize";
|
||||||
} from "@/contexts/common/infrastructure/sequelize";
|
|
||||||
import {
|
|
||||||
Email,
|
|
||||||
ICollection,
|
|
||||||
IQueryCriteria,
|
|
||||||
Name,
|
|
||||||
UniqueID,
|
|
||||||
} from "@shared/contexts";
|
|
||||||
import { Transaction } from "sequelize";
|
|
||||||
import { User } from "../domain";
|
import { User } from "../domain";
|
||||||
import { IUserRepository } from "../domain/repository";
|
import { IUserRepository } from "../domain/repository";
|
||||||
import { IUserContext } from "./User.context";
|
import { IUserContext } from "./User.context";
|
||||||
@ -20,10 +11,7 @@ export type QueryParams = {
|
|||||||
filters: Record<string, any>;
|
filters: Record<string, any>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class UserRepository
|
export class UserRepository extends SequelizeRepository<User> implements IUserRepository {
|
||||||
extends SequelizeRepository<User>
|
|
||||||
implements IUserRepository
|
|
||||||
{
|
|
||||||
protected mapper: IUserMapper;
|
protected mapper: IUserMapper;
|
||||||
|
|
||||||
public constructor(props: {
|
public constructor(props: {
|
||||||
@ -60,11 +48,7 @@ export class UserRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async findUserByEmail(email: Email): Promise<User | null> {
|
public async findUserByEmail(email: Email): Promise<User | null> {
|
||||||
const rawUser: any = await this._getBy(
|
const rawUser: any = await this._getBy("User_Model", "email", email.toPrimitive());
|
||||||
"User_Model",
|
|
||||||
"email",
|
|
||||||
email.toPrimitive(),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!rawUser === true) {
|
if (!rawUser === true) {
|
||||||
return null;
|
return null;
|
||||||
@ -73,12 +57,10 @@ export class UserRepository
|
|||||||
return this.mapper.mapToDomain(rawUser);
|
return this.mapper.mapToDomain(rawUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async findAll(
|
public async findAll(queryCriteria?: IQueryCriteria): Promise<ICollection<any>> {
|
||||||
queryCriteria?: IQueryCriteria,
|
|
||||||
): Promise<ICollection<any>> {
|
|
||||||
const { rows, count } = await this._findAll(
|
const { rows, count } = await this._findAll(
|
||||||
"User_Model",
|
"User_Model",
|
||||||
queryCriteria,
|
queryCriteria
|
||||||
/*{
|
/*{
|
||||||
include: [], // esto es para quitar las asociaciones al hacer la consulta
|
include: [], // esto es para quitar las asociaciones al hacer la consulta
|
||||||
}*/
|
}*/
|
||||||
@ -91,6 +73,18 @@ export class UserRepository
|
|||||||
return this._removeById("User_Model", id);
|
return this._removeById("User_Model", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async existsAdminUser(): Promise<boolean> {
|
||||||
|
const _model = this._adapter.getModel("User_Model");
|
||||||
|
|
||||||
|
// Verificar si el usuario administrador existe
|
||||||
|
const count: number = await _model.count({
|
||||||
|
where: { roles: { [Op.like]: ["%ROLE_ADMIN%"] } },
|
||||||
|
transaction: this._transaction,
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.resolve(Boolean(count !== 0));
|
||||||
|
}
|
||||||
|
|
||||||
public async existsUserWithId(id: UniqueID): Promise<boolean> {
|
public async existsUserWithId(id: UniqueID): Promise<boolean> {
|
||||||
return this._exists("User_Model", "id", id.toPrimitive());
|
return this._exists("User_Model", "id", id.toPrimitive());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,3 @@
|
|||||||
import { RepositoryManager } from "@/contexts/common/domain";
|
import { UserContext } from "@/contexts/users/infrastructure/User.context";
|
||||||
import { createSequelizeAdapter } from "@/contexts/common/infrastructure/sequelize";
|
|
||||||
|
|
||||||
export const createContextMiddleware = () => ({
|
export const createContextMiddleware = () => UserContext.getInstance();
|
||||||
adapter: createSequelizeAdapter(),
|
|
||||||
repositoryManager: RepositoryManager.getInstance(),
|
|
||||||
services: {},
|
|
||||||
});
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import { trace } from "console";
|
|||||||
import { config } from "../../config";
|
import { config } from "../../config";
|
||||||
import { app } from "../express/app";
|
import { app } from "../express/app";
|
||||||
import { initLogger } from "../logger";
|
import { initLogger } from "../logger";
|
||||||
|
import { initializeAdminUser } from "../sequelize/initializeAdminUser";
|
||||||
|
|
||||||
process.env.TZ = "UTC";
|
process.env.TZ = "UTC";
|
||||||
Settings.defaultLocale = "es-ES";
|
Settings.defaultLocale = "es-ES";
|
||||||
@ -106,6 +107,10 @@ const server: http.Server = http
|
|||||||
try {
|
try {
|
||||||
//firebirdConn.sync().then(() => {
|
//firebirdConn.sync().then(() => {
|
||||||
sequelizeConn.sync({ force: false, alter: true }).then(() => {
|
sequelizeConn.sync({ force: false, alter: true }).then(() => {
|
||||||
|
//
|
||||||
|
|
||||||
|
initializeAdminUser();
|
||||||
|
|
||||||
// Launch server
|
// Launch server
|
||||||
server.listen(currentState.server.port, () => {
|
server.listen(currentState.server.port, () => {
|
||||||
const now = DateTime.now();
|
const now = DateTime.now();
|
||||||
|
|||||||
10
server/src/infrastructure/sequelize/initializeAdminUser.ts
Normal file
10
server/src/infrastructure/sequelize/initializeAdminUser.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { initializeAdmin } from "@/contexts/users/application/userServices";
|
||||||
|
import { UserContext } from "@/contexts/users/infrastructure/User.context";
|
||||||
|
import { registerUserRepository } from "@/contexts/users/infrastructure/User.repository";
|
||||||
|
|
||||||
|
export const initializeAdminUser = () => {
|
||||||
|
const context = UserContext.getInstance();
|
||||||
|
registerUserRepository(context);
|
||||||
|
|
||||||
|
initializeAdmin(context.adapter, context.repositoryManager.getRepository("User"));
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user