utils/attributes/generate-attributes.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAttributes = generateAttributes;
const fetch_parcel_by_pin_1 = require("../../fetch/fetch-parcel-by-pin");
/**
 * @namespace utils
 * @description Utility functions for processing data.
 *
*/
/**
 * Generates a set of attributes based on the provided schema and input attributes.
 * @function generateAttributes
 * @memberof utils
 * @param attributes - The input attributes to be processed.
 * @param schema - The schema keys that define how to process the attributes.
 * @param spatialReference - The spatial reference information.
 * @param county - The name of the county for reference in the output.
 * @returns An object containing the processed attributes.
 *
 * The function processes the following:
 * - Splits deed and plat book/page information if specified in the schema.
 * - Formats the last sale timestamp if specified in the schema.
 * - Constructs owner name and address strings based on the schema keys.
 * - Constructs text references for property sale and plat information.
 *
 * The final attributes object includes:
 * - `spatialReference`: The provided spatial reference or an empty string.
 * - `LegalDescr`: The legal description or "N/F" if not provided.
 * - `OwnerName`: The owner name prefixed with "N/F" if a legal description is provided.
 * - `PIN`: The property identification number.
 * - `Address`: The concatenated address string.
 * - `DeedBook`: The deed book number or "XXX" if not provided.
 * - `DeedPage`: The deed page number or "XXX" if not provided.
 * - `PlatBook`: The plat book number or null if not provided.
 * - `PlatPage`: The plat page number or null if not provided.
 * - `platRefText`: The uppercase reference text for the plat book/page.
 * - `lastPropertySaleText`: The uppercase text for the last property sale.
 * - `lastPropertySaleDeedText`: The uppercase text for the deed book/page.
 */
function generateAttributes(attributes, schema, spatialReference, county) {
    const { splitDeed, splitPlat, isTimeStamp } = schema.booleans;
    const PIN = attributes[schema.pinKey];
    const LegalDescr = attributes[schema.legalKey];
    let DeedBook = attributes[schema.deedBookKey];
    let DeedPage = attributes[schema.deedPageKey];
    let PlatBook = attributes[schema.platBookKey];
    let PlatPage = attributes[schema.platPageKey];
    let LastSale = attributes[schema.lastSaleKey];
    if (splitDeed) {
        const { book, page } = (0, fetch_parcel_by_pin_1.formatBookPage)(DeedBook);
        DeedBook = book,
            DeedPage = page;
    }
    if (splitPlat) {
        const { book, page } = (0, fetch_parcel_by_pin_1.formatBookPage)(PlatBook);
        PlatBook = book,
            PlatPage = page;
    }
    if (isTimeStamp) {
        const formattedTime = (0, fetch_parcel_by_pin_1.formatTimestampToDate)(LastSale);
        LastSale = formattedTime;
    }
    const lastPropertySaleText = `Last property transfer: ${LastSale}`;
    const lastPropertySaleDeedText = `Deed book: ${DeedBook ? DeedBook : 'XXX'}  Page: ${DeedPage ? DeedPage : 'XXX'}`;
    const platRefText = `Reference to a map recorded in plat book: ${PlatBook ? PlatBook : 'XXX'}, page ${PlatPage ? PlatPage : 'XXX'} at the ${county} county register of deeds.`;
    let OwnerName = '';
    if (schema.ownerNameKeys) {
        // Filter out only the truthy owner name values
        const ownerNames = schema.ownerNameKeys
            .map((key) => attributes[key])
            .filter((name) => name); // Keep only non-empty, truthy names
        if (ownerNames.length > 1) {
            // Join all names except the last one with a space, then add " & " and the last name
            OwnerName = ownerNames.slice(0, -1).join(' ') + ' & ' + ownerNames[ownerNames.length - 1];
        }
        else {
            // If there's only one name, just use it as is
            OwnerName = ownerNames.join(' ');
        }
        console.log(OwnerName); // This will print the concatenated owner name string
    }
    let Address = '';
    if (schema.addressKeys) {
        // Filter out only the truthy address values and join them into a single string
        const addresses = schema.addressKeys
            .map((el) => attributes[el])
            .filter((addr) => addr) // Keep only non-empty, truthy addresses
            .join(' '); // You can use any delimiter you want (e.g., ', ')
        Address = addresses;
        console.log(Address); // This will print the concatenated string of addresses
        console.log(`Total address keys used: ${addresses.split(' ').length}`); // Count the keys
    }
    const finalAttributes = {
        spatialReference: spatialReference ? spatialReference : "",
        LegalDescr: LegalDescr ? LegalDescr : "N/F",
        OwnerName: LegalDescr ? `N/F ${OwnerName}` : OwnerName,
        PIN,
        Address,
        DeedBook: DeedBook ? DeedBook : "XXX",
        DeedPage: DeedPage ? DeedPage : "XXX",
        PlatBook: PlatBook ? PlatBook : null,
        PlatPage: PlatPage ? PlatPage : null,
        platRefText: platRefText.toUpperCase(),
        lastPropertySaleText: lastPropertySaleText.toUpperCase(),
        lastPropertySaleDeedText: lastPropertySaleDeedText.toUpperCase(),
    };
    return {
        attributes: finalAttributes
    };
}