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 { Between, In, Repository } from 'typeorm'; import { QueueTicket } from '../../domain/entities/ticket.entity'; import { QueueItemModel, QueueOrderModel, QueueTicketModel, } from '../models/queue.model'; import { QueueOrder } from '../../domain/entities/order.entity'; import * as moment from 'moment'; @Injectable() export class TicketDataService extends BaseDataService { constructor( @InjectRepository(QueueTicketModel, CONNECTION_NAME.DEFAULT) private repo: Repository, @InjectRepository(QueueOrderModel, CONNECTION_NAME.DEFAULT) private order: Repository, @InjectRepository(QueueItemModel, CONNECTION_NAME.DEFAULT) private item: Repository, ) { super(repo); } async createQueueOrder(order: QueueOrder): Promise { return await this.order.save(order); } async loginQueue(id: string): Promise { return this.order.findOneOrFail({ relations: ['tickets'], where: [{ transaction_id: id }, { code: id }], }); } 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: { id: order_id, }, }); if (order.transaction_id != null) { return this.order.find({ where: { code: order.code, }, }); } return [order]; } async transactions(transaction_id: string): Promise { const order = await this.order.findOneOrFail({ where: { transaction_id, }, }); if (order.transaction_id != null) { return this.order.find({ where: { code: order.code, }, }); } return [order]; } async deleteQueue(transaction_id: string): Promise { const transactions = await this.transactions(transaction_id); await this.order.remove(transactions); } async orderIds(order_id: string): Promise { const orders = await this.orders(order_id); return orders.map((order) => order.id); } async orderItems( order_id: string, item_ids: string[], ): Promise { const order = await this.orderIds(order_id); return this.order.findOneOrFail({ relations: ['tickets', 'tickets.items'], where: { tickets: { order_id: In(order), items: { id: In(item_ids), }, }, }, }); } async queuePosTickets(order_id: string): Promise { return this.repo.find({ relations: [ 'items', 'items.queue', 'items.item', 'items.item.item_queue', ], where: { order_id, }, }); } async queueTickets(order_id: string): Promise { const order = await this.orderIds(order_id); const start = moment().startOf('day').valueOf(); const end = moment().endOf('day').valueOf(); return this.repo.find({ relations: [ 'items', 'items.queue', 'items.item', 'items.item.item_queue', ], where: { order_id: In(order), date: Between(start, end), }, }); } async queueTicketItems( order_id: string, ticket_id: string, ): Promise { const order = await this.orderIds(order_id); return this.repo.find({ relations: [ 'items', 'items.queue', 'items.item', 'items.item.item_queue', ], where: { order_id: In(order), id: ticket_id, }, }); } async queueTicketActive( order_id: string, ticket_id: string, ): Promise { // const order = await this.orderIds(order_id); return this.repo.find({ relations: ['items', 'items.queue'], where: { // order_id: In(order), id: ticket_id, // items: { // queue: { // status: In(['waiting']), // }, // }, }, }); } async queueItemTickets( order_id: string, item_id: string, ): Promise { const order = await this.orderIds(order_id); return this.repo.find({ relations: [ 'items', 'items.queue', 'items.item', 'items.item.item_queue', ], where: { order_id: In(order), items: [{ item_id }, { item: { item_queue: { id: item_id } } }], }, }); } async queueItems(order_id: string): Promise { const order = await this.orderIds(order_id); return this.item.find({ relations: ['queue', 'ticket'], where: { ticket: { order_id: In(order), }, }, }); } }