|
| 1 | +import { onDocumentUpdated } from "firebase-functions/v2/firestore"; |
| 2 | +import * as admin from "firebase-admin"; |
| 3 | +import * as logger from "firebase-functions/logger"; |
| 4 | +import { normalizeError } from "../common/error"; |
| 5 | + |
| 6 | +const LOCATION = "asia-northeast3"; |
| 7 | +const BATCH_SIZE = 200; |
| 8 | + |
| 9 | +export const syncTodoNotificationCategory = onDocumentUpdated({ |
| 10 | + document: "users/{userId}/todoLists/{todoId}", |
| 11 | + region: LOCATION |
| 12 | + }, |
| 13 | + async (event) => { |
| 14 | + const beforeData = event.data?.before.data(); |
| 15 | + const afterData = event.data?.after.data(); |
| 16 | + const userId = event.params.userId; |
| 17 | + const todoId = event.params.todoId; |
| 18 | + |
| 19 | + const beforeCategory = typeof beforeData?.category === "string" ? beforeData.category.trim() : ""; |
| 20 | + const afterCategory = typeof afterData?.category === "string" ? afterData.category.trim() : ""; |
| 21 | + |
| 22 | + if (!beforeCategory || !afterCategory || beforeCategory == afterCategory) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + await Promise.all([ |
| 28 | + updateNotifications(userId, todoId, afterCategory), |
| 29 | + updateNotificationTasks(userId, todoId, afterCategory) |
| 30 | + ]); |
| 31 | + } catch (error) { |
| 32 | + logger.error("todo 카테고리 변경 후 알림 데이터 동기화 실패", { |
| 33 | + userId, |
| 34 | + todoId, |
| 35 | + beforeCategory, |
| 36 | + afterCategory, |
| 37 | + error: normalizeError(error) |
| 38 | + }); |
| 39 | + throw error; |
| 40 | + } |
| 41 | + } |
| 42 | +); |
| 43 | + |
| 44 | +async function updateNotifications( |
| 45 | + userId: string, |
| 46 | + todoId: string, |
| 47 | + todoCategory: string |
| 48 | +): Promise<void> { |
| 49 | + let lastDocument: |
| 50 | + FirebaseFirestore.QueryDocumentSnapshot<FirebaseFirestore.DocumentData> | undefined; |
| 51 | + |
| 52 | + while (true) { |
| 53 | + let query = admin.firestore() |
| 54 | + .collection(`users/${userId}/notifications`) |
| 55 | + .where("todoId", "==", todoId) |
| 56 | + .orderBy(admin.firestore.FieldPath.documentId()) |
| 57 | + .limit(BATCH_SIZE); |
| 58 | + |
| 59 | + if (lastDocument) { |
| 60 | + query = query.startAfter(lastDocument); |
| 61 | + } |
| 62 | + |
| 63 | + const snapshot = await query.get(); |
| 64 | + if (snapshot.empty) { return; } |
| 65 | + |
| 66 | + const batch = admin.firestore().batch(); |
| 67 | + snapshot.docs.forEach((document) => { |
| 68 | + batch.update(document.ref, { todoCategory }); |
| 69 | + }); |
| 70 | + await batch.commit(); |
| 71 | + |
| 72 | + if (snapshot.size < BATCH_SIZE) { return; } |
| 73 | + lastDocument = snapshot.docs[snapshot.docs.length - 1]; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +async function updateNotificationTasks( |
| 78 | + userId: string, |
| 79 | + todoId: string, |
| 80 | + todoCategory: string |
| 81 | +): Promise<void> { |
| 82 | + let lastDocument: |
| 83 | + FirebaseFirestore.QueryDocumentSnapshot<FirebaseFirestore.DocumentData> | undefined; |
| 84 | + |
| 85 | + while (true) { |
| 86 | + let query = admin.firestore() |
| 87 | + .collection("notificationTasks") |
| 88 | + .where("userId", "==", userId) |
| 89 | + .where("todoId", "==", todoId) |
| 90 | + .orderBy(admin.firestore.FieldPath.documentId()) |
| 91 | + .limit(BATCH_SIZE); |
| 92 | + |
| 93 | + if (lastDocument) { |
| 94 | + query = query.startAfter(lastDocument); |
| 95 | + } |
| 96 | + |
| 97 | + const snapshot = await query.get(); |
| 98 | + if (snapshot.empty) { return; } |
| 99 | + |
| 100 | + const batch = admin.firestore().batch(); |
| 101 | + snapshot.docs.forEach((document) => { |
| 102 | + batch.update(document.ref, { todoCategory }); |
| 103 | + }); |
| 104 | + await batch.commit(); |
| 105 | + |
| 106 | + if (snapshot.size < BATCH_SIZE) { return; } |
| 107 | + lastDocument = snapshot.docs[snapshot.docs.length - 1]; |
| 108 | + } |
| 109 | +} |
0 commit comments