feat(SPG-123) Abstraction Transaction Data
parent
e442ca717e
commit
98f9fecc27
|
@ -0,0 +1,41 @@
|
|||
import { HttpStatus, UnprocessableEntityException } from '@nestjs/common';
|
||||
import { BaseDataService } from 'src/core/modules/data/service/base-data.service';
|
||||
|
||||
export class CheckDuplicateHelper {
|
||||
constructor(
|
||||
private dataService: BaseDataService<any>,
|
||||
private duplicateColumn: string[],
|
||||
private entity: any,
|
||||
private entityId?: string,
|
||||
) {}
|
||||
|
||||
async execute() {
|
||||
for (const column of this.duplicateColumn) {
|
||||
const queryBuilder = this.dataService
|
||||
.getRepository()
|
||||
.createQueryBuilder();
|
||||
queryBuilder.orWhere(
|
||||
`replace(trim(lower(${column})), ' ',' ') = :query`,
|
||||
{
|
||||
query: this.entity[column]
|
||||
?.toLowerCase()
|
||||
.trim()
|
||||
.replace(/ +(?= )/g, ''),
|
||||
},
|
||||
);
|
||||
|
||||
if (this.entityId) {
|
||||
queryBuilder.andWhere(`id Not In ('${this.entityId}')`);
|
||||
}
|
||||
|
||||
const data_exists = await queryBuilder.getCount();
|
||||
if (data_exists > 0) {
|
||||
throw new UnprocessableEntityException({
|
||||
statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
|
||||
message: `Entity with ${column} : ${this.entity[column]} already exist`,
|
||||
error: 'Unprocessable Entity',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +1,4 @@
|
|||
import {
|
||||
Inject,
|
||||
Injectable,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { 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';
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import { CheckDuplicateHelper } from 'src/core/helpers/query/check-duplicate.helpers';
|
||||
import { BaseManager } from '../base.manager';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
export abstract class BaseCreateManager<Entity> extends BaseManager {
|
||||
protected result: Entity;
|
||||
protected duplicateColumn: string[];
|
||||
abstract get entityTarget(): any;
|
||||
abstract get uniqueColumns(): string[];
|
||||
|
||||
setData(entity: Entity): void {
|
||||
this.data = entity;
|
||||
|
@ -19,6 +20,17 @@ export abstract class BaseCreateManager<Entity> extends BaseManager {
|
|||
});
|
||||
}
|
||||
|
||||
async validateProcess(): Promise<void> {
|
||||
if (this.uniqueColumns.length) {
|
||||
await new CheckDuplicateHelper(
|
||||
this.dataService,
|
||||
this.uniqueColumns,
|
||||
this.data,
|
||||
).execute();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
this.result = await this.dataService.create(
|
||||
this.queryRunner,
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
import { BaseManager } from '../base.manager';
|
||||
|
||||
export abstract class BaseCustomManager<Entity> extends BaseManager {
|
||||
protected result: Entity;
|
||||
abstract get entityTarget(): any;
|
||||
|
||||
setData(entity: Entity): void {
|
||||
this.data = entity;
|
||||
}
|
||||
|
||||
async prepareData(): Promise<void> {
|
||||
if (this.data)
|
||||
Object.assign(this.data, {
|
||||
editor_id: this.user.id,
|
||||
editor_name: this.user.name,
|
||||
updated_at: new Date().getTime(),
|
||||
});
|
||||
}
|
||||
|
||||
abstract getResult(): any;
|
||||
}
|
|
@ -1,8 +1,4 @@
|
|||
import {
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
UnprocessableEntityException,
|
||||
} from '@nestjs/common';
|
||||
import { HttpStatus, UnprocessableEntityException } from '@nestjs/common';
|
||||
import { BaseManager } from '../base.manager';
|
||||
|
||||
export abstract class BaseDeleteManager<Entity> extends BaseManager {
|
||||
|
|
|
@ -1,24 +1,23 @@
|
|||
import { BaseReadManager } from "../base-read.manager";
|
||||
import { BaseReadManager } from '../base-read.manager';
|
||||
|
||||
export abstract class BaseDetailManager<Entity> extends BaseReadManager {
|
||||
protected dataId: string;
|
||||
protected result: Entity;
|
||||
|
||||
protected dataId: string;
|
||||
protected result: Entity;
|
||||
abstract get selectData(): string[];
|
||||
abstract get relationData(): string[];
|
||||
abstract get setFindProperties(): any;
|
||||
|
||||
abstract get selectData(): string[];
|
||||
abstract get relationData(): string[];
|
||||
abstract get setFindProperties(): any;
|
||||
setData(dataId: string): void {
|
||||
this.dataId = dataId;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
this.queryBuilder.select(this.selectData).where(this.setFindProperties);
|
||||
this.result = await this.queryBuilder.getOne();
|
||||
}
|
||||
|
||||
getResult(): Entity {
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
getResult(): Entity {
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,54 +1,57 @@
|
|||
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";
|
||||
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
|
||||
import { BaseReadManager } from '../base-read.manager';
|
||||
import { SelectQueryBuilder } from 'typeorm';
|
||||
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[];
|
||||
protected result: PaginationResponse<Entity>;
|
||||
public filterParam: any;
|
||||
abstract get specificFilter(): Param[];
|
||||
|
||||
setFilterParam(param: BaseFilterEntity): void {
|
||||
this.filterParam = param;
|
||||
}
|
||||
setFilterParam(param: any): void {
|
||||
this.filterParam = param;
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
// const filterSearch: string[] = this.setFilterSearch();
|
||||
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, ' ') ?? ''
|
||||
// }%`,
|
||||
// });
|
||||
// });
|
||||
// }),
|
||||
// );
|
||||
// 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);
|
||||
// new SpecificSearchFilter<Entity>(
|
||||
// this.queryBuilder,
|
||||
// this.tableName,
|
||||
// this.specificFilter,
|
||||
// ).getFilter();
|
||||
this.setQueryFilter(this.queryBuilder);
|
||||
|
||||
this.result = await this.dataService.getIndex(
|
||||
this.queryBuilder,
|
||||
this.filterParam,
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
setFilterSearch(): string[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
abstract setQueryFilter(
|
||||
queryBuilder: SelectQueryBuilder<Entity>,
|
||||
): SelectQueryBuilder<Entity>;
|
||||
|
||||
getResult(): PaginationResponse<Entity> {
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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';
|
||||
import { UserPrivilegeModel } from 'src/modules/user-related/user-privilege/data/models/user-privilege.model';
|
||||
|
||||
export abstract class BaseUpdateStatusManager<Entity> extends BaseManager {
|
||||
protected dataId: string;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { CheckDuplicateHelper } from 'src/core/helpers/query/check-duplicate.helpers';
|
||||
import { BaseManager } from '../base.manager';
|
||||
|
||||
export abstract class BaseUpdateManager<Entity> extends BaseManager {
|
||||
|
@ -6,6 +6,7 @@ export abstract class BaseUpdateManager<Entity> extends BaseManager {
|
|||
protected result: Entity;
|
||||
protected duplicateColumn: string[];
|
||||
abstract get entityTarget(): any;
|
||||
abstract get uniqueColumns(): string[];
|
||||
|
||||
setData(id: string, entity: Entity): void {
|
||||
this.dataId = id;
|
||||
|
@ -18,6 +19,15 @@ export abstract class BaseUpdateManager<Entity> extends BaseManager {
|
|||
editor_name: this.user.name,
|
||||
updated_at: new Date().getTime(),
|
||||
});
|
||||
|
||||
if (this.uniqueColumns.length) {
|
||||
await new CheckDuplicateHelper(
|
||||
this.dataService,
|
||||
this.uniqueColumns,
|
||||
this.data,
|
||||
this.dataId,
|
||||
).execute();
|
||||
}
|
||||
}
|
||||
|
||||
async process(): Promise<void> {
|
||||
|
|
Loading…
Reference in New Issue