diff --git a/src/app.module.ts b/src/app.module.ts index 2c9e87e..ca71ede 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -76,6 +76,7 @@ import { PosLogModel } from './modules/configuration/log/data/models/pos-log.mod import { ExportModule } from './modules/configuration/export/export.module'; import { TransactionDemographyModel } from './modules/transaction/transaction/data/models/transaction-demography.model'; import { SupersetModule } from './modules/configuration/superset/superset.module'; +import { GateScanModule } from './modules/gates/gate.module'; @Module({ imports: [ @@ -178,6 +179,8 @@ import { SupersetModule } from './modules/configuration/superset/superset.module // superset SupersetModule, + + GateScanModule, ], controllers: [], providers: [ diff --git a/src/core/response/constants.ts b/src/core/response/constants.ts index 1da092d..ae59a5a 100644 --- a/src/core/response/constants.ts +++ b/src/core/response/constants.ts @@ -1 +1,2 @@ export const PAGINATION_RESPONSE = 'PAGINATION_RESPONSE'; +export const GATE_RESPONSE = 'GATE_RESPONSE'; diff --git a/src/core/response/domain/decorators/pagination.response.ts b/src/core/response/domain/decorators/pagination.response.ts index 482db0c..b9ba684 100644 --- a/src/core/response/domain/decorators/pagination.response.ts +++ b/src/core/response/domain/decorators/pagination.response.ts @@ -1,5 +1,5 @@ import { SetMetadata } from '@nestjs/common'; -import { PAGINATION_RESPONSE } from '../../constants'; +import { GATE_RESPONSE, PAGINATION_RESPONSE } from '../../constants'; /** * This decorator will tell the response, @@ -7,3 +7,5 @@ import { PAGINATION_RESPONSE } from '../../constants'; */ export const Pagination = (isPagination = true) => SetMetadata(PAGINATION_RESPONSE, isPagination); + +export const Gate = () => SetMetadata(GATE_RESPONSE, true); diff --git a/src/core/response/domain/response.interceptor.ts b/src/core/response/domain/response.interceptor.ts index f9893d4..9f4d8de 100644 --- a/src/core/response/domain/response.interceptor.ts +++ b/src/core/response/domain/response.interceptor.ts @@ -8,13 +8,20 @@ import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { Request } from 'express'; import { Reflector } from '@nestjs/core'; -import { PAGINATION_RESPONSE } from '../constants'; +import { GATE_RESPONSE, PAGINATION_RESPONSE } from '../constants'; import { createPaginationResponse } from './utils/pagination-meta.helper'; @Injectable() export class TransformInterceptor implements NestInterceptor { constructor(protected readonly reflector: Reflector) {} intercept(context: ExecutionContext, next: CallHandler): Observable { + const isGate = this.reflector.getAllAndOverride(GATE_RESPONSE, [ + context.getHandler(), + context.getClass(), + ]); + + if (isGate) return next.handle(); + const isPagination = this.reflector.getAllAndOverride( PAGINATION_RESPONSE, [context.getHandler(), context.getClass()], diff --git a/src/modules/gates/domain/entity/gate-request.entity.ts b/src/modules/gates/domain/entity/gate-request.entity.ts new file mode 100644 index 0000000..299e086 --- /dev/null +++ b/src/modules/gates/domain/entity/gate-request.entity.ts @@ -0,0 +1,5 @@ +export interface GateScanEntity { + gate_id: string; + type: string; + uuid: string; +} diff --git a/src/modules/gates/domain/entity/gate-response.entity.ts b/src/modules/gates/domain/entity/gate-response.entity.ts new file mode 100644 index 0000000..1d8019d --- /dev/null +++ b/src/modules/gates/domain/entity/gate-response.entity.ts @@ -0,0 +1,8 @@ +export interface GateResponseEntity { + code: number; + message: string; +} + +export interface GateMasterEntity { + codes: string[]; +} diff --git a/src/modules/gates/gate.module.ts b/src/modules/gates/gate.module.ts new file mode 100644 index 0000000..0f2577c --- /dev/null +++ b/src/modules/gates/gate.module.ts @@ -0,0 +1,17 @@ +import { Global, Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { GateController } from './infrastructure/gate.controller'; + +@Global() +@Module({ + imports: [ + ConfigModule.forRoot(), + // TypeOrmModule.forFeature( + // [], + // CONNECTION_NAME.DEFAULT, + // ), + ], + controllers: [GateController], + providers: [], +}) +export class GateScanModule {} diff --git a/src/modules/gates/infrastructure/gate.controller.ts b/src/modules/gates/infrastructure/gate.controller.ts new file mode 100644 index 0000000..a41da89 --- /dev/null +++ b/src/modules/gates/infrastructure/gate.controller.ts @@ -0,0 +1,71 @@ +import { Body, Controller, Get, Param, Post } from '@nestjs/common'; +import { ApiTags } from '@nestjs/swagger'; +import { Public } from 'src/core/guards'; +import { GateScanEntity } from '../domain/entity/gate-request.entity'; +import { + GateMasterEntity, + GateResponseEntity, +} from '../domain/entity/gate-response.entity'; +import { Gate } from 'src/core/response'; + +const masterGates = [ + '319b6d3e-b661-4d19-8695-0dd6fb76465e', + '9afdb79d-7162-43e6-8ac6-f1941adea7ba', + '7e4c0281-8cf2-420e-aba1-c8ff834de450', + '19318ac8-caa0-47e4-8a41-2aac238d3665', + '495bc25f-42c4-4007-8e79-3747fa1054b6', + 'b90fc9a9-efd9-4216-a8af-7ed120b141de', + '4399e93c-a839-4802-a49d-f933c72b1433', + '970673a7-6370-444a-931a-9784220dd35d', + '151ab50e-4e54-4252-b3ab-f5c0817b27a0', + '4c0e6924-baf5-47fb-a15b-fd1cd0958cc0', +]; + +const gateResponses = [ + { + code: 1, + message: 'Berhasil Check In', + }, + { + code: 2, + message: 'Gagal melakukan Check In. Karena tiket telah kadaluarsa', + }, + { + code: 3, + message: 'Gagal melakukan Check In. Tiket tidak tersedia', + }, +]; + +@ApiTags(`Gate - read`) +@Controller(`v1/gate`) +@Public(true) +@Gate() +export class GateController { + @Post('scan') + async scan(@Body() data: GateScanEntity): Promise { + if (masterGates.includes(data.uuid)) return gateResponses[0]; + + const response = Math.floor(Math.random() * 3); + return gateResponses[response]; + } + + @Get(':id/master') + async detail(@Param('id') id: string): Promise { + if (id == '1') return { codes: masterGates }; + return { + codes: this.createRandomStringArray(masterGates), + }; + } + + createRandomStringArray(inputArray: string[]): string[] { + const randomLength = Math.floor(Math.random() * 4) + 2; // Random length between 2 and 5 + const outputArray: string[] = []; + + while (outputArray.length < randomLength) { + const randomIndex = Math.floor(Math.random() * inputArray.length); + outputArray.push(inputArray[randomIndex]); + } + + return outputArray; + } +}