|
| 1 | +import { |
| 2 | + Checkout, CheckoutResource, Item, ItemResource, ItemOptions, ItemOptionsResource, |
| 3 | + CertificateItemOptionsResource, CertifiedCopyItemOptionsResource, |
| 4 | + MissingImageDeliveryItemOptionsResource |
| 5 | +} from "./types"; |
| 6 | +import { MemberDetails } from "../certificates"; |
| 7 | + |
| 8 | +export default class CheckoutMapping { |
| 9 | + public static mapCheckoutResourceToCheckout (checkoutResource: CheckoutResource): Checkout { |
| 10 | + const checkout: Checkout = { |
| 11 | + deliveryDetails: { |
| 12 | + addressLine1: checkoutResource?.delivery_details?.address_line_1, |
| 13 | + addressLine2: checkoutResource?.delivery_details?.address_line_2, |
| 14 | + country: checkoutResource?.delivery_details?.country, |
| 15 | + forename: checkoutResource?.delivery_details?.forename, |
| 16 | + locality: checkoutResource?.delivery_details?.locality, |
| 17 | + poBox: checkoutResource?.delivery_details?.po_box, |
| 18 | + postalCode: checkoutResource?.delivery_details?.postal_code, |
| 19 | + region: checkoutResource?.delivery_details?.region, |
| 20 | + surname: checkoutResource?.delivery_details?.surname |
| 21 | + }, |
| 22 | + items: checkoutResource.items.map((item) => { |
| 23 | + return this.mapItemResourceToItem(item) |
| 24 | + }), |
| 25 | + etag: checkoutResource.etag, |
| 26 | + kind: checkoutResource.kind, |
| 27 | + links: { |
| 28 | + self: checkoutResource.links.self, |
| 29 | + payment: checkoutResource.links.payment |
| 30 | + }, |
| 31 | + paidAt: checkoutResource.paid_at, |
| 32 | + checkedOutBy: { |
| 33 | + email: checkoutResource.checked_out_by.email, |
| 34 | + id: checkoutResource.checked_out_by.id |
| 35 | + }, |
| 36 | + status: checkoutResource.status, |
| 37 | + paymentReference: checkoutResource.payment_reference, |
| 38 | + reference: checkoutResource.reference, |
| 39 | + totalOrderCost: checkoutResource.total_order_cost |
| 40 | + } |
| 41 | + return checkout; |
| 42 | + } |
| 43 | + |
| 44 | + private static mapItemResourceToItem (itemResource: ItemResource): Item { |
| 45 | + return { |
| 46 | + companyName: itemResource.company_name, |
| 47 | + companyNumber: itemResource.company_number, |
| 48 | + customerReference: itemResource.customer_reference, |
| 49 | + description: itemResource.description, |
| 50 | + descriptionIdentifier: itemResource.description_identifier, |
| 51 | + descriptionValues: itemResource.description_values, |
| 52 | + etag: itemResource.etag, |
| 53 | + id: itemResource.id, |
| 54 | + itemCosts: itemResource.item_costs.map((i) => ({ |
| 55 | + calculatedCost: i?.calculated_cost, |
| 56 | + discountApplied: i?.discount_applied, |
| 57 | + itemCost: i?.item_cost, |
| 58 | + productType: i?.product_type |
| 59 | + })), |
| 60 | + itemOptions: this.mapItemOptionsResourceToItemOptions(itemResource.item_options, itemResource.kind), |
| 61 | + itemUri: itemResource.item_uri, |
| 62 | + kind: itemResource.kind, |
| 63 | + links: { |
| 64 | + self: itemResource.links.self |
| 65 | + }, |
| 66 | + postageCost: itemResource.postage_cost, |
| 67 | + postalDelivery: itemResource.postal_delivery, |
| 68 | + quantity: itemResource.quantity, |
| 69 | + satisfiedAt: itemResource.satisfied_at, |
| 70 | + status: itemResource.status, |
| 71 | + totalItemCost: itemResource.total_item_cost |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static removeEmptyObjects<T> (input: T): T { |
| 76 | + return Object.values(input).some((value) => value !== undefined) ? input : undefined; |
| 77 | + } |
| 78 | + |
| 79 | + private static mapItemOptionsResourceToItemOptions (itemResource: ItemOptionsResource, kind: string): ItemOptions { |
| 80 | + if (kind === "item#certificate") { |
| 81 | + itemResource = itemResource as CertificateItemOptionsResource; |
| 82 | + const directorDetails = this.removeEmptyObjects({ |
| 83 | + includeBasicInformation: itemResource?.director_details?.include_basic_information, |
| 84 | + includeAddress: itemResource?.director_details?.include_address, |
| 85 | + includeAppointmentDate: itemResource?.director_details?.include_appointment_date, |
| 86 | + includeCountryOfResidence: itemResource?.director_details?.include_country_of_residence, |
| 87 | + includeNationality: itemResource?.director_details?.include_nationality, |
| 88 | + includeOccupation: itemResource?.director_details?.include_occupation, |
| 89 | + includeDobType: itemResource?.director_details?.include_dob_type |
| 90 | + }); |
| 91 | + |
| 92 | + const secretaryDetails = this.removeEmptyObjects({ |
| 93 | + includeBasicInformation: itemResource?.secretary_details?.include_basic_information, |
| 94 | + includeAddress: itemResource?.secretary_details?.include_address, |
| 95 | + includeAppointmentDate: itemResource?.secretary_details?.include_appointment_date, |
| 96 | + includeCountryOfResidence: itemResource?.secretary_details?.include_country_of_residence, |
| 97 | + includeNationality: itemResource?.secretary_details?.include_nationality, |
| 98 | + includeOccupation: itemResource?.secretary_details?.include_occupation, |
| 99 | + includeDobType: itemResource?.secretary_details?.include_dob_type |
| 100 | + }); |
| 101 | + |
| 102 | + const memberDetails = CheckoutMapping.mapMemberDetails(itemResource?.member_details); |
| 103 | + |
| 104 | + const designatedMemberDetails = CheckoutMapping.mapMemberDetails(itemResource?.designated_member_details); |
| 105 | + |
| 106 | + const registeredOfficeAddressDetails = this.removeEmptyObjects({ |
| 107 | + includeAddressRecordsType: itemResource?.registered_office_address_details?.include_address_records_type |
| 108 | + }); |
| 109 | + |
| 110 | + const principalPlaceOfBusinessDetails = this.removeEmptyObjects({ |
| 111 | + includeAddressRecordsType: itemResource?.principal_place_of_business_details?.include_address_records_type |
| 112 | + }); |
| 113 | + |
| 114 | + const generalPartnerDetails = this.removeEmptyObjects({ |
| 115 | + includeBasicInformation: itemResource?.general_partner_details?.include_basic_information |
| 116 | + }); |
| 117 | + |
| 118 | + const limitedPartnerDetails = this.removeEmptyObjects({ |
| 119 | + includeBasicInformation: itemResource?.limited_partner_details?.include_basic_information |
| 120 | + }); |
| 121 | + |
| 122 | + return { |
| 123 | + certificateType: itemResource.certificate_type, |
| 124 | + companyType: itemResource.company_type, |
| 125 | + deliveryTimescale: itemResource.delivery_timescale, |
| 126 | + deliveryMethod: itemResource.delivery_method, |
| 127 | + designatedMemberDetails: designatedMemberDetails, |
| 128 | + includeGeneralNatureOfBusinessInformation: itemResource.include_general_nature_of_business_information, |
| 129 | + includeGoodStandingInformation: itemResource.include_good_standing_information, |
| 130 | + includeCompanyObjectsInformation: itemResource.include_company_objects_information, |
| 131 | + generalPartnerDetails: generalPartnerDetails, |
| 132 | + limitedPartnerDetails: limitedPartnerDetails, |
| 133 | + memberDetails: memberDetails, |
| 134 | + registeredOfficeAddressDetails: registeredOfficeAddressDetails, |
| 135 | + principalPlaceOfBusinessDetails: principalPlaceOfBusinessDetails, |
| 136 | + secretaryDetails: secretaryDetails, |
| 137 | + directorDetails: directorDetails, |
| 138 | + forename: itemResource.forename, |
| 139 | + surname: itemResource.surname |
| 140 | + } |
| 141 | + } else if (kind === "item#certified-copy") { |
| 142 | + itemResource = itemResource as CertifiedCopyItemOptionsResource; |
| 143 | + return { |
| 144 | + deliveryTimescale: itemResource.delivery_timescale, |
| 145 | + deliveryMethod: itemResource.delivery_method, |
| 146 | + filingHistoryDocuments: itemResource.filing_history_documents.map(f => ({ |
| 147 | + filingHistoryDate: f.filing_history_date, |
| 148 | + filingHistoryDescription: f.filing_history_description, |
| 149 | + filingHistoryId: f.filing_history_id, |
| 150 | + filingHistoryType: f.filing_history_type, |
| 151 | + filingHistoryDescriptionValues: f.filing_history_description_values, |
| 152 | + filingHistoryCost: f.filing_history_cost |
| 153 | + })) |
| 154 | + } |
| 155 | + } else { |
| 156 | + itemResource = itemResource as MissingImageDeliveryItemOptionsResource; |
| 157 | + return { |
| 158 | + filingHistoryDate: itemResource.filing_history_date, |
| 159 | + filingHistoryDescription: itemResource.filing_history_description, |
| 160 | + filingHistoryId: itemResource.filing_history_id, |
| 161 | + filingHistoryType: itemResource.filing_history_type, |
| 162 | + filingHistoryDescriptionValues: itemResource.filing_history_description_values |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + static mapMemberDetails = (member_details: { |
| 168 | + include_address?: boolean, |
| 169 | + include_appointment_date?: boolean, |
| 170 | + include_basic_information?: boolean, |
| 171 | + include_country_of_residence?: boolean, |
| 172 | + include_dob_type?: string |
| 173 | + }): MemberDetails => { |
| 174 | + return CheckoutMapping.removeEmptyObjects({ |
| 175 | + includeAddress: member_details?.include_address, |
| 176 | + includeAppointmentDate: member_details?.include_appointment_date, |
| 177 | + includeBasicInformation: member_details?.include_basic_information, |
| 178 | + includeCountryOfResidence: member_details?.include_country_of_residence, |
| 179 | + includeDobType: member_details?.include_dob_type |
| 180 | + }); |
| 181 | + } |
| 182 | +} |
0 commit comments