Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Retrieve staff members list according to specific roles assigned to them

Retrieve staff members list according to specific roles assigned to them

Sambhav
Visitor
1 0 0

Hello, I created the functionality that retrieves the ID of the logged-in customer and, based on that ID, identifies the staff assigned to the company associated with the logged-in customer. I have few roles assigned to staff members in admin, So my question is I want to filter the staff member list according to specific roles assigned to them. For example, Staff member - Jaspreet Singh and his role is B2B-Sales Op Manager and same there are few more roles assigned to different staff members. Show on storefront. Below is my code..

<script>
// Step 1: Get the logged-in customer ID using Liquid
const customerId = 'gid://shopify/Customer/{{ customer.id | json }}';
const accessToken = 'xxxxxxxxxxxxxxxxxxxxx'; // Replace with your actual access token
console.log('customerId:', customerId);
// Step 2: Query to fetch the company ID associated with the customer
const companyQuery = `
query {
customer(id: "${customerId}") {
companyContactProfiles {
company {
id
}
}
}
}
`;

fetch(`https://usa-therabody.myshopify.com/admin/api/unstable/graphql.json`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': accessToken,
},
body: JSON.stringify({ query: companyQuery }),
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
const company = data.data.customer.companyContactProfiles[0]?.company;
if (!company) {
throw new Error('Company data not found');
}
const companyId = company.id;

// Step 3: Query to fetch staff members and customers assigned to the company
const graphqlQuery = `
query {
company(id: "${companyId}") {
locations(first: 10) {
edges {
node {
id
name
staffMemberAssignments(first: 10) {
edges {
node {
id
staffMember {
id
firstName
lastName
email
}
}
}
}
}
}
}
}
}
`;

// Step 4: Fetch staff members and customers assigned to the company
return fetch(`https://usa-therabody.myshopify.com/admin/api/unstable/graphql.json`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': accessToken,
},
body: JSON.stringify({ query: graphqlQuery }),
});
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
const companyData = data.data.company;
if (!companyData) {
throw new Error('Company data not found');
}
const staffAssignments = companyData.locations.edges.flatMap(location => {
return location.node.staffMemberAssignments.edges.map(edge => edge.node);
});

const staffList = document.getElementById('staff-members');
staffList.innerHTML = ''; // Clear existing content

staffAssignments.forEach(assignment => {
const staffMember = assignment.staffMember;
const listItem = document.createElement('li');
listItem.textContent = `${staffMember.firstName} ${staffMember.lastName} - ${staffMember.email}`;
staffList.appendChild(listItem);
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
</script>

 

Replies 0 (0)