Skip to content

Commit 36c39af

Browse files
committed
feat: 프로필 TIL 개수 연동
1 parent 9cde924 commit 36c39af

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

services/write/til.service.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
deleteDoc,
88
serverTimestamp,
99
Timestamp,
10+
getCountFromServer,
1011
} from 'firebase/firestore';
1112
import { db } from '@/lib/firebase';
1213
import { bumpDailyStat } from '@/services/heatmap/dailyStat.service';
@@ -29,19 +30,20 @@ export async function createTil(uid: string, content: string, title: string) {
2930
updatedAt: serverTimestamp(),
3031
});
3132
await bumpDailyStat(uid, 1, 0);
33+
await fetchTilCount(uid);
3234
return docRef.id;
3335
}
3436

35-
/** 런타임 최소 검증(안전하게 any 제거) */
36-
function isRecord(v: unknown): v is Record<string, unknown> {
37+
/** 런타임 최소 검증 */
38+
const isRecord = (v: unknown): v is Record<string, unknown> => {
3739
return typeof v === 'object' && v !== null;
38-
}
40+
};
3941

40-
function parseTilData(raw: unknown): TilData | null {
42+
const parseTilData = (raw: unknown): TilData | null => {
4143
if (!isRecord(raw)) return null;
4244

4345
const content = raw.content;
44-
if (typeof content !== 'string') return null; // content는 필수
46+
if (typeof content !== 'string') return null;
4547

4648
const title = raw.title;
4749
if (typeof title !== 'string') return null;
@@ -51,13 +53,12 @@ function parseTilData(raw: unknown): TilData | null {
5153
const updatedAt = raw.updatedAt instanceof Timestamp ? raw.updatedAt : null;
5254

5355
return { title, content, createdAt, updatedAt };
54-
}
56+
};
5557

56-
export async function fetchMyTil(
58+
export const fetchMyTil = async (
5759
uid: string | null | undefined,
5860
tilId: string | null | undefined
59-
): Promise<Til | null> {
60-
// // 여기서 uid/tilId 실물 확인
61+
): Promise<Til | null> => {
6162
// console.log('[fetchMyPost] path =', { uid, tilId });
6263

6364
if (!uid || !tilId) return null;
@@ -70,22 +71,32 @@ export async function fetchMyTil(
7071
if (!parsed) return null;
7172

7273
return { id: snap.id, ...parsed };
73-
}
74+
};
7475

75-
export async function updateTil(
76+
export const updateTil = async (
7677
uid: string,
7778
tilId: string,
7879
title: string,
7980
content: string
80-
) {
81+
) => {
8182
const ref = doc(db, 'users', uid, 'tils', tilId);
8283
await updateDoc(ref, {
8384
title,
8485
content,
8586
updatedAt: serverTimestamp(),
8687
});
87-
}
88+
};
8889

89-
export async function deleteTil(uid: string, tilId: string) {
90+
export const deleteTil = async (uid: string, tilId: string) => {
9091
await deleteDoc(doc(db, 'users', uid, 'tils', tilId));
91-
}
92+
await fetchTilCount(uid);
93+
};
94+
95+
export const fetchTilCount = async (uid: string) => {
96+
const colRef = collection(db, 'users', uid, 'tils');
97+
const ref = doc(db, 'users', uid);
98+
const snap = await getCountFromServer(colRef);
99+
await updateDoc(ref, {
100+
tilCount: snap.data().count,
101+
});
102+
};

0 commit comments

Comments
 (0)