Skip to content

Commit

Permalink
Merge pull request #157 from Plant-for-the-Planet-org/feature/add-wha…
Browse files Browse the repository at this point in the history
…tsapp-nativeapp

Add WhatsApp to NativeApp
  • Loading branch information
dhakalaashish authored Feb 27, 2024
2 parents 642ba35 + 7e75f22 commit 10608f8
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 32 deletions.
13 changes: 8 additions & 5 deletions apps/nativeapp/app/components/phoneInput/PhoneInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PhoneNoInput from 'react-native-phone-number-input';
import {CountryCode} from 'react-native-country-picker-modal';

import {Colors, Typography} from '../../styles';
import {EXCLUDED_COUNTRIES} from '../../constants';
import {DISABLE_SMS_COUNTRY_CODE , DISABLE_WHATSAPP_COUNTRY_CODE} from '../../constants';

const IS_ANDROID = Platform.OS === 'android';

Expand Down Expand Up @@ -37,10 +37,13 @@ const PhoneInput = ({
const countryCode = phoneInput.current?.getCountryCode(value);
valid(checkValid ? checkValid : false);
inputValue(formattedValue);
if (
verificationType === 'Sms' &&
EXCLUDED_COUNTRIES.includes(countryCode)
) {
if ((
verificationType === 'Sms' &&
DISABLE_SMS_COUNTRY_CODE.includes(countryCode)
) || (
verificationType === 'Whatsapp' &&
DISABLE_WHATSAPP_COUNTRY_CODE.includes(countryCode)
)) {
destinationFlag(true);
return;
} else {
Expand Down
10 changes: 7 additions & 3 deletions apps/nativeapp/app/constants/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const POINT_RADIUS_ARR = [
{name: 'Within 1 km', value: 1000},
];

// Excluded countries for sms type alert methods
const EXCLUDED_COUNTRIES = [
// Disable sms and whatsapp alert methods for some countries
const DISABLE_SMS_COUNTRY_CODE = [
'RU',
'TJ',
'MG',
Expand All @@ -30,4 +30,8 @@ const EXCLUDED_COUNTRIES = [
'BZ',
];

export {WEB_URLS, RADIUS_ARR, POINT_RADIUS_ARR, EXCLUDED_COUNTRIES};
const DISABLE_WHATSAPP_COUNTRY_CODE = [
'IN'
]

export {WEB_URLS, RADIUS_ARR, POINT_RADIUS_ARR, DISABLE_SMS_COUNTRY_CODE, DISABLE_WHATSAPP_COUNTRY_CODE};
2 changes: 1 addition & 1 deletion apps/nativeapp/app/screens/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Login = () => {
prompt: 'login',
audience: 'urn:plant-for-the-planet',
},
{ephemeralSession: false},
{ephemeralSession: false, useLegacyCallbackUrl: true},
)
.then(cred => {
const request = {
Expand Down
15 changes: 8 additions & 7 deletions apps/nativeapp/app/screens/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
TrashOutlineIcon,
VerificationWarning,
DisabledTrashOutlineIcon,
WhatsAppIcon
} from '../../assets/svgs';
import {trpc} from '../../services/trpc';
import {Colors, Typography} from '../../styles';
Expand Down Expand Up @@ -449,11 +450,11 @@ const Settings = ({navigation}) => {
};

// ----------------- handle whatsapp, and Geostationary
// const handleAddWhatsapp = () => {
// navigation.navigate('Verification', {
// verificationType: 'Whatsapp',
// });
// };
const handleAddWhatsapp = () => {
navigation.navigate('Verification', {
verificationType: 'Whatsapp',
});
};

// const handleGeostationary = val => {
// let detectionMethods = [...userDetails?.data?.detectionMethods];
Expand Down Expand Up @@ -902,7 +903,7 @@ const Settings = ({navigation}) => {
)}
</View>
{/* whatsapp */}
{/* <View style={styles.mySiteNameMainContainer}>
<View style={styles.mySiteNameMainContainer}>
<View style={styles.mySiteNameSubContainer}>
<View style={styles.mobileContainer}>
<WhatsAppIcon />
Expand Down Expand Up @@ -966,7 +967,7 @@ const Settings = ({navigation}) => {
))}
</View>
)}
</View> */}
</View>
{/* sms */}
<View style={styles.mySiteNameMainContainer}>
<View style={styles.mySiteNameSubContainer}>
Expand Down
16 changes: 14 additions & 2 deletions apps/nativeapp/app/screens/Verification/Verification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,21 @@ const Verification = ({navigation, route}) => {
) {
return toast.show('Incorrect Number', {type: 'warning'});
}
if (verificationType === 'Sms' && limitDestination) {
if (
(verificationType === 'Sms' && limitDestination)
) {
return toast.show(
`Apologies, sms services to your region are currently unsupported. Please select a different method.`,
{
type: 'warning',
},
);
}
if (
(verificationType === 'Whatsapp' && limitDestination)
) {
return toast.show(
'Destination is restricted due to country limitations',
`Meta's API change in India affects delivery of FireAlerts over WhatsApp. Please select a different method.`,
{
type: 'warning',
},
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/Services/Notifier/Notifier/SMSNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class SMSNotifier implements Notifier {
}

// If the destination is a restricted Country, return false, log error.
if (isPhoneNumberRestricted(destination)) {
if (isPhoneNumberRestricted('sms', destination)) {
// If destination is a restricted phone number
// Then, notification was created before FireAlert introduced SMS Restriction
// Thus, notification must be deleted, so that it is not constantly marked as "not-delivered"
Expand Down
14 changes: 7 additions & 7 deletions apps/server/src/pages/api/tests/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ export default async function testWhatsApp(req: NextApiRequest, res: NextApiResp
const notificationParameters_alert: NotificationParameters = {
message: "Fire detected inside Las Americas 7A",
subject: "FireAlert",
url: "https://firealert.plant-for-the-planet.org/alert/ed1cf199-6c3a-4406-bac0-eb5519391e2e",
url: "https://firealert.plant-for-the-planet.org/alert/6769f0f5-86cf-4cea-ba27-6e2dc4b58376",
id: "notificationId",
// authenticationMessage: true,
// otp: "12345",
siteName: 'Las Americas',
siteName: 'PROBOSQUE 22',
alert:{
id: "ed1cf199-6c3a-4406-bac0-eb5519391e2e",
id: "6769f0f5-86cf-4cea-ba27-6e2dc4b58376",
type: 'fire',
confidence: 'high',
source: "TEST",
date: new Date(),
longitude: 80.45728,
latitude: 66.66537,
longitude: -100.12768,
latitude: 18.72452,
distance: 0,
siteId: "siteId1",
siteName: "SiteName",
siteId: "clikwjyg1004ymk0egij2a716",
siteName: "PROBOSQUE 22",
data: {},
}
};
Expand Down
17 changes: 14 additions & 3 deletions apps/server/src/server/api/routers/alertMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const alertMethodRouter = createTRPCRouter({
const destination = alertMethod.destination
const method = alertMethod.method
if(method === 'sms'){
if (isPhoneNumberRestricted(destination)) {
if (isPhoneNumberRestricted('sms', destination)) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: `Cannot Verify AlertMethod. ${destination} is restricted due to country limitations.`,
Expand Down Expand Up @@ -218,11 +218,22 @@ export const alertMethodRouter = createTRPCRouter({
// If Method is sms, restrict phone number to only allowed countries
if(input.method === 'sms'){
// Check if the destination falls inside of accepted countries
const isDestinationAccepted = !isPhoneNumberRestricted(input.destination);
const isDestinationAccepted = !isPhoneNumberRestricted('sms', input.destination);
if(isDestinationAccepted === false){
throw new TRPCError({
code: 'BAD_REQUEST',
message: `Destination is restricted due to country limitations`,
message: `Phone number is restricted due to country limitations.`,
});
}
}
// If Method is whatsapp, restrict phone number to only allowed countries
if(input.method === 'whatsapp'){
// Check if the destination falls inside of accepted countries
const isDestinationAccepted = !isPhoneNumberRestricted('whatsapp', input.destination);
if(isDestinationAccepted === false){
throw new TRPCError({
code: 'BAD_REQUEST',
message: `Phone number is restricted due to country limitations.`,
});
}
}
Expand Down
14 changes: 11 additions & 3 deletions apps/server/src/utils/notification/restrictedSMS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import phone from 'phone';

const restrictedCountryCodes = [
const restrictedCountryCodesSms = [
'RU', // Russia
'TJ', // Tajikistan
'MG', // Madagascar
Expand All @@ -17,8 +17,16 @@ const restrictedCountryCodes = [
'BZ', // Belize
];

export const isPhoneNumberRestricted = (phoneNumber: string) => {
const restrictedCountryCodesWhatsapp = [
'IN', // India
]

export const isPhoneNumberRestricted = (alertMethodMethod: string, phoneNumber: string) => {
const phoneResult = phone(phoneNumber);
const countryCode = phoneResult.countryIso2 as string;
return restrictedCountryCodes.includes(countryCode);
if(alertMethodMethod === 'sms'){
return restrictedCountryCodesSms.includes(countryCode);
} else if (alertMethodMethod === 'whatsapp'){
return restrictedCountryCodesWhatsapp.includes(countryCode);
}
}

0 comments on commit 10608f8

Please sign in to comment.