|
1 | 1 | import { Injectable } from '@nestjs/common'; |
2 | 2 | import { createId } from '@paralleldrive/cuid2'; |
3 | 3 | import { PrismaService } from '@/db/prisma.service'; |
4 | | -import { Channel } from '@api-sdk'; |
| 4 | +import { Channel, MenuCategory } from '@api-sdk'; |
5 | 5 | import { CreateChannelDto } from '@/channel/dto/create-channel.dto'; |
| 6 | +import { ProductVariantService } from '@/product-variant/product-variant.service'; |
6 | 7 |
|
7 | 8 | @Injectable() |
8 | 9 | export class ChannelService { |
9 | | - constructor(private readonly prisma: PrismaService) {} |
| 10 | + constructor( |
| 11 | + private readonly prisma: PrismaService, |
| 12 | + private readonly productVariant: ProductVariantService, |
| 13 | + ) {} |
10 | 14 |
|
11 | 15 | async findAllChannels(): Promise<Channel[] | null> { |
12 | 16 | return this.prisma.channel.findMany({ |
@@ -57,4 +61,39 @@ export class ChannelService { |
57 | 61 | }, |
58 | 62 | }); |
59 | 63 | } |
| 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 | + } |
60 | 99 | } |
0 commit comments