(taskData, taskId = null)
| 69 | * @return {Promise<{taskId: string}>} |
| 70 | */ |
| 71 | const updateTask = async (taskData, taskId = null) => { |
| 72 | try { |
| 73 | taskData = await toFirestoreData(taskData); |
| 74 | if (taskId) { |
| 75 | const task = await tasksModel.doc(taskId).get(); |
| 76 | if (taskData?.assignee && task.data().status === TASK_STATUS.AVAILABLE) { |
| 77 | taskData = { ...taskData, status: TASK_STATUS.ASSIGNED }; |
| 78 | } |
| 79 | if (taskData.status === "VERIFIED") { |
| 80 | taskData = { ...taskData, endsOn: Math.floor(Date.now() / 1000) }; |
| 81 | } |
| 82 | const { dependsOn, ...taskWithoutDependsOn } = taskData; |
| 83 | await tasksModel.doc(taskId).set({ |
| 84 | ...task.data(), |
| 85 | ...taskWithoutDependsOn, |
| 86 | }); |
| 87 | if (dependsOn) { |
| 88 | await firestore.runTransaction(async (transaction) => { |
| 89 | const dependencyQuery = dependencyModel.where("taskId", "==", taskId); |
| 90 | const existingDependenciesSnapshot = await transaction.get(dependencyQuery); |
| 91 | const existingDependsOnIds = existingDependenciesSnapshot.docs.map((doc) => doc.data().dependsOn); |
| 92 | const newDependencies = dependsOn.filter((dependency) => !existingDependsOnIds.includes(dependency)); |
| 93 | if (newDependencies.length > 0) { |
| 94 | for (const dependency of newDependencies) { |
| 95 | const dependencyDoc = await tasksModel.doc(dependency).get(); |
| 96 | if (dependencyDoc.exists) { |
| 97 | const taskDependsOn = { |
| 98 | taskId: taskId, |
| 99 | dependsOn: dependency, |
| 100 | }; |
| 101 | const docRef = dependencyModel.doc(); |
| 102 | transaction.set(docRef, taskDependsOn); |
| 103 | } else { |
| 104 | throw new Error("Invalid dependency passed"); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | }); |
| 109 | } |
| 110 | return { taskId }; |
| 111 | } |
| 112 | const taskInfo = await tasksModel.add(taskData); |
| 113 | const result = { |
| 114 | taskId: taskInfo.id, |
| 115 | taskDetails: await fromFirestoreData(taskData), |
| 116 | }; |
| 117 | return result; |
| 118 | } catch (err) { |
| 119 | logger.error("Error in updating task", err); |
| 120 | throw err; |
| 121 | } |
| 122 | }; |
| 123 | const addDependency = async (data) => { |
| 124 | try { |
| 125 | const { taskId, dependsOn } = data; |
no test coverage detected