Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit 895c32d

Browse files
snt-pedroJoao-Pedro-P-Holanda
authored andcommitted
Refatorando getItensAcervo para retornar corretamente itens privados
1 parent 310f95c commit 895c32d

File tree

1 file changed

+62
-42
lines changed

1 file changed

+62
-42
lines changed

src/Utils/itemAcervoFirebase.ts

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,6 @@ import { FirebaseError } from "firebase/app";
2626
import { Colecao } from "../interfaces/Colecao";
2727
import Imagem from "../interfaces/Imagem";
2828

29-
const getItensAcervo = async (includePrivate: boolean = false): Promise<ItemAcervo[]> => {
30-
try {
31-
const itensAcervo: ItemAcervo[] = [];
32-
const colecoesRef = collection(db, "colecoes/publico/lista");
33-
const colecoesSnapshot = await getDocs(colecoesRef);
34-
35-
for (const colecaoDoc of colecoesSnapshot.docs) {
36-
const colecaoPath = colecaoDoc.ref.path;
37-
38-
// Busca itens públicos
39-
const itensPublicosRef = collection(db, `${colecaoPath}/publico`);
40-
const itensPublicosSnapshot = await getDocs(itensPublicosRef);
41-
42-
for (const itemDoc of itensPublicosSnapshot.docs) {
43-
const itemData = itemDoc.data() as ItemAcervo;
44-
itemData.id = itemDoc.id;
45-
itemData.colecao = colecaoDoc.id;
46-
itensAcervo.push(await getItemAcervo(itemDoc.ref.path));
47-
// itensAcervo.push(await processarImagens(itemData));
48-
}
49-
50-
// Busca itens privados se includePrivate for true
51-
if (includePrivate) {
52-
const itensPrivadosRef = collection(db, `${colecaoPath}/privado`);
53-
const itensPrivadosSnapshot = await getDocs(itensPrivadosRef);
54-
55-
for (const itemDoc of itensPrivadosSnapshot.docs) {
56-
const itemData = itemDoc.data() as ItemAcervo;
57-
itemData.id = itemDoc.id;
58-
itemData.colecao = colecaoDoc.id;
59-
itensAcervo.push(await getItemAcervo(itemDoc.ref.path));
60-
}
61-
}
62-
}
63-
64-
return itensAcervo;
65-
} catch (error) {
66-
console.error("Erro ao buscar itens do acervo:", error);
67-
throw new Error("Não foi possível carregar os itens do acervo");
68-
}
69-
};
70-
7129
const getItemAcervo = async (fullPath: string) => {
7230
try {
7331
const docRef: DocumentReference = doc(db, fullPath);
@@ -105,6 +63,68 @@ const getItemAcervo = async (fullPath: string) => {
10563
}
10664
};
10765

66+
const getItensAcervo = async (includePrivate: boolean = false): Promise<ItemAcervo[]> => {
67+
try {
68+
const itensAcervo: ItemAcervo[] = [];
69+
70+
// Busca coleções públicas
71+
const colecoesPublicasRef = collection(db, "colecoes/publico/lista");
72+
const colecoesPublicasSnapshot = await getDocs(colecoesPublicasRef);
73+
74+
for (const colecaoDoc of colecoesPublicasSnapshot.docs) {
75+
const colecaoPath = colecaoDoc.ref.path;
76+
77+
// Busca itens públicos dentro de coleções públicas
78+
const itensPublicosRef = collection(db, `${colecaoPath}/publico`);
79+
const itensPublicosSnapshot = await getDocs(itensPublicosRef);
80+
81+
for (const itemDoc of itensPublicosSnapshot.docs) {
82+
const itemData = itemDoc.data() as ItemAcervo;
83+
itemData.id = `publico-${itemDoc.id}`;
84+
itemData.colecao = colecaoDoc.id;
85+
itensAcervo.push(await getItemAcervo(itemDoc.ref.path));
86+
}
87+
88+
// Se includePrivate for true, busca itens privados dentro de coleções públicas
89+
if (includePrivate) {
90+
const itensPrivadosRef = collection(db, `${colecaoPath}/privado`);
91+
const itensPrivadosSnapshot = await getDocs(itensPrivadosRef);
92+
93+
for (const itemDoc of itensPrivadosSnapshot.docs) {
94+
const itemData = itemDoc.data() as ItemAcervo;
95+
itemData.id = `privado-${itemDoc.id}`;
96+
itemData.colecao = colecaoDoc.id;
97+
itensAcervo.push(await getItemAcervo(itemDoc.ref.path));
98+
}
99+
}
100+
}
101+
102+
// Se includePrivate for true, busca coleções privadas
103+
if (includePrivate) {
104+
const colecoesPrivadasRef = collection(db, "colecoes/privado/lista");
105+
const colecoesPrivadasSnapshot = await getDocs(colecoesPrivadasRef);
106+
107+
for (const colecaoPrivadaDoc of colecoesPrivadasSnapshot.docs) {
108+
const colecaoPrivadaPath = colecaoPrivadaDoc.ref.path;
109+
const itensPrivadosRef = collection(db, `${colecaoPrivadaPath}/itens`);
110+
const itensPrivadosSnapshot = await getDocs(itensPrivadosRef);
111+
112+
for (const itemDoc of itensPrivadosSnapshot.docs) {
113+
const itemData = itemDoc.data() as ItemAcervo;
114+
itemData.id = `privado-${itemDoc.id}`;
115+
itemData.colecao = colecaoPrivadaDoc.id;
116+
itensAcervo.push(await getItemAcervo(itemDoc.ref.path));
117+
}
118+
}
119+
}
120+
121+
return itensAcervo;
122+
} catch (error) {
123+
console.error("Erro ao buscar itens do acervo:", error);
124+
throw new Error("Não foi possível carregar os itens do acervo");
125+
}
126+
};
127+
108128
async function getImagemItemAcervo(
109129
storageRef: StorageReference
110130
): Promise<Imagem> {

0 commit comments

Comments
 (0)