-
Notifications
You must be signed in to change notification settings - Fork 0
[#215] 푸시 알림에서 기록을 사용자가 제거하면 동일 todo에 대한 반복 알림이 오는 현상을 해결한다 #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
869d9d6
feat: 중복용 데이터 컬렉션과 유저가 인터렉션하는 컬렉션 분리
opficdev 73b4330
style: 변수명 수정
opficdev 943aeeb
feat: todo가 제거되면 중복 처리 컬렉션에서도 제거되도록 추가
opficdev 6c10d26
feat: todo가 제거되었을 때, 푸시알림 리스트에서도 제거되도록 추가
opficdev 1c64386
feat: todo가 완료되고 dueDate도 이미 지났을 때 receipt에서 제거되도록 구현
opficdev 4cac1de
fix: 최신 Todo 상태를 기준으로 리마인더 알림을 발송하도록 수정
opficdev 082e2ad
fix: 마감일이 지난 완료 Todo의 receipt를 주기적으로 제거하도록 수정
opficdev 2a91ddf
fix: 오래된 Todo receipt 정리 배치를 추가하고 remove 네이밍으로 정리
opficdev 40494d4
refactor: 년월일 로직 단순화 및 추출 실패 시 경고 로그 출력
opficdev 6158168
refactor: 공통 로직 병합
opficdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,14 +43,57 @@ export const sendPushNotification = onTaskDispatched({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { userId, todoId, todoKind, dueDateKey, title, body } = parsed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const settingsDoc = await admin.firestore().doc(`users/${userId}/userData/settings`).get(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const allowPushNotification = settingsDoc.data()?.allowPushNotification ?? true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!allowPushNotification) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const settingsDocRef = admin.firestore().doc(`users/${userId}/userData/settings`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const todoDocRef = admin.firestore().doc(`users/${userId}/todoLists/${todoId}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [settingsDoc, todoDoc] = await Promise.all([ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| settingsDocRef.get(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| todoDocRef.get() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const settingsData = settingsDoc.data(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const allowPushNotification = settingsData?.allowPushNotification ?? true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!allowPushNotification) { return; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const todoData = todoDoc.data(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!todoDoc.exists || !todoData || todoData.isCompleted === true) { return; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const candidate = settingsData?.timeZone ?? settingsData?.timezone ?? settingsData?.region; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let timeZone = "UTC"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof candidate === "string" && candidate.trim()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new Intl.DateTimeFormat("en-US", { timeZone: candidate }).format(new Date()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeZone = candidate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeZone = "UTC"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const notificationDocId = `${todoId}_${dueDateKey}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const notificationDocRef = admin.firestore().doc(`users/${userId}/notifications/${notificationDocId}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dueDateValue = todoData.dueDate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const currentDueDate = dueDateValue instanceof admin.firestore.Timestamp ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dueDateValue.toDate() : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dueDateValue instanceof Date ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dueDateValue : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!currentDueDate) { return; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (formatDateKey(currentDueDate, timeZone) !== dueDateKey) { return; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const id = `${todoId}_${dueDateKey}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const receiptDocRef = admin.firestore().doc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `users/${userId}/notificationReceipts/${id}` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const notificationDocRef = admin.firestore().doc(`users/${userId}/notifications/${id}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await receiptDocRef.create({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| todoId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dueDateKey, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| createdAt: admin.firestore.FieldValue.serverTimestamp() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isAlreadyExistsError(error)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const notificationData = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title: "Todo 알림", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -60,14 +103,7 @@ export const sendPushNotification = onTaskDispatched({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| todoId: todoId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| todoKind: todoKind | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await notificationDocRef.create(notificationData); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isAlreadyExistsError(error)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await notificationDocRef.set(notificationData, { merge: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 1. 사용자 FCM 토큰 가져오기 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const tokenDoc = await admin.firestore().doc(`users/${userId}/userData/tokens`).get(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -117,10 +153,6 @@ function isValidTaskId(value: unknown): value is string { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return typeof value === "string" && /^[A-Za-z0-9_-]{1,128}$/.test(value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function hasPathSeparator(value: string): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return value.includes("/"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function parseTaskPayload(data: FirebaseFirestore.DocumentData | undefined): TaskPayload | null { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -142,7 +174,7 @@ function parseTaskPayload(data: FirebaseFirestore.DocumentData | undefined): Tas | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hasPathSeparator(userId) || hasPathSeparator(todoId)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (userId.includes("/") || todoId.includes("/")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -160,3 +192,19 @@ function isAlreadyExistsError(error: unknown): boolean { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const code = (error as FirestoreErrorLike)?.code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return code === 6 || code === "6" || code === "already-exists"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function formatDateKey(date: Date, timeZone: string): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parts = new Intl.DateTimeFormat("en-US", { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeZone, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| year: "numeric", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| month: "2-digit", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| day: "2-digit" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }).formatToParts(date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const byType = (type: string): number => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const found = parts.find((part) => part.type === type)?.value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Number(found); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return `${byType("year")}-${byType("month").toString().padStart(2, "0")}-${byType("day").toString().padStart(2, "0")}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+188
to
+210
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
사용자의 타임존을 확인하는 로직이
Firebase/functions/src/fcm/schedule.ts파일 (71-80행)에도 중복으로 존재합니다. 이러한 코드 중복은 향후 유지보수를 어렵게 만들 수 있습니다. 한 곳을 수정할 때 다른 곳도 함께 수정해야 하는 번거로움이 생깁니다.이 로직을 별도의 유틸리티 함수(예:
resolveTimeZone(settings))로 추출하여 공통으로 사용하는 것이 좋습니다. 이렇게 하면 코드가 더 깔끔해지고 유지보수성이 향상됩니다.이번 PR에서
schedule.ts에 있던resolveTimeZone헬퍼 함수를 제거하고 내용을 인라인으로 바꾸셨는데, 동일한 코드가notification.ts에도 추가되면서 코드 중복이 발생했습니다. 헬퍼 함수를 다시 도입하여 두 파일에서 모두 사용하도록 리팩토링하는 것을 권장합니다.