master #1
|
@ -29,6 +29,7 @@ lerna-debug.log*
|
|||
|
||||
# IDE - VSCode
|
||||
.vscode/*
|
||||
.env
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
PORT="3346"
|
||||
|
||||
JWT_SECRET="ftyYM4t4kjuj/0ixvIrS18gpdvBJw42NnW71GrFrEhcn0alQkkH7TQIHU5MFFJ1e"
|
||||
JWT_EXPIRES="24h"
|
||||
JWT_REFRESH_EXPIRES="7d"
|
||||
ENC_KEY="921c83f3b90c92dca4ba9b947f99b4c9"
|
||||
IV="a671a96159e97a4f"
|
||||
|
||||
DEFAULT_DB_HOST="localhost"
|
||||
DEFAULT_DB_PORT="5432"
|
||||
DEFAULT_DB_USER="postgres"
|
||||
DEFAULT_DB_PASS="secret"
|
||||
DEFAULT_DB_NAME="skyworld_pos"
|
||||
|
||||
ELASTIC_APM_ACTIVATE=true
|
||||
ELASTIC_APM_SERVICE_NAME="Skyworld POS"
|
||||
ELASTIC_APM_SERVER_URL="http://172.10.10.10:8200"
|
21
package.json
21
package.json
|
@ -21,18 +21,29 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@nestjs/common": "^10.0.0",
|
||||
"@nestjs/config": "^3.2.2",
|
||||
"@nestjs/core": "^10.0.0",
|
||||
"@nestjs/cqrs": "^10.2.7",
|
||||
"@nestjs/jwt": "^10.2.0",
|
||||
"@nestjs/platform-express": "^10.0.0",
|
||||
"@nestjs/swagger": "^7.3.1",
|
||||
"@nestjs/typeorm": "^10.0.2",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.1",
|
||||
"elastic-apm-node": "^4.5.4",
|
||||
"nano": "^10.1.3",
|
||||
"pg": "^8.11.5",
|
||||
"plop": "^4.0.1",
|
||||
"reflect-metadata": "^0.2.0",
|
||||
"rxjs": "^7.5.0"
|
||||
"rxjs": "^7.5.0",
|
||||
"typeorm": "^0.3.20"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "^9.0.0",
|
||||
"@nestjs/schematics": "^9.0.0",
|
||||
"@nestjs/testing": "^9.0.0",
|
||||
"@nestjs/cli": "^10.0.0",
|
||||
"@nestjs/schematics": "^10.0.0",
|
||||
"@nestjs/testing": "^10.0.0",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/jest": "29.2.4",
|
||||
"@types/jest": "29.5.12",
|
||||
"@types/node": "18.11.18",
|
||||
"@types/supertest": "^2.0.11",
|
||||
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
||||
|
|
|
@ -4,9 +4,40 @@ import { AuthModule } from './auth/auth.module';
|
|||
import { JWTGuard } from './core/guards';
|
||||
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
|
||||
import { HttpExceptionFilter, TransformInterceptor } from './core/response';
|
||||
import { ApmModule } from './core/apm';
|
||||
import { CONNECTION_NAME } from './core/strings/constants/base.constants';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { UserPrivilegeModule } from './modules/user-related/user-privilege/user-privilege.module';
|
||||
import { UserPrivilegeModel } from './modules/user-related/user-privilege/data/model/user-privilege.model';
|
||||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
import { CouchModule } from './modules/configuration/couch/couch.module';
|
||||
|
||||
@Module({
|
||||
imports: [SessionModule, AuthModule],
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
}),
|
||||
TypeOrmModule.forRoot({
|
||||
name: CONNECTION_NAME.DEFAULT,
|
||||
type: 'postgres',
|
||||
host: process.env.DEFAULT_DB_HOST,
|
||||
port: parseInt(process.env.DEFAULT_DB_PORT),
|
||||
username: process.env.DEFAULT_DB_USER,
|
||||
password: process.env.DEFAULT_DB_PASS,
|
||||
database: process.env.DEFAULT_DB_NAME,
|
||||
entities: [
|
||||
UserPrivilegeModel,
|
||||
],
|
||||
synchronize: true,
|
||||
}),
|
||||
CqrsModule,
|
||||
SessionModule,
|
||||
AuthModule,
|
||||
CouchModule,
|
||||
|
||||
UserPrivilegeModule,
|
||||
],
|
||||
controllers: [],
|
||||
providers: [
|
||||
/**
|
||||
|
|
|
@ -19,9 +19,9 @@ export class AuthService {
|
|||
|
||||
const token = this.session.createAccessToken({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
// username: user.username,
|
||||
name: user.name,
|
||||
roles: user.roles,
|
||||
// roles: user.roles,
|
||||
});
|
||||
|
||||
return token;
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
import {
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
ExecutionContext,
|
||||
CallHandler,
|
||||
HttpException,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
import { ApmService } from './apm.service';
|
||||
|
||||
@Injectable()
|
||||
export class ApmInterceptor implements NestInterceptor {
|
||||
constructor(private readonly apmService: ApmService) {}
|
||||
|
||||
intercept(
|
||||
context: ExecutionContext,
|
||||
next: CallHandler,
|
||||
): Observable<Response> {
|
||||
const request = context.switchToHttp().getRequest<Request>();
|
||||
const user = request.headers['e-user'];
|
||||
const rules = request.headers['e-rules'];
|
||||
this.apmService.setCustomContext({ user, rules });
|
||||
|
||||
return next.handle().pipe(
|
||||
catchError((error) => {
|
||||
if (error instanceof HttpException) {
|
||||
this.apmService.captureError(error.message);
|
||||
} else {
|
||||
this.apmService.captureError(error);
|
||||
}
|
||||
throw error;
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
import { DynamicModule } from '@nestjs/common';
|
||||
import { ApmService } from './apm.service';
|
||||
import { APP_INTERCEPTOR } from '@nestjs/core';
|
||||
import { ApmInterceptor } from './apm.interceptor';
|
||||
// import { startAPM } from './start'
|
||||
|
||||
export class ApmModule {
|
||||
static register(): DynamicModule {
|
||||
return {
|
||||
module: ApmModule,
|
||||
imports: [],
|
||||
providers: [
|
||||
ApmService,
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: ApmInterceptor,
|
||||
},
|
||||
],
|
||||
exports: [ApmService],
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import * as APM from 'elastic-apm-node';
|
||||
import apm = require('elastic-apm-node');
|
||||
|
||||
@Injectable()
|
||||
export class ApmService {
|
||||
private readonly apm: apm.Agent;
|
||||
|
||||
constructor() {
|
||||
this.apm = APM;
|
||||
}
|
||||
|
||||
captureError(data: Error | string): void {
|
||||
this.apm.captureError(data);
|
||||
}
|
||||
|
||||
startTransaction(
|
||||
name?: string,
|
||||
options?: apm.TransactionOptions,
|
||||
): apm.Transaction | null {
|
||||
return this.apm.startTransaction(name, options);
|
||||
}
|
||||
|
||||
setTransactionName(name: string): void {
|
||||
this.apm.setTransactionName(name);
|
||||
}
|
||||
|
||||
startSpan(name?: string, options?: apm.SpanOptions): apm.Span | null {
|
||||
return this.apm.startSpan(name, options);
|
||||
}
|
||||
|
||||
setCustomContext(context: Record<string, unknown>): void {
|
||||
this.apm.setCustomContext(context);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
export * from './start';
|
||||
export * from './apm.module';
|
||||
export * from './apm.service';
|
||||
export * from './apm.interceptor';
|
|
@ -0,0 +1,27 @@
|
|||
import * as dotenv from 'dotenv';
|
||||
dotenv.config(); //
|
||||
|
||||
import * as apmAgent from 'elastic-apm-node';
|
||||
|
||||
const options: apmAgent.AgentConfigOptions = {
|
||||
active: process.env.ELASTIC_APM_ACTIVATE === 'true',
|
||||
};
|
||||
if (process.env.ELASTIC_APM_SERVICE_NAME) {
|
||||
options['serviceName'] = process.env.ELASTIC_APM_SERVICE_NAME;
|
||||
}
|
||||
if (process.env.ELASTIC_APM_SECRET_TOKEN) {
|
||||
options['secretToken'] = process.env.ELASTIC_APM_SECRET_TOKEN;
|
||||
}
|
||||
if (process.env.ELASTIC_APM_API_KEY) {
|
||||
options['apiKey'] = process.env.ELASTIC_APM_API_KEY;
|
||||
}
|
||||
if (process.env.ELASTIC_APM_SERVER_URL) {
|
||||
options['serverUrl'] = process.env.ELASTIC_APM_SERVER_URL;
|
||||
}
|
||||
if (process.env.ELASTIC_APM_DISABLE_INSTRUMENTATIONS) {
|
||||
options['disableInstrumentations'] =
|
||||
process.env.ELASTIC_APM_DISABLE_INSTRUMENTATIONS.split(',');
|
||||
}
|
||||
|
||||
const apm: apmAgent.Agent = apmAgent.start(options);
|
||||
export { apm };
|
|
@ -0,0 +1,63 @@
|
|||
import { Brackets, SelectQueryBuilder } from 'typeorm';
|
||||
|
||||
export interface Param {
|
||||
cols: string;
|
||||
data: string[];
|
||||
additional?: any[];
|
||||
leftJoin?: any[];
|
||||
}
|
||||
|
||||
export class SpecificSearchFilter<Entity = any> {
|
||||
constructor(
|
||||
private query: SelectQueryBuilder<Entity>,
|
||||
private readonly table: string,
|
||||
private readonly params: Param[],
|
||||
) {}
|
||||
|
||||
getFilter() {
|
||||
const params = this.params?.filter((item) => {
|
||||
return item.data !== undefined;
|
||||
});
|
||||
return this.bySearch(this.query, params);
|
||||
}
|
||||
|
||||
bySearch(query: SelectQueryBuilder<Entity>, params: Param[]) {
|
||||
query.andWhere(
|
||||
new Brackets((qb) => {
|
||||
params.forEach((param) => {
|
||||
const { cols, data, additional, leftJoin } = param;
|
||||
|
||||
const arr = data?.map((el) =>
|
||||
el.includes("'")
|
||||
? `'%${el.trim().replace(/'/g, "''").replace(/\s+/g, ' ')}%'`
|
||||
: `'%${el.trim().replace(/\s+/g, ' ')}%'`,
|
||||
);
|
||||
|
||||
const aliases = !cols.match(/\./g)
|
||||
? this.table.concat(`.${cols}`)
|
||||
: cols;
|
||||
|
||||
qb['andWhere'](`${aliases} ILIKE ANY(ARRAY[${arr}])`);
|
||||
|
||||
if (additional?.length > 0) {
|
||||
qb['orWhere'](
|
||||
new Brackets((subQb) => {
|
||||
for (const addition of additional) {
|
||||
subQb['orWhere'](`${addition}`);
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
if (leftJoin?.length) {
|
||||
for (const join of leftJoin) {
|
||||
qb['leftJoinAndSelect'](`${join.relations}`, `${join.alias}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
import { Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { BaseCoreEntity } from "../../domain/entities/base-core.entity";
|
||||
|
||||
@Entity()
|
||||
export abstract class BaseCoreModel<Entity> implements BaseCoreEntity {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import { Column, Entity } from "typeorm";
|
||||
import { BaseModel } from "./base.model";
|
||||
import { STATUS } from "src/core/strings/constants/base.constants";
|
||||
import { BaseStatusEntity } from "../../domain/entities/base-status.entity";
|
||||
|
||||
@Entity()
|
||||
export abstract class BaseStatusModel<Entity> extends BaseModel<Entity> implements BaseStatusEntity {
|
||||
@Column('enum', { name: 'status', enum: STATUS, default: STATUS.DRAFT })
|
||||
status: STATUS;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
import { Column, Entity } from 'typeorm';
|
||||
import { BaseCoreModel } from './base-core.model';
|
||||
import { BaseEntity } from '../../domain/entities/base.entity';
|
||||
|
||||
@Entity()
|
||||
export abstract class BaseModel<Entity> extends BaseCoreModel<Entity> implements BaseEntity {
|
||||
@Column('varchar', { name: 'creator_id', length: 36, nullable: true })
|
||||
creator_id: string;
|
||||
|
||||
@Column('varchar', { name: 'creator_name', length: 125, nullable: true })
|
||||
creator_name: string;
|
||||
|
||||
@Column('varchar', { name: 'editor_id', length: 36, nullable: true })
|
||||
editor_id: string;
|
||||
|
||||
@Column('varchar', { name: 'editor_name', length: 125, nullable: true })
|
||||
editor_name: string;
|
||||
|
||||
@Column({ type: 'bigint', nullable: false })
|
||||
created_at: number;
|
||||
|
||||
@Column({ type: 'bigint', nullable: false })
|
||||
updated_at: number;
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
import { EntityTarget, FindManyOptions, QueryRunner, Repository } from "typeorm";
|
||||
|
||||
export abstract class BaseDataService<Entity> {
|
||||
|
||||
constructor(private repository: Repository<Entity>) {}
|
||||
|
||||
getRepository(): Repository<Entity> {
|
||||
return this.repository;
|
||||
}
|
||||
|
||||
async create(
|
||||
queryRunner: QueryRunner,
|
||||
entityTarget: EntityTarget<Entity>,
|
||||
entity: Entity,
|
||||
): Promise<Entity> {
|
||||
const newEntity = queryRunner.manager.create(entityTarget, entity);
|
||||
return await queryRunner.manager.save(newEntity);
|
||||
}
|
||||
|
||||
async update(
|
||||
queryRunner: QueryRunner,
|
||||
entityTarget: EntityTarget<Entity>,
|
||||
filterUpdate: any,
|
||||
entity: Entity,
|
||||
): Promise<Entity> {
|
||||
const newEntity = await queryRunner.manager.findOne(entityTarget, {
|
||||
where: filterUpdate,
|
||||
});
|
||||
|
||||
if (!newEntity) throw new Error('Data not found!');
|
||||
Object.assign(newEntity, entity);
|
||||
return await queryRunner.manager.save(newEntity);
|
||||
}
|
||||
|
||||
async deleteById(
|
||||
queryRunner: QueryRunner,
|
||||
entityTarget: EntityTarget<Entity>,
|
||||
id: string,
|
||||
): Promise<void> {
|
||||
await queryRunner.manager.delete(entityTarget, { id });
|
||||
}
|
||||
|
||||
async deleteByOptions(
|
||||
queryRunner: QueryRunner,
|
||||
entityTarget: EntityTarget<Entity>,
|
||||
findManyOptions: FindManyOptions<Entity>,
|
||||
): Promise<void> {
|
||||
await queryRunner.manager.delete(entityTarget, findManyOptions);
|
||||
}
|
||||
|
||||
async getOneByOptions(findOneOptions): Promise<Entity> {
|
||||
return await this.repository.findOne(findOneOptions);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
import { FindOneOptions, Repository, SelectQueryBuilder } from "typeorm";
|
||||
import { BaseFilterEntity } from "../../domain/entities/base-filter.entity";
|
||||
import { PaginationResponse } from "src/core/response/domain/ok-response.interface";
|
||||
|
||||
export abstract class BaseReadService<Entity> {
|
||||
|
||||
constructor(private repository: Repository<Entity>) {}
|
||||
|
||||
getRepository(): Repository<Entity> {
|
||||
return this.repository;
|
||||
}
|
||||
|
||||
async getIndex(
|
||||
queryBuilder: SelectQueryBuilder<Entity>,
|
||||
params: BaseFilterEntity,
|
||||
): Promise<PaginationResponse<Entity>> {
|
||||
const [data, total] = await queryBuilder
|
||||
.take(+params.limit)
|
||||
.skip(+params.limit * +params.page - +params.limit)
|
||||
.getManyAndCount();
|
||||
|
||||
return {
|
||||
data,
|
||||
total,
|
||||
};
|
||||
}
|
||||
|
||||
async getOneByOptions(findOneOptions): Promise<Entity> {
|
||||
return await this.repository.findOne(findOneOptions);
|
||||
}
|
||||
|
||||
async getOneOrFailByOptions(
|
||||
findOneOptions: FindOneOptions<Entity>,
|
||||
): Promise<Entity> {
|
||||
return await this.repository.findOneOrFail(findOneOptions);
|
||||
}
|
||||
|
||||
async getManyByOptions(findManyOptions): Promise<Entity[]> {
|
||||
return await this.repository.find(findManyOptions);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
import { Repository, TreeRepository } from "typeorm";
|
||||
import { BaseReadService } from "./base-read.service";
|
||||
|
||||
export abstract class BaseTreeReadService<Entity> extends BaseReadService<Entity> {
|
||||
|
||||
constructor(
|
||||
private dataRepository: Repository<Entity>,
|
||||
private treeRepository: TreeRepository<Entity>
|
||||
) {
|
||||
super(dataRepository);
|
||||
}
|
||||
|
||||
async findRoots() {
|
||||
return this.treeRepository.findRoots()
|
||||
}
|
||||
|
||||
async findDescendants(
|
||||
parent,
|
||||
relations = [],
|
||||
): Promise<Entity[]> {
|
||||
return this.treeRepository.findDescendants(parent, {
|
||||
relations: relations,
|
||||
});
|
||||
}
|
||||
|
||||
async findDescendantsTree(
|
||||
parent,
|
||||
relations = [],
|
||||
): Promise<Entity> {
|
||||
return this.treeRepository.findDescendantsTree(parent, {
|
||||
relations: relations,
|
||||
});
|
||||
}
|
||||
|
||||
async findAncestors(
|
||||
parent,
|
||||
relations = [],
|
||||
): Promise<Entity[]> {
|
||||
return await this.treeRepository.findAncestors(parent, {
|
||||
relations: relations,
|
||||
});
|
||||
}
|
||||
|
||||
async findAncestorsTree(
|
||||
parent,
|
||||
relations = [],
|
||||
): Promise<Entity> {
|
||||
return await this.treeRepository.findAncestorsTree(parent, {
|
||||
relations: relations,
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
export interface BaseCoreEntity {
|
||||
id: string;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
import { ORDER_TYPE, STATUS } from "src/core/strings/constants/base.constants";
|
||||
|
||||
export interface BaseFilterEntity {
|
||||
page: number;
|
||||
limit: number;
|
||||
q?: string;
|
||||
names: string[];
|
||||
entity_ids: string[];
|
||||
order_by: string;
|
||||
order_type: ORDER_TYPE;
|
||||
statuses: STATUS[];
|
||||
created_ids: string[];
|
||||
created_from: number;
|
||||
created_to: number;
|
||||
updated_ids: string[];
|
||||
updated_from: number;
|
||||
updated_to: number;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
import { STATUS } from "src/core/strings/constants/base.constants";
|
||||
import { BaseEntity } from "./base.entity";
|
||||
|
||||
export interface BaseStatusEntity extends BaseEntity {
|
||||
status: STATUS;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import { BaseCoreEntity } from "./base-core.entity";
|
||||
|
||||
export interface BaseEntity extends BaseCoreEntity {
|
||||
creator_id: string;
|
||||
creator_name: string;
|
||||
editor_id: string;
|
||||
editor_name: string;
|
||||
created_at: number;
|
||||
updated_at: number;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
import { Inject, Injectable, Logger } from "@nestjs/common";
|
||||
import { UserProvider, UsersSession } from "src/core/sessions";
|
||||
import { BLANK_USER } from "src/core/strings/constants/base.constants";
|
||||
import { TABLE_NAME } from "src/core/strings/constants/table.constants";
|
||||
|
||||
@Injectable()
|
||||
export abstract class BaseReadManager {
|
||||
|
||||
public user: UsersSession;
|
||||
public dataService: any;
|
||||
public queryBuilder: any;
|
||||
protected tableName: TABLE_NAME;
|
||||
@Inject()
|
||||
protected userProvider: UserProvider;
|
||||
|
||||
private readonly baseLog = new Logger(BaseReadManager.name);
|
||||
|
||||
setUser() {
|
||||
try {
|
||||
this.user = this.userProvider?.user;
|
||||
} catch (error) {
|
||||
this.user = BLANK_USER;
|
||||
}
|
||||
}
|
||||
|
||||
setService(dataService) {
|
||||
this.dataService = dataService;
|
||||
this.queryBuilder = this.dataService.getRepository().createQueryBuilder(this.tableName);
|
||||
}
|
||||
|
||||
async execute(): Promise<void> {
|
||||
this.baseLog.log(`prepareData`, BaseReadManager.name);
|
||||
await this.prepareData();
|
||||
|
||||
this.baseLog.log(`beforeProcess`, BaseReadManager.name);
|
||||
await this.beforeProcess();
|
||||
|
||||
this.baseLog.log('process', BaseReadManager.name);
|
||||
await this.process();
|
||||
|
||||
this.baseLog.log('afterProcess', BaseReadManager.name);
|
||||
await this.afterProcess();
|
||||
}
|
||||
|
||||
abstract prepareData(): Promise<void>;
|
||||
|
||||
abstract beforeProcess(): Promise<void>;
|
||||
|
||||
abstract process(): Promise<void>;
|
||||
|
||||
abstract afterProcess(): Promise<void>;
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
import { BadRequestException, Inject, Injectable, Logger } from "@nestjs/common";
|
||||
import { EventBus } from "@nestjs/cqrs";
|
||||
import { UserProvider, UsersSession } from "src/core/sessions";
|
||||
import { BLANK_USER } from "src/core/strings/constants/base.constants";
|
||||
import { EventTopics } from "src/core/strings/constants/interface.constants";
|
||||
import { QueryRunner } from "typeorm";
|
||||
|
||||
@Injectable()
|
||||
export abstract class BaseManager {
|
||||
public user: UsersSession;
|
||||
public queryRunner: QueryRunner;
|
||||
public dataService: any;
|
||||
protected data: any;
|
||||
@Inject()
|
||||
protected userProvider: UserProvider;
|
||||
@Inject()
|
||||
protected eventBus: EventBus;
|
||||
|
||||
private readonly baseLog = new Logger(BaseManager.name);
|
||||
|
||||
setUser() {
|
||||
try {
|
||||
this.user = this.userProvider?.user;
|
||||
} catch (error) {
|
||||
this.user = BLANK_USER;
|
||||
}
|
||||
}
|
||||
|
||||
setService(dataService) {
|
||||
this.dataService = dataService;
|
||||
this.queryRunner = this.dataService.getRepository().manager.connection.createQueryRunner();
|
||||
}
|
||||
|
||||
abstract get eventTopics(): EventTopics[];
|
||||
|
||||
async execute(): Promise<void> {
|
||||
try {
|
||||
this.setUser();
|
||||
|
||||
this.queryRunner.startTransaction();
|
||||
this.baseLog.verbose('prepareData');
|
||||
await this.prepareData();
|
||||
|
||||
if (!this.data || !this.dataService) {
|
||||
throw new Error("data or service not implemented.");
|
||||
}
|
||||
|
||||
this.baseLog.verbose('validateProcess');
|
||||
await this.validateProcess();
|
||||
|
||||
this.baseLog.verbose('beforeProcess');
|
||||
await this.beforeProcess();
|
||||
|
||||
this.baseLog.verbose('process');
|
||||
await this.process();
|
||||
|
||||
this.baseLog.verbose('afterProcess');
|
||||
await this.afterProcess();
|
||||
|
||||
this.baseLog.verbose('commitTransaction');
|
||||
await this.queryRunner.commitTransaction();
|
||||
|
||||
this.publishEvents();
|
||||
|
||||
await this.queryRunner.release();
|
||||
} catch (e) {
|
||||
if (e.response) throw new Error(JSON.stringify(e.response));
|
||||
else throw new Error(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
abstract prepareData(): Promise<void>;
|
||||
|
||||
abstract validateProcess(): Promise<void>;
|
||||
|
||||
abstract beforeProcess(): Promise<void>;
|
||||
|
||||
abstract process(): Promise<void>;
|
||||
|
||||
abstract afterProcess(): Promise<void>;
|
||||
|
||||
async publishEvents() {
|
||||
if (!this.eventTopics.length) return
|
||||
};
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
import { BaseManager } from "../base.manager";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export abstract class BaseCreateManager<Entity> extends BaseManager {
|
||||
|
||||
protected result: Entity;
|
||||
protected duplicateColumn: string[];
|
||||
abstract get entityTarget(): any;
|
||||
|
||||
setData(entity: Entity): void {
|
||||
this.data = entity;
|
||||
}
|
||||
|
||||
async prepareData(): Promise<void> {
|
||||
Object.assign(this.data, {
|
||||
creator_id: this.user.id,
|
||||
creator_name: this.user.name,
|
||||
created_at: new Date().getTime(),
|
||||
updated_at: new Date().getTime(),
|
||||
});
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
this.result = await this.dataService.create(
|
||||
this.queryRunner,
|
||||
this.entityTarget,
|
||||
this.data,
|
||||
);
|
||||
}
|
||||
|
||||
async getResult(): Promise<Entity> {
|
||||
return await this.dataService.getOneByOptions({
|
||||
where: {
|
||||
id: this.result['id']
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
import { HttpStatus, Injectable, UnauthorizedException, UnprocessableEntityException } from "@nestjs/common";
|
||||
import { BaseManager } from "../base.manager";
|
||||
|
||||
@Injectable()
|
||||
export abstract class BaseDeleteManager<Entity> extends BaseManager {
|
||||
|
||||
protected dataId: string;
|
||||
protected result: Entity;
|
||||
abstract get entityTarget(): any;
|
||||
|
||||
setData(id: string): void {
|
||||
this.dataId = id;
|
||||
}
|
||||
|
||||
async prepareData(): Promise<void> {
|
||||
this.data = await this.dataService.getOneByOptions({
|
||||
where: {
|
||||
id: this.dataId
|
||||
}
|
||||
})
|
||||
|
||||
if (!this.data)
|
||||
throw new UnprocessableEntityException({
|
||||
statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
|
||||
message: `Data with id ${this.dataId} not found`,
|
||||
error: 'Unprocessable Entity',
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
await this.dataService.deleteById(
|
||||
this.queryRunner,
|
||||
this.entityTarget,
|
||||
this.dataId,
|
||||
);
|
||||
}
|
||||
|
||||
abstract getResult(): string;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
import { BaseReadManager } from "../base-read.manager";
|
||||
|
||||
export abstract class BaseDetailManager<Entity> extends BaseReadManager {
|
||||
|
||||
protected dataId: string;
|
||||
protected result: Entity;
|
||||
|
||||
abstract get selectData(): string[];
|
||||
abstract get relationData(): string[];
|
||||
abstract get setFindProperties(): any;
|
||||
|
||||
setData(dataId: string): void {
|
||||
this.dataId = dataId;
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
this.queryBuilder.select(this.selectData).where(this.setFindProperties);
|
||||
this.result = await this.queryBuilder.getOne();
|
||||
}
|
||||
|
||||
getResult(): Entity {
|
||||
return this.result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
import { PaginationResponse } from "src/core/response/domain/ok-response.interface";
|
||||
import { BaseReadManager } from "../base-read.manager";
|
||||
import { SelectQueryBuilder } from "typeorm";
|
||||
import { BaseFilterEntity } from "../../entities/base-filter.entity";
|
||||
import { Param, SpecificSearchFilter } from "src/core/helpers/query/specific-search.helper";
|
||||
|
||||
export abstract class BaseIndexManager<Entity> extends BaseReadManager {
|
||||
|
||||
protected result: PaginationResponse<Entity>;
|
||||
public filterParam: BaseFilterEntity;
|
||||
abstract get specificFilter(): Param[];
|
||||
|
||||
setFilterParam(param: BaseFilterEntity): void {
|
||||
this.filterParam = param;
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
// const filterSearch: string[] = this.setFilterSearch();
|
||||
|
||||
// this.queryBuilder.andWhere(
|
||||
// new Brackets((qb) => {
|
||||
// filterSearch.map((fSearch) => {
|
||||
// qb.orWhere(`${fSearch} ILIKE :query`, {
|
||||
// query: `%${
|
||||
// this.filterParam.q.trim().replace(/\s+/g, ' ') ?? ''
|
||||
// }%`,
|
||||
// });
|
||||
// });
|
||||
// }),
|
||||
// );
|
||||
|
||||
new SpecificSearchFilter<Entity>(this.queryBuilder, this.tableName, this.specificFilter).getFilter();
|
||||
this.setQueryFilter(this.queryBuilder);
|
||||
|
||||
this.result = await this.dataService.getIndex(
|
||||
this.queryBuilder,
|
||||
this.filterParam,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
setFilterSearch(): string[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
abstract setQueryFilter(
|
||||
queryBuilder: SelectQueryBuilder<Entity>,
|
||||
): SelectQueryBuilder<Entity>;
|
||||
|
||||
|
||||
getResult(): PaginationResponse<Entity> {
|
||||
return this.result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseManager } from "../base.manager";
|
||||
import { STATUS } from "src/core/strings/constants/base.constants";
|
||||
import { UserPrivilegeModel } from "src/modules/user-related/user-privilege/data/model/user-privilege.model";
|
||||
|
||||
@Injectable()
|
||||
export abstract class BaseUpdateStatusManager<Entity> extends BaseManager {
|
||||
|
||||
protected dataId: string;
|
||||
protected result: Entity;
|
||||
protected dataStatus: STATUS;
|
||||
protected duplicateColumn: string[];
|
||||
abstract get entityTarget(): any;
|
||||
|
||||
setData(id: string, status: STATUS): void {
|
||||
this.dataId = id;
|
||||
this.dataStatus = status;
|
||||
}
|
||||
|
||||
async prepareData(): Promise<void> {
|
||||
this.data = new UserPrivilegeModel();
|
||||
|
||||
Object.assign(this.data, {
|
||||
editor_id: this.user.id,
|
||||
editor_name: this.user.name,
|
||||
updated_at: new Date().getTime(),
|
||||
id: this.dataId,
|
||||
status: this.dataStatus,
|
||||
});
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
this.result = await this.dataService.update(
|
||||
this.queryRunner,
|
||||
this.entityTarget,
|
||||
{ id: this.dataId },
|
||||
this.data,
|
||||
);
|
||||
}
|
||||
|
||||
abstract getResult(): string;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseManager } from "../base.manager";
|
||||
|
||||
@Injectable()
|
||||
export abstract class BaseUpdateManager<Entity> extends BaseManager {
|
||||
|
||||
protected dataId: string;
|
||||
protected result: Entity;
|
||||
protected duplicateColumn: string[];
|
||||
abstract get entityTarget(): any;
|
||||
|
||||
setData(id: string, entity: Entity): void {
|
||||
this.dataId = id;
|
||||
this.data = entity;
|
||||
}
|
||||
|
||||
async prepareData(): Promise<void> {
|
||||
Object.assign(this.data, {
|
||||
editor_id: this.user.id,
|
||||
editor_name: this.user.name,
|
||||
updated_at: new Date().getTime(),
|
||||
});
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
this.result = await this.dataService.update(
|
||||
this.queryRunner,
|
||||
this.entityTarget,
|
||||
{ id: this.dataId },
|
||||
this.data,
|
||||
);
|
||||
}
|
||||
|
||||
async getResult(): Promise<Entity> {
|
||||
return await this.dataService.getOneByOptions({
|
||||
where: {
|
||||
id: this.dataId
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
import { BaseDataOrchestrator } from "./base-data.orchestrator";
|
||||
|
||||
export abstract class BaseDataTransactionOrchestrator<Entity> extends BaseDataOrchestrator<Entity> {
|
||||
abstract active(dataId: string): Promise<String>;
|
||||
abstract confirm(dataId: string): Promise<String>;
|
||||
abstract inactive(dataId: string): Promise<String>;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
export abstract class BaseDataOrchestrator<Entity> {
|
||||
|
||||
abstract create(data: Entity): Promise<Entity>;
|
||||
abstract update(dataId: string, data: Entity): Promise<Entity>;
|
||||
abstract delete(dataId: string): Promise<String>;
|
||||
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
import { PaginationResponse } from "src/core/response/domain/ok-response.interface";
|
||||
|
||||
export abstract class BaseReadOrchestrator<Entity> {
|
||||
abstract index(params): Promise<PaginationResponse<Entity>>;
|
||||
abstract detail(dataId: string): Promise<Entity>;
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { BaseCoreEntity } from "../../domain/entities/base-core.entity";
|
||||
|
||||
export class BaseCoreDto implements BaseCoreEntity {
|
||||
id: string;
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { BaseFilterEntity } from "../../domain/entities/base-filter.entity";
|
||||
import { Transform } from "class-transformer";
|
||||
import { IsArray, IsEnum, IsNumber, IsString, ValidateIf } from "class-validator";
|
||||
import { ORDER_TYPE, STATUS } from "src/core/strings/constants/base.constants";
|
||||
|
||||
export class BaseFilterDto implements BaseFilterEntity {
|
||||
@ApiProperty({ type: Number, required: false, default: 1 })
|
||||
@Transform((body) => Number(body.value))
|
||||
@ValidateIf((body) => body.page)
|
||||
@IsNumber()
|
||||
page = 1;
|
||||
|
||||
@ApiProperty({ type: Number, required: false, default: 10 })
|
||||
@Transform((body) => Number(body.value))
|
||||
@ValidateIf((body) => body.limit)
|
||||
@IsNumber()
|
||||
limit = 10;
|
||||
|
||||
@ApiProperty({ type: String, required: false })
|
||||
q: string;
|
||||
|
||||
@ApiProperty({ type: ['string'], required: false })
|
||||
@Transform((body) => {
|
||||
return Array.isArray(body.value) ? body.value : [body.value];
|
||||
})
|
||||
names: string[];
|
||||
|
||||
@ApiProperty({ type: String, required: false })
|
||||
order_by: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: `Select ("${ORDER_TYPE.ASC}", "${ORDER_TYPE.DESC}")`,
|
||||
})
|
||||
@ValidateIf((body) => body.order_type)
|
||||
@IsEnum(ORDER_TYPE, {
|
||||
message: `order_type must be a valid enum ${JSON.stringify(
|
||||
Object.values(ORDER_TYPE),
|
||||
)}`,
|
||||
})
|
||||
order_type: ORDER_TYPE;
|
||||
|
||||
@ApiProperty({
|
||||
type: ['string'],
|
||||
required: false,
|
||||
description: `Select ["${STATUS.ACTIVE}"]`,
|
||||
})
|
||||
@Transform((body) => {
|
||||
return Array.isArray(body.value) ? body.value : [body.value];
|
||||
})
|
||||
@ValidateIf((body) => {
|
||||
return body.status;
|
||||
})
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
@IsEnum(STATUS, {
|
||||
message: `Status must be a valid enum ${JSON.stringify(
|
||||
Object.values(STATUS),
|
||||
)}`,
|
||||
each: true,
|
||||
})
|
||||
statuses: STATUS[];
|
||||
|
||||
@ApiProperty({ type: [String], required: false })
|
||||
@Transform((body) => {
|
||||
return Array.isArray(body.value) ? body.value : [body.value];
|
||||
})
|
||||
@ValidateIf((body) => body.created_ids)
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
created_ids: string[];
|
||||
|
||||
@ApiProperty({ type: 'integer', required: false })
|
||||
@ValidateIf((body) => body.created_from)
|
||||
created_from: number;
|
||||
|
||||
@ApiProperty({ type: 'integer', required: false })
|
||||
@ValidateIf((body) => body.created_to)
|
||||
created_to: number;
|
||||
|
||||
@ApiProperty({ type: [String], required: false })
|
||||
@Transform((body) => {
|
||||
return Array.isArray(body.value) ? body.value : [body.value];
|
||||
})
|
||||
@ValidateIf((body) => body.updated_ids)
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
updated_ids: string[];
|
||||
|
||||
@ApiProperty({ type: 'integer', required: false })
|
||||
@Transform((body) => {
|
||||
return typeof body.value == 'string' ? Number(body.value) : body.value;
|
||||
})
|
||||
@ValidateIf((body) => body.updated_from)
|
||||
updated_from: number;
|
||||
|
||||
@ApiProperty({ type: 'integer', required: false })
|
||||
@Transform((body) => {
|
||||
return typeof body.value == 'string' ? Number(body.value) : body.value;
|
||||
})
|
||||
@ValidateIf((body) => body.updated_to)
|
||||
updated_to: number;
|
||||
|
||||
@ApiProperty({ type: [String], required: false })
|
||||
@Transform((body) => {
|
||||
return Array.isArray(body.value) ? body.value : [body.value];
|
||||
})
|
||||
@ValidateIf((body) => body.entity_ids)
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
entity_ids: string[];
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
import { STATUS } from "src/core/strings/constants/base.constants";
|
||||
import { BaseStatusEntity } from "../../domain/entities/base-status.entity";
|
||||
import { BaseDto } from "./base.dto";
|
||||
|
||||
export class BaseStatusDto extends BaseDto implements BaseStatusEntity {
|
||||
status: STATUS;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
import { BaseEntity } from "../../domain/entities/base.entity";
|
||||
import { BaseCoreDto } from "./base-core.dto";
|
||||
|
||||
export class BaseDto extends BaseCoreDto implements BaseEntity {
|
||||
creator_id: string;
|
||||
creator_name: string;
|
||||
editor_id: string;
|
||||
editor_name: string;
|
||||
created_at: number;
|
||||
updated_at: number;
|
||||
}
|
|
@ -3,16 +3,44 @@ import {
|
|||
Catch,
|
||||
ArgumentsHost,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
|
||||
@Catch(HttpException)
|
||||
@Catch()
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
catch(exception: HttpException, host: ArgumentsHost) {
|
||||
catch(exception: any, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const status = exception.getStatus();
|
||||
let status: HttpStatus;
|
||||
let body: any;
|
||||
let exceptionResponse;
|
||||
|
||||
response.status(status).json(exception.getResponse());
|
||||
try {
|
||||
exceptionResponse = JSON.parse(exception.message);
|
||||
} catch (error) {}
|
||||
|
||||
if (exception instanceof HttpException) {
|
||||
if (Array.isArray(exception.getResponse()['message'])) {
|
||||
exception.getResponse()['message'] = exception
|
||||
.getResponse()
|
||||
['message'].join(',');
|
||||
}
|
||||
|
||||
status = exception.getStatus();
|
||||
body = exception.getResponse();
|
||||
} else if (typeof exceptionResponse == 'object') {
|
||||
status = exceptionResponse.statusCode;
|
||||
body = exceptionResponse;
|
||||
} else {
|
||||
status = HttpStatus.FORBIDDEN;
|
||||
body = {
|
||||
statusCode: HttpStatus.FORBIDDEN,
|
||||
message: exception.message,
|
||||
error: exception.name,
|
||||
};
|
||||
}
|
||||
|
||||
response.status(status).json(body);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
export interface UsersSession {
|
||||
id: number;
|
||||
username: string;
|
||||
name: string;
|
||||
roles: string[];
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
export enum STATUS {
|
||||
ACTIVE = 'active',
|
||||
CANCEL = 'cancel',
|
||||
CONFIRMED = 'confirmed',
|
||||
DRAFT = 'draft',
|
||||
EXPIRED = 'expired',
|
||||
INACTIVE = 'inactive',
|
||||
PENDING = 'pending',
|
||||
REFUNDED = 'refunded',
|
||||
SETTLED = 'settled',
|
||||
}
|
||||
|
||||
export enum ORDER_TYPE {
|
||||
ASC = 'ASC',
|
||||
DESC = 'DESC',
|
||||
}
|
||||
|
||||
export enum CONNECTION_NAME {
|
||||
DEFAULT = 'default',
|
||||
}
|
||||
|
||||
export enum METHOD {
|
||||
POST = 'POST',
|
||||
GET = 'GET',
|
||||
PUT = 'PUT',
|
||||
PATCH = 'PATCH',
|
||||
DELETE = 'DELETE',
|
||||
}
|
||||
|
||||
export enum OPERATION {
|
||||
CREATE = 'Create',
|
||||
READ = 'Read',
|
||||
UPDATE = 'Update',
|
||||
UPDATE_BATCH = 'Batch Update',
|
||||
DELETE = 'Delete',
|
||||
DELETE_BATCH = 'Batch Delete',
|
||||
SYNC = 'Sync',
|
||||
}
|
||||
|
||||
export const BLANK_USER = {
|
||||
id: null,
|
||||
name: null,
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
import { UsersSession } from "src/core/sessions";
|
||||
import { OPERATION } from "./base.constants";
|
||||
|
||||
export interface EventTopics {
|
||||
topic: any,
|
||||
data: IEvent,
|
||||
}
|
||||
|
||||
export interface IEvent<Entity = any> {
|
||||
id: string;
|
||||
old: null | Entity;
|
||||
data: null | Entity;
|
||||
user: UsersSession;
|
||||
description: null | string;
|
||||
module: string;
|
||||
op: OPERATION;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
export enum MODULE_NAME {
|
||||
USER_PRIVILEGE = 'user-privileges'
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
export enum TABLE_NAME {
|
||||
USER_PRIVILEGE = 'user_privileges'
|
||||
}
|
27
src/main.ts
27
src/main.ts
|
@ -1,8 +1,33 @@
|
|||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { DocumentBuilder, SwaggerCustomOptions, SwaggerModule } from '@nestjs/swagger';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
|
||||
app.setGlobalPrefix('api/v1');
|
||||
app.enableCors();
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({ transform: true, forbidUnknownValues: false }),
|
||||
);
|
||||
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('Skyworld POS')
|
||||
.setDescription('POS API Documentation')
|
||||
.setVersion('1.0')
|
||||
.addBearerAuth(
|
||||
{ type: 'http', scheme: 'bearer', bearerFormat: 'JWT' },
|
||||
'JWT',
|
||||
)
|
||||
.build();
|
||||
|
||||
const options: SwaggerCustomOptions = {
|
||||
swaggerOptions: { docExpansion: 'list' },
|
||||
};
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('api/v1/pos/docs', app, document, options);
|
||||
|
||||
await app.listen(process.env.PORT || 3000);
|
||||
}
|
||||
bootstrap();
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
import { ConfigModule } from "@nestjs/config";
|
||||
import { CouchDataController } from "./infrastructure/couch.controller";
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
// TypeOrmModule.forFeature([UserPrivilegeModel], CONNECTION_NAME.DEFAULT),
|
||||
// CqrsModule,
|
||||
],
|
||||
controllers: [
|
||||
CouchDataController
|
||||
],
|
||||
providers: [
|
||||
],
|
||||
})
|
||||
export class CouchModule {}
|
|
@ -0,0 +1,66 @@
|
|||
import { Body, Controller, Get, Post } from "@nestjs/common";
|
||||
|
||||
import { ApiTags } from "@nestjs/swagger";
|
||||
import { Unprotected } from "src/core/guards";
|
||||
import * as Nano from 'nano'
|
||||
import { CreateUserPrivilegeDto } from "src/modules/user-related/user-privilege/infrastructure/dto/create-user-privilege.dto";
|
||||
|
||||
@ApiTags(`couch`)
|
||||
@Controller('couch')
|
||||
@Unprotected()
|
||||
export class CouchDataController {
|
||||
|
||||
@Post()
|
||||
async createDoc(
|
||||
@Body() entity: CreateUserPrivilegeDto
|
||||
) {
|
||||
try {
|
||||
let n = Nano('http://admin:secret@127.0.0.1:5984')
|
||||
let db = await n.db.create(entity.name)
|
||||
} catch (error) {
|
||||
console.log(error, 'dsa')
|
||||
}
|
||||
}
|
||||
|
||||
@Post('doc')
|
||||
async createDocs(
|
||||
@Body() entity: CreateUserPrivilegeDto
|
||||
) {
|
||||
try {
|
||||
const nano = require("nano")("http://admin:secret@127.0.0.1:5984");
|
||||
const people = nano.db.use('string');
|
||||
console.log(await people.info())
|
||||
const data = {
|
||||
id: '1212',
|
||||
name: 'dsadas'
|
||||
}
|
||||
// await people.insert(data)
|
||||
|
||||
people.changesReader.start()
|
||||
.on('change', (change) => { console.log(change) })
|
||||
.on('batch', (b) => {
|
||||
console.log('a batch of', b.length, 'changes has arrived');
|
||||
}).on('seq', (s) => {
|
||||
console.log('sequence token', s);
|
||||
}).on('error', (e) => {
|
||||
console.error('error', e);
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.log(error, 'dsa')
|
||||
}
|
||||
}
|
||||
|
||||
@Get()
|
||||
async getDoc(
|
||||
) {
|
||||
try {
|
||||
let n = Nano('http://admin:secret@127.0.0.1:5984')
|
||||
const people = n.use('string');
|
||||
|
||||
// return people.get();
|
||||
} catch (error) {
|
||||
console.log(error, 'dsa')
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import { BaseStatusModel } from "src/core/modules/data/model/base-status.model";
|
||||
import { TABLE_NAME } from "src/core/strings/constants/table.constants";
|
||||
import { Column, Entity } from "typeorm";
|
||||
import { UserPrivilegeEntity } from "../../domain/entities/user-privilege.entity";
|
||||
|
||||
@Entity(TABLE_NAME.USER_PRIVILEGE)
|
||||
export class UserPrivilegeModel extends BaseStatusModel<UserPrivilegeEntity> implements UserPrivilegeEntity {
|
||||
@Column('varchar', { name: 'name', length: 125 })
|
||||
name: string;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseDataService } from "src/core/modules/data/service/base-data.service";
|
||||
import { UserPrivilegeEntity } from "../../domain/entities/user-privilege.entity";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { UserPrivilegeModel } from "../model/user-privilege.model";
|
||||
import { CONNECTION_NAME } from "src/core/strings/constants/base.constants";
|
||||
import { Repository } from "typeorm";
|
||||
|
||||
@Injectable()
|
||||
export class UserPrivilegeDataService extends BaseDataService<UserPrivilegeEntity> {
|
||||
|
||||
constructor(
|
||||
@InjectRepository(UserPrivilegeModel, CONNECTION_NAME.DEFAULT)
|
||||
private repo: Repository<UserPrivilegeModel>,
|
||||
) {
|
||||
super(repo);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { UserPrivilegeEntity } from "../../domain/entities/user-privilege.entity";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { UserPrivilegeModel } from "../model/user-privilege.model";
|
||||
import { CONNECTION_NAME } from "src/core/strings/constants/base.constants";
|
||||
import { Repository } from "typeorm";
|
||||
import { BaseReadService } from "src/core/modules/data/service/base-read.service";
|
||||
|
||||
@Injectable()
|
||||
export class UserPrivilegeReadService extends BaseReadService<UserPrivilegeEntity> {
|
||||
|
||||
constructor(
|
||||
@InjectRepository(UserPrivilegeModel, CONNECTION_NAME.DEFAULT)
|
||||
private repo: Repository<UserPrivilegeModel>,
|
||||
) {
|
||||
super(repo);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { IEvent } from "@nestjs/cqrs";
|
||||
|
||||
export class UserPrivilegeChangeStatusEvent {
|
||||
constructor(public readonly data: IEvent) {}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { IEvent } from "@nestjs/cqrs";
|
||||
|
||||
export class UserPrivilegeCreatedEvent {
|
||||
constructor(public readonly data: IEvent) {}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { IEvent } from "@nestjs/cqrs";
|
||||
|
||||
export class UserPrivilegeDeletedEvent {
|
||||
constructor(public readonly data: IEvent) {}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { IEvent } from "@nestjs/cqrs";
|
||||
|
||||
export class UserPrivilegeUpdatedEvent {
|
||||
constructor(public readonly data: IEvent) {}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { BaseFilterEntity } from "src/core/modules/domain/entities/base-filter.entity";
|
||||
|
||||
export interface FilterUserPrivilegeEntity extends BaseFilterEntity {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { BaseStatusEntity } from "src/core/modules/domain/entities/base-status.entity";
|
||||
|
||||
export interface UserPrivilegeEntity extends BaseStatusEntity {
|
||||
name: string;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseUpdateStatusManager } from "src/core/modules/domain/usecase/managers/base-update-status.manager";
|
||||
import { UserPrivilegeEntity } from "../../entities/user-privilege.entity";
|
||||
import { EventTopics } from "src/core/strings/constants/interface.constants";
|
||||
import { UserPrivilegeModel } from "../../../data/model/user-privilege.model";
|
||||
import { UserPrivilegeChangeStatusEvent } from "../../entities/event/user-privilege-change-status.event";
|
||||
|
||||
@Injectable()
|
||||
export class ActiveUserPrivilegeManager extends BaseUpdateStatusManager<UserPrivilegeEntity> {
|
||||
|
||||
getResult(): string {
|
||||
return `Success active data ${this.result.name}`;
|
||||
}
|
||||
|
||||
async validateProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
return UserPrivilegeModel;
|
||||
}
|
||||
|
||||
get eventTopics(): EventTopics[] {
|
||||
return [
|
||||
{
|
||||
topic: UserPrivilegeChangeStatusEvent,
|
||||
data: this.data,
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseUpdateStatusManager } from "src/core/modules/domain/usecase/managers/base-update-status.manager";
|
||||
import { UserPrivilegeEntity } from "../../entities/user-privilege.entity";
|
||||
import { EventTopics } from "src/core/strings/constants/interface.constants";
|
||||
import { UserPrivilegeModel } from "../../../data/model/user-privilege.model";
|
||||
import { UserPrivilegeChangeStatusEvent } from "../../entities/event/user-privilege-change-status.event";
|
||||
|
||||
@Injectable()
|
||||
export class ConfirmUserPrivilegeManager extends BaseUpdateStatusManager<UserPrivilegeEntity> {
|
||||
|
||||
getResult(): string {
|
||||
return `Success active data ${this.result.name}`;
|
||||
}
|
||||
|
||||
async validateProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
return UserPrivilegeModel;
|
||||
}
|
||||
|
||||
get eventTopics(): EventTopics[] {
|
||||
return [
|
||||
{
|
||||
topic: UserPrivilegeChangeStatusEvent,
|
||||
data: this.data,
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
import { BaseCreateManager } from "src/core/modules/domain/usecase/managers/base-create.manager";
|
||||
import { UserPrivilegeEntity } from "../../entities/user-privilege.entity";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { UserPrivilegeModel } from "../../../data/model/user-privilege.model";
|
||||
import { EventTopics } from "src/core/strings/constants/interface.constants";
|
||||
import { UserPrivilegeCreatedEvent } from "../../entities/event/user-privilege-created.event";
|
||||
|
||||
@Injectable()
|
||||
export class CreateUserPrivilegeManager extends BaseCreateManager<UserPrivilegeEntity> {
|
||||
|
||||
async validateProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
get eventTopics(): EventTopics[] {
|
||||
return [
|
||||
{
|
||||
topic: UserPrivilegeCreatedEvent,
|
||||
data: this.data,
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
return UserPrivilegeModel;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseDeleteManager } from "src/core/modules/domain/usecase/managers/base-delete.manager";
|
||||
import { UserPrivilegeEntity } from "../../entities/user-privilege.entity";
|
||||
import { EventTopics } from "src/core/strings/constants/interface.constants";
|
||||
import { UserPrivilegeModel } from "../../../data/model/user-privilege.model";
|
||||
import { UserPrivilegeDeletedEvent } from "../../entities/event/user-privilege-deleted.event";
|
||||
|
||||
@Injectable()
|
||||
export class DeleteUserPrivilegeManager extends BaseDeleteManager<UserPrivilegeEntity> {
|
||||
|
||||
getResult(): string {
|
||||
return `Success`;
|
||||
}
|
||||
|
||||
async validateProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
return UserPrivilegeModel;
|
||||
}
|
||||
|
||||
get eventTopics(): EventTopics[] {
|
||||
return [
|
||||
{
|
||||
topic: UserPrivilegeDeletedEvent,
|
||||
data: this.data,
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseDetailManager } from "src/core/modules/domain/usecase/managers/base-detail.manager";
|
||||
import { UserPrivilegeEntity } from "../../entities/user-privilege.entity";
|
||||
import { FindOneOptions } from "typeorm";
|
||||
import { TABLE_NAME } from "src/core/strings/constants/table.constants";
|
||||
|
||||
@Injectable()
|
||||
export class DetailUserPrivilegeManager extends BaseDetailManager<UserPrivilegeEntity> {
|
||||
|
||||
get setFindProperties(): any {
|
||||
return {
|
||||
id: this.dataId
|
||||
}
|
||||
}
|
||||
|
||||
get selectData(): string[] {
|
||||
return [
|
||||
'id',
|
||||
];
|
||||
}
|
||||
|
||||
get relationData(): string[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
async prepareData(): Promise<void> {
|
||||
this.tableName = TABLE_NAME.USER_PRIVILEGE;
|
||||
return;
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseUpdateStatusManager } from "src/core/modules/domain/usecase/managers/base-update-status.manager";
|
||||
import { UserPrivilegeEntity } from "../../entities/user-privilege.entity";
|
||||
import { EventTopics } from "src/core/strings/constants/interface.constants";
|
||||
import { UserPrivilegeModel } from "../../../data/model/user-privilege.model";
|
||||
import { UserPrivilegeChangeStatusEvent } from "../../entities/event/user-privilege-change-status.event";
|
||||
|
||||
@Injectable()
|
||||
export class InactiveUserPrivilegeManager extends BaseUpdateStatusManager<UserPrivilegeEntity> {
|
||||
|
||||
getResult(): string {
|
||||
return `Success inactive data ${this.result.name}`;
|
||||
}
|
||||
|
||||
async validateProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
return UserPrivilegeModel;
|
||||
}
|
||||
|
||||
get eventTopics(): EventTopics[] {
|
||||
return [
|
||||
{
|
||||
topic: UserPrivilegeChangeStatusEvent,
|
||||
data: this.data,
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseIndexManager } from "src/core/modules/domain/usecase/managers/base-index.manager";
|
||||
import { UserPrivilegeEntity } from "../../entities/user-privilege.entity";
|
||||
import { SelectQueryBuilder } from "typeorm";
|
||||
import { BaseFilterEntity } from "src/core/modules/domain/entities/base-filter.entity";
|
||||
import { TABLE_NAME } from "src/core/strings/constants/table.constants";
|
||||
import { Param } from "src/core/helpers/query/specific-search.helper";
|
||||
|
||||
@Injectable()
|
||||
export class IndexUserPrivilegeManager extends BaseIndexManager<UserPrivilegeEntity> {
|
||||
|
||||
async prepareData(): Promise<void> {
|
||||
this.tableName = TABLE_NAME.USER_PRIVILEGE;
|
||||
return
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
return
|
||||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
return
|
||||
}
|
||||
|
||||
get specificFilter(): Param[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
setQueryFilter(
|
||||
queryBuilder: SelectQueryBuilder<UserPrivilegeEntity>
|
||||
): SelectQueryBuilder<UserPrivilegeEntity> {
|
||||
|
||||
if (this.filterParam.q) {
|
||||
queryBuilder.andWhere('status = :q', {
|
||||
q: this.filterParam.q
|
||||
})
|
||||
}
|
||||
|
||||
return queryBuilder;
|
||||
}
|
||||
|
||||
setFilterSearch(): string[] {
|
||||
return [];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { BaseUpdateManager } from "src/core/modules/domain/usecase/managers/base-update.manager";
|
||||
import { UserPrivilegeEntity } from "../../entities/user-privilege.entity";
|
||||
import { EventTopics } from "src/core/strings/constants/interface.constants";
|
||||
import { UserPrivilegeModel } from "../../../data/model/user-privilege.model";
|
||||
import { UserPrivilegeUpdatedEvent } from "../../entities/event/user-privilege-updated.event";
|
||||
|
||||
@Injectable()
|
||||
export class UpdateUserPrivilegeManager extends BaseUpdateManager<UserPrivilegeEntity> {
|
||||
|
||||
async validateProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
return UserPrivilegeModel;
|
||||
}
|
||||
|
||||
get eventTopics(): EventTopics[] {
|
||||
return [
|
||||
{
|
||||
topic: UserPrivilegeUpdatedEvent,
|
||||
data: this.data,
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { CreateUserPrivilegeManager } from "./managers/create-user-privilege.manager";
|
||||
import { UserPrivilegeDataService } from "../../data/service/user-privilege-data.service";
|
||||
import { UserPrivilegeEntity } from "../entities/user-privilege.entity";
|
||||
import { DeleteUserPrivilegeManager } from "./managers/delete-user-privilege.manager";
|
||||
import { UpdateUserPrivilegeManager } from "./managers/update-user-privilege.manager";
|
||||
import { BaseDataTransactionOrchestrator } from "src/core/modules/domain/usecase/orchestrators/base-data-transaction.orchestrator";
|
||||
import { ActiveUserPrivilegeManager } from "./managers/active-user-privilege.manager";
|
||||
import { InactiveUserPrivilegeManager } from "./managers/inactive-user-privilege.manager";
|
||||
import { ConfirmUserPrivilegeManager } from "./managers/confirm-user-privilege.manager";
|
||||
import { STATUS } from "src/core/strings/constants/base.constants";
|
||||
|
||||
@Injectable()
|
||||
export class UserPrivilegeDataOrchestrator extends BaseDataTransactionOrchestrator<UserPrivilegeEntity> {
|
||||
|
||||
constructor(
|
||||
private createManager: CreateUserPrivilegeManager,
|
||||
private updateManager: UpdateUserPrivilegeManager,
|
||||
private deleteManager: DeleteUserPrivilegeManager,
|
||||
private activeManager: ActiveUserPrivilegeManager,
|
||||
private confirmManager: ConfirmUserPrivilegeManager,
|
||||
private inactiveManager: InactiveUserPrivilegeManager,
|
||||
private serviceData: UserPrivilegeDataService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async create(data): Promise<UserPrivilegeEntity> {
|
||||
this.createManager.setData(data);
|
||||
this.createManager.setService(this.serviceData);
|
||||
await this.createManager.execute()
|
||||
return this.createManager.getResult();
|
||||
}
|
||||
|
||||
async update(dataId, data): Promise<UserPrivilegeEntity> {
|
||||
this.updateManager.setData(dataId, data);
|
||||
this.updateManager.setService(this.serviceData);
|
||||
await this.updateManager.execute();
|
||||
return this.updateManager.getResult();
|
||||
}
|
||||
|
||||
async delete(dataId): Promise<String> {
|
||||
this.deleteManager.setData(dataId);
|
||||
this.deleteManager.setService(this.serviceData);
|
||||
await this.deleteManager.execute();
|
||||
return this.deleteManager.getResult();
|
||||
}
|
||||
|
||||
async active(dataId): Promise<String> {
|
||||
this.activeManager.setData(dataId, STATUS.ACTIVE)
|
||||
this.activeManager.setService(this.serviceData);
|
||||
await this.activeManager.execute();
|
||||
return this.activeManager.getResult();
|
||||
}
|
||||
|
||||
async confirm(dataId): Promise<String> {
|
||||
this.confirmManager.setData(dataId, STATUS.ACTIVE)
|
||||
this.confirmManager.setService(this.serviceData);
|
||||
await this.confirmManager.execute();
|
||||
return this.confirmManager.getResult();
|
||||
}
|
||||
|
||||
async inactive(dataId): Promise<String> {
|
||||
this.inactiveManager.setData(dataId, STATUS.INACTIVE)
|
||||
this.inactiveManager.setService(this.serviceData);
|
||||
await this.inactiveManager.execute();
|
||||
return this.inactiveManager.getResult();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { IndexUserPrivilegeManager } from "./managers/index-user-privilege.manager";
|
||||
import { UserPrivilegeReadService } from "../../data/service/user-privilege-read.service";
|
||||
import { UserPrivilegeEntity } from "../entities/user-privilege.entity";
|
||||
import { PaginationResponse } from "src/core/response/domain/ok-response.interface";
|
||||
import { BaseReadOrchestrator } from "src/core/modules/domain/usecase/orchestrators/base-read.orchestrator";
|
||||
import { DetailUserPrivilegeManager } from "./managers/detail-user-privilege.manager";
|
||||
|
||||
@Injectable()
|
||||
export class UserPrivilegeReadOrchestrator extends BaseReadOrchestrator<UserPrivilegeEntity> {
|
||||
|
||||
constructor(
|
||||
private indexManager: IndexUserPrivilegeManager,
|
||||
private detailManager: DetailUserPrivilegeManager,
|
||||
private serviceData: UserPrivilegeReadService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async index(params): Promise<PaginationResponse<UserPrivilegeEntity>> {
|
||||
this.indexManager.setFilterParam(params)
|
||||
this.indexManager.setService(this.serviceData)
|
||||
await this.indexManager.execute()
|
||||
return this.indexManager.getResult();
|
||||
}
|
||||
|
||||
async detail(dataId: string): Promise<UserPrivilegeEntity> {
|
||||
this.detailManager.setData(dataId);
|
||||
this.detailManager.setService(this.serviceData);
|
||||
await this.detailManager.execute();
|
||||
return this.detailManager.getResult();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import { BaseStatusDto } from "src/core/modules/infrastructure/dto/base-status.dto";
|
||||
import { UserPrivilegeEntity } from "../../domain/entities/user-privilege.entity";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsString } from "class-validator";
|
||||
|
||||
export class CreateUserPrivilegeDto extends BaseStatusDto implements UserPrivilegeEntity {
|
||||
@ApiProperty({ name: 'name', required: true })
|
||||
@IsString()
|
||||
name: string;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
import { BaseFilterDto } from "src/core/modules/infrastructure/dto/base-filter.dto";
|
||||
import { FilterUserPrivilegeEntity } from "../../domain/entities/filter-user-privilege.entity";
|
||||
|
||||
export class FilterUserPrivilegeDto extends BaseFilterDto implements FilterUserPrivilegeEntity {}
|
|
@ -0,0 +1,102 @@
|
|||
import { Body, Controller, Delete, Param, Patch, Post, Put } from "@nestjs/common";
|
||||
import { UserPrivilegeDataOrchestrator } from "../domain/usecases/user-privilege-data.orchestrator";
|
||||
import { CreateUserPrivilegeDto } from "./dto/create-user-privilege.dto";
|
||||
import { MODULE_NAME } from "src/core/strings/constants/module.constants";
|
||||
import { ApiTags } from "@nestjs/swagger";
|
||||
import { Unprotected } from "src/core/guards";
|
||||
import * as Nano from 'nano'
|
||||
import { UserPrivilegeEntity } from "../domain/entities/user-privilege.entity";
|
||||
|
||||
@ApiTags(`${MODULE_NAME.USER_PRIVILEGE.split('-').join(' ')} - data`)
|
||||
@Controller(MODULE_NAME.USER_PRIVILEGE)
|
||||
@Unprotected()
|
||||
export class UserPrivilegeDataController {
|
||||
constructor(
|
||||
private orchestrator: UserPrivilegeDataOrchestrator
|
||||
) {}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@Body() data: CreateUserPrivilegeDto,
|
||||
): Promise<UserPrivilegeEntity> {
|
||||
return await this.orchestrator.create(data);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('id') dataId: string,
|
||||
@Body() data: CreateUserPrivilegeDto,
|
||||
): Promise<UserPrivilegeEntity> {
|
||||
return await this.orchestrator.update(dataId, data);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(
|
||||
@Param('id') dataId: string,
|
||||
): Promise<String> {
|
||||
return await this.orchestrator.delete(dataId);
|
||||
}
|
||||
|
||||
@Patch(':id/active')
|
||||
async active(
|
||||
@Param('id') dataId: string,
|
||||
): Promise<String> {
|
||||
return await this.orchestrator.active(dataId);
|
||||
}
|
||||
|
||||
@Patch(':id/confirm')
|
||||
async confirm(
|
||||
@Param('id') dataId: string,
|
||||
): Promise<String> {
|
||||
return await this.orchestrator.confirm(dataId);
|
||||
}
|
||||
|
||||
@Patch(':id/inactive')
|
||||
async inactive(
|
||||
@Param('id') dataId: string,
|
||||
): Promise<String> {
|
||||
return await this.orchestrator.inactive(dataId);
|
||||
}
|
||||
|
||||
|
||||
// @Post('pouch')
|
||||
// async createDoc(
|
||||
// @Body() entity: CreateUserPrivilegeDto
|
||||
// ) {
|
||||
// try {
|
||||
// let n = Nano('http://admin:secret@127.0.0.1:5984')
|
||||
// let db = await n.db.create('people')
|
||||
// } catch (error) {
|
||||
// console.log(error, 'dsa')
|
||||
// }
|
||||
// }
|
||||
|
||||
// @Post('pouch/doc')
|
||||
// async createDocs(
|
||||
// @Body() entity: CreateUserPrivilegeDto
|
||||
// ) {
|
||||
// try {
|
||||
// let n = Nano('http://admin:secret@127.0.0.1:5984')
|
||||
// const people = n.use('people');
|
||||
// const data = {
|
||||
// id: '1212',
|
||||
// name: 'dsadas'
|
||||
// }
|
||||
// return await people.insert({'_id': 'dsa', '_rev': JSON.stringify({'dsa': 'dsa'})})
|
||||
// } catch (error) {
|
||||
// console.log(error, 'dsa')
|
||||
// }
|
||||
// }
|
||||
|
||||
// @Get('pouch')
|
||||
// async getDoc(
|
||||
// ) {
|
||||
// try {
|
||||
// let n = Nano('http://admin:secret@127.0.0.1:5984')
|
||||
// const people = n.use('people');
|
||||
// // return people.get();
|
||||
// } catch (error) {
|
||||
// console.log(error, 'dsa')
|
||||
// }
|
||||
// }
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
import { Controller, Get, Param, Query, Req } from "@nestjs/common";
|
||||
import { FilterUserPrivilegeDto } from "./dto/filter-user-privilege.dto";
|
||||
import { Pagination } from "src/core/response";
|
||||
import { PaginationResponse } from "src/core/response/domain/ok-response.interface";
|
||||
import { UserPrivilegeEntity } from "../domain/entities/user-privilege.entity";
|
||||
import { UserPrivilegeReadOrchestrator } from "../domain/usecases/user-privilege-read.orchestrator";
|
||||
import { ApiTags } from "@nestjs/swagger";
|
||||
import { MODULE_NAME } from "src/core/strings/constants/module.constants";
|
||||
import { Unprotected } from "src/core/guards";
|
||||
|
||||
@ApiTags(`${MODULE_NAME.USER_PRIVILEGE.split('-').join(' ')} - read`)
|
||||
@Controller(MODULE_NAME.USER_PRIVILEGE)
|
||||
@Unprotected()
|
||||
export class UserPrivilegeReadController {
|
||||
constructor(
|
||||
private orchestrator: UserPrivilegeReadOrchestrator
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
@Pagination()
|
||||
async index(
|
||||
@Query() params: FilterUserPrivilegeDto
|
||||
): Promise<PaginationResponse<UserPrivilegeEntity>> {
|
||||
return await this.orchestrator.index(params);
|
||||
}
|
||||
|
||||
|
||||
@Get(':id')
|
||||
async detail(
|
||||
@Param('id') id: string,
|
||||
): Promise<UserPrivilegeEntity> {
|
||||
return await this.orchestrator.detail(id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
import { Module } from "@nestjs/common";
|
||||
import { UserPrivilegeModel } from "./data/model/user-privilege.model";
|
||||
import { ConfigModule } from "@nestjs/config";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { CONNECTION_NAME } from "src/core/strings/constants/base.constants";
|
||||
import { UserPrivilegeDataService } from "./data/service/user-privilege-data.service";
|
||||
import { UserPrivilegeReadService } from "./data/service/user-privilege-read.service";
|
||||
import { UserPrivilegeReadController } from "./infrastructure/user-privilege-read.controller";
|
||||
import { UserPrivilegeReadOrchestrator } from "./domain/usecases/user-privilege-read.orchestrator";
|
||||
import { UserPrivilegeDataController } from "./infrastructure/user-privilege-data.controller";
|
||||
import { UserPrivilegeDataOrchestrator } from "./domain/usecases/user-privilege-data.orchestrator";
|
||||
import { CreateUserPrivilegeManager } from "./domain/usecases/managers/create-user-privilege.manager";
|
||||
import { CqrsModule } from "@nestjs/cqrs";
|
||||
import { IndexUserPrivilegeManager } from "./domain/usecases/managers/index-user-privilege.manager";
|
||||
import { DeleteUserPrivilegeManager } from "./domain/usecases/managers/delete-user-privilege.manager";
|
||||
import { UpdateUserPrivilegeManager } from "./domain/usecases/managers/update-user-privilege.manager";
|
||||
import { ActiveUserPrivilegeManager } from "./domain/usecases/managers/active-user-privilege.manager";
|
||||
import { ConfirmUserPrivilegeManager } from "./domain/usecases/managers/confirm-user-privilege.manager";
|
||||
import { InactiveUserPrivilegeManager } from "./domain/usecases/managers/inactive-user-privilege.manager";
|
||||
import { DetailUserPrivilegeManager } from "./domain/usecases/managers/detail-user-privilege.manager";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
TypeOrmModule.forFeature([UserPrivilegeModel], CONNECTION_NAME.DEFAULT),
|
||||
CqrsModule,
|
||||
],
|
||||
controllers: [
|
||||
UserPrivilegeDataController,
|
||||
UserPrivilegeReadController
|
||||
],
|
||||
providers: [
|
||||
IndexUserPrivilegeManager,
|
||||
DetailUserPrivilegeManager,
|
||||
CreateUserPrivilegeManager,
|
||||
DeleteUserPrivilegeManager,
|
||||
UpdateUserPrivilegeManager,
|
||||
ActiveUserPrivilegeManager,
|
||||
ConfirmUserPrivilegeManager,
|
||||
InactiveUserPrivilegeManager,
|
||||
|
||||
UserPrivilegeDataService,
|
||||
UserPrivilegeReadService,
|
||||
|
||||
UserPrivilegeDataOrchestrator,
|
||||
UserPrivilegeReadOrchestrator,
|
||||
],
|
||||
})
|
||||
export class UserPrivilegeModule {}
|
Loading…
Reference in New Issue