feat: merge item queue

pull/115/head
shancheas 2024-11-11 14:29:17 +07:00
parent d413f1fa7b
commit c129a59d47
6 changed files with 60 additions and 22 deletions

View File

@ -1,6 +1,7 @@
import { BaseCoreEntity } from 'src/core/modules/domain/entities/base-core.entity'; import { BaseCoreEntity } from 'src/core/modules/domain/entities/base-core.entity';
import { ItemEntity } from 'src/modules/item-related/item/domain/entities/item.entity'; import { ItemEntity } from 'src/modules/item-related/item/domain/entities/item.entity';
import { QueueTicket } from './ticket.entity'; import { QueueTicket } from './ticket.entity';
import { QueueModel } from '../../data/models/queue.model';
export interface QueueItem extends BaseCoreEntity { export interface QueueItem extends BaseCoreEntity {
ticket?: QueueTicket; ticket?: QueueTicket;
@ -8,3 +9,12 @@ export interface QueueItem extends BaseCoreEntity {
item_id: string; item_id: string;
qty: number; qty: number;
} }
export interface MergedItemQueue extends BaseCoreEntity {
id: string;
queue_item_id: string;
title: string;
image_url: string;
qty: number;
queues: QueueModel[];
}

View File

@ -10,6 +10,7 @@ export class CustomerQueueDetailManager extends CustomerQueueManager {
get data() { get data() {
const queueCondition = new QueueCondition(this.queues); const queueCondition = new QueueCondition(this.queues);
return this.tickets.map((ticket) => { return this.tickets.map((ticket) => {
const queueItems = this.mergeQueueItems(ticket);
return { return {
id: ticket.id, id: ticket.id,
code: ticket.code, code: ticket.code,
@ -22,19 +23,18 @@ export class CustomerQueueDetailManager extends CustomerQueueManager {
total_used: this.totalUsedTickets(ticket), total_used: this.totalUsedTickets(ticket),
total_queue: this.totalQueueTickets(ticket), total_queue: this.totalQueueTickets(ticket),
}, },
items: ticket.items.map((item) => { items: queueItems.map((item) => {
const queueItem = item.item.item_queue ?? item.item;
return { return {
id: item.item_id, id: item.id,
item_queue_id: item.id, item_queue_id: item.queue_item_id,
title: queueItem.name, title: item.title,
image_url: item.item.image_url, image_url: item.image_url,
summary: { summary: {
total_tickets: item.qty, total_tickets: item.qty,
total_used: this.totalUsedItems(item), total_used: this.totalUsedItems(item.queues),
total_queue: this.totalQueueItems(item), total_queue: this.totalQueueItems(item.queues),
}, },
queue: item.queue queue: item.queues
.sort((a, b) => b.time - a.time) .sort((a, b) => b.time - a.time)
.map((q) => { .map((q) => {
return { return {
@ -44,7 +44,7 @@ export class CustomerQueueDetailManager extends CustomerQueueManager {
status: q.status, status: q.status,
}; };
}), }),
queue_condition: queueCondition.condition(queueItem.id), queue_condition: queueCondition.condition(item.queue_item_id),
}; };
}), }),
}; };

View File

@ -12,10 +12,9 @@ export class CustomerQueueItemListManager extends CustomerQueueManager {
} }
get data() { get data() {
const tickets = this.tickets; const tickets = this.tickets;
const ticketItems = {};
const queueCondition = new QueueCondition(this.queues); const queueCondition = new QueueCondition(this.queues);
const ticketItems = {};
tickets.forEach((ticket) => { tickets.forEach((ticket) => {
ticket.items.forEach((item) => { ticket.items.forEach((item) => {
const item_id = item.item.item_queue?.id ?? item.item.id; const item_id = item.item.item_queue?.id ?? item.item.id;

View File

@ -1,7 +1,9 @@
import { import {
QueueItemModel, QueueItemModel,
QueueModel,
QueueTicketModel, QueueTicketModel,
} from '../../../data/models/queue.model'; } from '../../../data/models/queue.model';
import { MergedItemQueue } from '../../entities/queue-item.entity';
import { toTime } from '../../helpers/time.helper'; import { toTime } from '../../helpers/time.helper';
export class CustomerQueueManager { export class CustomerQueueManager {
@ -22,32 +24,61 @@ export class CustomerQueueManager {
return ticket.items.reduce((acc, item) => acc + item.qty, 0); return ticket.items.reduce((acc, item) => acc + item.qty, 0);
} }
totalUsedItems(item: QueueItemModel): number { totalUsedItems(queues: QueueModel[]): number {
return item.queue return queues
.filter((q) => ['done', 'called'].includes(q.status)) .filter((q) => ['done', 'called'].includes(q.status))
.reduce((acc, item) => acc + item.qty, 0); .reduce((acc, item) => acc + item.qty, 0);
} }
totalUsedTickets(ticket: QueueTicketModel): number { totalUsedTickets(ticket: QueueTicketModel): number {
const tickets = ticket.items.map((item) => { const tickets = ticket.items.map((item) => {
return this.totalUsedItems(item); return this.totalUsedItems(item.queue);
}); });
const reducer = (accumulator, currentValue) => accumulator + currentValue; const reducer = (accumulator, currentValue) => accumulator + currentValue;
return tickets.reduce(reducer, 0); return tickets.reduce(reducer, 0);
} }
totalQueueItems(item: QueueItemModel): number { totalQueueItems(queues: QueueModel[]): number {
return item.queue return queues
.filter((q) => ['waiting'].includes(q.status)) .filter((q) => ['waiting'].includes(q.status))
.reduce((acc, item) => acc + item.qty, 0); .reduce((acc, item) => acc + item.qty, 0);
} }
totalQueueTickets(ticket: QueueTicketModel): number { totalQueueTickets(ticket: QueueTicketModel): number {
const tickets = ticket.items.map((item) => { const tickets = ticket.items.map((item) => {
return this.totalQueueItems(item); return this.totalQueueItems(item.queue);
}); });
const reducer = (accumulator, currentValue) => accumulator + currentValue; const reducer = (accumulator, currentValue) => accumulator + currentValue;
return tickets.reduce(reducer, 0); return tickets.reduce(reducer, 0);
} }
mergeQueueItems(ticket: QueueTicketModel): MergedItemQueue[] {
const ticketItems = {};
ticket.items.forEach((item) => {
const item_id = item.item.item_queue?.id ?? item.item.id;
const currentItem = ticketItems[item_id];
ticketItems[item_id] = currentItem ? [...currentItem, item] : [item];
});
return Object.values<QueueItemModel[]>(ticketItems).map((items) => {
const item = items[0];
const queues: QueueModel[] = [];
items.forEach((item) => {
queues.push(...item.queue);
});
const item_qty = items.reduce((acc, item) => acc + item.qty, 0);
const queueItem = item.item.item_queue ?? item.item;
return {
id: item.item_id,
queue_item_id: item.id,
title: queueItem.name,
image_url: item.item.image_url,
qty: item_qty,
queues,
};
});
}
} }

View File

@ -7,7 +7,6 @@ import {
TransactionUserType, TransactionUserType,
TransactionPaymentType, TransactionPaymentType,
} from '../../constants'; } from '../../constants';
import { TransactionItemEntity } from '../../domain/entities/transaction-item.entity';
import { TransactionItemModel } from './transaction-item.model'; import { TransactionItemModel } from './transaction-item.model';
import { TransactionTaxModel } from './transaction-tax.model'; import { TransactionTaxModel } from './transaction-tax.model';
import { STATUS } from 'src/core/strings/constants/base.constants'; import { STATUS } from 'src/core/strings/constants/base.constants';
@ -239,7 +238,7 @@ export class TransactionModel
onDelete: 'CASCADE', onDelete: 'CASCADE',
onUpdate: 'CASCADE', onUpdate: 'CASCADE',
}) })
items: TransactionItemEntity[]; items: TransactionItemModel[];
// relations to tax data // relations to tax data
@OneToMany(() => TransactionTaxModel, (model) => model.transaction, { @OneToMany(() => TransactionTaxModel, (model) => model.transaction, {

View File

@ -1,13 +1,12 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { BaseDataService } from 'src/core/modules/data/service/base-data.service'; import { BaseDataService } from 'src/core/modules/data/service/base-data.service';
import { TransactionEntity } from '../../domain/entities/transaction.entity';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { TransactionModel } from '../models/transaction.model'; import { TransactionModel } from '../models/transaction.model';
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants'; import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
@Injectable() @Injectable()
export class TransactionDataService extends BaseDataService<TransactionEntity> { export class TransactionDataService extends BaseDataService<TransactionModel> {
constructor( constructor(
@InjectRepository(TransactionModel, CONNECTION_NAME.DEFAULT) @InjectRepository(TransactionModel, CONNECTION_NAME.DEFAULT)
private repo: Repository<TransactionModel>, private repo: Repository<TransactionModel>,