feat(SPG-125) wip Logging

pull/2/head
ashar 2024-06-05 09:29:43 +07:00
parent cefff6d5c7
commit f36e3b26d4
23 changed files with 323 additions and 90 deletions

View File

@ -1,6 +1,5 @@
import { Module, Scope } from '@nestjs/common';
import { RefreshTokenInterceptor, SessionModule } from './core/sessions';
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';
@ -16,6 +15,9 @@ import { PrivilegeService } from './core/guards/domain/services/privilege.servic
import { UserModel } from './modules/user-related/user/data/models/user.model';
import { AuthModule } from './modules/configuration/auth/auth.module';
import { UserModule } from './modules/user-related/user/user.module';
import { LogModel } from './modules/configuration/log/data/models/log.model';
import { ErrorLogModel } from './modules/configuration/log/data/models/error-log.model';
import { LogModule } from './modules/configuration/log/log.module';
@Module({
imports: [
@ -31,13 +33,19 @@ import { UserModule } from './modules/user-related/user/user.module';
username: process.env.DEFAULT_DB_USER,
password: process.env.DEFAULT_DB_PASS,
database: process.env.DEFAULT_DB_NAME,
entities: [...UserPrivilegeModels, UserModel],
entities: [
...UserPrivilegeModels,
UserModel,
LogModel,
ErrorLogModel,
],
synchronize: false,
}),
CqrsModule,
SessionModule,
AuthModule,
CouchModule,
LogModule,
UserModule,
UserPrivilegeModule,

View File

@ -3,14 +3,16 @@ 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 { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { QueryRunner } from 'typeorm';
@Injectable()
export abstract class BaseManager {
public user: UsersSession;
public queryRunner: QueryRunner;
public dataService: any;
protected data: any;
public queryRunner: QueryRunner;
protected tableName: TABLE_NAME;
@Inject()
protected userProvider: UserProvider;
@Inject()
@ -26,11 +28,12 @@ export abstract class BaseManager {
}
}
setService(dataService) {
setService(dataService, tableName) {
this.dataService = dataService;
this.tableName = tableName;
this.queryRunner = this.dataService
.getRepository()
.manager.connection.createQueryRunner();
.manager.connection.createQueryRunner(tableName);
}
abstract get eventTopics(): EventTopics[];
@ -62,8 +65,6 @@ export abstract class BaseManager {
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));
@ -80,8 +81,4 @@ export abstract class BaseManager {
abstract process(): Promise<void>;
abstract afterProcess(): Promise<void>;
async publishEvents() {
if (!this.eventTopics.length) return;
}
}

View File

@ -1,5 +1,7 @@
import { CheckDuplicateHelper } from 'src/core/helpers/query/check-duplicate.helpers';
import { BaseManager } from '../base.manager';
import { RecordLog } from 'src/modules/configuration/log/domain/entities/log.event';
import { OPERATION } from 'src/core/strings/constants/base.constants';
export abstract class BaseCreateManager<Entity> extends BaseManager {
protected result: Entity;
@ -37,6 +39,8 @@ export abstract class BaseCreateManager<Entity> extends BaseManager {
this.entityTarget,
this.data,
);
this.publishEvents();
}
async getResult(): Promise<Entity> {
@ -46,4 +50,21 @@ export abstract class BaseCreateManager<Entity> extends BaseManager {
},
});
}
async publishEvents() {
this.eventBus.publish(
new RecordLog({
id: this.result['id'],
old: null,
data: this.result,
user: this.user,
description: '',
module: this.tableName,
op: OPERATION.CREATE,
})
)
// if (!this.eventTopics.length) return;
}
}

View File

@ -5,10 +5,18 @@ import {
HttpException,
HttpStatus,
} from '@nestjs/common';
import { EventBus } from '@nestjs/cqrs';
import { Response } from 'express';
import { OPERATION } from 'src/core/strings/constants/base.constants';
import { RecordErrorLog } from 'src/modules/configuration/log/domain/entities/error-log.event';
@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
constructor(
private eventBus: EventBus,
) {}
catch(exception: any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
@ -41,6 +49,18 @@ export class HttpExceptionFilter implements ExceptionFilter {
};
}
this.eventBus.publish(
new RecordErrorLog({
id: '',
old: null,
data: null,
user: null,
description: '',
module: null,
op: OPERATION.CREATE,
})
)
response.status(status).json(body);
}
}

