Untitled

 avatar
unknown
plain_text
a year ago
4.4 kB
5
Indexable
  async getInvoice(invoice_id, errorCodeHandlingsService) {
    try {
      if (invoice_id != undefined) {
        var masterOrder = await this.invoiceRepo.findOne({
          // select: { items: { id: true, variant: { id: true, product: { id: true } } } },
          where: { id: invoice_id },
          relations: {
            orders: {
              order_track: true,
            //  billing_address: { country: true, emirate: true },
            //  shipping_address: { country: true, emirate: true },
              items: { variant: { product: { images: true } } },
            },
            supplier: true,
            buyer: true,
            outlet: { addresses: { country: true, emirate: true }, customer_businesses: true },
            warehouse: { addresses: { country: true, emirate: true }, customer_businesses: true },
          },
        });
        var currentMasterOrderData = await dataSource
          .getRepository(MasterOrder)
          .createQueryBuilder('master_order')
          .select([
            'master_order.id',
            'master_order.vat_tax',
            'master_order.delivery_charge',
            'master_order.status',
            'master_order.rating',
            'master_order.notes',
            'master_order.actual_vat',
            'master_order.sub_total',
            'master_order.actual_amount',
            'master_order.total',
            'master_order.actual_subtotal',
          ])
          .where('master_order.id=:order_id', { order_id: masterOrder.order_id })
          .getRawOne();
        if (masterOrder == undefined) {
          throw await errorCodeHandlingsService.getErrorMessage(5003);
        } else {
          if (currentMasterOrderData != null) {
            masterOrder.orders.vat_tax = currentMasterOrderData.master_order_vat_tax;
            masterOrder.orders.delivery_charge = currentMasterOrderData.master_order_delivery_charge;
            masterOrder.orders.actual_vat = currentMasterOrderData.master_order_actual_vat;
            masterOrder.orders.sub_total = currentMasterOrderData.master_order_sub_total;
            masterOrder.orders.actual_amount = currentMasterOrderData.master_order_actual_amount;
            masterOrder.orders.total = currentMasterOrderData.master_order_total;
            masterOrder.orders.actual_subtotal = currentMasterOrderData.master_order_actual_subtotal;
          }
          let itemsPromises = masterOrder.orders.items.map(async item => {
            // console.log(item.variant.product['country_id'], 'product');
            let country = await dataSource
              .getRepository('country')
              .createQueryBuilder('cntry')
              .select(['cntry.name as name', 'cntry.flag as flag'])
              .where('cntry.iso_2 = :countryId', { countryId: item.variant?.product['country_id'] })
              .getRawOne();
            item['country'] = {
              name: country.name,
              flag: process.env.MINIO_CUSTOM_ENDPOINT + '/' + country?.flag ?? '',
            };
            // console.log(item);
            // if (item?.metadata != null && item?.metadata?.offer != null && Number(item?.metadata?.offer) == 0)
            //   item.metadata.offer = '0.01';
            // if (
            //   item?.variant?.metadata != null &&
            //   item?.variant?.metadata?.offer != null &&
            //   Number(item?.variant?.metadata?.offer) == 0
            // )
            //   item.variant.metadata.offer = '0.01';
            if (item?.variant?.product == null) throw 'product deleted'; // when the given product is deleted
            if (item.variant.product.images == (undefined || null)) item.variant.product.images = [];
            item.variant.product.images.map(img => {
              img.url = process.env.MINIO_CUSTOM_ENDPOINT + '/' + img.url;
            });
            return item;
          });

          masterOrder.orders.items = await Promise.all(itemsPromises);
          // console.log(masterOrder);
          return masterOrder;
        }
      } else {
        throw await errorCodeHandlingsService.getErrorMessage(4005);
      }
    } catch (error) {
      throw error;
      console.log(error);
      if (error == 'product deleted') throw await errorCodeHandlingsService.getErrorMessage(4005);
      else throw await errorCodeHandlingsService.getErrorMessage(5006);
    }
  }
Editor is loading...
Leave a Comment