-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetSurchargesRepository.ts
More file actions
116 lines (108 loc) · 4.4 KB
/
getSurchargesRepository.ts
File metadata and controls
116 lines (108 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { GetSurchargesRepositoryResponse, SurchargeStatus } from './DTO/GetSurchargesRepositoryResponse'
import { database } from "@data/firebase"
// import { storage, getDownloadURL } from "@data/firebase"
export function GetSurchargesRepository(placeId: string): Promise<GetSurchargesRepositoryResponse>
export function GetSurchargesRepository(placeIds: string[]): Promise<GetSurchargesRepositoryResponse[]>
export function GetSurchargesRepository({}): Promise<GetSurchargesRepositoryResponse[]>
export async function GetSurchargesRepository(param: string | string[] | {}): Promise<GetSurchargesRepositoryResponse | GetSurchargesRepositoryResponse[] | {}> {
if (param === undefined || (typeof param === 'object' && Object.keys(param).length === 0)) {
return await _GetAllSurcharges();
} else if (typeof param === 'string') {
return await _GetSurcharge(param);
} else if (Array.isArray(param)) {
return await _GetSurcharges(param);
} else {
throw new Error("Invalid parameter provided to GetSurchargesRepository.");
}
}
async function _GetSurcharge(placeId: string): Promise<GetSurchargesRepositoryResponse | {} > {
try {
const dbRef = database.collection('surcharges')
const docRef = dbRef.doc(placeId)
const doc = await docRef.get()
const surcharge = doc.data()
if (!surcharge) {
console.log("Surcharge data is undefined for the given place.");
return {}
} else {
const result: GetSurchargesRepositoryResponse = {
id: surcharge.id,
placeInformation: surcharge.placeInformation, // Convert Firestore reference to string
rate: surcharge.rate,
reportedDate: surcharge.reportedDate, // Keep as Firestore Timestamp
totalAmount: surcharge.totalAmount,
surchargeAmount: surcharge.surchargeAmount,
surchargeStatus: surcharge.surchargeStatus
};
return result;
}
} catch (error) {
console.error("Error fetching surcharge:", error);
throw error;
}
}
async function _GetSurcharges(placeIds: string[]): Promise<GetSurchargesRepositoryResponse[] | {}> {
try {
const surchargesPlaceReferences = placeIds.map((placeId) => {
return database.doc('places/' + placeId) // Firestore reference
})
const surcharges = await database
.collection('surcharges')
.where("placeInformation", "in", surchargesPlaceReferences) // Firestore allows querying with an "in" operator for multiple IDs
.get()
if (!surcharges){
console.log("Surcharges data are undefined for the given places ids.");
return {}
} else {
const matchedSurcharges = surcharges.docs.map((surcharge) => {
const data = surcharge.data()
return {
id: surcharge.id,
placeInformation: data.placeInformation, // Convert Firestore reference to string
rate: data.rate,
reportedDate: data.reportedDate, // Keep as Firestore Timestamp
totalAmount: data.totalAmount,
surchargeAmount: data.surchargeAmount,
surchargeStatus: data.surchargeStatus as SurchargeStatus
}
})
return matchedSurcharges
}
} catch (error) {
console.error("Error fetching surcharges:", error);
throw error;
}
}
async function _GetAllSurcharges(): Promise<GetSurchargesRepositoryResponse[] | {}> {
try {
const surcharges = await database.collection('surcharges').get();
if (surcharges.empty) {
console.log('There is no surcharges data yet');
return {};
}
// Fetch all surcharges and handle image retrieval
const AllSurcharges = await Promise.all(
surcharges.docs.map(async (surcharge) => {
const data = surcharge.data();
// const fileName = data.image;
// const file = storage.bucket().file(fileName);
// const url = await getDownloadURL(file)
return {
id: surcharge.id,
image: data.image, // Download url
placeInformation: data.placeInformation, // Convert Firestore reference to string if necessary
rate: data.rate,
reportedDate: data.reportedDate, // Keep as Firestore Timestamp
totalAmount: data.totalAmount,
surchargeAmount: data.surchargeAmount,
surchargeStatus: data.surchargeStatus,
};
})
);
console.log('All surcharges fetched successfully:');
return AllSurcharges;
} catch (error) {
console.error('Error fetching surcharges:', error);
throw error;
}
}