summaryrefslogtreecommitdiffstats
path: root/src/features/offices/services/favoritesService.ts
blob: 39e757343f878b9d5659ea0036daf3ba4ee9ec7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import {
  collection,
  deleteDoc,
  doc,
  getDocs,
  serverTimestamp,
  setDoc,
} from 'firebase/firestore';
import { db } from '@/shared/lib/firebase/config';

export const favoritesService = {
  async getByUserId(userId: string): Promise<string[]> {
    const snapshot = await getDocs(collection(db, 'users', userId, 'favorites'));
    return snapshot.docs.map((item) => item.id);
  },

  async add(userId: string, officeId: string): Promise<void> {
    await setDoc(doc(db, 'users', userId, 'favorites', officeId), {
      officeId,
      createdAt: serverTimestamp(),
    });
  },

  async remove(userId: string, officeId: string): Promise<void> {
    await deleteDoc(doc(db, 'users', userId, 'favorites', officeId));
  },
};