This repository was archived by the owner on Mar 13, 2024. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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' ,
Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff 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 (
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 } ,
You can’t perform that action at this time.
0 commit comments