diff options
Diffstat (limited to 'src/features/offices/services/officesService.ts')
| -rw-r--r-- | src/features/offices/services/officesService.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/features/offices/services/officesService.ts b/src/features/offices/services/officesService.ts new file mode 100644 index 0000000..72cf081 --- /dev/null +++ b/src/features/offices/services/officesService.ts @@ -0,0 +1,30 @@ +import { collection, doc, getDoc, getDocs } from 'firebase/firestore'; +import { db } from '@/shared/lib/firebase/config'; +import { Office } from '@/features/offices/types'; + +function mapOffice(id: string, data: any): Office { + return { + id, + name: data.name ?? '', + location: data.location ?? '', + capacity: data.capacity ?? 0, + description: data.description ?? '', + }; +} + +export const officesService = { + async getAll(): Promise<Office[]> { + const snapshot = await getDocs(collection(db, 'offices')); + return snapshot.docs.map((item) => mapOffice(item.id, item.data())); + }, + + async getById(id: string): Promise<Office | null> { + const snapshot = await getDoc(doc(db, 'offices', id)); + + if (!snapshot.exists()) { + return null; + } + + return mapOffice(snapshot.id, snapshot.data()); + }, +}; |