Subversion Repositories SmartDukaan

Rev

Rev 37409 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37388 vikas 1
package com.spice.profitmandi.service.whatsapp;
2
 
3
/**
4
 * Recipient-number handling shared by the WhatsApp providers.
5
 *
6
 * <p>Lives here rather than in a general string utility because "prefix 91" is an India-MSISDN rule
7
 * that only holds for this integration.</p>
8
 */
9
public final class WhatsappNumbers {
10
 
11
    private WhatsappNumbers() {
12
    }
13
 
14
    /**
15
     * Normalises a recipient to a bare MSISDN: strips everything non-numeric, prefixes {@code 91} when
16
     * exactly ten digits remain, and passes longer numbers (already country-coded) through. Returns
17
     * {@code null} when nothing usable is left, which callers treat as "do not send".
18
     */
19
    public static String toMsisdn(String mobile) {
20
        if (mobile == null) {
21
            return null;
22
        }
23
        String digits = mobile.replaceAll("[^0-9]", "");
24
        if (digits.isEmpty()) {
25
            return null;
26
        }
27
        if (digits.length() == 10) {
28
            return "91" + digits;
29
        }
30
        return digits;
31
    }
32
 
33
    /**
34
     * The legacy Gupshup admission check: the raw string must be exactly ten characters, with no
35
     * cleanup first. Deliberately stricter than {@link #toMsisdn} — Gupshup has been dropping numbers
36
     * that fail this for years, and loosening it during a refactor would start delivering a backlog of
37
     * messages that were never meant to go out in one burst. Only Gupshup should use this.
38
     */
39
    public static boolean isTenDigit(String mobile) {
40
        return mobile != null && mobile.length() == 10;
41
    }
42
}