1
1
import { initializeApp } from "firebase/app" ;
2
- import { addDoc , collection , deleteDoc , doc , getFirestore , setDoc } from "firebase/firestore" ;
3
- import { firebaseConfig } from "../firebase/firebase.config"
2
+ import {
3
+ addDoc ,
4
+ collection ,
5
+ deleteDoc ,
6
+ doc ,
7
+ getDocs ,
8
+ getFirestore ,
9
+ setDoc ,
10
+ query ,
11
+ } from "firebase/firestore" ;
12
+ import { firebaseConfig } from "../firebase/firebase.config" ;
4
13
5
14
initializeApp ( firebaseConfig ) ;
6
15
export const db = getFirestore ( ) ;
@@ -30,13 +39,19 @@ export async function deleteQuestion(questionId: string) {
30
39
}
31
40
}
32
41
33
- export async function updateQuestion ( questionId : string , question : Question ) : Promise < Question > {
42
+ export async function updateQuestion (
43
+ questionId : string ,
44
+ question : Question
45
+ ) : Promise < Question > {
34
46
try {
35
- let questionDoc : Omit < Question , "examples" > = question
47
+ let questionDoc : Omit < Question , "examples" > = question ;
36
48
const docRef = doc ( db , "questions" , questionId ) ;
37
49
await setDoc ( docRef , questionDoc ) ;
38
50
for ( let i = 0 ; i < question . examples . length ; i ++ ) {
39
- const add = setDoc ( doc ( docRef , "examples" , ( i + 1 ) . toString ( ) ) , question . examples [ i ] ) ;
51
+ const add = setDoc (
52
+ doc ( docRef , "examples" , ( i + 1 ) . toString ( ) ) ,
53
+ question . examples [ i ]
54
+ ) ;
40
55
}
41
56
return Promise . resolve ( question ) ;
42
57
} catch ( error ) {
@@ -46,14 +61,34 @@ export async function updateQuestion(questionId: string, question: Question): Pr
46
61
47
62
export async function addQuestion ( question : Question ) : Promise < Question > {
48
63
try {
49
- let questionDoc : Omit < Question , "examples" > = question
64
+ let questionDoc : Omit < Question , "examples" > = question ;
50
65
const docRef = await addDoc ( collection ( db , "questions" ) , questionDoc ) ;
51
66
for ( let i = 0 ; i < question . examples . length ; i ++ ) {
52
- const add = setDoc ( doc ( docRef , "examples" , ( i + 1 ) . toString ( ) ) , question . examples [ i ] ) ;
67
+ const add = setDoc (
68
+ doc ( docRef , "examples" , ( i + 1 ) . toString ( ) ) ,
69
+ question . examples [ i ]
70
+ ) ;
53
71
}
54
72
return Promise . resolve ( question ) ;
55
73
} catch ( error ) {
56
74
return Promise . reject ( error ) ;
57
75
}
58
76
}
59
77
78
+ export async function isValidToken ( token : string ) : Promise < boolean > {
79
+ try {
80
+ const q = query ( collection ( db , "users" ) ) ;
81
+ const querySnapshot = await getDocs ( q ) ;
82
+ const tokens = new Set ( ) ;
83
+ querySnapshot . forEach ( ( doc ) => {
84
+ const user = doc . data ( ) ;
85
+ tokens . add ( user . token ) ;
86
+ } ) ;
87
+ if ( tokens . has ( token ) ) {
88
+ return Promise . resolve ( true ) ;
89
+ }
90
+ return Promise . resolve ( false ) ;
91
+ } catch ( error ) {
92
+ return Promise . reject ( error ) ;
93
+ }
94
+ }
0 commit comments