Skip to content

Commit

Permalink
fix sort for aggregated data when aggregate value is same
Browse files Browse the repository at this point in the history
  • Loading branch information
Leouarz committed Feb 22, 2022
1 parent 12b22d5 commit c257dc7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/api/helpers/nftHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export async function populateNFTIpfs(NFT: INFT): Promise<any> {
const response = await fetchTimeout(
fetchUrl,
null,
Number(process.env.IPFS_REQUEST_TIMEOUT) || 8000
Number(process.env.IPFS_REQUEST_TIMEOUT) || 2000
).catch((_e) => {
L.error("fetch error:" + _e);
L.error("Fetch error:" + _e);
throw new Error("Could not retrieve NFT data from " + NFT.nftIpfs);
});
if (response) {
Expand Down
8 changes: 4 additions & 4 deletions src/api/services/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ export class NFTService {
totalOwnedListedInMarketplaceByRequestingUser: number,
smallestPrice: string
}> {
const marketplaceId = query.filter.marketplaceId ?? null;
const owner = query.filter.owner ?? null;
const marketplaceId = query.filter?.marketplaceId ?? null;
const owner = query.filter?.owner ?? null;

try {
const [totalRequest, totalListedRequest, totalFilteredRequest, totalListedInMarketplaceRequest, totalOwnedByRequestingUserRequest, totalOwnedListedByRequestingUserRequest, totalOwnedListedInMarketplaceByRequestingUserRequest, smallestPriceRequest] = await Promise.all([
Expand Down Expand Up @@ -424,7 +424,7 @@ export class NFTService {
try {
const aggregateQuery = [{ $group: { _id: "$serieId", totalLikes: { $sum: 1 } } }]
const aggregate = NftLikeModel.aggregate(aggregateQuery);
const data = await NftLikeModel.aggregatePaginate(aggregate, {page: query.pagination.page, limit: query.pagination.limit, sort:{totalLikes: -1}})
const data = await NftLikeModel.aggregatePaginate(aggregate, {page: query.pagination.page, limit: query.pagination.limit, sort:{totalLikes: -1, _id: -1}})
const seriesSorted = data.docs.map(x => x._id)
const queryNfts = {filter:{series: seriesSorted}}
const gqlQuery = QueriesBuilder.distinctNFTs(queryNfts);
Expand Down Expand Up @@ -455,7 +455,7 @@ export class NFTService {
try {
const aggregateQuery = [{ $group: { _id: "$viewedSerie", totalViews: { $sum: 1 } } }]
const aggregate = NftViewModel.aggregate(aggregateQuery);
const data = await NftViewModel.aggregatePaginate(aggregate, {page: query.pagination.page, limit: query.pagination.limit, sort:{totalViews: -1}})
const data = await NftViewModel.aggregatePaginate(aggregate, {page: query.pagination.page, limit: query.pagination.limit, sort:{totalViews: -1, _id: -1}})
const seriesSorted = data.docs.map(x => x._id)
const queryNfts = {filter:{series: seriesSorted}}
const gqlQuery = QueriesBuilder.distinctNFTs(queryNfts);
Expand Down
2 changes: 1 addition & 1 deletion src/api/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class UserService {
try{
const aggregateQuery = [{ $group: { _id: "$followed", totalViews: { $sum: 1 } } }]
const aggregate = FollowModel.aggregate(aggregateQuery);
const res = await FollowModel.aggregatePaginate(aggregate, {page: query.pagination.page, limit: query.pagination.limit, sort:{totalViews: -1}})
const res = await FollowModel.aggregatePaginate(aggregate, {page: query.pagination.page, limit: query.pagination.limit, sort:{totalViews: -1, _id: -1}})
const walletIdsSorted = res.docs.map(x => x._id)
const filterDbUser = {walletIds: walletIdsSorted}
const resDbUsers = await fetch(`${TERNOA_API_URL}/api/users/?filter=${JSON.stringify(filterDbUser)}`)
Expand Down
2 changes: 0 additions & 2 deletions src/interfaces/IUser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export interface IUser {
_id: string;
walletId: string;
nbFollowers?: number;
nbFollowing?: number;
likedNFTs?: { serieId: string, nftId: string, walletId?: string }[];
viewsCount?: number;
twitterName?: string;
Expand Down

0 comments on commit c257dc7

Please sign in to comment.