Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.9 kB
0
Indexable
 if (chain) {
        const results = await Promise.all([
          search().search(
            new ViewProductC(),
            {
              params: {
                type: DatabaseCollection.view_products,
                isActive: true,
                isApproved: true,
                chainIds: [chain.id],
                constraintCodes: ['', 'frozen', 'fresh', 'energyDrink'],
              },
              limit: iterator,
            },
            true
          ),
          search()
            .search(
              new CategoryC(),
              {
                params: {
                  type: DatabaseCollection.categories,
                  show: true,
                  chainIds: [chain.id],
                },
              },
              true
            )
            .then((d) => d?.data),
        ]);

        const productScroll = results[0];
        const categories = results[1];

        if (!productScroll || !categories) return res.status(404).send(null);

        const products = productScroll.data;
        if (
          productScroll.count &&
          productScroll.count > iterator &&
          (limit > iterator || limit === 0)
        ) {
          const totalLimit = limit > 0 ? limit : productScroll.count;
          for (let i = iterator; i < totalLimit; i = i + iterator) {
            const productsLeft = totalLimit - i;
            const result = await search().search(
              new ViewProductC(),
              {
                params: {
                  type: DatabaseCollection.view_products,
                  isActive: true,
                  isApproved: true,
                  chainIds: [chain.id],
                  constraintCodes: ['', 'frozen', 'fresh', 'energyDrink'],
                },
                fromIndex: i,
                limit: productsLeft > iterator ? iterator : productsLeft,
              },
              true
            );
            if (result) products.push(...result.data);
          }
        }

        const chainName = chain.name[defaultLocale] ?? '';

        const doc = xml(
          {
            products: products.map((vp) => {
              const category = categories.find((c) => c.id === vp.categoryId);
              const name = vp.name[defaultLocale] ?? '';
              const categoryName = category?.name[defaultLocale] ?? '';

              return {
                product: [
                  { _attr: { id: `${vp.productId}_${vp.chainId}` } },
                  { title: { _cdata: name } },
                  { price: vp.actualPrice(true).toFixed(2) },
                  // { condition: 'new' },
                  // { stock: 10 },
                  // { ean_code: vp.barcodes.length ? vp.barcodes[0] : 0 },
                  { manufacturer: { _cdata: vp.producer } },
                  { image_url: { _cdata: vp.photoUrl } },
                  {
                    product_url: {
                      _cdata: `${baseUrl}/${defaultLocale}/product/${slugifyName(
                        chainName
                      )}/${slugifyName(name)}`,
                    },
                  },
                  // { category_id: vp.categoryId },
                  { category_name: { _cdata: categoryName } },
                  // {
                  //   category_link: {
                  //     _cdata: `${other.prefix}/lt/chain/category/${slugifyName(
                  //       category?.name.lt ?? 'category'
                  //     )}/${category?.id ?? 'id'}/${vp.chainId}`,
                  //   },
                  // },
                  // { delivery_price: 0 },
                  // { delivery_time: 0 },
                  // { short_message: 'PRISTATYMAS Į NAMUS PER 1 VALANDĄ!' },
                ],
              };
            }),
          },
          { declaration: { encoding: 'UTF-8' }, indent: '  ' }
        );

        return res.setHeader('Content-Type', 'text/xml').status(200).send(doc);
      }
Leave a Comment