Skip to content

Commit

Permalink
Merge pull request #90 from DigiChanges/feat/NR/add-multi-construtor-…
Browse files Browse the repository at this point in the history
…entities

Feat/nr/add multi construtor entities
  • Loading branch information
Murzbul authored Mar 16, 2022
2 parents 1f3ffae + 4a99cfa commit 5b5ce98
Show file tree
Hide file tree
Showing 223 changed files with 1,816 additions and 13,079 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM node:16-alpine as node

# Builder stage
FROM node AS builder
FROM node AS dev

WORKDIR /home/node/app

Expand All @@ -19,7 +19,7 @@ EXPOSE ${PORT}
ENTRYPOINT [ "yarn", "dev:watch" ]

# Final stage
FROM node AS final
FROM node AS prod

ENV NODE_ENV production

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ down:
@echo '************ ************'
docker-compose down

stop:
@echo '************ ************'
@echo '************ STOP CONTAINERS ************'
@echo '************ ************'
docker-compose stop

dev:
@echo '************ ************'
@echo '************ DEV INIT ************'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Hello! **NExp** *(Node Experience)* is a boilerplate for [**Node**](https://node
Each module is divided by business domain:

- App
- Auth
- AuthHelper
- File
- Item
- Notification
Expand Down
11 changes: 7 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ services:
node:
container_name: experience_node_1
restart: always
image: digichanges/nexp:1.0
working_dir: /usr/app
entrypoint: bash dev.build.sh
build:
context: .
dockerfile: Dockerfile
target: dev
working_dir: /home/node/app
volumes:
- .:/usr/app:cached
- .:/home/node/app:cached
- ./node_modules:/home/node/app/node_modules:cached
ports:
- "8089:8089"
networks:
Expand Down
2 changes: 1 addition & 1 deletion ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
name: "node-experience",
script: "./dist/src/index.js",
error_file: "./dist/src/logs/err.log",
watch: true,
watch: false,
instances: 1,
ignore_watch: './dist/src/logs/*',
instance_var: "0",
Expand Down
2 changes: 1 addition & 1 deletion jest-mongodb-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ module.exports = {
},
autoStart: false
}
};
};
8 changes: 2 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,19 @@ module.exports = {
coveragePathIgnorePatterns: [
"<rootDir>/node_modules/",
"<rootDir>/.eslintrc.js",
"<rootDir>/src/app.ts",
"<rootDir>/src/Logger.ts",
"<rootDir>/src/command.ts",
"<rootDir>/src/File/*",
"<rootDir>/src/AppExpress/Domain/*",
"<rootDir>/src/Infrastructure/Notifications/*",
"<rootDir>/src/User/Presentation/Commands/*",
"<rootDir>/src/Item/Presentation/Commands/*",
"<rootDir>/src/Role/Presentation/Commands/*",
"<rootDir>/src/AppExpress/Presentation/Criterias/NotificationFilter.ts",
"<rootDir>/src/AppExpress/Presentation/Criterias/NotificationSort.ts",
"<rootDir>/src/AppExpress/Presentation/Criterias/*",
"<rootDir>/src/File/Infrastructure/Repositories/FileSqlRepository.ts",
"<rootDir>/src/User/Infrastructure/Repositories/UserSqlRepository.ts",
"<rootDir>/src/Role/Infrastructure/Repositories/RoleSqlRepository.ts",
"<rootDir>/src/Item/Infrastructure/Repositories/ItemSqlRepository.ts",
"<rootDir>/src/Auth/Infrastructure/Repositories/TokenRedisRepository.ts",
"<rootDir>/src/AppExpress/Domain/UseCases/GetLogViewUseCase.ts",
"<rootDir>/src/Auth/Infrastructure/Repositories/TokenRedisRepository.ts"
],

// Indicates which provider should be used to instrument code for coverage
Expand Down
15 changes: 5 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@
"inversify-inject-decorators": "^3.1.0",
"jsonwebtoken": "^8.5.1",
"jwt-simple": "^0.5.6",
"koa": "^2.13.1",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"koa-cors": "^0.0.16",
"koa-hbs": "^0.9.0",
"koa-helmet": "^6.1.0",
"koa-pino-logger": "^3.0.0",
"koa-qs": "^3.0.0",
"koa-ratelimit": "^5.0.1",
"koa-router": "^10.0.0",
Expand All @@ -99,14 +98,12 @@
"pg": "^8.7.1",
"pg-mem": "^2.2.0",
"pg-promise": "^10.11.1",
"pino-express": "^1.1.1",
"pino-multi-stream": "^6.0.0",
"pino-pretty": "^7.2.0",
"promise-events": "^0.2.4",
"reflect-metadata": "^0.1.13",
"shelljs": "^0.8.5",
"tedis": "^0.1.12",
"ts-mixer": "^6.0.0",
"tslog": "^3.3.2",
"typeorm": "^0.2.38",
"uuid": "^8.3.2",
"web-push": "^3.4.4"
Expand All @@ -115,7 +112,7 @@
"@commitlint/cli": "^13.2.1",
"@commitlint/config-conventional": "^13.2.0",
"@parcel/transformer-typescript-tsc": "^2.3.1",
"@shelf/jest-mongodb": "^2.1.0",
"@shelf/jest-mongodb": "^2.2.1",
"@types/bcrypt": "^3.0.1",
"@types/bcryptjs": "^2.4.2",
"@types/bson": "^4.2.0",
Expand All @@ -137,7 +134,6 @@
"@types/koa-bodyparser": "^4.3.1",
"@types/koa-cors": "^0.0.2",
"@types/koa-hbs": "^1.0.7",
"@types/koa-pino-logger": "^3.0.0",
"@types/koa-ratelimit": "^4.2.3",
"@types/koa-router": "^7.4.2",
"@types/koa__multer": "^2.0.3",
Expand All @@ -150,7 +146,6 @@
"@types/node-cron": "^3.0.0",
"@types/nodemailer": "^6.4.1",
"@types/pg": "^8.6.1",
"@types/pino": "^7.0.5",
"@types/shelljs": "^0.8.8",
"@types/supertest": "^2.0.11",
"@types/uuid": "^8.3.0",
Expand All @@ -160,7 +155,7 @@
"@typescript-eslint/parser": "^5.10.2",
"concurrently": "^6.3.0",
"cpy-cli": "^3.1.1",
"eslint": "^8.8.0",
"eslint": "^8.10.0",
"husky": "^7.0.4",
"jest": "^27.4.5",
"lint-staged": "^11.2.6",
Expand All @@ -172,7 +167,7 @@
"supertest": "^6.1.3",
"ts-jest": "^27.0.7",
"ts-node": "^8.9.1",
"typescript": "^4.4.4"
"typescript": "^4.6.2"
},
"engines": {
"yarn": "^1.*"
Expand Down
6 changes: 3 additions & 3 deletions src/App/Domain/Entities/Base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 as uuidv4 } from 'uuid';
import { v4 as uuidV4 } from 'uuid';
import IBaseDomain from '../../../App/InterfaceAdapters/IBaseDomain';

