diff --git a/src/modules/netvisor/fetcher.ts b/src/modules/netvisor/fetcher.ts index 23b77216..6a50718f 100644 --- a/src/modules/netvisor/fetcher.ts +++ b/src/modules/netvisor/fetcher.ts @@ -1,4 +1,5 @@ import { Injectable } from '@nestjs/common'; +import get from 'lodash/get'; import { XmlJsService } from '../xml-js/service'; @@ -109,8 +110,13 @@ export class NetvisorFetcher { xmlString, ); - if (Array.isArray(data.Root.PurchaseInvoiceList[0].PurchaseInvoice)) { - return data.Root.PurchaseInvoiceList[0].PurchaseInvoice.map((invoice) => { + const purchaseInvoice = get( + data, + 'Root.PurchaseInvoiceList[0].PurchaseInvoice', + ); + + if (Array.isArray(purchaseInvoice)) { + return purchaseInvoice.map((invoice) => { return invoice.NetvisorKey[0]; }); } diff --git a/src/modules/netvisor/schedule.ts b/src/modules/netvisor/schedule.ts index e89c5a68..f6d35880 100644 --- a/src/modules/netvisor/schedule.ts +++ b/src/modules/netvisor/schedule.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { PaidStatus } from 'hero24-types'; import { CustomScheduleService } from '../custom-schedule/service'; @@ -14,6 +14,8 @@ import { OfferRequestService } from '$modules/offer-request/offer-request.servic @Injectable() export class NetvisorSchedule { + private readonly logger = new Logger(NetvisorSchedule.name); + constructor( private readonly netvisorFetcher: NetvisorFetcher, private readonly offerService: OfferService, @@ -32,27 +34,40 @@ export class NetvisorSchedule { } async updatePaidStatus(): Promise { - const startDate = getScheduleFetchDate( - this.customScheduleService.getLastJobDate(NETVISOR_FETCH_JOB), - ); + try { + const startDate = getScheduleFetchDate( + this.customScheduleService.getLastJobDate(NETVISOR_FETCH_JOB), + ); - const paidInvoices = await this.netvisorFetcher.fetchPurchaseInvoiceList( - startDate, - ); + const paidInvoices = await this.netvisorFetcher.fetchPurchaseInvoiceList( + startDate, + ); - if (!paidInvoices) { - return; - } + if (!paidInvoices) { + return; + } - const offers = await this.offerService.getOffersByInvoiceIds(paidInvoices); + const offers = await this.offerService.getOffersByInvoiceIds( + paidInvoices, + ); - offers.forEach((offer) => { - const { offerRequestId } = offer.data.initial; + const promises = offers.map(async (offer) => { + try { + const { offerRequestId } = offer.data.initial; - void this.offerRequestService.updatePaidStatus( - offerRequestId, - PaidStatus.PAID, - ); - }); + await this.offerRequestService.updatePaidStatus( + offerRequestId, + PaidStatus.PAID, + ); + } catch (error) { + this.logger.error(error); + this.logger.debug(offer); + } + }); + + await Promise.all(promises); + } catch (error) { + this.logger.error(error); + } } }