Skip to content

Commit 1a8564b

Browse files
committed
refactor code in tasks model
1 parent 642f813 commit 1a8564b

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

models/tasks.js

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const firestore = require('../utils/firestore')
22
const tasksModel = firestore.collection('tasks')
33
const userUtils = require('../utils/users')
4-
const { fromFirestoreData, toFirestoreData } = require('../utils/tasks')
4+
const { fromFirestoreData, toFirestoreData, buildTasks } = require('../utils/tasks')
55
const { TASK_TYPE, TASK_STATUS } = require('../constants/tasks')
66

77
/**
@@ -43,13 +43,7 @@ const updateTask = async (taskData, taskId = null) => {
4343
const fetchTasks = async () => {
4444
try {
4545
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)
5347
const promises = tasks.map(async (task) => fromFirestoreData(task))
5448
const updatedTasks = await Promise.all(promises)
5549
return updatedTasks
@@ -120,43 +114,26 @@ const fetchUserTasks = async (username, statuses = []) => {
120114
return { userNotFound: true }
121115
}
122116

123-
let tasksSnapshot = []
124-
let assigneeSnapshot = []
117+
let groupTasksSnapshot = []
118+
let featureTasksSnapshot = []
125119

126120
if (statuses && statuses.length) {
127-
tasksSnapshot = await tasksModel.where('participants', 'array-contains', userId)
121+
groupTasksSnapshot = await tasksModel.where('participants', 'array-contains', userId)
128122
.where('status', 'in', statuses)
129123
.get()
130-
assigneeSnapshot = await tasksModel.where('assignee', '==', userId)
124+
featureTasksSnapshot = await tasksModel.where('assignee', '==', userId)
131125
.where('status', 'in', statuses)
132126
.get()
133127
} else {
134-
tasksSnapshot = await tasksModel.where('participants', 'array-contains', userId)
128+
groupTasksSnapshot = await tasksModel.where('participants', 'array-contains', userId)
135129
.get()
136130

137-
assigneeSnapshot = await tasksModel.where('assignee', '==', userId)
131+
featureTasksSnapshot = await tasksModel.where('assignee', '==', userId)
138132
.get()
139133
}
140134

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)
160137

161138
const promises = tasks.map(async (task) => fromFirestoreData(task))
162139
const updatedTasks = await Promise.all(promises)

utils/tasks.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,21 @@ const toFirestoreData = async (task) => {
4141
return updatedTask
4242
}
4343

44+
const buildTasks = (tasks, initialTaskArray = []) => {
45+
if (!tasks.empty) {
46+
tasks.forEach((task) => {
47+
initialTaskArray.push({
48+
id: task.id,
49+
...task.data()
50+
})
51+
})
52+
}
53+
54+
return initialTaskArray
55+
}
56+
4457
module.exports = {
4558
fromFirestoreData,
46-
toFirestoreData
59+
toFirestoreData,
60+
buildTasks
4761
}

0 commit comments

Comments
 (0)