abstract class Base implements IBaseDomain
Expand All @@ -10,7 +10,7 @@ abstract class Base implements IBaseDomain

constructor()
{
this._id = uuidv4();
this._id = uuidV4();
}

getId(): string
Expand All @@ -25,7 +25,7 @@ abstract class Base implements IBaseDomain

clone(): void
{
this._id = uuidv4();
this._id = uuidV4();
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/App/Domain/Payloads/PasswordPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Password from '../ValueObjects/Password';

interface PasswordPayload
{
password: Password;
}

export default PasswordPayload;
6 changes: 3 additions & 3 deletions src/App/Infrastructure/Repositories/BaseMongoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class BaseMongoRepository<T extends IBaseDomain, D extends Document & T

async update(entity: T): Promise<T>
{
return this.repository.findOneAndUpdate({ _id: entity.getId() } as FilterQuery<T>, { $set: entity } as UpdateQuery<T>, { new: true }).populate(this.populate);
return this.repository.findOneAndUpdate({ _id: entity.getId() } as FilterQuery<T>, { $set: entity } as UpdateQuery<T>, { new: true }).populate(this.populate as string | string[]) as any;
}

async delete(id: string): Promise<T>
Expand All @@ -58,7 +58,7 @@ abstract class BaseMongoRepository<T extends IBaseDomain, D extends Document & T
{
const { initThrow = true, populate = null } = options;

const entity = await this.repository.findOne(condition as FilterQuery<T>).populate(populate).exec();
const entity = await this.repository.findOne(condition as FilterQuery<T>).populate(populate as string | string[]).exec();

if (initThrow && !entity)
{
Expand All @@ -75,7 +75,7 @@ abstract class BaseMongoRepository<T extends IBaseDomain, D extends Document & T
initThrow = initThrow ?? false;
populate = populate ?? null;

const entities = await this.repository.find(condition as FilterQuery<T>).populate(populate).exec();
const entities = await this.repository.find(condition as FilterQuery<T>).populate(populate as string | string[]).exec();

if (initThrow && entities.length === 0)
{
Expand Down
6 changes: 0 additions & 6 deletions src/App/InterfaceAdapters/IKind.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/App/Presentation/Middlewares/Express/LoggerMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Logger from '../../../../Shared/Logger/Logger';

const LoggerMiddleware = (req: any, res: any, next: any) =>
{
Logger.debug(`${req.method}: ${req.path} - ${req.ip}`);
next();
};

export default LoggerMiddleware;
10 changes: 10 additions & 0 deletions src/App/Presentation/Middlewares/Koa/LoggerMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ParameterizedContext, Next } from 'koa';
import Logger from '../../../../Shared/Logger/Logger';

const LoggerMiddleware = async(ctx: ParameterizedContext, next: Next) =>
{
Logger.debug(`${ctx.status} ${ctx.method}: ${ctx.path} - ${ctx.ip}`);
await next();
};

export default LoggerMiddleware;
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import Responder from '../../Shared/Koa/Responder';
import FormatError from '../../Shared/FormatError';
import { StatusCode } from '@digichanges/shared-experience';
import RouteNotFoundHttpException from '../../Exceptions/RouteNotFoundHttpException';

const RedirectRouteNotFoundMiddleware = (ctx: any) =>
{
const responder = new Responder();
const formatError = new FormatError();

responder.error(formatError.getFormat(new RouteNotFoundHttpException()), ctx, StatusCode.HTTP_NOT_FOUND);
responder.error(new RouteNotFoundHttpException(), ctx, StatusCode.HTTP_NOT_FOUND);
};

export default RedirectRouteNotFoundMiddleware;
10 changes: 5 additions & 5 deletions src/App/Presentation/Requests/IdRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { decorate } from 'ts-mixer';

class IdRequest implements IdPayload
{
@decorate(IsUUID('4'))
id: string;
protected _id: string;

constructor({ id }: { id: string })
{
this.id = id;
this._id = id;
}

getId(): string
@decorate(IsUUID('4'))
get id(): string
{
return this.id;
return this._id;
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/App/Presentation/Shared/Express/AppExpress.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import 'reflect-metadata';
import express from 'express';
import { InversifyExpressServer } from 'inversify-express-utils';
import compression from 'compression';
import cors from 'cors';
import helmet from 'helmet';
import exphbs from 'express-handlebars';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pinoExpress = require('pino-express');

import '../../Handlers/Express/IndexHandler';
import '../../../../Item/Presentation/Handlers/Express/ItemHandler';
Expand All @@ -30,6 +27,7 @@ import Logger from '../../../../Shared/Logger/Logger';
import MainConfig from '../../../../Config/mainConfig';
import { RequestContext } from '@mikro-orm/core';
import { orm } from '../../../../Shared/Database/MikroORMCreateConnection';
import LoggerMiddleware from '../../Middlewares/Express/LoggerMiddleware';

class AppExpress implements IApp
{
Expand Down Expand Up @@ -79,7 +77,7 @@ class AppExpress implements IApp
});
}

app.use(pinoExpress(Logger));
app.use(LoggerMiddleware);
app.use('/api/', Throttle);
app.use(AuthenticationMiddleware);
app.use(VerifyTokenMiddleware);
Expand Down
2 changes: 1 addition & 1 deletion src/App/Presentation/Shared/Express/ErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ErrorHandler

if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test')
{
Logger.debug(err.stack);
Logger.trace(err.stack);
}

responder.error(formatError.getFormat(exception), req, res, exception.statusCode, exception.metadata);
Expand Down
6 changes: 3 additions & 3 deletions src/App/Presentation/Shared/Express/Responder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Transformer
} from '@digichanges/shared-experience';
import { TYPES } from '../../../../Config/Injects/types';
import IFileDTO from '../../../../File/Domain/Payloads/IFileDTO';
import IFileDTO from '../../../../File/Domain/Models/IFileDTO';
import IFormatResponder from '../../../../Shared/InterfaceAdapters/IFormatResponder';

@injectable()
Expand All @@ -34,15 +34,15 @@ class Responder

data = await transformer.handle(data);

response.status(status.code).send(this.formatResponder.getFormatData(data, status, metadata));
response.status(status.code).send(this.formatResponder.getFormatData(data, metadata));
}

// TODO: Refactor to encapsulate this logic
public async paginate(paginator: IPaginator, request: Request | any, response: Response, status: IHttpStatusCode, transformer: Transformer = null)
{
const data = await paginator.paginate();
const metadata = paginator.getMetadata();
const result = this.formatResponder.getFormatData(data, status, metadata);
const result = this.formatResponder.getFormatData(data, metadata);

if (!transformer)
{
Expand Down
3 changes: 0 additions & 3 deletions src/App/Presentation/Shared/FormatError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class FormatError
}

return {
status: statusCode.status,
code: statusCode.code,
statusCode: statusCode.statusCode,
message: statusCode.code === StatusCode.HTTP_INTERNAL_SERVER_ERROR.code ? 'Internal Error Server' : message,
errorCode,
errors: _.isEmpty(validationModels) ? null : validationModels,
Expand Down
6 changes: 1 addition & 5 deletions src/App/Presentation/Shared/FormatResponder.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { injectable } from 'inversify';
import { IStatusCode } from '@digichanges/shared-experience';
import IFormatResponder from '../../../Shared/InterfaceAdapters/IFormatResponder';

@injectable()
class FormatResponder implements IFormatResponder
{
getFormatData = (data: any, statusCode: IStatusCode, metadata: Record<string, any> = null): any =>
getFormatData = (data: unknown, metadata: Record<string, any> = null): any =>
{
return {
status: statusCode.status,
code: statusCode.code,
statusCode: statusCode.statusCode,
data,
metadata: metadata ?? undefined
};
Expand Down
Loading

0 comments on commit 5b5ce98

Please sign in to comment.