Subversion Repositories SmartDukaan

Rev

Rev 37409 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.whatsapp;

/**
 * Recipient-number handling shared by the WhatsApp providers.
 *
 * <p>Lives here rather than in a general string utility because "prefix 91" is an India-MSISDN rule
 * that only holds for this integration.</p>
 */
public final class WhatsappNumbers {

    private WhatsappNumbers() {
    }

    /**
     * Normalises a recipient to a bare MSISDN: strips everything non-numeric, prefixes {@code 91} when
     * exactly ten digits remain, and passes longer numbers (already country-coded) through. Returns
     * {@code null} when nothing usable is left, which callers treat as "do not send".
     */
    public static String toMsisdn(String mobile) {
        if (mobile == null) {
            return null;
        }
        String digits = mobile.replaceAll("[^0-9]", "");
        if (digits.isEmpty()) {
            return null;
        }
        if (digits.length() == 10) {
            return "91" + digits;
        }
        return digits;
    }

    /**
     * The legacy Gupshup admission check: the raw string must be exactly ten characters, with no
     * cleanup first. Deliberately stricter than {@link #toMsisdn} — Gupshup has been dropping numbers
     * that fail this for years, and loosening it during a refactor would start delivering a backlog of
     * messages that were never meant to go out in one burst. Only Gupshup should use this.
     */
    public static boolean isTenDigit(String mobile) {
        return mobile != null && mobile.length() == 10;
    }
}