| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import { Injectable } from '@angular/core';
- import {
- collection,
- getDocs,
- doc,
- getDoc,
- setDoc,
- deleteDoc,
- updateDoc
- } from 'firebase/firestore';
-
- import { db } from '../firebase';
- import { Danka } from '../models/danka';
-
- @Injectable({
- providedIn: 'root',
- })
- export class DankaService {
- private path = 'danka';
- private readonly recentDankaStorageKey = 'kaimyo-management.recent-danka';
-
- // 一覧
- async getDankaList(): Promise<Danka[]> {
- const snap = await getDocs(collection(db, this.path));
-
- return snap.docs.map(d => ({
- id: d.id,
- ...(d.data() as Omit<Danka, 'id'>)
- }));
- }
-
- // 1件
- async getDankaById(id: string): Promise<Danka | undefined> {
- const ref = doc(db, this.path, id);
- const snap = await getDoc(ref);
-
- if (!snap.exists()) return undefined;
-
- return {
- id: snap.id,
- ...(snap.data() as Omit<Danka, 'id'>)
- };
- }
-
- // 作成・更新
- async saveDanka(danka: Danka): Promise<void> {
- const ref = doc(db, this.path, danka.id);
- await setDoc(ref, danka);
- }
-
- // 部分更新
- async updateDanka(id: string, data: Partial<Danka>): Promise<void> {
- const ref = doc(db, this.path, id);
- await updateDoc(ref, data as any);
- }
-
- // 削除
- async deleteDanka(id: string): Promise<void> {
- const ref = doc(db, this.path, id);
- await deleteDoc(ref);
- }
-
- // -----------------------------
- // 最近開いた檀家
- // -----------------------------
-
- private getRecentDankaIds(): string[] {
- if (!this.canUseLocalStorage()) return [];
-
- const value = localStorage.getItem(this.recentDankaStorageKey);
- if (!value) return [];
-
- try {
- const parsed = JSON.parse(value);
- return Array.isArray(parsed)
- ? parsed.filter((id): id is string => typeof id === 'string')
- : [];
- } catch {
- return [];
- }
- }
-
- private saveRecentDankaIds(ids: string[]): void {
- if (!this.canUseLocalStorage()) return;
- localStorage.setItem(this.recentDankaStorageKey, JSON.stringify(ids));
- }
-
- private canUseLocalStorage(): boolean {
- return typeof localStorage !== 'undefined';
- }
-
- // ★ここが重要:async化
- async recordDankaOpened(id: string): Promise<void> {
- const danka = await this.getDankaById(id);
- if (!danka) return;
-
- const recentIds = [
- id,
- ...this.getRecentDankaIds().filter(rid => rid !== id),
- ].slice(0, 5);
-
- this.saveRecentDankaIds(recentIds);
- }
-
- // ★ここも async 必須
- async getRecentDankaList(limit = 5): Promise<Danka[]> {
- const ids = this.getRecentDankaIds().slice(0, limit);
-
- const list = await Promise.all(
- ids.map(id => this.getDankaById(id))
- );
-
- const filtered = list.filter((d): d is Danka => d !== undefined);
-
- if (filtered.length > 0) {
- return filtered;
- }
-
- // fallback(updatedAtがある前提なら)
- const all = await this.getDankaList();
-
- return [...all]
- .sort((a: any, b: any) =>
- (b.updatedAt ?? '').localeCompare(a.updatedAt ?? '')
- )
- .slice(0, limit);
- }
- }
|