feat: add gate scanner module
continuous-integration/drone/tag Build is passing Details

feat/fix-couch-transaction 20.1.55-alpha.1
shancheas 2024-09-02 13:59:19 +07:00
parent b9927da0c4
commit 45c4bde838
8 changed files with 116 additions and 2 deletions

View File

@ -76,6 +76,7 @@ import { PosLogModel } from './modules/configuration/log/data/models/pos-log.mod
import { ExportModule } from './modules/configuration/export/export.module'; import { ExportModule } from './modules/configuration/export/export.module';
import { TransactionDemographyModel } from './modules/transaction/transaction/data/models/transaction-demography.model'; import { TransactionDemographyModel } from './modules/transaction/transaction/data/models/transaction-demography.model';
import { SupersetModule } from './modules/configuration/superset/superset.module'; import { SupersetModule } from './modules/configuration/superset/superset.module';
import { GateScanModule } from './modules/gates/gate.module';
@Module({ @Module({
imports: [ imports: [
@ -178,6 +179,8 @@ import { SupersetModule } from './modules/configuration/superset/superset.module
// superset // superset
SupersetModule, SupersetModule,
GateScanModule,
], ],
controllers: [], controllers: [],
providers: [ providers: [

View File

@ -1 +1,2 @@
export const PAGINATION_RESPONSE = 'PAGINATION_RESPONSE'; export const PAGINATION_RESPONSE = 'PAGINATION_RESPONSE';
export const GATE_RESPONSE = 'GATE_RESPONSE';

View File

@ -1,5 +1,5 @@
import { SetMetadata } from '@nestjs/common'; import { SetMetadata } from '@nestjs/common';
import { PAGINATION_RESPONSE } from '../../constants'; import { GATE_RESPONSE, PAGINATION_RESPONSE } from '../../constants';
/** /**
* This decorator will tell the response, * This decorator will tell the response,
@ -7,3 +7,5 @@ import { PAGINATION_RESPONSE } from '../../constants';
*/ */
export const Pagination = (isPagination = true) => export const Pagination = (isPagination = true) =>
SetMetadata(PAGINATION_RESPONSE, isPagination); SetMetadata(PAGINATION_RESPONSE, isPagination);
export const Gate = () => SetMetadata(GATE_RESPONSE, true);

View File

@ -8,13 +8,20 @@ import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { Request } from 'express'; import { Request } from 'express';
import { Reflector } from '@nestjs/core'; import { Reflector } from '@nestjs/core';
import { PAGINATION_RESPONSE } from '../constants'; import { GATE_RESPONSE, PAGINATION_RESPONSE } from '../constants';
import { createPaginationResponse } from './utils/pagination-meta.helper'; import { createPaginationResponse } from './utils/pagination-meta.helper';
@Injectable() @Injectable()
export class TransformInterceptor implements NestInterceptor { export class TransformInterceptor implements NestInterceptor {
constructor(protected readonly reflector: Reflector) {} constructor(protected readonly reflector: Reflector) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const isGate = this.reflector.getAllAndOverride<boolean>(GATE_RESPONSE, [
context.getHandler(),
context.getClass(),
]);
if (isGate) return next.handle();
const isPagination = this.reflector.getAllAndOverride<boolean>( const isPagination = this.reflector.getAllAndOverride<boolean>(
PAGINATION_RESPONSE, PAGINATION_RESPONSE,
[context.getHandler(), context.getClass()], [context.getHandler(), context.getClass()],

View File

@ -0,0 +1,5 @@
export interface GateScanEntity {
gate_id: string;
type: string;
uuid: string;
}

View File

@ -0,0 +1,8 @@
export interface GateResponseEntity {
code: number;
message: string;
}
export interface GateMasterEntity {
codes: string[];
}

View File

@ -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 {}

View File

@ -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<GateResponseEntity> {
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<GateMasterEntity> {
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;
}
}