View File

@ -1,4 +1,6 @@
export enum TABLE_NAME {
LOG = 'logs',
ERROR_LOG = 'log_errors',
USER = 'users',
USER_PRIVILEGE = 'user_privileges',
USER_PRIVILEGE_CONFIGURATION = 'user_privilege_configurations',

View File

@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { UserDataService } from 'src/modules/user-related/user/data/services.ts/user-data.service';
import { LoginManager } from './managers/login.manager';
import { LogoutManager } from './managers/logout.manager';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
@Injectable()
export class AuthOrchestrator {
@ -13,13 +14,13 @@ export class AuthOrchestrator {
async login(data): Promise<any> {
this.loginManager.setData(data);
this.loginManager.setService(this.serviceData);
this.loginManager.setService(this.serviceData, TABLE_NAME.USER);
await this.loginManager.execute();
return this.loginManager.getResult();
}
async logout(): Promise<any> {
this.logoutManager.setService(this.serviceData);
this.logoutManager.setService(this.serviceData, TABLE_NAME.USER);
await this.logoutManager.execute();
return this.logoutManager.getResult();
}

View File

@ -1,17 +1,14 @@
import { ConfigModule } from "@nestjs/config";
import { CouchDataController } from "./infrastructure/couch.controller";
import { Module } from "@nestjs/common";
import { ConfigModule } from '@nestjs/config';
import { CouchDataController } from './infrastructure/couch.controller';
import { Module } from '@nestjs/common';
@Module({
imports: [
ConfigModule.forRoot(),
imports: [
ConfigModule.forRoot(),
// TypeOrmModule.forFeature([UserPrivilegeModel], CONNECTION_NAME.DEFAULT),
// CqrsModule,
],
controllers: [
CouchDataController
],
providers: [
],
})
export class CouchModule {}
],
controllers: [CouchDataController],
providers: [],
})
export class CouchModule {}

View File

@ -1,66 +1,64 @@
import { Body, Controller, Get, Post } from "@nestjs/common";
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";
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()
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)
@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')
}
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');
@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')
}
// return people.get();
} catch (error) {
console.log(error, 'dsa');
}
}
}

View File

@ -0,0 +1,29 @@
import { BaseCoreModel } from "src/core/modules/data/model/base-core.model";
import { ErrorLogEntity } from "../../domain/entities/error-log.entity";
import { Column } from "typeorm";
export class ErrorLogModel extends BaseCoreModel<ErrorLogEntity> implements ErrorLogEntity {
@Column('varchar', { name: 'data_id', nullable: true })
data_id: string;
@Column('varchar', { name: 'module', nullable: true })
module: string;
@Column('varchar', { name: 'sub_module', nullable: true })
sub_module: string;
@Column('text', { name: 'description', nullable: true })
description: string;
@Column('json', { name: 'message', nullable: true })
message: string;
@Column('bigint', { name: 'created_at', nullable: true })
created_at: number;
@Column('varchar', { name: 'creator_name', nullable: true })
creator_name: string;
@Column('varchar', { name: 'creator_id', nullable: true })
creator_id: string;
}

View File

@ -0,0 +1,38 @@
import { BaseCoreModel } from "src/core/modules/data/model/base-core.model";
import { LogEntity } from "../../domain/entities/log.entity";
import { Column, Entity } from "typeorm";
import { OPERATION } from "src/core/strings/constants/base.constants";
import { TABLE_NAME } from "src/core/strings/constants/table.constants";
@Entity(TABLE_NAME.LOG)
export class LogModel extends BaseCoreModel<LogEntity> implements LogEntity {
@Column('varchar', { name: 'data_id', nullable: true })
data_id: string;
@Column('varchar', { name: 'module', nullable: true })
module: string;
@Column('varchar', { name: 'sub_module', nullable: true })
sub_module: string;
@Column('text', { name: 'description', nullable: true })
description: string;
@Column('varchar', { name: 'process', default: OPERATION.CREATE })
process: OPERATION;
@Column('json', { name: 'data', nullable: true })
data: any;
@Column('json', { name: 'old_data', nullable: true })
old_data: any;
@Column('bigint', { name: 'created_at', nullable: true })
created_at: number;
@Column('varchar', { name: 'creator_name', nullable: true })
creator_name: string;
@Column('varchar', { name: 'creator_id', nullable: true })
creator_id: string;
}

