@@ -25,10 +25,10 @@ protocol NotificationAPIServiceProtocol {
2525}
2626
2727final class NotificationAPIService : NotificationAPIServiceProtocol {
28-
28+
2929 private let client : Client
3030 private let jsonDecoder : JSONDecoder
31-
31+
3232 init ( tokenProvider: TokenProvider = KeychainTokenProvider ( ) ) {
3333 self . client = CodiveAPIProvider . createClient (
3434 middlewares: [ CodiveAuthMiddleware ( provider: tokenProvider) ]
@@ -41,7 +41,7 @@ extension NotificationAPIService {
4141 func patchEachNotification( notificationId: Int64 ) async throws {
4242 let input = Operations . updateReadStatus. Input ( path: . init( notificationId: notificationId) )
4343 let response = try await client. updateReadStatus ( input)
44-
44+
4545 switch response {
4646 case . ok:
4747 return
@@ -64,18 +64,11 @@ extension NotificationAPIService {
6464}
6565
6666extension NotificationAPIService {
67- private func formatDate( _ date: Date ? ) -> String {
68- guard let date else { return " " }
67+ func fetchNotificationList(
68+ lastNotificationId: Int64 ? ,
69+ size: Int32
70+ ) async throws -> NotificationListResponseDTO {
6971
70- let formatter = ISO8601DateFormatter ( )
71- formatter. formatOptions = [
72- . withInternetDateTime,
73- . withFractionalSeconds
74- ]
75- return formatter. string ( from: date)
76- }
77-
78- func fetchNotificationList( lastNotificationId: Int64 ? , size: Int32 ) async throws -> NotificationListResponseDTO {
7972 let input = Operations . Notification_getNotificationList. Input (
8073 query: . init(
8174 lastNotificationId: lastNotificationId,
@@ -87,26 +80,32 @@ extension NotificationAPIService {
8780
8881 switch response {
8982 case . ok( let okResponse) :
90- let data = try await Data ( collecting: okResponse. body. any, upTo: . max)
91-
92- let decoded = try jsonDecoder. decode ( Components . Schemas. BaseResponseSliceResponseNotificationListResponse. self, from: data)
93-
94- let content : [ NotificationListResponseItem ] = decoded. result? . content? . map { item in
95- NotificationListResponseItem (
96- notificationId: item. notificationId ?? 0 ,
97- notificationImageUrl: item. notificationImageUrl ?? " " ,
98- notificationContent: item. notificationContent ?? " " ,
99- redirectInfo: item. action? . redirectInfo ?? " " ,
100- redirectType: item. action? . redirectType? . rawValue ?? " NONE " ,
101- readStatus: item. readStatus? . rawValue ?? " NOT_READ " ,
102- createdAt: formatDate ( item. createdAt)
103- )
104- } ?? [ ]
105-
106- return NotificationListResponseDTO ( content: content, isLast: decoded. result? . isLast ?? true )
107-
83+ let data = try await Data (
84+ collecting: okResponse. body. any,
85+ upTo: . max
86+ )
87+
88+ let decoded = try jsonDecoder. decode (
89+ Components . Schemas. BaseResponseSliceResponseNotificationListResponse. self,
90+ from: data
91+ )
92+
93+ guard let result = decoded. result else {
94+ throw NotificationAPIError . invalidResponse
95+ }
96+
97+ let content = mapNotificationItems ( result. content)
98+
99+ return NotificationListResponseDTO (
100+ content: content,
101+ isLast: result. isLast ?? true
102+ )
103+
108104 case . undocumented( statusCode: let code, _) :
109- throw NotificationAPIError . serverError ( statusCode: code, message: " 알림 목록 조회 실패 " )
105+ throw NotificationAPIError . serverError (
106+ statusCode: code,
107+ message: " 알림 목록 조회 실패 "
108+ )
110109 }
111110 }
112111
@@ -124,7 +123,7 @@ extension NotificationAPIService {
124123 guard let item = decoded. result else {
125124 throw NotificationAPIError . invalidResponse
126125 }
127-
126+
128127 return ReportReceivedAPIResponseDTO (
129128 isReported: item. isReported ?? false ,
130129 targetType: item. targetType. flatMap { ReportType ( rawValue: $0. rawValue) }
@@ -137,7 +136,6 @@ extension NotificationAPIService {
137136}
138137
139138extension NotificationAPIService {
140-
141139 func fetchNotificationExist( ) async throws -> NotificationExistAPIResponseDTO {
142140
143141 let input = Operations . Notification_existsUnreadNotification. Input ( )
@@ -163,6 +161,50 @@ extension NotificationAPIService {
163161 }
164162}
165163
164+
165+ extension NotificationAPIService {
166+ private func mapNotificationItems(
167+ _ items: [ Components . Schemas . NotificationListResponse ] ?
168+ ) -> [ NotificationListResponseItem ] {
169+ items? . map { item in
170+ let notificationType = NotificationType (
171+ rawValue: item. notificationType? . rawValue ?? " "
172+ ) ?? . unknown
173+
174+ let readStatus = ReadStatus (
175+ rawValue: item. readStatus? . rawValue ?? " "
176+ ) ?? . notRead
177+
178+ let redirectType = NotificationRedirectType (
179+ rawValue: item. action? . redirectType? . rawValue ?? " "
180+ ) ?? . none
181+
182+ return NotificationListResponseItem (
183+ notificationId: item. notificationId ?? 0 ,
184+ notificationImageUrl: item. notificationImageUrl ?? " " ,
185+ notificationContent: item. notificationContent ?? " " ,
186+ notificationType: notificationType,
187+ action: NotificationActionDTO (
188+ redirectType: redirectType,
189+ redirectInfo: item. action? . redirectInfo ?? " "
190+ ) ,
191+ readStatus: readStatus,
192+ createdAt: formatDate ( item. createdAt)
193+ )
194+ } ?? [ ]
195+ }
196+
197+ private func formatDate( _ date: Date ? ) -> String {
198+ guard let date else { return " " }
199+
200+ let formatter = ISO8601DateFormatter ( )
201+ formatter. formatOptions = [
202+ . withInternetDateTime,
203+ . withFractionalSeconds
204+ ]
205+ return formatter. string ( from: date)
206+ }
207+ }
166208// MARK: - ClothAPIError
167209
168210enum NotificationAPIError : LocalizedError {
@@ -172,7 +214,7 @@ enum NotificationAPIError: LocalizedError {
172214 case s3UploadFailed( statusCode: Int )
173215 case noClothIdsReturned
174216 case serverError( statusCode: Int , message: String )
175-
217+
176218 var errorDescription : String ? {
177219 switch self {
178220 case . presignedUrlMismatch:
0 commit comments