async search(option: PaginationInfluencerRequestOptionDTO): Promise<
[
InfluencerRequest[],
number,
{
totalCount: number;
pendingCount: number;
returnedCount: number;
completedCount: number;
}
]
> {
const where: Prisma.InfluencerRequestWhereInput = {
content: { contains: option.query },
deletedAt: null || undefined,
};
const orderBy: Prisma.InfluencerRequestOrderByWithAggregationInput = {};
orderBy[option.orderBy] = option.align;
const userGroup = await this.prismaService.influencerRequest.groupBy({
by: ['userId'],
});
const userIds = userGroup.map((group) => group.userId);
const influencerRequests = await Promise.all(
userIds.map(async (userId) => {
const userRequests = await this.prismaService.influencerRequest.findMany({
where: {
...where,
userId,
},
orderBy,
include: {
user: true,
logs: true,
},
take: 1,
});
return userRequests[0];
})
);
let filterEntities;
if (option.status !== 'ALL') {
filterEntities = influencerRequests.filter(
(item) => item.status === option.status
);
} else {
filterEntities = influencerRequests;
}
const count = filterEntities.length;
const counts = {
totalCount: filterEntities.length,
pendingCount: influencerRequests
.filter((item) => item && item.status === InfluencerRequestStatus.PENDING)
.filter(Boolean).length,
returnedCount: influencerRequests
.filter((item) => item && item.status === InfluencerRequestStatus.RETURN)
.filter(Boolean).length,
completedCount: influencerRequests
.filter((item) => item && item.status === InfluencerRequestStatus.COMPLETED)
.filter(Boolean).length,
};
return [filterEntities, count, counts];
}