Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit c269135

Browse files
committed
getTopSearchInChannel
1 parent 0a4e947 commit c269135

5 files changed

Lines changed: 74 additions & 1 deletion

File tree

sdk/core.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ export class MainAPI {
119119
);
120120
}
121121

122+
public async getTopSearchInChannel(
123+
channelId: string,
124+
externalConfig?: NextFetchRequestConfig,
125+
) {
126+
return this.coreRequest<ProductVariant[] | null>(
127+
`channel/${channelId}/search`,
128+
'GET',
129+
undefined,
130+
externalConfig,
131+
);
132+
}
133+
122134
public async getAllMedia(externalConfig?: NextFetchRequestConfig) {
123135
return this.coreRequest<Media[]>(
124136
'media/list',

sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next-orders/api-sdk",
3-
"version": "0.2.23",
3+
"version": "0.2.24",
44
"description": "TS Lib: Easy ability to make requests to Main API via NPM package. 100% typed.",
55
"scripts": {
66
"build": "tsup",

src/channel/channel.controller.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ export class ChannelController {
2626
return channels;
2727
}
2828

29+
@Public()
30+
@Get(':id/search')
31+
async getTopSearchInChannel(@Param('id') id: string) {
32+
const top = await this.service.getTopSearchInChannel(id);
33+
if (!top) {
34+
throw new NotFoundException();
35+
}
36+
37+
return top;
38+
}
39+
2940
@Public()
3041
@Get(':id/search/:query')
3142
async searchInChannel(

src/channel/channel.service.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,37 @@ export class ChannelService {
6262
});
6363
}
6464

65+
async getTopSearchInChannel(channelId: string) {
66+
const [channel, foundProducts] = await Promise.all([
67+
this.findChannelById(channelId),
68+
this.productVariant.findPopularProductVariants(),
69+
]);
70+
71+
if (!foundProducts) {
72+
return null;
73+
}
74+
75+
const menus = channel?.menus;
76+
if (!menus) {
77+
return null;
78+
}
79+
80+
// Get all possible categories for this channel
81+
const categories: MenuCategory[] = [];
82+
for (const menu of menus) {
83+
for (const category of menu.categories) {
84+
categories.push(category);
85+
}
86+
}
87+
88+
// Filter only in this categories
89+
foundProducts.filter((product) =>
90+
categories.some((value) => value.id === product.category.id),
91+
);
92+
93+
return foundProducts;
94+
}
95+
6596
async searchInChannel(channelId: string, query: string) {
6697
if (query.length < 2) {
6798
return null;

src/product-variant/product-variant.service.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ export class ProductVariantService {
7474
return product;
7575
}
7676

77+
async findPopularProductVariants(): Promise<ProductVariant[] | null> {
78+
const products = await this.prisma.productVariant.findMany({
79+
take: 5,
80+
include: {
81+
category: true,
82+
media: {
83+
include: {
84+
media: true,
85+
},
86+
},
87+
},
88+
});
89+
if (!products) {
90+
return null;
91+
}
92+
93+
return products;
94+
}
95+
7796
async findProductVariantById(id: string): Promise<ProductVariant | null> {
7897
const product = await this.prisma.productVariant.findUnique({
7998
where: { id },

0 commit comments

Comments
 (0)