Compare commits
2 Commits
e48f498348
...
0e3dcd712c
| Author | SHA1 | Date |
|---|---|---|
|
|
0e3dcd712c | |
|
|
cd0de7331b |
|
|
@ -1,16 +1,10 @@
|
|||
import { WebBidsService } from '@/modules/bids/services/web-bids.service';
|
||||
import AppResponse from '@/response/app-response';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { paginate, PaginateQuery } from 'nestjs-paginate';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ScrapItem } from '../entities/scrap-item.entity';
|
||||
import AppResponse from '@/response/app-response';
|
||||
import {
|
||||
FilterOperator,
|
||||
FilterSuffix,
|
||||
paginate,
|
||||
PaginateQuery,
|
||||
} from 'nestjs-paginate';
|
||||
import { Column } from 'nestjs-paginate/lib/helper';
|
||||
import { WebBidsService } from '@/modules/bids/services/web-bids.service';
|
||||
|
||||
@Injectable()
|
||||
export class ScrapItemsService {
|
||||
|
|
@ -78,7 +72,7 @@ export class ScrapItemsService {
|
|||
}
|
||||
|
||||
async index(query: PaginateQuery) {
|
||||
const data = await paginate(query, this.scrapItemRepo, {
|
||||
const { data, meta } = await paginate(query, this.scrapItemRepo, {
|
||||
sortableColumns: ['id'],
|
||||
searchableColumns: ['id'],
|
||||
defaultLimit: 15,
|
||||
|
|
@ -87,16 +81,26 @@ export class ScrapItemsService {
|
|||
},
|
||||
defaultSortBy: [['updated_at', 'DESC']],
|
||||
maxLimit: 100,
|
||||
relations: { scrap_config: { web_bid: true } },
|
||||
});
|
||||
|
||||
const sources = await this.webService.webBidRepo.find({
|
||||
select: { origin_url: true },
|
||||
});
|
||||
|
||||
return AppResponse.toPagination<ScrapItem>(
|
||||
{ ...data, bonus: { sources } },
|
||||
true,
|
||||
ScrapItem,
|
||||
);
|
||||
const newData = data.map(({ scrap_config, ...item }) => {
|
||||
return {
|
||||
...item,
|
||||
source: scrap_config.web_bid.origin_url,
|
||||
};
|
||||
});
|
||||
|
||||
return AppResponse.toPaginationAny({
|
||||
data: newData,
|
||||
meta,
|
||||
bonus: {
|
||||
sources: sources.map((item) => item.origin_url),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,32 @@ export default class AppResponse {
|
|||
});
|
||||
}
|
||||
|
||||
public static toPaginationAny(
|
||||
{ data, meta, bonus }: any,
|
||||
transform = true,
|
||||
dtoClass?: new () => any,
|
||||
) {
|
||||
const transformedData =
|
||||
transform && dtoClass
|
||||
? data.map((item) => plainToClass(dtoClass, item))
|
||||
: data;
|
||||
|
||||
return this.toResponse(transformedData, {
|
||||
bonus: {
|
||||
current_page: meta.currentPage,
|
||||
from: meta.currentPage * meta.itemsPerPage - (meta.itemsPerPage - 1),
|
||||
to:
|
||||
meta.currentPage * meta.itemsPerPage > meta.totalItems
|
||||
? meta.totalItems
|
||||
: meta.currentPage * meta.itemsPerPage,
|
||||
last_page: meta.totalPages,
|
||||
per_page: meta.itemsPerPage,
|
||||
total: meta.totalItems,
|
||||
...bonus,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public static processFilters<T>(
|
||||
queryFilters: any,
|
||||
filterableColumns: {
|
||||
|
|
|
|||
Loading…
Reference in New Issue