View File

@ -0,0 +1,17 @@
import { BaseDataService } from "src/core/modules/data/service/base-data.service";
import { ErrorLogEntity } from "../../domain/entities/error-log.entity";
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { ErrorLogModel } from "../models/error-log.model";
import { Repository } from "typeorm";
import { CONNECTION_NAME } from "src/core/strings/constants/base.constants";
@Injectable()
export class ErrorLogService extends BaseDataService<ErrorLogEntity> {
constructor(
@InjectRepository(ErrorLogModel, CONNECTION_NAME.DEFAULT)
private repo: Repository<ErrorLogModel>,
) {
super(repo);
}
}

View File

@ -0,0 +1,17 @@
import { Injectable } from "@nestjs/common";
import { BaseDataService } from "src/core/modules/data/service/base-data.service";
import { LogEntity } from "../../domain/entities/log.entity";
import { LogModel } from "../models/log.model";
import { InjectRepository } from "@nestjs/typeorm";
import { CONNECTION_NAME } from "src/core/strings/constants/base.constants";
import { Repository } from "typeorm";
@Injectable()
export class LogService extends BaseDataService<LogEntity> {
constructor(
@InjectRepository(LogModel, CONNECTION_NAME.DEFAULT)
private repo: Repository<LogModel>,
) {
super(repo);
}
}

View File

@ -0,0 +1,12 @@
import { BaseCoreEntity } from "src/core/modules/domain/entities/base-core.entity";
export interface ErrorLogEntity extends BaseCoreEntity {
data_id: string;
message: string;
description: string;
module: string;
sub_module: string;
created_at: number;
creator_name: string;
creator_id: string;
}

View File

@ -0,0 +1,5 @@
import { IEvent } from "src/core/strings/constants/interface.constants";
export class RecordErrorLog {
constructor(public readonly data: IEvent) {}
}

View File

@ -0,0 +1,15 @@
import { BaseCoreEntity } from "src/core/modules/domain/entities/base-core.entity";
import { OPERATION } from "src/core/strings/constants/base.constants";
export interface LogEntity extends BaseCoreEntity {
data_id: string;
process: OPERATION;
data: any;
old_data: any;
description: string;
module: string;
sub_module: string;
created_at: number;
creator_name: string;
creator_id: string;
}

View File

@ -0,0 +1,5 @@
import { IEvent } from "src/core/strings/constants/interface.constants";
export class RecordLog {
constructor(public readonly data: IEvent) {}
}

View File

@ -0,0 +1,11 @@
import { EventsHandler, IEventHandler } from "@nestjs/cqrs";
import { RecordErrorLog } from "../entities/error-log.event";
@EventsHandler(RecordErrorLog)
export class RecordErrorLogHandler implements IEventHandler<RecordErrorLog> {
async handle(event: RecordErrorLog) {
console.log(event, 'das error')
}
}

View File

@ -0,0 +1,11 @@
import { EventsHandler, IEventHandler } from "@nestjs/cqrs";
import { RecordLog } from "../entities/log.event";
@EventsHandler(RecordLog)
export class RecordLogHandler implements IEventHandler<RecordLog> {
async handle(event: RecordLog) {
console.log(event, 'log')
}
}

View File

View File

@ -0,0 +1,28 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { CqrsModule } from "@nestjs/cqrs";
import { TypeOrmModule } from "@nestjs/typeorm";
import { LogModel } from "./data/models/log.model";
import { ErrorLogModel } from "./data/models/error-log.model";
import { CONNECTION_NAME } from "src/core/strings/constants/base.constants";
import { RecordErrorLogHandler } from "./domain/handlers/error-log.handler";
import { RecordLogHandler } from "./domain/handlers/log.handler";
import { ErrorLogService } from "./data/services/error-log.service";
import { LogService } from "./data/services/log.service";
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forFeature([LogModel, ErrorLogModel], CONNECTION_NAME.DEFAULT),
CqrsModule,
],
controllers: [],
providers: [
RecordLogHandler,
RecordErrorLogHandler,
LogService,
ErrorLogService,
],
})
export class LogModule {}

