feature/IO-3182-Phone-Number-Consent - Checkpoint
This commit is contained in:
@@ -8,7 +8,7 @@ import { connect } from "react-redux";
|
|||||||
import { createStructuredSelector } from "reselect";
|
import { createStructuredSelector } from "reselect";
|
||||||
import { DELETE_OWNER, UPDATE_OWNER } from "../../graphql/owners.queries";
|
import { DELETE_OWNER, UPDATE_OWNER } from "../../graphql/owners.queries";
|
||||||
import { selectBodyshop } from "../../redux/user/user.selectors"; // Adjust path
|
import { selectBodyshop } from "../../redux/user/user.selectors"; // Adjust path
|
||||||
import { checkPhoneOptOutStatus } from "../../utils/phoneOptOutService.js"; // Adjust path
|
import { phoneNumberOptOutService } from "../../utils/phoneOptOutService.js"; // Adjust path
|
||||||
import OwnerDetailFormComponent from "./owner-detail-form.component";
|
import OwnerDetailFormComponent from "./owner-detail-form.component";
|
||||||
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";
|
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";
|
||||||
import { phone } from "phone"; // Import phone utility for formatting
|
import { phone } from "phone"; // Import phone utility for formatting
|
||||||
@@ -34,7 +34,7 @@ function OwnerDetailFormContainer({ owner, refetch, bodyshop }) {
|
|||||||
const fetchOptOutStatus = async () => {
|
const fetchOptOutStatus = async () => {
|
||||||
if (bodyshop?.id && (owner?.ownr_ph1 || owner?.ownr_ph2)) {
|
if (bodyshop?.id && (owner?.ownr_ph1 || owner?.ownr_ph2)) {
|
||||||
const phoneNumbers = [owner.ownr_ph1, owner.ownr_ph2].filter(Boolean);
|
const phoneNumbers = [owner.ownr_ph1, owner.ownr_ph2].filter(Boolean);
|
||||||
const optOutSet = await checkPhoneOptOutStatus(apolloClient, bodyshop.id, phoneNumbers);
|
const optOutSet = await phoneNumberOptOutService(apolloClient, bodyshop.id, phoneNumbers);
|
||||||
setOptedOutPhones(optOutSet);
|
setOptedOutPhones(optOutSet);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -27,6 +27,20 @@ export const GET_PHONE_NUMBER_OPT_OUTS = gql`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
export const GET_PHONE_NUMBER_OPT_OUTS_BY_NUMBERS = gql`
|
||||||
|
query GET_PHONE_NUMBER_OPT_OUTS_BY_NUMBERS($bodyshopid: uuid!, $phone_numbers: [String!]) {
|
||||||
|
phone_number_opt_out(
|
||||||
|
where: { bodyshopid: { _eq: $bodyshopid }, phone_number: { _in: $phone_numbers } }
|
||||||
|
) {
|
||||||
|
id
|
||||||
|
bodyshopid
|
||||||
|
phone_number
|
||||||
|
created_at
|
||||||
|
updated_at
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
export const SEARCH_OWNERS_BY_PHONE_NUMBERS = gql`
|
export const SEARCH_OWNERS_BY_PHONE_NUMBERS = gql`
|
||||||
query SEARCH_OWNERS_BY_PHONE_NUMBERS($bodyshopid: uuid!, $phone_numbers: [String!]) {
|
query SEARCH_OWNERS_BY_PHONE_NUMBERS($bodyshopid: uuid!, $phone_numbers: [String!]) {
|
||||||
owners(
|
owners(
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { phone } from "phone";
|
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
|
* 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
|
* @param {string[]} phoneNumbers - Array of phone numbers to check
|
||||||
* @returns {Promise<Set<string>>} - Set of normalized opted-out phone numbers
|
* @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) {
|
if (!apolloClient || !bodyshopId || !phoneNumbers?.length) {
|
||||||
return new Set();
|
return new Set();
|
||||||
}
|
}
|
||||||
@@ -19,25 +19,29 @@ export const checkPhoneOptOutStatus = async (apolloClient, bodyshopId, phoneNumb
|
|||||||
.map((num) => phone(num, "CA").phoneNumber?.replace(/^\+1/, ""))
|
.map((num) => phone(num, "CA").phoneNumber?.replace(/^\+1/, ""))
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
||||||
|
if (!normalizedPhones.length) {
|
||||||
|
return new Set();
|
||||||
|
}
|
||||||
|
|
||||||
const optedOutPhones = new Set();
|
const optedOutPhones = new Set();
|
||||||
|
|
||||||
for (const phoneNum of normalizedPhones) {
|
try {
|
||||||
try {
|
const { data } = await apolloClient.query({
|
||||||
const { data } = await apolloClient.query({
|
query: GET_PHONE_NUMBER_OPT_OUTS_BY_NUMBERS,
|
||||||
query: GET_PHONE_NUMBER_OPT_OUT,
|
variables: {
|
||||||
variables: {
|
bodyshopid: bodyshopId,
|
||||||
bodyshopid: bodyshopId,
|
phone_numbers: normalizedPhones // Array of phone numbers
|
||||||
phone_number: phoneNum // Single string
|
},
|
||||||
},
|
fetchPolicy: "network-only"
|
||||||
fetchPolicy: "network-only"
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if (data?.phone_number_opt_out?.length) {
|
if (data?.phone_number_opt_out?.length) {
|
||||||
optedOutPhones.add(phoneNum);
|
data.phone_number_opt_out.forEach((optOut) => {
|
||||||
}
|
optedOutPhones.add(optOut.phone_number);
|
||||||
} catch (error) {
|
});
|
||||||
console.error(`Error checking opt-out for ${phoneNum}:`, error);
|
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error checking opt-out statuses:", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return optedOutPhones;
|
return optedOutPhones;
|
||||||
|
|||||||
Reference in New Issue
Block a user