cash shift
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
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): Promise<CashShift | null> {
|
||||
return this.findOne(
|
||||
{ restaurant: { id: restId }, status: CashShiftStatus.OPEN },
|
||||
{ populate: ['admin'] },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user