Skip to content

Commit

Permalink
Add : Firestore init
Browse files Browse the repository at this point in the history
  • Loading branch information
in63119 committed Apr 7, 2024
1 parent 44cc89e commit 1502b3f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 135 deletions.
22 changes: 10 additions & 12 deletions Front-end/src/db/firestore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initializeApp, getApp, FirebaseApp } from 'firebase/app';
import { initializeApp, FirebaseApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';

// Firebase
Expand All @@ -12,14 +12,12 @@ const firebaseConfig = {
measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID,
};

let app: FirebaseApp;

try {
app = getApp('app');
} catch (e) {
app = initializeApp(firebaseConfig, 'app');
}

const db = getFirestore(app);

export { app, db };
export const initialization = () => {
try {
const app: FirebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(app);
return { app, db };
} catch (e) {
console.log('initialization : ', e);
}
};
232 changes: 109 additions & 123 deletions Front-end/src/db/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { db } from './firestore';
import { collection, getDocs, query, getDoc, doc, setDoc } from 'firebase/firestore';
import { initialization } from './firestore';
import { Firestore, collection, getDocs, query, getDoc, doc, setDoc } from 'firebase/firestore';

// Type
import { TinstitutionLogin } from '../utils/type';
Expand All @@ -8,6 +8,21 @@ import { TinstitutionLogin } from '../utils/type';
Firestore 기능 모음
*/

export async function withFirestore<T>(operation: (db: Firestore) => Promise<T>): Promise<T | undefined> {
try {
const initializationResult = initialization();
if (!initializationResult) {
throw new Error('Database initialization failed');
}
const { db } = initializationResult;
return operation(db);
} catch (error) {
console.error(error);
// 여기서 더 상세한 에러 처리를 할 수 있습니다.
return undefined;
}
}

/*
getDocuments
@dev param에 따라서 Documents 조회
Expand All @@ -16,18 +31,15 @@ import { TinstitutionLogin } from '../utils/type';
2. "user" => 유저의 아이디 조회
@returns => [string]
*/
export const getDocuments = async (select: any) => {
try {
const result: any = [];
export const getDocuments = (select: string) => {
return withFirestore<string[]>(async (db) => {
const result: string[] = [];
const querySnapshot = await getDocs(collection(db, select));
querySnapshot.forEach((doc) => {
result.push(doc.id);
// console.log(doc.id, " => ", doc.data());
});
return result;
} catch (err) {
console.log(err);
}
});
};

/*
Expand All @@ -41,146 +53,120 @@ export const getDocuments = async (select: any) => {
ex. "유랑단학원" or "3011940250"
@returns => boolean
*/
export const checkDoc = async (select: any, docName: any) => {
const document = doc(db, select, `${docName}`);
const documentSnapshot = await getDoc(document);

const result = documentSnapshot.exists();
// const test2 = documentSnapshot.id;
// const test3 = documentSnapshot.data();

// console.log("docName : ", docName);
// console.log("documentSnapshot.exists() : ", result);
// console.log("documentSnapshot.id : ", test2);
// console.log("documentSnapshot.data() : ", test3);

return result;
export const checkDoc = (select: string, docName: string) => {
return withFirestore<boolean>(async (db) => {
const document = doc(db, select, docName);
const documentSnapshot = await getDoc(document);
return documentSnapshot.exists();
});
};

// 유저 추가
export const addUser = async (kakaoId: any, kakaoEmail: any) => {
const document = await doc(db, 'user', `${kakaoId}`);
const data = {
email: kakaoEmail,
'wallet-address': '',
'wallet-name': '',
};

await setDoc(document, data);
const addedDocumentId = document.id;

if (addedDocumentId === `${kakaoId}`) {
return true;
} else {
return false;
}
export const addUser = (kakaoId: string, kakaoEmail: string) => {
return withFirestore<boolean>(async (db) => {
const document = doc(db, 'user', kakaoId);
const data = {
email: kakaoEmail,
'wallet-address': '',
'wallet-name': '',
};
await setDoc(document, data);
return document.id === kakaoId;
});
};

// 자녀가 있는지 확인
export const getKids = async (kakaoId: any, name: any) => {
let result = false;
const q = query(collection(db, 'user', `${kakaoId}`, 'kids'));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
// const docObj = {
// ...doc.data(),
// id: doc.id,
// };
if (doc.id === name) {
result = true;
}
export const getKids = (kakaoId: string, name: string) => {
return withFirestore<boolean>(async (db) => {
const q = query(collection(db, 'user', kakaoId, 'kids'));
const querySnapshot = await getDocs(q);
let result = false;
querySnapshot.forEach((doc) => {
if (doc.id === name) {
result = true;
}
});
return result;
});

return result;
};

// 자녀가 기관에 등록 되있는지 확인
export const getKidRegistered = async (kakaoId: any, name: any) => {
let result;
const q = query(collection(db, 'user', `${kakaoId}`, 'kids'));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
if (name === doc.id) {
result = doc.data().isRegistered;
}
export const getKidRegistered = (kakaoId: string, name: string) => {
return withFirestore<boolean | undefined>(async (db) => {
const q = query(collection(db, 'user', kakaoId, 'kids'));
const querySnapshot = await getDocs(q);
let result;
querySnapshot.forEach((doc) => {
if (name === doc.id) {
result = doc.data().isRegistered;
}
});
return result;
});

return result;
};

// 자녀 등록 & 수정
export const setKid = async (kakaoId: any, name: any, info: any) => {
try {
const docRef = doc(collection(db, 'user', `${kakaoId}`, 'kids'), name);
export const setKid = (kakaoId: string, name: string, info: any) => {
return withFirestore<boolean>(async (db) => {
const docRef = doc(collection(db, 'user', kakaoId, 'kids'), name);
await setDoc(docRef, info);

const addedDocumentId = docRef.id;
if (addedDocumentId === name) {
return true;
} else {
return false;
}
} catch (e) {
console.log(e);
}
return docRef.id === name;
});
};

// 기업 로그인
export const getInstitutionInfo = async (data: TinstitutionLogin) => {
const result = {
result: false,
message: '',
incorrectData: '',
name: '',
};

const querySnapshot = await getDocs(collection(db, 'institution'));
querySnapshot.forEach((doc) => {
const innerData = doc.data();
// console.log(doc.id, " => ", doc.data());

if (innerData.id === data.id) {
if (innerData.password === data.password) {
result.result = true;
result.message = '정보가 일치합니다.';
result.incorrectData = 'success';
result.name = doc.id;
export const getInstitutionInfo = (data: TinstitutionLogin) => {
return withFirestore(async (db) => {
const result = {
result: false,
message: '',
incorrectData: '',
name: '',
};

const querySnapshot = await getDocs(collection(db, 'institution'));
querySnapshot.forEach((doc) => {
const innerData = doc.data();
if (innerData.id === data.id) {
if (innerData.password === data.password) {
result.result = true;
result.message = '정보가 일치합니다.';
result.incorrectData = 'success';
result.name = doc.id;
} else {
result.message = '비밀번호가 잘못됨.';
result.incorrectData = 'password';
}
} else {
result.message = '비밀번호가 잘못됨.';
result.incorrectData = 'password';
result.message = '아이디가 잘못됨.';
result.incorrectData = 'id';
}
} else {
result.message = '아이디가 잘못됨.';
result.incorrectData = 'id';
}
});
});

return result;
return result;
});
};

// 기관 등록 요청 대기열에 추가
export const registrationRequest = async (info: any) => {
const docRef = doc(collection(db, 'institution', `${info.institution}`, 'RegistrationRequest'), info.name);
await setDoc(docRef, info);

const addedDocumentId = docRef.id;
if (addedDocumentId === info.name) {
return true;
} else {
return false;
}
export const registrationRequest = (info: any) => {
return withFirestore(async (db) => {
const docRef = doc(collection(db, 'institution', info.institution, 'RegistrationRequest'), info.name);
await setDoc(docRef, info);
return docRef.id === info.name;
});
};

// 아이가 기관 등록 대기열에 있는지 조회
export const checkRegistrationRequest = async (info: any) => {
let result = false;
const q = query(collection(db, 'institution', `${info.institution}`, 'RegistrationRequest'));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
if (info.name === doc.id && info.birth === doc.data().birth && info.gender === doc.data().gender) {
result = true;
}
export const checkRegistrationRequest = (info: any) => {
return withFirestore(async (db) => {
let result = false;
const q = query(collection(db, 'institution', info.institution, 'RegistrationRequest'));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
if (info.name === doc.id && info.birth === doc.data().birth && info.gender === doc.data().gender) {
result = true;
}
});
return result;
});

return result;
};

0 comments on commit 1502b3f

Please sign in to comment.