@@ -23,6 +23,13 @@ export class InvoiceController {
|
||||
return this.invoiceService.findAll(userId, dto);
|
||||
}
|
||||
|
||||
@Get('public/invoice/tab-counts')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiOperation({ summary: 'Get invoice tab counts for the current user' })
|
||||
getTabCounts(@Query() dto: FindInvoicesDto, @UserId() userId: string) {
|
||||
return this.invoiceService.getTabCounts(dto, userId);
|
||||
}
|
||||
|
||||
@Get('public/invoice/:id')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiOperation({ summary: 'Get one invoice by id' })
|
||||
@@ -55,6 +62,14 @@ export class InvoiceController {
|
||||
return this.invoiceService.findAllAsAdmin(dto);
|
||||
}
|
||||
|
||||
@Get('admin/invoice/tab-counts')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@Permissions(PermissionEnum.VIEW_INVOICES)
|
||||
@ApiOperation({ summary: 'Get invoice tab counts for admin invoice list' })
|
||||
getTabCountsAsAdmin(@Query() dto: FindInvoicesDto) {
|
||||
return this.invoiceService.getTabCounts(dto);
|
||||
}
|
||||
|
||||
@Get('admin/invoice/item/:id')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@Permissions(PermissionEnum.VIEW_INVOICES)
|
||||
|
||||
@@ -20,6 +20,12 @@ type ResolvedItemDiscount = {
|
||||
discountPercent?: number;
|
||||
};
|
||||
|
||||
export type InvoiceTabCounts = {
|
||||
pending: number;
|
||||
confirmed: number;
|
||||
archived: number;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class InvoiceService {
|
||||
constructor(
|
||||
@@ -176,6 +182,39 @@ export class InvoiceService {
|
||||
return this.invoiceRepository.findAllPaginated(dto);
|
||||
}
|
||||
|
||||
getTabCounts(dto: FindInvoicesDto, userId?: string): Promise<InvoiceTabCounts> {
|
||||
const baseFilter: FindInvoicesDto & { userId?: string } = {
|
||||
...dto,
|
||||
userId,
|
||||
};
|
||||
|
||||
return this.countTabs(baseFilter);
|
||||
}
|
||||
|
||||
private async countTabs(
|
||||
baseFilter: FindInvoicesDto & { userId?: string },
|
||||
): Promise<InvoiceTabCounts> {
|
||||
const [pending, confirmed, archived] = await Promise.all([
|
||||
this.invoiceRepository.countByFilters({
|
||||
...baseFilter,
|
||||
statuses: [InvoiceConfirmStatusEnum.PENDING],
|
||||
}),
|
||||
this.invoiceRepository.countByFilters({
|
||||
...baseFilter,
|
||||
statuses: [
|
||||
InvoiceConfirmStatusEnum.PARTIALLY_CONFIRMED,
|
||||
InvoiceConfirmStatusEnum.CONFIRMED,
|
||||
],
|
||||
}),
|
||||
this.invoiceRepository.countByFilters({
|
||||
...baseFilter,
|
||||
statuses: [InvoiceConfirmStatusEnum.ARCHIVED],
|
||||
}),
|
||||
]);
|
||||
|
||||
return { pending, confirmed, archived };
|
||||
}
|
||||
|
||||
async findOne(id: string, userId: string) {
|
||||
const invoice = await this.em.findOne(
|
||||
Invoice,
|
||||
|
||||
@@ -16,13 +16,9 @@ export class InvoiceRepository extends EntityRepository<Invoice> {
|
||||
super(em, Invoice);
|
||||
}
|
||||
|
||||
async findAllPaginated(opts: FindInvoicesOpts): Promise<PaginatedResult<Invoice>> {
|
||||
private buildWhere(opts: FindInvoicesOpts): FilterQuery<Invoice> {
|
||||
const {
|
||||
page = 1,
|
||||
limit = 10,
|
||||
search,
|
||||
orderBy = 'createdAt',
|
||||
order = 'desc',
|
||||
userId,
|
||||
from,
|
||||
to,
|
||||
@@ -31,8 +27,6 @@ export class InvoiceRepository extends EntityRepository<Invoice> {
|
||||
statuses,
|
||||
} = opts;
|
||||
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const where: FilterQuery<Invoice> = {};
|
||||
|
||||
if (userId) {
|
||||
@@ -86,6 +80,20 @@ export class InvoiceRepository extends EntityRepository<Invoice> {
|
||||
where.$or = searchConditions;
|
||||
}
|
||||
|
||||
return where;
|
||||
}
|
||||
|
||||
async findAllPaginated(opts: FindInvoicesOpts): Promise<PaginatedResult<Invoice>> {
|
||||
const {
|
||||
page = 1,
|
||||
limit = 10,
|
||||
orderBy = 'createdAt',
|
||||
order = 'desc',
|
||||
} = opts;
|
||||
|
||||
const offset = (page - 1) * limit;
|
||||
const where = this.buildWhere(opts);
|
||||
|
||||
const [data, total] = await this.findAndCount(where, {
|
||||
limit,
|
||||
offset,
|
||||
@@ -105,4 +113,8 @@ export class InvoiceRepository extends EntityRepository<Invoice> {
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async countByFilters(opts: FindInvoicesOpts): Promise<number> {
|
||||
return this.count(this.buildWhere(opts));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user