feat(SPG-325) REST API Read Users
continuous-integration/drone/tag Build is passing Details

pull/2/head devel_6.1
ashar 2024-06-10 14:14:34 +07:00
parent cfb896921b
commit 417e90dbc5
10 changed files with 253 additions and 6 deletions

View File

@ -1,6 +1,6 @@
import { BaseFilterEntity } from 'src/core/modules/domain/entities/base-filter.entity'; import { BaseFilterEntity } from 'src/core/modules/domain/entities/base-filter.entity';
export interface FilterUserEntity extends BaseFilterEntity { export interface FilterTenantEntity extends BaseFilterEntity {
emails: string[]; emails: string[];
usernames: string[]; usernames: string[];
} }

View File

@ -19,8 +19,13 @@ export class DetailTenantManager extends BaseDetailManager<UserEntity> {
get relations(): RelationParam { get relations(): RelationParam {
return { return {
// relation only join (for query purpose)
joinRelations: [], joinRelations: [],
selectRelations: [],
// relation join and select (relasi yang ingin ditampilkan),
selectRelations: [''],
// relation yang hanya ingin dihitung (akan return number)
countRelations: [], countRelations: [],
}; };
} }

View File

@ -24,8 +24,13 @@ export class IndexTenantManager extends BaseIndexManager<UserEntity> {
get relations(): RelationParam { get relations(): RelationParam {
return { return {
// relation only join (for query purpose)
joinRelations: [], joinRelations: [],
selectRelations: [],
// relation join and select (relasi yang ingin ditampilkan),
selectRelations: [''],
// relation yang hanya ingin dihitung (akan return number)
countRelations: [], countRelations: [],
}; };
} }
@ -65,7 +70,6 @@ export class IndexTenantManager extends BaseIndexManager<UserEntity> {
setQueryFilter( setQueryFilter(
queryBuilder: SelectQueryBuilder<UserEntity>, queryBuilder: SelectQueryBuilder<UserEntity>,
): SelectQueryBuilder<UserEntity> { ): SelectQueryBuilder<UserEntity> {
// tampilkan hanya role tenant // tampilkan hanya role tenant
// ? karena menggunakan table user (admin, staff, tenant) // ? karena menggunakan table user (admin, staff, tenant)
queryBuilder.andWhere(`${this.tableName}.role = :tenant`, { queryBuilder.andWhere(`${this.tableName}.role = :tenant`, {

View File

@ -1,9 +1,12 @@
import { BaseFilterDto } from 'src/core/modules/infrastructure/dto/base-filter.dto'; import { BaseFilterDto } from 'src/core/modules/infrastructure/dto/base-filter.dto';
import { FilterUserEntity } from '../../domain/entities/filter-tenant.entity'; import { FilterTenantEntity } from '../../domain/entities/filter-tenant.entity';
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer'; import { Transform } from 'class-transformer';
export class FilterTenantDto extends BaseFilterDto implements FilterUserEntity { export class FilterTenantDto
extends BaseFilterDto
implements FilterTenantEntity
{
@ApiProperty({ type: ['string'], required: false }) @ApiProperty({ type: ['string'], required: false })
@Transform((body) => { @Transform((body) => {
return Array.isArray(body.value) ? body.value : [body.value]; return Array.isArray(body.value) ? body.value : [body.value];

View File

@ -0,0 +1,10 @@
import { BaseFilterEntity } from 'src/core/modules/domain/entities/base-filter.entity';
export interface FilterUserEntity extends BaseFilterEntity {
// for searching
usernames: string[];
user_privileges: string[];
// for filtering
user_privilege_ids: string[];
}

View File

@ -0,0 +1,54 @@
import { Injectable } from '@nestjs/common';
import { BaseDetailManager } from 'src/core/modules/domain/usecase/managers/base-detail.manager';
import { UserEntity } from '../../entities/user.entity';
import { RelationParam } from 'src/core/modules/domain/entities/base-filter.entity';
@Injectable()
export class DetailUserManager extends BaseDetailManager<UserEntity> {
async prepareData(): Promise<void> {
return;
}
async beforeProcess(): Promise<void> {
return;
}
async afterProcess(): Promise<void> {
return;
}
get relations(): RelationParam {
return {
// relation only join (for query purpose)
joinRelations: [],
// relation join and select (relasi yang ingin ditampilkan),
selectRelations: ['user_privilege'],
// relation yang hanya ingin dihitung (akan return number)
countRelations: [],
};
}
get selects(): string[] {
return [
`${this.tableName}.id`,
`${this.tableName}.status`,
`${this.tableName}.name`,
`${this.tableName}.username`,
`${this.tableName}.created_at`,
`${this.tableName}.creator_name`,
`${this.tableName}.updated_at`,
`${this.tableName}.editor_name`,
'user_privilege.id',
'user_privilege.name',
];
}
get setFindProperties(): any {
return {
id: this.dataId,
};
}
}

View File

@ -0,0 +1,82 @@
import { Injectable } from '@nestjs/common';
import { BaseIndexManager } from 'src/core/modules/domain/usecase/managers/base-index.manager';
import { UserEntity } from '../../entities/user.entity';
import { SelectQueryBuilder } from 'typeorm';
import {
Param,
RelationParam,
} from 'src/core/modules/domain/entities/base-filter.entity';
import { UserRole } from '../../../constants';
@Injectable()
export class IndexUserManager extends BaseIndexManager<UserEntity> {
async prepareData(): Promise<void> {
return;
}
async beforeProcess(): Promise<void> {
return;
}
async afterProcess(): Promise<void> {
return;
}
get relations(): RelationParam {
return {
// relation only join (for query purpose)
joinRelations: [],
// relation join and select (relasi yang ingin ditampilkan),
selectRelations: ['user_privilege'],
// relation yang hanya ingin dihitung (akan return number)
countRelations: [],
};
}
get selects(): string[] {
return [
`${this.tableName}.id`,
`${this.tableName}.status`,
`${this.tableName}.name`,
`${this.tableName}.username`,
`${this.tableName}.created_at`,
`${this.tableName}.creator_name`,
`${this.tableName}.updated_at`,
`${this.tableName}.editor_name`,
'user_privilege.id',
'user_privilege.name',
];
}
get specificFilter(): Param[] {
return [
{
cols: `${this.tableName}.name`,
data: this.filterParam.names,
},
{
cols: `${this.tableName}.username`,
data: this.filterParam.usernames,
},
{
cols: `user_privilege.name`,
data: this.filterParam.user_privileges,
},
];
}
setQueryFilter(
queryBuilder: SelectQueryBuilder<UserEntity>,
): SelectQueryBuilder<UserEntity> {
// jangan tampilkan role tenant
// ? karena menggunakan table user (admin, staff, tenant)
queryBuilder.andWhere(`${this.tableName}.role != :tenant`, {
tenant: UserRole.TENANT,
});
return queryBuilder;
}
}

View File

@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import { IndexUserManager } from './managers/index-user.manager';
import { UserReadService } from '../../data/services/user-read.service';
import { UserEntity } from '../entities/user.entity';
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
import { BaseReadOrchestrator } from 'src/core/modules/domain/usecase/orchestrators/base-read.orchestrator';
import { DetailUserManager } from './managers/detail-user.manager';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
@Injectable()
export class UserReadOrchestrator extends BaseReadOrchestrator<UserEntity> {
constructor(
private indexManager: IndexUserManager,
private detailManager: DetailUserManager,
private serviceData: UserReadService,
) {
super();
}
async index(params): Promise<PaginationResponse<UserEntity>> {
this.indexManager.setFilterParam(params);
this.indexManager.setService(this.serviceData, TABLE_NAME.USER);
await this.indexManager.execute();
return this.indexManager.getResult();
}
async detail(dataId: string): Promise<UserEntity> {
this.detailManager.setData(dataId);
this.detailManager.setService(this.serviceData, TABLE_NAME.USER);
await this.detailManager.execute();
return this.detailManager.getResult();
}
}

View File

@ -0,0 +1,26 @@
import { BaseFilterDto } from 'src/core/modules/infrastructure/dto/base-filter.dto';
import { FilterUserEntity } from '../../domain/entities/filter-user.entity';
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
export class FilterUserDto extends BaseFilterDto implements FilterUserEntity {
// for searching
@ApiProperty({ type: ['string'], required: false })
@Transform((body) => {
return Array.isArray(body.value) ? body.value : [body.value];
})
usernames: string[];
@ApiProperty({ type: ['string'], required: false })
@Transform((body) => {
return Array.isArray(body.value) ? body.value : [body.value];
})
user_privileges: string[];
// for filtering
@ApiProperty({ type: ['string'], required: false })
@Transform((body) => {
return Array.isArray(body.value) ? body.value : [body.value];
})
user_privilege_ids: string[];
}

View File

@ -0,0 +1,30 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { FilterUserDto } from './dto/filter-user.dto';
import { Pagination } from 'src/core/response';
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
import { UserEntity } from '../domain/entities/user.entity';
import { UserReadOrchestrator } from '../domain/usecases/user-read.orchestrator';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { MODULE_NAME } from 'src/core/strings/constants/module.constants';
import { Public } from 'src/core/guards';
@ApiTags(`${MODULE_NAME.USER.split('-').join(' ')} - read`)
@Controller(MODULE_NAME.USER)
@Public(false)
@ApiBearerAuth('JWT')
export class UserReadController {
constructor(private orchestrator: UserReadOrchestrator) {}
@Get()
@Pagination()
async index(
@Query() params: FilterUserDto,
): Promise<PaginationResponse<UserEntity>> {
return await this.orchestrator.index(params);
}
@Get(':id')
async detail(@Param('id') id: string): Promise<UserEntity> {
return await this.orchestrator.detail(id);
}
}