import { Application, Binding } from "@loopback/core"; import { Component } from "@loopback/core"; import { FlclError } from "fc-logger"; import { setRequiredGlobals } from "../config"; import { DelhiveryRatesRepository } from "../repositories"; import { Shipments, Tracking } from "../services"; import { DelhiveryOptions } from "../types"; import { RateParams, RateRequestData } from "./types"; import { MyGlobal as app } from './../globals'; export default class Delhivery implements Component { bindings?: Binding[] = []; app: Application repo: DelhiveryRatesRepository ds: any constructor( app?: Application, options?: DelhiveryOptions ) { if (app) { this.app = app; this.ds = app.get('flcl.carriers.ds'); this.repo = new DelhiveryRatesRepository(this.ds); } setRequiredGlobals(options); } async getRates(data: RateRequestData) { try { if (!this.ds) throw FlclError.create({ message: 'Database connection is not binded to fetch the rates' }) const DelhiveryShipment = new Shipments(); const rateParams: RateParams = { originISO: data.targetFromCountry || data?.shipfrom_address?.country, destinationISO: data.targetToCountry || data?.shipto_address?.country, weight: DelhiveryShipment.getWeight(data.total_weight, data.weight_unit.toLowerCase()) } app.logger?.info({ className: 'Delhivery', methodName: 'getRates', propertyName: 'rateParams', propertyValue: rateParams }); const rateResponse = await this.repo.getRates(rateParams); app.logger?.info({ className: 'Delhivery', methodName: 'getRates', propertyName: 'rateResponse', propertyValue: rateResponse }); return rateResponse; } catch (e) { app.logger?.error({ className: 'Delhivery', methodName: 'getRates', message: 'Something went wrong while fetching rates.', cause: e }) throw e; } } async getLabelsFromDelhivery(data: any) { try { const Shipment = new Shipments(); return await Shipment.createShipments(data); } catch (e) { app.logger?.error({ className: 'Delhivery', methodName: 'getLabelsFromDelhivery', message: 'Something went wrong while fetching labels.', cause: e }); throw e; } } async getTrackingStatusFromDelhivery(data: any) { try { const DelhiveryTracking = new Tracking(); return await DelhiveryTracking.getTrackingStatus(data) } catch (e) { app.logger?.error({ className: 'Delhivery', methodName: 'getTrackingStatusFromDelhivery', message: 'Something went wrong while fetching trackinng status.', cause: e }); throw e; } } }