From 5f46432327d9b68b37cdcc2a07f05b4d5d5d29b7 Mon Sep 17 00:00:00 2001 From: shancheas Date: Mon, 4 Nov 2024 12:35:53 +0700 Subject: [PATCH] feat: login customer --- .../queue/data/services/ticket.service.ts | 17 ++++++++++++- .../queue/domain/queue.orchestrator.ts | 25 +++++++++++-------- .../controllers/dto/login-queue.dto.ts | 20 +++++++++++++++ .../controllers/dto/split-queue.dto.ts | 9 ++++++- .../controllers/queue.controller.ts | 6 +++++ 5 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 src/modules/queue/infrastructure/controllers/dto/login-queue.dto.ts diff --git a/src/modules/queue/data/services/ticket.service.ts b/src/modules/queue/data/services/ticket.service.ts index 38e62a3..f0514d9 100644 --- a/src/modules/queue/data/services/ticket.service.ts +++ b/src/modules/queue/data/services/ticket.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import { BaseDataService } from 'src/core/modules/data/service/base-data.service'; import { InjectRepository } from '@nestjs/typeorm'; import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants'; -import { In, Repository } from 'typeorm'; +import { Between, In, Repository } from 'typeorm'; import { QueueTicket } from '../../domain/entities/ticket.entity'; import { QueueItemModel, @@ -10,6 +10,7 @@ import { QueueTicketModel, } from '../models/queue.model'; import { QueueOrder } from '../../domain/entities/order.entity'; +import * as moment from 'moment'; @Injectable() export class TicketDataService extends BaseDataService { @@ -39,6 +40,20 @@ export class TicketDataService extends BaseDataService { }); } + async loginPhone(user: string, phone: string): Promise { + const start = moment().startOf('day').valueOf(); + const end = moment().endOf('day').valueOf(); + + return this.order.findOneOrFail({ + relations: ['tickets'], + where: { + customer: user, + phone: `+${phone}`, + date: Between(start, end), + }, + }); + } + async orders(order_id: string): Promise { const order = await this.order.findOneOrFail({ where: { diff --git a/src/modules/queue/domain/queue.orchestrator.ts b/src/modules/queue/domain/queue.orchestrator.ts index e3314ff..d600c31 100644 --- a/src/modules/queue/domain/queue.orchestrator.ts +++ b/src/modules/queue/domain/queue.orchestrator.ts @@ -16,6 +16,8 @@ import { CustomerQueueItemListManager } from './usecases/queue/customer-queue-it import { CustomerQueueListManager } from './usecases/queue/customer-queue-list.manager'; import { SplitQueueDto } from '../infrastructure/controllers/dto/split-queue.dto'; import { SplitQueueManager } from './usecases/split-queue.manager'; +import { LoginQueueDto } from '../infrastructure/controllers/dto/login-queue.dto'; +import * as moment from 'moment'; @Injectable() export class QueueOrchestrator { @@ -38,6 +40,19 @@ export class QueueOrchestrator { } } + async loginPhone(login: LoginQueueDto): Promise { + const { name, phone } = login; + const now = moment().format('DD/MM/YYYY'); + try { + return await this.dataService.loginPhone(name, phone); + } catch (error) { + throw new UnauthorizedException({ + message: `Antrian atas nama ${name} dan nomor telepon ${phone} pada tanggal ${now} tidak ditemukan`, + error: 'INVOICE_NOT_FOUND', + }); + } + } + async create(data: RegisterQueueDto): Promise { const queue = await this.queueService.getTicketItems( data.ticket_id, @@ -56,16 +71,6 @@ export class QueueOrchestrator { async split(data: SplitQueueDto): Promise { const queueIds = data.items.map((i) => i.queue_item_id); const queue = await this.dataService.orderItems(data.order_id, queueIds); - // queue.tickets = queue.tickets.map((ticket) => { - // ticket.items = ticket.items.map((item) => { - // const itemQty = data.items.find((i) => i.queue_item_id === item.id); - // return { - // ...item, - // qty: itemQty.qty, - // }; - // }); - // return ticket; - // }); this.splitQueueManager.setRequestData(data); this.splitQueueManager.setData(queue); diff --git a/src/modules/queue/infrastructure/controllers/dto/login-queue.dto.ts b/src/modules/queue/infrastructure/controllers/dto/login-queue.dto.ts new file mode 100644 index 0000000..6a0678d --- /dev/null +++ b/src/modules/queue/infrastructure/controllers/dto/login-queue.dto.ts @@ -0,0 +1,20 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString, Matches } from 'class-validator'; + +export class LoginQueueDto { + @ApiProperty({ name: 'name', required: true }) + @IsString() + @IsNotEmpty() + name: string; + + @ApiProperty({ + name: 'phone', + required: true, + description: 'Phone Number Must Start With 62, no + or 0', + example: '628123456789', + }) + @IsString() + @IsNotEmpty() + @Matches(/^628\d+$/i, { message: 'Nomor Telepon tidak valid' }) + phone: string; +} diff --git a/src/modules/queue/infrastructure/controllers/dto/split-queue.dto.ts b/src/modules/queue/infrastructure/controllers/dto/split-queue.dto.ts index 482267c..efdfecb 100644 --- a/src/modules/queue/infrastructure/controllers/dto/split-queue.dto.ts +++ b/src/modules/queue/infrastructure/controllers/dto/split-queue.dto.ts @@ -4,6 +4,7 @@ import { IsNotEmpty, IsNumber, IsString, + Matches, Min, ValidateNested, } from 'class-validator'; @@ -35,9 +36,15 @@ export class SplitQueueDto { @IsNotEmpty() name: string; - @ApiProperty({ name: 'phone', required: true }) + @ApiProperty({ + name: 'phone', + required: true, + description: 'Phone Number Must Start With 62, no + or 0', + example: '+628123456789', + }) @IsString() @IsNotEmpty() + @Matches(/^\+628\d+$/i, { message: 'Nomor Telepon tidak valid' }) phone: string; @ApiProperty({ diff --git a/src/modules/queue/infrastructure/controllers/queue.controller.ts b/src/modules/queue/infrastructure/controllers/queue.controller.ts index f132bd2..34a85af 100644 --- a/src/modules/queue/infrastructure/controllers/queue.controller.ts +++ b/src/modules/queue/infrastructure/controllers/queue.controller.ts @@ -9,6 +9,7 @@ import { QueueOrder } from '../../domain/entities/order.entity'; import { Queue } from '../../domain/entities/queue.entity'; import { RegisterQueueDto } from './dto/register-queue.dto'; import { SplitQueueDto } from './dto/split-queue.dto'; +import { LoginQueueDto } from './dto/login-queue.dto'; @ApiTags(`Queue`) @Controller(`v1/${MODULE_NAME.QUEUE}`) @@ -27,6 +28,11 @@ export class QueueController { return await this.orchestrator.split(data); } + @Post('login') + async loginQueue(@Body() data: LoginQueueDto): Promise { + return await this.orchestrator.loginPhone(data); + } + @Get('login/:id') async loginCustomer(@Param('id') id: string): Promise { return await this.orchestrator.loginCustomer(id);