In my system, each user is associated with multiple groups. Each user's group membership is stored as an array within their user document. Additionally, there is a tasks collection where each task contains an array of authorizedGroups that correspond to the user's groups. While most tasks have just one authorizedGroup, some may have multiple. Tasks are assigned from one user to another.
I am looking to query for all tasks that are available to a specific user and set up listeners for each query. Although using get() has been successful in retrieving data, it does not provide the active listener functionality I require. Is there a method to generate listeners for each group? The onSnapshot method does not return a promise, making it unclear how I can achieve this desired outcome. Any suggestions or guidance on this matter would be greatly appreciated.
created() {
const getTasksPerGroup = async (groups) => {
// Make a get request using each group in the user's group membership list
const queries = groups.map((group) => {
return this.$store.state.db
.collection('tasks')
.where('authorizedGroups', 'array-contains', group)
.where('taskAssignedTo', '==', this.$store.state.auth.user.displayName)
.get()
})
// Use Await Promise.all to acquire an array of querySnapshots
const result = await Promise.all(queries).then((querySnapshot) => {
return querySnapshot.map((querySnapshot) => querySnapshot.docs).reduce((acc, docs) => [...acc, ...docs])
})
// Result is an array of querySnapshots...pushing each qs.data() to the tasks array
let tasks = []
result.forEach((qs) => {
let item = qs.data()
tasks.push(item)
console.log('tasks:', tasks)
this.$store.commit('tasks/mutate_tasksArray', tasks)
})
}
// Invoke async function that gets the user's groups array
getTasksPerGroup(this.allFirmGroups)
}