fix(SPG-544) BE-Validasi Tenant dan Item Tenant
parent
f170c2e017
commit
766909798f
|
@ -17,12 +17,29 @@ export class ValidateRelationHelper<Entity> {
|
|||
|
||||
// load relation
|
||||
for (const relation of this.relations) {
|
||||
if (relation.singleQuery) {
|
||||
queryBuilder.leftJoinAndMapOne(
|
||||
`${ this.tableName }.${ relation.relation }`,
|
||||
`${ this.tableName }.${ relation.relation }`,
|
||||
relation.relation,
|
||||
)
|
||||
}
|
||||
else if (relation.query) {
|
||||
queryBuilder.loadRelationCountAndMap(
|
||||
`${ this.tableName }.total_${ relation.relation }`,
|
||||
`${ this.tableName }.${ relation.relation }`,
|
||||
`total_${ relation.relation }`,
|
||||
relation.query,
|
||||
);
|
||||
}
|
||||
else {
|
||||
queryBuilder.loadRelationCountAndMap(
|
||||
`${ this.tableName }.total_${ relation.relation }`,
|
||||
`${ this.tableName }.${ relation.relation }`,
|
||||
`total_${ relation.relation }`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// filtering data only with specific data
|
||||
queryBuilder.where(`${ this.tableName }.id in ('${ this.dataId }')`);
|
||||
|
@ -36,8 +53,25 @@ export class ValidateRelationHelper<Entity> {
|
|||
relation.message ??
|
||||
`Failed! this data already connected to ${ relation.relation }`;
|
||||
|
||||
if (data[`total_${relation.relation}`])
|
||||
if (relation.singleQuery) {
|
||||
const relationColumn = data[relation.relation]?.[`${ relation.singleQuery[0] }`]
|
||||
if (this.mappingValidator(relationColumn, relation.singleQuery[1], relation.singleQuery[2]))
|
||||
throw new UnprocessableEntityException(message);
|
||||
} else if (data[`total_${ relation.relation } `])
|
||||
throw new UnprocessableEntityException(message);
|
||||
}
|
||||
}
|
||||
|
||||
mappingValidator(column, operator, value) {
|
||||
switch (operator) {
|
||||
case '!=':
|
||||
return column != value;
|
||||
|
||||
case '==':
|
||||
return column == value;
|
||||
|
||||
default:
|
||||
return column == value;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
import { UsersSession } from 'src/core/sessions';
|
||||
import { OPERATION } from './base.constants';
|
||||
import { SelectQueryBuilder } from 'typeorm';
|
||||
|
||||
export interface EventTopics {
|
||||
topic: any;
|
||||
|
@ -7,7 +8,16 @@ export interface EventTopics {
|
|||
}
|
||||
|
||||
export interface validateRelations {
|
||||
// nama relasi
|
||||
relation: string;
|
||||
|
||||
// query digunakan untuk query optional jika ingin specifik data
|
||||
query?: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>;
|
||||
|
||||
// query ini sama dengan yang diatas, akan tetapi ini khusus untuk ManyToOne relation
|
||||
singleQuery?: [string, any, any];
|
||||
|
||||
// custom message
|
||||
message?: string;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { HttpStatus, Injectable, UnprocessableEntityException } from '@nestjs/common';
|
||||
import { BaseUpdateStatusManager } from 'src/core/modules/domain/usecase/managers/base-update-status.manager';
|
||||
import { ItemEntity } from '../../entities/item.entity';
|
||||
import {
|
||||
|
@ -7,6 +7,7 @@ import {
|
|||
} from 'src/core/strings/constants/interface.constants';
|
||||
import { ItemModel } from '../../../data/models/item.model';
|
||||
import { ItemChangeStatusEvent } from '../../entities/event/item-change-status.event';
|
||||
import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
|
||||
@Injectable()
|
||||
export class ActiveItemManager extends BaseUpdateStatusManager<ItemEntity> {
|
||||
|
@ -27,7 +28,11 @@ export class ActiveItemManager extends BaseUpdateStatusManager<ItemEntity> {
|
|||
}
|
||||
|
||||
get validateRelations(): validateRelations[] {
|
||||
return [];
|
||||
return [{
|
||||
relation: 'tenant',
|
||||
singleQuery: ['status', '!=', STATUS.ACTIVE],
|
||||
message: `Failed! Tenant of item must be active first`,
|
||||
}];
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
|
|
|
@ -7,7 +7,8 @@ import {
|
|||
import { ItemModel } from '../../../data/models/item.model';
|
||||
import { ItemChangeStatusEvent } from '../../entities/event/item-change-status.event';
|
||||
import { BatchResult } from 'src/core/response/domain/ok-response.interface';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { HttpStatus, Injectable, UnprocessableEntityException } from '@nestjs/common';
|
||||
import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
|
||||
@Injectable()
|
||||
export class BatchActiveItemManager extends BaseBatchUpdateStatusManager<ItemEntity> {
|
||||
|
@ -24,7 +25,11 @@ export class BatchActiveItemManager extends BaseBatchUpdateStatusManager<ItemEnt
|
|||
}
|
||||
|
||||
get validateRelations(): validateRelations[] {
|
||||
return [];
|
||||
return [{
|
||||
relation: 'tenant',
|
||||
singleQuery: ['status', '!=', STATUS.ACTIVE],
|
||||
message: `Failed! Tenant of item must be active first`,
|
||||
}];
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
|
|
|
@ -8,10 +8,11 @@ import { ItemModel } from '../../../data/models/item.model';
|
|||
import { ItemChangeStatusEvent } from '../../entities/event/item-change-status.event';
|
||||
import { BatchResult } from 'src/core/response/domain/ok-response.interface';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
|
||||
@Injectable()
|
||||
export class BatchConfirmItemManager extends BaseBatchUpdateStatusManager<ItemEntity> {
|
||||
validateData(data: ItemEntity): Promise<void> {
|
||||
async validateData(data: ItemEntity): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -24,7 +25,11 @@ export class BatchConfirmItemManager extends BaseBatchUpdateStatusManager<ItemEn
|
|||
}
|
||||
|
||||
get validateRelations(): validateRelations[] {
|
||||
return [];
|
||||
return [{
|
||||
relation: 'tenant',
|
||||
singleQuery: ['status', '!=', STATUS.ACTIVE],
|
||||
message: `Failed! Tenant of item must be active first`,
|
||||
}];
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { HttpStatus, Injectable, UnprocessableEntityException } from '@nestjs/common';
|
||||
import { BaseUpdateStatusManager } from 'src/core/modules/domain/usecase/managers/base-update-status.manager';
|
||||
import { ItemEntity } from '../../entities/item.entity';
|
||||
import {
|
||||
|
@ -7,6 +7,7 @@ import {
|
|||
} from 'src/core/strings/constants/interface.constants';
|
||||
import { ItemModel } from '../../../data/models/item.model';
|
||||
import { ItemChangeStatusEvent } from '../../entities/event/item-change-status.event';
|
||||
import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
|
||||
@Injectable()
|
||||
export class ConfirmItemManager extends BaseUpdateStatusManager<ItemEntity> {
|
||||
|
@ -27,7 +28,11 @@ export class ConfirmItemManager extends BaseUpdateStatusManager<ItemEntity> {
|
|||
}
|
||||
|
||||
get validateRelations(): validateRelations[] {
|
||||
return [];
|
||||
return [{
|
||||
relation: 'tenant',
|
||||
singleQuery: ['status', '!=', STATUS.ACTIVE],
|
||||
message: `Failed! Tenant of item must be active first`,
|
||||
}];
|
||||
}
|
||||
|
||||
get entityTarget(): any {
|
||||
|
|
|
@ -8,11 +8,19 @@ import { BatchResult } from 'src/core/response/domain/ok-response.interface';
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { UserEntity } from 'src/modules/user-related/user/domain/entities/user.entity';
|
||||
import { UserModel } from 'src/modules/user-related/user/data/models/user.model';
|
||||
import { SelectQueryBuilder } from 'typeorm';
|
||||
import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
|
||||
@Injectable()
|
||||
export class BatchDeleteTenantManager extends BaseBatchDeleteManager<UserEntity> {
|
||||
get validateRelations(): validateRelations[] {
|
||||
return [{ relation: 'items' }];
|
||||
return [{
|
||||
relation: 'items',
|
||||
query: (qb: SelectQueryBuilder<any>) => {
|
||||
return qb.andWhere('total_items.status In (:...statuses)', { statuses: [STATUS.ACTIVE] });
|
||||
},
|
||||
message: 'Failed! There is active item'
|
||||
}];
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
|
|
|
@ -8,11 +8,19 @@ import { BatchResult } from 'src/core/response/domain/ok-response.interface';
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { UserEntity } from 'src/modules/user-related/user/domain/entities/user.entity';
|
||||
import { UserModel } from 'src/modules/user-related/user/data/models/user.model';
|
||||
import { SelectQueryBuilder } from 'typeorm';
|
||||
import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
|
||||
@Injectable()
|
||||
export class BatchInactiveTenantManager extends BaseBatchUpdateStatusManager<UserEntity> {
|
||||
get validateRelations(): validateRelations[] {
|
||||
return [{ relation: 'items' }];
|
||||
return [{
|
||||
relation: 'items',
|
||||
query: (qb: SelectQueryBuilder<any>) => {
|
||||
return qb.andWhere('total_items.status In (:...statuses)', { statuses: [STATUS.ACTIVE] });
|
||||
},
|
||||
message: 'Failed! There is active item'
|
||||
}];
|
||||
}
|
||||
|
||||
validateData(data: UserEntity): Promise<void> {
|
||||
|
|
|
@ -7,11 +7,19 @@ import {
|
|||
import { TenantDeletedEvent } from '../../entities/event/tenant-deleted.event';
|
||||
import { UserModel } from 'src/modules/user-related/user/data/models/user.model';
|
||||
import { UserEntity } from 'src/modules/user-related/user/domain/entities/user.entity';
|
||||
import { SelectQueryBuilder } from 'typeorm';
|
||||
import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
|
||||
@Injectable()
|
||||
export class DeleteTenantManager extends BaseDeleteManager<UserEntity> {
|
||||
get validateRelations(): validateRelations[] {
|
||||
return [{ relation: 'items' }];
|
||||
return [{
|
||||
relation: 'items',
|
||||
query: (qb: SelectQueryBuilder<any>) => {
|
||||
return qb.andWhere('total_items.status In (:...statuses)', { statuses: [STATUS.ACTIVE] });
|
||||
},
|
||||
message: 'Failed! There is active item'
|
||||
}];
|
||||
}
|
||||
|
||||
getResult(): string {
|
||||
|
|
|
@ -7,11 +7,19 @@ import {
|
|||
import { TenantChangeStatusEvent } from '../../entities/event/tenant-change-status.event';
|
||||
import { UserModel } from 'src/modules/user-related/user/data/models/user.model';
|
||||
import { UserEntity } from 'src/modules/user-related/user/domain/entities/user.entity';
|
||||
import { SelectQueryBuilder } from 'typeorm';
|
||||
import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
|
||||
@Injectable()
|
||||
export class InactiveTenantManager extends BaseUpdateStatusManager<UserEntity> {
|
||||
get validateRelations(): validateRelations[] {
|
||||
return [{ relation: 'items' }];
|
||||
return [{
|
||||
relation: 'items',
|
||||
query: (qb: SelectQueryBuilder<any>) => {
|
||||
return qb.andWhere('total_items.status In (:...statuses)', { statuses: [STATUS.ACTIVE] });
|
||||
},
|
||||
message: 'Failed! There is active item'
|
||||
}];
|
||||
}
|
||||
|
||||
getResult(): string {
|
||||
|
|
Loading…
Reference in New Issue