@@ -41,22 +41,30 @@ final class PushNotificationService {
4141
4242 /// 푸시 알림 시간 설정
4343 func fetchPushNotificationTime( ) async throws -> DateComponents {
44+ logger. info ( " Fetching push notification time " )
45+
4446 guard let uid = Auth . auth ( ) . currentUser? . uid else {
47+ logger. error ( " User not authenticated " )
4548 throw AuthError . notAuthenticated
4649 }
4750
48- let settingsRef = store. document ( " users/ \( uid) /userData/settings " )
49- let doc = try await settingsRef. getDocument ( )
51+ do {
52+ let settingsRef = store. document ( " users/ \( uid) /userData/settings " )
53+ let doc = try await settingsRef. getDocument ( )
5054
51- guard let hour = doc. data ( ) ? [ " pushNotificationHour " ] as? Int else {
52- throw FirestoreError . dataNotFound ( " pushNotificationHour " )
53- }
55+ guard let hour = doc. data ( ) ? [ " pushNotificationHour " ] as? Int else {
56+ throw FirestoreError . dataNotFound ( " pushNotificationHour " )
57+ }
5458
55- guard let minute = doc. data ( ) ? [ " pushNotificationMinute " ] as? Int else {
56- throw FirestoreError . dataNotFound ( " pushNotificationMinute " )
57- }
59+ guard let minute = doc. data ( ) ? [ " pushNotificationMinute " ] as? Int else {
60+ throw FirestoreError . dataNotFound ( " pushNotificationMinute " )
61+ }
5862
59- return DateComponents ( hour: hour, minute: minute)
63+ return DateComponents ( hour: hour, minute: minute)
64+ } catch {
65+ logger. error ( " Failed to fetch push notification time " , error: error)
66+ throw error
67+ }
6068 }
6169
6270 /// 푸시 알림 설정 업데이트
@@ -94,35 +102,40 @@ final class PushNotificationService {
94102 _ notificationQuery: PushNotificationQuery ,
95103 cursor: PushNotificationCursorDTO ?
96104 ) async throws -> PushNotificationPageResponse {
97- guard let uid = Auth . auth ( ) . currentUser? . uid else { throw AuthError . notAuthenticated }
105+ do {
106+ guard let uid = Auth . auth ( ) . currentUser? . uid else { throw AuthError . notAuthenticated }
98107
99- var firestoreQuery = makeQuery ( uid: uid, query: notificationQuery)
108+ var firestoreQuery = makeQuery ( uid: uid, query: notificationQuery)
100109
101- if let cursor {
102- firestoreQuery = firestoreQuery. start ( after: [
103- Timestamp ( date: cursor. receivedAt) ,
104- cursor. documentID
105- ] )
106- }
110+ if let cursor {
111+ firestoreQuery = firestoreQuery. start ( after: [
112+ Timestamp ( date: cursor. receivedAt) ,
113+ cursor. documentID
114+ ] )
115+ }
107116
108- let snapshot = try await firestoreQuery
109- . limit ( to: notificationQuery. pageSize)
110- . getDocuments ( )
117+ let snapshot = try await firestoreQuery
118+ . limit ( to: notificationQuery. pageSize)
119+ . getDocuments ( )
111120
112- let items = snapshot. documents. compactMap { makeResponse ( from: $0) }
121+ let items = snapshot. documents. compactMap { makeResponse ( from: $0) }
113122
114- let nextCursor : PushNotificationCursorDTO ? = snapshot. documents. last. map { document in
115- guard let receivedAt = document. data ( ) [ Key . receivedAt. rawValue] as? Timestamp else {
116- return nil
117- }
123+ let nextCursor : PushNotificationCursorDTO ? = snapshot. documents. last. map { document in
124+ guard let receivedAt = document. data ( ) [ Key . receivedAt. rawValue] as? Timestamp else {
125+ return nil
126+ }
118127
119- return PushNotificationCursorDTO (
120- receivedAt: receivedAt. dateValue ( ) ,
121- documentID: document. documentID
122- )
123- } ?? nil
128+ return PushNotificationCursorDTO (
129+ receivedAt: receivedAt. dateValue ( ) ,
130+ documentID: document. documentID
131+ )
132+ } ?? nil
124133
125- return PushNotificationPageResponse ( items: items, nextCursor: nextCursor)
134+ return PushNotificationPageResponse ( items: items, nextCursor: nextCursor)
135+ } catch {
136+ logger. error ( " Failed to request notifications " , error: error)
137+ throw error
138+ }
126139 }
127140
128141 func observeNotifications(
@@ -160,37 +173,47 @@ final class PushNotificationService {
160173
161174 /// 푸시 알림 기록 삭제
162175 func deleteNotification( _ notificationID: String ) async throws {
163- guard let uid = Auth . auth ( ) . currentUser? . uid else { throw AuthError . notAuthenticated }
176+ do {
177+ guard let uid = Auth . auth ( ) . currentUser? . uid else { throw AuthError . notAuthenticated }
164178
165- let docRef = store. collection ( " users/ \( uid) /notifications " ) . document ( notificationID)
179+ let docRef = store. collection ( " users/ \( uid) /notifications " ) . document ( notificationID)
166180
167- try await docRef. delete ( )
181+ try await docRef. delete ( )
182+ } catch {
183+ logger. error ( " Failed to delete notification " , error: error)
184+ throw error
185+ }
168186 }
169187
170188 /// 푸시 알림 읽음/안읽음 토글
171189 func toggleNotificationRead( _ todoId: String ) async throws {
172190 logger. info ( " Toggling notification read for todoId: \( todoId) " )
173191
174- guard let uid = Auth . auth ( ) . currentUser? . uid else {
175- logger. error ( " User not authenticated " )
176- throw AuthError . notAuthenticated
177- }
192+ do {
193+ guard let uid = Auth . auth ( ) . currentUser? . uid else {
194+ logger. error ( " User not authenticated " )
195+ throw AuthError . notAuthenticated
196+ }
178197
179- let collection = store. collection ( " users/ \( uid) /notifications " )
180- let snapshot = try await collection. whereField ( " todoId " , isEqualTo: todoId) . getDocuments ( )
198+ let collection = store. collection ( " users/ \( uid) /notifications " )
199+ let snapshot = try await collection. whereField ( " todoId " , isEqualTo: todoId) . getDocuments ( )
181200
182- guard let document = snapshot. documents. first else {
183- logger. error ( " Notification not found for todoId: \( todoId) " )
184- throw FirestoreError . dataNotFound ( " notification " )
185- }
201+ guard let document = snapshot. documents. first else {
202+ logger. error ( " Notification not found for todoId: \( todoId) " )
203+ throw FirestoreError . dataNotFound ( " notification " )
204+ }
186205
187- guard let currentValue = document. data ( ) [ " isRead " ] as? Bool else {
188- logger. error ( " isRead not found for notification: \( document. documentID) " )
189- throw FirestoreError . dataNotFound ( " isRead " )
190- }
206+ guard let currentValue = document. data ( ) [ " isRead " ] as? Bool else {
207+ logger. error ( " isRead not found for notification: \( document. documentID) " )
208+ throw FirestoreError . dataNotFound ( " isRead " )
209+ }
191210
192- try await document. reference. updateData ( [ " isRead " : !currentValue] )
193- logger. info ( " Successfully toggled notification read " )
211+ try await document. reference. updateData ( [ " isRead " : !currentValue] )
212+ logger. info ( " Successfully toggled notification read " )
213+ } catch {
214+ logger. error ( " Failed to toggle notification read " , error: error)
215+ throw error
216+ }
194217 }
195218}
196219
0 commit comments