rudderlabs / community-user-transformations

MIT License
1 stars 1 forks source link

Adding international dialing code to phone number #7

Open pgiannone33 opened 1 year ago

pgiannone33 commented 1 year ago

Contact Details

p.giannone36@gmail.com

Language

Javascript

Category

Data Processing and Enrichment

Description

Purpose

Suppose you are an e-commerce business that operates in multiple countries and receives orders from customers all over the world. To ensure smooth communication with your customers, you need to store their phone numbers with the correct international prefix. However, manually formatting each phone number can be time-consuming and error-prone, especially if you have a large customer base.

Approach

To handle the complication of phone numbers with international prefixes, this code utilizes an approach that involves an HTTP request to the restcountries.com REST API service to retrieve the international dialing code for the country associated with the phone number. If the request is successful, the international dialing code is added to the phone number using the "addInternationalCode" function. The function checks if the phone number already has the international prefix and adds it only if it's not already present. The formatted phone number and international dialing code are then saved to the formattedPhone and internationalDialingCode respectively.

Please note that in order for the transformation to work correctly, it requires the presence of the 'country' field in English and the 'phone' field in the event object. In the provided code, these fields are accessed using the following lines:

    const country = event.context?.traits?.address?.country;
    const phone = event.context?.traits?.phone;

Code Block

export async function transformEvent(event) {
    const country = event.context?.traits?.address?.country;
    const phone = event.context?.traits?.phone;

    if (country && phone) {
        const response = await fetch(`https://restcountries.com/v3.1/name/${country}?fullText=true&fields=idd`);

        const root = response?.[0]?.idd?.root || "";
        const suffix = response?.[0]?.idd?.suffixes[0] || "";

        const internationalDialingCode = root + suffix;
        event.context.traits.internationalDialingCode = internationalDialingCode;
        event.context.traits.formattedPhone = addInternationalDialingCode(phone, internationalDialingCode);
    }

    return event;
}

function addInternationalDialingCode(phoneNumber, internationalDialingCode) {
    const formattedPhone = phoneNumber.startsWith("+") ? phoneNumber : `+${phoneNumber}`;
    const formattedInternationalCode = internationalDialingCode.startsWith("+") ? internationalDialingCode : `+${internationalDialingCode}`;

    if (formattedPhone.startsWith(formattedInternationalCode)) {
        return formattedPhone;
    } else {
        return `${formattedInternationalCode}${formattedPhone.slice(1)}`;
    }
}

Input Payload for testing

[
  {
    "anonymousId": "8d872292709c6fbe",
    "channel": "mobile",
    "context": {
      "app": {
        "build": "1",
        "name": "AMTestProject",
        "namespace": "com.rudderstack.android.rudderstack.sampleAndroidApp",
        "version": "1.0"
      },
      "device": {
        "id": "8d872292709c6fbe",
        "manufacturer": "Google",
        "model": "AOSPonIAEmulator",
        "name": "generic_x86_arm",
        "type": "android"
      },
      "library": {
        "name": "com.rudderstack.android.sdk.core",
        "version": "1.0.2"
      },
      "locale": "en-US",
      "network": {
        "carrier": "Android",
        "bluetooth": false,
        "cellular": true,
        "wifi": true
      },
      "os": {
        "name": "Android",
        "version": "9"
      },
      "screen": {
        "density": 420,
        "height": 1794,
        "width": 1080
      },
      "timezone": "Asia/Kolkata",
      "traits": {
        "address": {
          "city": "Manchester",
          "country": "United Kingdom",
          "postalcode": "M220WW",
          "state": "England",
          "street": "Dunkery Road Woodhouse Park"
        },
        "age": "30",
        "anonymousId": "8d872292709c6fbe",
        "birthday": "2020-05-26",
        "createdat": "18th March 2020",
        "description": "Premium User for 3 years",
        "email": "identify@test.com",
        "firstname": "John",
        "userId": "sample_user_id",
        "lastname": "Sparrow",
        "name": "John Sparrow",
        "id": "sample_user_id",
        "phone": "975777666",
        "username": "john_sparrow"
      },
      "userAgent": "Dalvik/2.1.0 (Linux; U; Android 9; AOSP on IA Emulator Build/PSR1.180720.117)"
    },
    "event": "Product Clicked",
    "integrations": {
      "All": true
    },
    "messageId": "1590431830915-73bed370-5889-436d-9a9e-0c0e0c809d06",
    "properties": {
      "revenue": "30",
      "currency": "USD",
      "quantity": "5",
      "test_key_2": {
        "test_child_key_1": "test_child_value_1"
      },
      "price": "58.0"
    },
    "originalTimestamp": "2020-05-25T18:37:10.917Z",
    "type": "track",
    "userId": "sample_user_id"
  }
]

License

gitcommitshow commented 1 year ago

Thank you for contributing to RudderStack Transformations. Your submission will be reviewed soon. Do follow the transformations-challenge channel on RudderStack slack community for updates on the challenge.