55 lines
1.2 KiB
TypeScript
Executable File
55 lines
1.2 KiB
TypeScript
Executable File
import { EntityManager, EntityRepository, FilterQuery } from '@mikro-orm/postgresql';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { Ticket } from '../entities/ticket.entity';
|
|
import { FindTicketQueryDto } from '../DTO/search-ticket-query.dto';
|
|
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
|
|
|
class FindOrdersOpts extends FindTicketQueryDto {
|
|
orderItemId: number;
|
|
};
|
|
|
|
@Injectable()
|
|
export class TicketRepository extends EntityRepository<Ticket> {
|
|
constructor(
|
|
readonly em: EntityManager,
|
|
) {
|
|
super(em, Ticket);
|
|
}
|
|
|
|
async findAllPaginated(opts: FindOrdersOpts): Promise<PaginatedResult<Ticket>> {
|
|
const {
|
|
page = 1,
|
|
limit = 10,
|
|
orderItemId: orderId
|
|
} = opts;
|
|
|
|
const offset = (page - 1) * limit;
|
|
|
|
const where: FilterQuery<Ticket> = {
|
|
orderItem: { id: orderId }
|
|
};
|
|
|
|
|
|
const [data, total] = await this.findAndCount(where, {
|
|
limit,
|
|
offset,
|
|
orderBy: { createdAt: 'desc' },
|
|
populate: ['admin', 'user']
|
|
});
|
|
|
|
|
|
const totalPages = Math.ceil(total / limit);
|
|
|
|
return {
|
|
data,
|
|
meta: {
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages,
|
|
},
|
|
};
|
|
}
|
|
|
|
}
|