|
| 1 | +import { |
| 2 | + addDoc, |
| 3 | + collection, |
| 4 | + deleteDoc, |
| 5 | + doc, |
| 6 | + type FieldValue, |
| 7 | + getDoc, |
| 8 | + serverTimestamp, |
| 9 | + setDoc, |
| 10 | + updateDoc, |
| 11 | +} from 'firebase/firestore'; |
| 12 | + |
| 13 | +import { type habitColors } from '@/ui/colors'; |
| 14 | + |
| 15 | +import { db } from '../common/firebase'; |
| 16 | +import { getUserById, type UserIdT } from '../users'; |
| 17 | +import { |
| 18 | + type DbHabitT, |
| 19 | + type DbParticipantT, |
| 20 | + type HabitCreationT, |
| 21 | + type HabitEntryT, |
| 22 | + type HabitIdT, |
| 23 | +} from './types'; |
| 24 | + |
| 25 | +type ColorName = keyof typeof habitColors; |
| 26 | + |
| 27 | +export const createHabit = async ( |
| 28 | + habitCreationInfo: HabitCreationT, |
| 29 | +): Promise<DbHabitT> => { |
| 30 | + const myId = '1' as UserIdT; // TODO: Get from auth context |
| 31 | + |
| 32 | + const newHabit = { |
| 33 | + title: habitCreationInfo.title, |
| 34 | + description: habitCreationInfo.description, |
| 35 | + colorName: habitCreationInfo.colorName as ColorName, |
| 36 | + icon: habitCreationInfo.icon, |
| 37 | + settings: { |
| 38 | + allowMultipleCompletions: habitCreationInfo.allowMultipleCompletions, |
| 39 | + }, |
| 40 | + participants: { |
| 41 | + [myId]: { |
| 42 | + displayName: 'Alex Chen', // TODO: Get from auth context |
| 43 | + username: 'alexchen', |
| 44 | + lastActivity: serverTimestamp(), |
| 45 | + isOwner: true, |
| 46 | + } satisfies Omit<DbParticipantT, 'lastActivity'> & { |
| 47 | + lastActivity: FieldValue; |
| 48 | + }, |
| 49 | + }, |
| 50 | + createdAt: serverTimestamp(), |
| 51 | + } satisfies Omit<DbHabitT, 'lastActivity' | 'createdAt'> & { |
| 52 | + createdAt: FieldValue; |
| 53 | + }; |
| 54 | + |
| 55 | + const docRef = await addDoc(collection(db, 'habits'), newHabit); |
| 56 | + const created = await getDoc(docRef); |
| 57 | + if (!created.exists()) throw new Error('Failed to create habit'); |
| 58 | + return created.data() as DbHabitT; |
| 59 | +}; |
| 60 | + |
| 61 | +export const deleteHabit = async (habitId: HabitIdT): Promise<void> => { |
| 62 | + await deleteDoc(doc(db, 'habits', habitId)); |
| 63 | +}; |
| 64 | + |
| 65 | +export const editHabit = async ( |
| 66 | + habitId: HabitIdT, |
| 67 | + newHabitInfo: HabitCreationT, |
| 68 | +): Promise<DbHabitT> => { |
| 69 | + const habitRef = doc(db, 'habits', habitId); |
| 70 | + const habitDoc = await getDoc(habitRef); |
| 71 | + if (!habitDoc.exists()) throw new Error('Habit not found'); |
| 72 | + |
| 73 | + const data = habitDoc.data() as DbHabitT; |
| 74 | + const updatedHabit = { |
| 75 | + ...data, |
| 76 | + title: newHabitInfo.title, |
| 77 | + description: newHabitInfo.description, |
| 78 | + colorName: newHabitInfo.colorName as ColorName, |
| 79 | + icon: newHabitInfo.icon, |
| 80 | + settings: { |
| 81 | + ...data.settings, |
| 82 | + allowMultipleCompletions: newHabitInfo.allowMultipleCompletions, |
| 83 | + }, |
| 84 | + } satisfies DbHabitT; |
| 85 | + |
| 86 | + await updateDoc(habitRef, updatedHabit); |
| 87 | + return updatedHabit; |
| 88 | +}; |
| 89 | + |
| 90 | +export const acceptHabitInvite = async (habitId: HabitIdT): Promise<void> => { |
| 91 | + const myId = '1' as UserIdT; // TODO: Get from auth context |
| 92 | + const me = await getUserById(myId); |
| 93 | + if (!me) throw new Error('User not found'); |
| 94 | + |
| 95 | + const habitRef = doc(db, 'habits', habitId); |
| 96 | + const habitDoc = await getDoc(habitRef); |
| 97 | + if (!habitDoc.exists()) throw new Error('Habit not found'); |
| 98 | + |
| 99 | + // Add the user as a participant |
| 100 | + await updateDoc(habitRef, { |
| 101 | + [`participants.${myId}`]: { |
| 102 | + displayName: me.displayName, |
| 103 | + username: me.username, |
| 104 | + lastActivity: serverTimestamp(), |
| 105 | + isOwner: false, |
| 106 | + } satisfies Omit<DbParticipantT, 'lastActivity'> & { |
| 107 | + lastActivity: FieldValue; |
| 108 | + }, |
| 109 | + }); |
| 110 | +}; |
| 111 | + |
| 112 | +export const modifyHabitEntry = async ( |
| 113 | + habitId: HabitIdT, |
| 114 | + userId: UserIdT, |
| 115 | + date: string, |
| 116 | + modifiedEntry: HabitEntryT, |
| 117 | +): Promise<void> => { |
| 118 | + const completionsRef = doc(db, 'habitCompletions', `${habitId}_${userId}`); |
| 119 | + const completionsDoc = await getDoc(completionsRef); |
| 120 | + |
| 121 | + // Clean up the entry by removing undefined values |
| 122 | + const cleanEntry = { |
| 123 | + numberOfCompletions: modifiedEntry.numberOfCompletions, |
| 124 | + ...(modifiedEntry.note && { note: modifiedEntry.note }), |
| 125 | + ...(modifiedEntry.image && { image: modifiedEntry.image }), |
| 126 | + }; |
| 127 | + |
| 128 | + if (!completionsDoc.exists()) { |
| 129 | + await setDoc(completionsRef, { |
| 130 | + habitId, |
| 131 | + userId, |
| 132 | + entries: { |
| 133 | + [date]: cleanEntry, |
| 134 | + }, |
| 135 | + }); |
| 136 | + } else { |
| 137 | + await updateDoc(completionsRef, { |
| 138 | + [`entries.${date}`]: cleanEntry, |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + // Update last activity |
| 143 | + const habitRef = doc(db, 'habits', habitId); |
| 144 | + const habitDoc = await getDoc(habitRef); |
| 145 | + if (!habitDoc.exists()) throw new Error('Habit not found'); |
| 146 | + |
| 147 | + await updateDoc(habitRef, { |
| 148 | + [`participants.${userId}.lastActivity`]: serverTimestamp(), |
| 149 | + }); |
| 150 | +}; |
0 commit comments