feature/IO-3182-Phone-Number-Consent - Checkpoint

This commit is contained in:
Dave Richer
2025-05-28 13:17:21 -04:00
parent 412efb06e5
commit 9466d36e69
3 changed files with 37 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
import { phone } from "phone";
import { GET_PHONE_NUMBER_OPT_OUT } from "../graphql/phone-number-opt-out.queries";
import { GET_PHONE_NUMBER_OPT_OUTS_BY_NUMBERS } from "../graphql/phone-number-opt-out.queries";
/**
* Check if phone numbers are opted out for a given bodyshop
@@ -8,7 +8,7 @@ import { GET_PHONE_NUMBER_OPT_OUT } from "../graphql/phone-number-opt-out.querie
* @param {string[]} phoneNumbers - Array of phone numbers to check
* @returns {Promise<Set<string>>} - Set of normalized opted-out phone numbers
*/
export const checkPhoneOptOutStatus = async (apolloClient, bodyshopId, phoneNumbers) => {
export const phoneNumberOptOutService = async (apolloClient, bodyshopId, phoneNumbers) => {
if (!apolloClient || !bodyshopId || !phoneNumbers?.length) {
return new Set();
}
@@ -19,25 +19,29 @@ export const checkPhoneOptOutStatus = async (apolloClient, bodyshopId, phoneNumb
.map((num) => phone(num, "CA").phoneNumber?.replace(/^\+1/, ""))
.filter(Boolean);
if (!normalizedPhones.length) {
return new Set();
}
const optedOutPhones = new Set();
for (const phoneNum of normalizedPhones) {
try {
const { data } = await apolloClient.query({
query: GET_PHONE_NUMBER_OPT_OUT,
variables: {
bodyshopid: bodyshopId,
phone_number: phoneNum // Single string
},
fetchPolicy: "network-only"
});
try {
const { data } = await apolloClient.query({
query: GET_PHONE_NUMBER_OPT_OUTS_BY_NUMBERS,
variables: {
bodyshopid: bodyshopId,
phone_numbers: normalizedPhones // Array of phone numbers
},
fetchPolicy: "network-only"
});
if (data?.phone_number_opt_out?.length) {
optedOutPhones.add(phoneNum);
}
} catch (error) {
console.error(`Error checking opt-out for ${phoneNum}:`, error);
if (data?.phone_number_opt_out?.length) {
data.phone_number_opt_out.forEach((optOut) => {
optedOutPhones.add(optOut.phone_number);
});
}
} catch (error) {
console.error("Error checking opt-out statuses:", error);
}
return optedOutPhones;