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

Commit 31119b8

Browse files
committed
super simple search endpoint on channel: find all products that contain query string
1 parent 155c207 commit 31119b8

6 files changed

Lines changed: 96 additions & 4 deletions

File tree

sdk/core.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ export class MainAPI {
106106
);
107107
}
108108

109+
public async searchInChannel(
110+
channelId: string,
111+
query: string,
112+
externalConfig?: NextFetchRequestConfig,
113+
) {
114+
return this.coreRequest<ProductVariant[] | null>(
115+
`channel/${channelId}/search/${query}`,
116+
'GET',
117+
undefined,
118+
externalConfig,
119+
);
120+
}
121+
109122
public async getAllMedia(externalConfig?: NextFetchRequestConfig) {
110123
return this.coreRequest<Media[]>(
111124
'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.22",
3+
"version": "0.2.23",
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ export class ChannelController {
2626
return channels;
2727
}
2828

29+
@Public()
30+
@Get(':id/search/:query')
31+
async searchInChannel(
32+
@Param('id') id: string,
33+
@Param('query') query: string,
34+
) {
35+
const found = await this.service.searchInChannel(id, query);
36+
if (!found) {
37+
throw new NotFoundException();
38+
}
39+
40+
return found;
41+
}
42+
2943
@Public()
3044
@Get(':id')
3145
async findChannelById(@Param('id') id: string) {

src/channel/channel.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
22
import { PrismaService } from '@/db/prisma.service';
33
import { ChannelController } from '@/channel/channel.controller';
44
import { ChannelService } from '@/channel/channel.service';
5+
import { ProductVariantService } from '@/product-variant/product-variant.service';
56

67
@Module({
78
controllers: [ChannelController],
8-
providers: [ChannelService, PrismaService],
9+
providers: [ChannelService, ProductVariantService, PrismaService],
910
})
1011
export class ChannelModule {}

src/channel/channel.service.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { Injectable } from '@nestjs/common';
22
import { createId } from '@paralleldrive/cuid2';
33
import { PrismaService } from '@/db/prisma.service';
4-
import { Channel } from '@api-sdk';
4+
import { Channel, MenuCategory } from '@api-sdk';
55
import { CreateChannelDto } from '@/channel/dto/create-channel.dto';
6+
import { ProductVariantService } from '@/product-variant/product-variant.service';
67

78
@Injectable()
89
export class ChannelService {
9-
constructor(private readonly prisma: PrismaService) {}
10+
constructor(
11+
private readonly prisma: PrismaService,
12+
private readonly productVariant: ProductVariantService,
13+
) {}
1014

1115
async findAllChannels(): Promise<Channel[] | null> {
1216
return this.prisma.channel.findMany({
@@ -57,4 +61,39 @@ export class ChannelService {
5761
},
5862
});
5963
}
64+
65+
async searchInChannel(channelId: string, query: string) {
66+
if (query.length < 2) {
67+
return null;
68+
}
69+
70+
const [channel, foundProducts] = await Promise.all([
71+
this.findChannelById(channelId),
72+
this.productVariant.findProductVariantByName(query),
73+
]);
74+
75+
if (!foundProducts) {
76+
return null;
77+
}
78+
79+
const menus = channel?.menus;
80+
if (!menus) {
81+
return null;
82+
}
83+
84+
// Get all possible categories for this channel
85+
const categories: MenuCategory[] = [];
86+
for (const menu of menus) {
87+
for (const category of menu.categories) {
88+
categories.push(category);
89+
}
90+
}
91+
92+
// Filter only in this categories
93+
foundProducts.filter((product) =>
94+
categories.some((value) => value.id === product.category.id),
95+
);
96+
97+
return foundProducts;
98+
}
6099
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ export class ProductVariantService {
4848
return product;
4949
}
5050

51+
async findProductVariantByName(
52+
name: string,
53+
): Promise<ProductVariant[] | null> {
54+
const product = await this.prisma.productVariant.findMany({
55+
where: {
56+
name: {
57+
contains: name,
58+
},
59+
},
60+
include: {
61+
category: true,
62+
media: {
63+
include: {
64+
media: true,
65+
},
66+
},
67+
},
68+
});
69+
if (!product) {
70+
return null;
71+
}
72+
73+
return product;
74+
}
75+
5176
async findProductVariantById(id: string): Promise<ProductVariant | null> {
5277
const product = await this.prisma.productVariant.findUnique({
5378
where: { id },

0 commit comments

Comments
 (0)