import { BuyingInfo, PartInfo, PriceRange, PriceSource, Row as OriginRowFormat, Selection as OriginSelection, CPNData } from '../../types/bom-api/row.interface'; import { Row as TargetRowFormat, Part, Manufacturer, Pricing, PriceOption, Selection, EndCustomer, CpnToEcRel, PackageTypeRestriction } from '../../types/custom/row.interface'; import { UserUploadData } from '../../types/user-uploaded-data.models'; export function rowMapper(origin: OriginRowFormat): TargetRowFormat | null { if (!origin || !(origin._id || origin.rowId || origin.id)) return null; const target: TargetRowFormat = {}; if (idMapper(origin)) target.id = idMapper(origin); if (origin.position) target.index = origin.position; if (origin.bom_id) target.bomId = origin.bom_id; if (origin.errorCode) target.errorCode = origin.errorCode; if (errorMapper(origin.hasError)) target.error = errorMapper(origin.hasError); if (origin.cpns) { //CPN info won't be expected to replace row info. target.cpnToEcRel = cpnMapper(origin.cpns); return target; } if (partMapper(origin.partInfo)) target.part = partMapper(origin.partInfo); if (origin.svhc) target.svhc = origin.svhc; const pricing = pricingMapper( origin.priceSources, origin.stockInfoGroups, origin.packageTypeRestrictions ); target.pricing = pricing ? pricing : null; if (origin.userUploadData) target.userUploadData = UUDMapper(origin.userUploadData); return target; } function idMapper(origin: OriginRowFormat): string | undefined { return origin._id ? origin._id : origin.rowId ? origin.rowId : origin.id; } function errorMapper(e: any): any { if (!e || !e.hasError) return null; return { date: e.eDate, code: e.errorCode, message: e.message }; } function pricingMapper( priceSources: any[], stockInfoGroups: any, packageRestrictions: PackageTypeRestriction ): Pricing { if (!(priceSources && priceSources.length) || !stockInfoGroups) return null; const target: Pricing = {}; target.options = priceSources.map(priceOptionMapper); target.packageTypeRestrictions = packageRestrictions; if ( stockInfoGroups.original && stockInfoGroups.original.buyingInfo && stockInfoGroups.original.buyingInfo.selectedOptions && stockInfoGroups.original.buyingInfo.selectedOptions.length ) { target.selected = stockInfoGroups.original.buyingInfo.selectedOptions.map( (origin: OriginSelection) => priceOptionSelectedMapper(origin, target.options) ); } target.best = []; // TODO: Add mapper if ( stockInfoGroups.original && stockInfoGroups.original.buyingInfo && typeof stockInfoGroups.original.buyingInfo.totalCount === 'number' && typeof stockInfoGroups.original.buyingInfo.totalDictatedCount === 'number' && typeof stockInfoGroups.original.buyingInfo.totalPrice === 'number' ) { target.total = priceTotalMapper(stockInfoGroups.original.buyingInfo); } if (stockInfoGroups.appliedCPN && stockInfoGroups.appliedCPN.cpn) { target.appliedCPN = stockInfoGroups.appliedCPN.cpn; if ( stockInfoGroups.appliedCPN.endCustomerId && stockInfoGroups.appliedCPN.endCustomerName ) { target.appliedEC = { id: stockInfoGroups.appliedCPN.endCustomerId, name: stockInfoGroups.appliedCPN.endCustomerName }; } } return target; } function priceOptionSelectedMapper( origin: OriginSelection, options: PriceOption[] ): Selection { const target: Selection = {}; if (origin.autoGenerated) target.auto = origin.autoGenerated; // if (origin.quantityRequested) target.quantity = origin.quantityRequested; if (origin.currentPrice) target.price = origin.currentPrice; if (origin.sourcePartId) { target.option = options.find( (option: PriceOption) => option.partId === origin.sourcePartId ); } return target; } function priceTotalMapper(origin: BuyingInfo): Pricing['total'] { const target: Pricing['total'] = { count: origin.totalCount, dictatedCount: origin.totalDictatedCount, price: origin.totalPrice }; return target; } function priceOptionMapper(origin: PriceSource): PriceOption { const target: PriceOption = {}; if (origin.availabilityMessage) { target.message = origin.availabilityMessage; } if (origin.availabilityQty) target.stock = origin.availabilityQty; if (origin.canAddToCart) target.isPurchasable = origin.canAddToCart; if (origin.increment) target.increment = origin.increment; if (origin.mfrLeadTime) target.leadTime = origin.mfrLeadTime; if (origin.minimum) target.minimum = origin.minimum; if (origin.packaging) target.packaging = origin.packaging; if (origin.partStatusCode) target.statusCode = origin.partStatusCode; if (origin.pipeLineDetails) target.pipelines = origin.pipeLineDetails; if (origin.priceRanges && origin.priceRanges.length) target.prices = origin.priceRanges.map(priceRangeMapper); if (origin.region) target.region = origin.region; if (origin.shipIn) target.shipsIn = origin.shipIn; if (origin.sourceCode) target.code = origin.sourceCode; if (origin.sourceOptionCode) target.optionCode = origin.sourceOptionCode; if (origin.sourcePartId) target.partId = origin.sourcePartId; if (origin.unitOfMeasure) target.unit = origin.unitOfMeasure; if (origin.quantityRequested) { target.quantityRequested = origin.quantityRequested; } if (origin.reelableFlag) target.reelable = origin.reelableFlag; if (origin.reelCharge) target.reelCharge = origin.reelCharge; return target; } function priceRangeMapper(origin: PriceRange): any { if (!origin.rangeLimit || !origin.rangePrice) return null; return { limit: origin.rangeLimit, price: origin.rangePrice }; } export function partMapper(origin: PartInfo): Part | null { if (!origin) return null; const target: Part = {}; if (origin.partId) target.id = origin.partId; if (origin.fullPart) target.number = origin.fullPart; if (origin.partSeo) target.seo = origin.partSeo; if (origin.partStatus) target.status = origin.partStatus; if (manufacturerMapper(origin.mfrID, origin.mfrName, origin.mfrSeo)) { target.manufacturer = manufacturerMapper( origin.mfrID, origin.mfrName, origin.mfrSeo ); } if (origin.commId) target.commId = origin.commId; if (origin.datasheetId) target.datasheetId = origin.datasheetId; if (origin.datasheet) target.datasheetUrl = origin.datasheet; if (origin.rohs) target.roHS = origin.rohs; if (origin.smallImage) target.thumbnailUrl = origin.smallImage; if (origin.description) target.description = origin.description; if (origin.alternates) target.alternates = origin.alternates.map(partMapper); if (origin.roHsInfo) target.roHsInfo = origin.roHsInfo; return target; } export function cpnMapper(origin: CPNData[]): CpnToEcRel { const target: CpnToEcRel = { cpns: [], ecMap: new Map(), approvedCPNs: [] }; const keyValuePairs = {}; for (const cpnData of origin) { target.approvedCPNs.push(cpnData); target.cpns.push(cpnData.cpn); target.ecMap.set(cpnData.cpn, cpnData.endCustomers); } return target; } function manufacturerMapper( id: number, name: string, seo: string ): Manufacturer | null { if (id && name && seo) { return { id, name, seo }; } return null; } function UUDMapper(origin: UserUploadData) { return origin; }