89 lines
2.1 KiB
TypeScript
89 lines
2.1 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
|
import { FilterQuery } from '@mikro-orm/core';
|
|
import { CashShift } from '../entities/cash-shift.entity';
|
|
import { CashShiftStatus } from '../interface/cash-shift.interface';
|
|
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
|
|
|
type FindCashShiftsOpts = {
|
|
page?: number;
|
|
limit?: number;
|
|
status?: CashShiftStatus;
|
|
adminId?: string;
|
|
startDate?: string;
|
|
endDate?: string;
|
|
orderBy?: string;
|
|
order?: 'asc' | 'desc';
|
|
};
|
|
|
|
@Injectable()
|
|
export class CashShiftRepository extends EntityRepository<CashShift> {
|
|
constructor(readonly em: EntityManager) {
|
|
super(em, CashShift);
|
|
}
|
|
|
|
async findAllPaginated(restId: string, opts: FindCashShiftsOpts = {}): Promise<PaginatedResult<CashShift>> {
|
|
const {
|
|
page = 1,
|
|
limit = 10,
|
|
status,
|
|
adminId,
|
|
startDate,
|
|
endDate,
|
|
orderBy = 'openedAt',
|
|
order = 'desc',
|
|
} = opts;
|
|
|
|
const offset = (page - 1) * limit;
|
|
const where: FilterQuery<CashShift> = { restaurant: { id: restId } };
|
|
|
|
if (status) {
|
|
where.status = status;
|
|
}
|
|
|
|
if (adminId) {
|
|
where.admin = { id: adminId };
|
|
}
|
|
|
|
if (startDate || endDate) {
|
|
where.openedAt = {};
|
|
if (startDate) {
|
|
where.openedAt.$gte = new Date(startDate);
|
|
}
|
|
if (endDate) {
|
|
where.openedAt.$lte = new Date(endDate);
|
|
}
|
|
}
|
|
|
|
const [data, total] = await this.findAndCount(where, {
|
|
limit,
|
|
offset,
|
|
orderBy: { [orderBy]: order },
|
|
populate: ['admin', 'restaurant'],
|
|
});
|
|
|
|
return {
|
|
data,
|
|
meta: {
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(total / limit),
|
|
},
|
|
};
|
|
}
|
|
|
|
findOpenShift(restId: string, adminId?: string): Promise<CashShift | null> {
|
|
const where: FilterQuery<CashShift> = {
|
|
restaurant: { id: restId },
|
|
status: CashShiftStatus.OPEN,
|
|
};
|
|
|
|
if (adminId) {
|
|
where.admin = { id: adminId };
|
|
}
|
|
|
|
return this.findOne(where, { populate: ['admin'] });
|
|
}
|
|
}
|