|
1 | 1 | const firestore = require('../utils/firestore')
|
2 | 2 | const tasksModel = firestore.collection('tasks')
|
3 | 3 | const userUtils = require('../utils/users')
|
4 |
| -const { fromFirestoreData, toFirestoreData } = require('../utils/tasks') |
| 4 | +const { fromFirestoreData, toFirestoreData, buildTasks } = require('../utils/tasks') |
5 | 5 | const { TASK_TYPE, TASK_STATUS } = require('../constants/tasks')
|
6 | 6 |
|
7 | 7 | /**
|
@@ -43,13 +43,7 @@ const updateTask = async (taskData, taskId = null) => {
|
43 | 43 | const fetchTasks = async () => {
|
44 | 44 | try {
|
45 | 45 | const tasksSnapshot = await tasksModel.get()
|
46 |
| - const tasks = [] |
47 |
| - tasksSnapshot.forEach((task) => { |
48 |
| - tasks.push({ |
49 |
| - id: task.id, |
50 |
| - ...task.data() |
51 |
| - }) |
52 |
| - }) |
| 46 | + const tasks = buildTasks(tasksSnapshot) |
53 | 47 | const promises = tasks.map(async (task) => fromFirestoreData(task))
|
54 | 48 | const updatedTasks = await Promise.all(promises)
|
55 | 49 | return updatedTasks
|
@@ -120,43 +114,26 @@ const fetchUserTasks = async (username, statuses = []) => {
|
120 | 114 | return { userNotFound: true }
|
121 | 115 | }
|
122 | 116 |
|
123 |
| - let tasksSnapshot = [] |
124 |
| - let assigneeSnapshot = [] |
| 117 | + let groupTasksSnapshot = [] |
| 118 | + let featureTasksSnapshot = [] |
125 | 119 |
|
126 | 120 | if (statuses && statuses.length) {
|
127 |
| - tasksSnapshot = await tasksModel.where('participants', 'array-contains', userId) |
| 121 | + groupTasksSnapshot = await tasksModel.where('participants', 'array-contains', userId) |
128 | 122 | .where('status', 'in', statuses)
|
129 | 123 | .get()
|
130 |
| - assigneeSnapshot = await tasksModel.where('assignee', '==', userId) |
| 124 | + featureTasksSnapshot = await tasksModel.where('assignee', '==', userId) |
131 | 125 | .where('status', 'in', statuses)
|
132 | 126 | .get()
|
133 | 127 | } else {
|
134 |
| - tasksSnapshot = await tasksModel.where('participants', 'array-contains', userId) |
| 128 | + groupTasksSnapshot = await tasksModel.where('participants', 'array-contains', userId) |
135 | 129 | .get()
|
136 | 130 |
|
137 |
| - assigneeSnapshot = await tasksModel.where('assignee', '==', userId) |
| 131 | + featureTasksSnapshot = await tasksModel.where('assignee', '==', userId) |
138 | 132 | .get()
|
139 | 133 | }
|
140 | 134 |
|
141 |
| - const tasks = [] |
142 |
| - |
143 |
| - if (!tasksSnapshot.empty) { |
144 |
| - tasksSnapshot.forEach((task) => { |
145 |
| - tasks.push({ |
146 |
| - id: task.id, |
147 |
| - ...task.data() |
148 |
| - }) |
149 |
| - }) |
150 |
| - } |
151 |
| - |
152 |
| - if (!assigneeSnapshot.empty) { |
153 |
| - assigneeSnapshot.forEach((task) => { |
154 |
| - tasks.push({ |
155 |
| - id: task.id, |
156 |
| - ...task.data() |
157 |
| - }) |
158 |
| - }) |
159 |
| - } |
| 135 | + const groupTasks = buildTasks(groupTasksSnapshot) |
| 136 | + const tasks = buildTasks(featureTasksSnapshot, groupTasks) |
160 | 137 |
|
161 | 138 | const promises = tasks.map(async (task) => fromFirestoreData(task))
|
162 | 139 | const updatedTasks = await Promise.all(promises)
|
|
0 commit comments