efelinto

Untitled

Mar 14th, 2021
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import PrismaService from '@/app/prisma/prisma.service';
  2.  
  3. export class ProductVersionRepository extends PrismaService {
  4.   async getLastAvailableProductVersion(productId: string, version?: string) {
  5.     const { productFavoriteProviderId } = await this.product.findUnique({
  6.       where: {
  7.         id: productId,
  8.       },
  9.       select: {
  10.         productFavoriteProviderId: true,
  11.       },
  12.     });
  13.  
  14.     const productVersion = await this.productVersion.findFirst({
  15.       /*
  16.         If provide product version:
  17.           1. productId + version + favoriteProviderId
  18.           2. productId + version
  19.         If does not provide product version
  20.           1. productId + favoriteProviderId
  21.           2. productId
  22.       */
  23.       where: {
  24.         OR: [
  25.           {
  26.             productId,
  27.             version: version || undefined,
  28.             providerId: productFavoriteProviderId || undefined,
  29.           },
  30.           {
  31.             productId,
  32.             version: version || undefined,
  33.           },
  34.         ],
  35.       },
  36.       orderBy: {
  37.         createdAt: 'desc',
  38.       },
  39.     });
  40.  
  41.     return productVersion;
  42.   }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment