Skip to content

Commit e08f89b

Browse files
committed
refactor: 커스텀 카테고리를 id로 관리
1 parent 7f81c35 commit e08f89b

9 files changed

Lines changed: 142 additions & 107 deletions

File tree

DevLog/Data/Repository/PushNotificationRepositoryImpl.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,23 @@ private extension PushNotificationRepositoryImpl {
143143
_ response: PushNotificationResponse,
144144
userTodoCategories: [UserTodoCategory]
145145
) throws -> PushNotificationResponse {
146-
let categoryName: String
146+
let id: String
147147
switch response.todoCategory {
148148
case .raw(let rawValue):
149-
categoryName = rawValue
149+
id = rawValue
150150
case .decoded:
151151
return response
152152
}
153153

154154
let todoCategory: TodoCategory
155-
if let systemTodoCategory = SystemTodoCategory(rawValue: categoryName) {
155+
if let systemTodoCategory = SystemTodoCategory(rawValue: id) {
156156
todoCategory = .system(systemTodoCategory)
157157
} else if let userTodoCategory = userTodoCategories.first(where: {
158-
$0.name == categoryName
158+
$0.id == id
159159
}) {
160160
todoCategory = .user(userTodoCategory)
161161
} else {
162-
throw DataError.invalidData("PushNotificationResponse.todoCategory is invalid: \(categoryName)")
162+
throw DataError.invalidData("PushNotificationResponse.todoCategory is invalid: \(id)")
163163
}
164164

165165
return PushNotificationResponse(

DevLog/Data/Repository/TodoRepositoryImpl.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,23 @@ private extension TodoRepositoryImpl {
8282
_ response: TodoResponse,
8383
userTodoCategories: [UserTodoCategory]
8484
) throws -> TodoResponse {
85-
let categoryName: String
85+
let id: String
8686
switch response.category {
8787
case .raw(let value):
88-
categoryName = value
88+
id = value
8989
case .decoded:
9090
return response
9191
}
9292

9393
let category: TodoCategory
94-
if let systemTodoCategory = SystemTodoCategory(rawValue: categoryName) {
94+
if let systemTodoCategory = SystemTodoCategory(rawValue: id) {
9595
category = .system(systemTodoCategory)
9696
} else if let userTodoCategory = userTodoCategories.first(where: {
97-
$0.name == categoryName
97+
$0.id == id
9898
}) {
9999
category = .user(userTodoCategory)
100100
} else {
101-
throw DataError.invalidData("TodoResponse.category is invalid: \(categoryName)")
101+
throw DataError.invalidData("TodoResponse.category is invalid: \(id)")
102102
}
103103

104104
return TodoResponse(

DevLog/Domain/Entity/TodoCategory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ enum TodoCategory: Equatable {
1616
case .system(let category):
1717
return category.rawValue
1818
case .user(let category):
19-
return category.name
19+
return category.id
2020
}
2121
}
2222
}

DevLog/Domain/Entity/UserTodoCategory.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import Foundation
99

1010
struct UserTodoCategory: Equatable {
11+
var id: String
1112
var name: String
1213
var colorHex: String
1314
}

DevLog/Infra/Service/TodoCategoryService.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class TodoCategoryService {
1212
private enum Field: String {
1313
case items
1414
case kind
15+
case id
1516
case systemCategory
1617
case name
1718
case colorHex
@@ -138,6 +139,7 @@ private extension TodoCategoryService {
138139
)
139140
case .user:
140141
guard
142+
let id = items[Field.id.rawValue] as? String,
141143
let name = items[Field.name.rawValue] as? String,
142144
let colorHex = items[Field.colorHex.rawValue] as? String
143145
else {
@@ -147,6 +149,7 @@ private extension TodoCategoryService {
147149
return TodoCategoryPreference(
148150
category: .user(
149151
UserTodoCategory(
152+
id: id,
150153
name: name,
151154
colorHex: colorHex
152155
)
@@ -167,6 +170,7 @@ private extension TodoCategoryService {
167170
case .user(let userTodoCategory):
168171
return [
169172
Field.kind.rawValue: Kind.user.rawValue,
173+
Field.id.rawValue: userTodoCategory.id,
170174
Field.name.rawValue: userTodoCategory.name,
171175
Field.colorHex.rawValue: userTodoCategory.colorHex,
172176
Field.isVisible.rawValue: preference.isVisible

DevLog/Presentation/Extension/UserTodoCategory+Presentation.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
import SwiftUI
99

10-
extension UserTodoCategory: Identifiable {
11-
var id: String { name }
12-
}
10+
extension UserTodoCategory: Identifiable { }
1311

1412
extension UserTodoCategory: Hashable {
1513
func hash(into hasher: inout Hasher) {
16-
hasher.combine(name)
14+
hasher.combine(id)
1715
hasher.combine(colorHex)
1816
}
1917
}

DevLog/Presentation/ViewModel/TodoManageViewModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ final class TodoManageViewModel: Store {
9595
TodoCategoryPreference(
9696
category: .user(
9797
UserTodoCategory(
98+
id: UUID().uuidString.lowercased(),
9899
name: trimmedCategoryName,
99100
colorHex: colorHex
100101
)

Firebase/functions/src/todo/update.ts

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -46,64 +46,86 @@ async function updateNotifications(
4646
todoId: string,
4747
todoCategory: string
4848
): 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-
}
49+
await updateNotificationBatch(userId, todoId, todoCategory)
7550
}
7651

7752
async function updateNotificationTasks(
7853
userId: string,
7954
todoId: string,
8055
todoCategory: string
8156
): 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-
}
57+
await updateNotificationTaskBatch(userId, todoId, todoCategory)
58+
}
59+
60+
async function updateNotificationBatch(
61+
userId: string,
62+
todoId: string,
63+
todoCategory: string,
64+
lastDocument?:
65+
FirebaseFirestore.QueryDocumentSnapshot<FirebaseFirestore.DocumentData>
66+
): Promise<void> {
67+
let query = admin.firestore()
68+
.collection(`users/${userId}/notifications`)
69+
.where("todoId", "==", todoId)
70+
.orderBy(admin.firestore.FieldPath.documentId())
71+
.limit(BATCH_SIZE);
72+
73+
if (lastDocument) {
74+
query = query.startAfter(lastDocument);
75+
}
76+
77+
const snapshot = await query.get();
78+
if (snapshot.empty) { return; }
79+
80+
const batch = admin.firestore().batch();
81+
snapshot.docs.forEach((document) => {
82+
batch.update(document.ref, { todoCategory });
83+
});
84+
await batch.commit();
9685

97-
const snapshot = await query.get();
98-
if (snapshot.empty) { return; }
86+
if (snapshot.size < BATCH_SIZE) { return; }
9987

100-
const batch = admin.firestore().batch();
101-
snapshot.docs.forEach((document) => {
102-
batch.update(document.ref, { todoCategory });
103-
});
104-
await batch.commit();
88+
await updateNotificationBatch(
89+
userId,
90+
todoId,
91+
todoCategory,
92+
snapshot.docs[snapshot.docs.length - 1]
93+
);
94+
}
10595

106-
if (snapshot.size < BATCH_SIZE) { return; }
107-
lastDocument = snapshot.docs[snapshot.docs.length - 1];
96+
async function updateNotificationTaskBatch(
97+
userId: string,
98+
todoId: string,
99+
todoCategory: string,
100+
lastDocument?:
101+
FirebaseFirestore.QueryDocumentSnapshot<FirebaseFirestore.DocumentData>
102+
): Promise<void> {
103+
let query = admin.firestore()
104+
.collection("notificationTasks")
105+
.where("userId", "==", userId)
106+
.where("todoId", "==", todoId)
107+
.orderBy(admin.firestore.FieldPath.documentId())
108+
.limit(BATCH_SIZE);
109+
110+
if (lastDocument) {
111+
query = query.startAfter(lastDocument);
108112
}
113+
114+
const snapshot = await query.get();
115+
if (snapshot.empty) { return; }
116+
117+
const batch = admin.firestore().batch();
118+
snapshot.docs.forEach((document) => {
119+
batch.update(document.ref, { todoCategory });
120+
});
121+
await batch.commit();
122+
123+
if (snapshot.size < BATCH_SIZE) { return; }
124+
125+
await updateNotificationTaskBatch(
126+
userId,
127+
todoId,
128+
todoCategory,
129+
snapshot.docs[snapshot.docs.length - 1]
130+
);
109131
}

0 commit comments

Comments
 (0)