View File

@ -16,7 +16,7 @@ export class UserPrivilegeConfigurationDataOrchestrator {
async update(data): Promise<UserPrivilegeConfigurationEntity> {
this.updateManager.setData(data);
this.updateManager.setService(this.serviceData);
this.updateManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE_CONFIGURATION);
await this.updateManager.execute();
return this.updateManager.getResult();
}

View File

@ -14,6 +14,7 @@ import { BatchConfirmUserPrivilegeManager } from './managers/batch-confirm-user-
import { BatchInactiveUserPrivilegeManager } from './managers/batch-inactive-user-privilege.manager';
import { BatchActiveUserPrivilegeManager } from './managers/batch-active-user-privilege.manager';
import { BatchDeleteUserPrivilegeManager } from './managers/batch-delete-user-privilege.manager';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
@Injectable()
export class UserPrivilegeDataOrchestrator extends BaseDataTransactionOrchestrator<UserPrivilegeEntity> {
@ -35,7 +36,7 @@ export class UserPrivilegeDataOrchestrator extends BaseDataTransactionOrchestrat
async create(data): Promise<UserPrivilegeEntity> {
this.createManager.setData(data);
this.createManager.setService(this.serviceData);
this.createManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.createManager.execute();
await this.createManager.generateConfig();
return this.createManager.getResult();
@ -43,63 +44,63 @@ export class UserPrivilegeDataOrchestrator extends BaseDataTransactionOrchestrat
async update(dataId, data): Promise<UserPrivilegeEntity> {
this.updateManager.setData(dataId, data);
this.updateManager.setService(this.serviceData);
this.updateManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.updateManager.execute();
return this.updateManager.getResult();
}
async delete(dataId): Promise<String> {
this.deleteManager.setData(dataId);
this.deleteManager.setService(this.serviceData);
this.deleteManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.deleteManager.execute();
return this.deleteManager.getResult();
}
async batchDelete(dataIds: string[]): Promise<BatchResult> {
this.batchDeleteManager.setData(dataIds);
this.batchDeleteManager.setService(this.serviceData);
this.batchDeleteManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.batchDeleteManager.execute();
return this.batchDeleteManager.getResult();
}
async active(dataId): Promise<String> {
this.activeManager.setData(dataId, STATUS.ACTIVE);
this.activeManager.setService(this.serviceData);
this.activeManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.activeManager.execute();
return this.activeManager.getResult();
}
async batchActive(dataIds: string[]): Promise<BatchResult> {
this.batchActiveManager.setData(dataIds, STATUS.ACTIVE);
this.batchActiveManager.setService(this.serviceData);
this.batchActiveManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.batchActiveManager.execute();
return this.batchActiveManager.getResult();
}
async confirm(dataId): Promise<String> {
this.confirmManager.setData(dataId, STATUS.ACTIVE);
this.confirmManager.setService(this.serviceData);
this.confirmManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.confirmManager.execute();
return this.confirmManager.getResult();
}
async batchConfirm(dataIds: string[]): Promise<BatchResult> {
this.batchConfirmManager.setData(dataIds, STATUS.ACTIVE);
this.batchConfirmManager.setService(this.serviceData);
this.batchConfirmManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.batchConfirmManager.execute();
return this.batchConfirmManager.getResult();
}
async inactive(dataId): Promise<String> {
this.inactiveManager.setData(dataId, STATUS.INACTIVE);
this.inactiveManager.setService(this.serviceData);
this.inactiveManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.inactiveManager.execute();
return this.inactiveManager.getResult();
}
async batchInactive(dataIds: string[]): Promise<BatchResult> {
this.batchInactiveManager.setData(dataIds, STATUS.INACTIVE);
this.batchInactiveManager.setService(this.serviceData);
this.batchInactiveManager.setService(this.serviceData, TABLE_NAME.USER_PRIVILEGE);
await this.batchInactiveManager.execute();
return this.batchInactiveManager.getResult();
}