Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/build-validator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Validate Build

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@master

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 22.0

- name: Install Dependencies
run: |
cd functions
npm install

- name: Build
run: |
cd functions
npm run build
2 changes: 2 additions & 0 deletions functions/src/data/firebase/Firebase.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { initializeApp } from "firebase-admin/app"
import { firestore } from "firebase-admin"
import { getStorage } from "firebase-admin/storage"
// import { getDownloadURL } from "firebase-admin/storage"

initializeApp()

// export {getDownloadURL}
export const database = firestore()
export const storage = getStorage()
export { File } from '@google-cloud/storage'
3 changes: 2 additions & 1 deletion functions/src/data/firebase/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { database } from "./Firebase"
export { storage } from "./Firebase"
export { File } from './Firebase'
export { File } from './Firebase'
// export {getDownloadURL} from './Firebase'
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { database } from '@data/firebase';

export async function GetFranchiseRepository(): Promise<string[]> {
try {
let franchises = []
const franchisesSnapshot = await database.collection('franchises').get();
if (franchisesSnapshot.empty) {
console.log("No franchises.");
return [];
}
for (const doc of franchisesSnapshot.docs) {
const data = doc.id;
// console.log("GetFranchiseRepository data: ", JSON.stringify(data));
franchises.push(data.toLowerCase())
}
// console.log("GetFranchiseRepository franchises: ", franchises)
return franchises
} catch (error) {
console.error('Error fetching franchises data:', error);
throw error;
}
}


1 change: 1 addition & 0 deletions functions/src/data/franchise/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {GetFranchiseRepository} from "./get/franchise/GetFranchiseRepository"
3 changes: 3 additions & 0 deletions functions/src/data/image/get/image/DTO/ImageDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type ImageDTO = {
image: string | []
}
28 changes: 28 additions & 0 deletions functions/src/data/image/get/image/GetImageRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { storage } from "@data/firebase";
import { ImageDTO } from "@data/image";

export async function GetImageRepository(image: string): Promise<ImageDTO> {
try {
const imageRef = storage.bucket().file(image);
if (!imageRef) {
console.log('Image does not exist in Firebase Storage');
return { image: [] }; // Empty Base64 string if image doesn't exist
}

const [fileBuffer] = await imageRef.download();
if (!fileBuffer) {
console.log('Failed to download image from Firebase Storage');
return { image: [] }; // Empty Base64 string if download fails
}

// Convert file buffer to Base64
const base64Image = fileBuffer.toString('base64');
const imageResult: ImageDTO = {
image: base64Image,
};
return imageResult;
} catch (error) {
console.error('Error fetching image data:', error);
throw error;
}
}
2 changes: 2 additions & 0 deletions functions/src/data/image/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { ImageDTO } from "./get/image/DTO/ImageDTO"
export { GetImageRepository } from "./get/image/GetImageRepository"
2 changes: 1 addition & 1 deletion functions/src/data/place/get/place/DTO/PlaceDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export type PlaceDTO = {
id: string,
displayName: LocalizedTextDTO,
addressComponents: AddressComponentsDTO[],
location: LatLngDTO
location: LatLngDTO | null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type GetPlacesRepositoryRequest = {
searchText: string
nextPageToken?: string
userLocation?: { latitude: number, longitude: number }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PlaceDTO } from "../../place/DTO/PlaceDTO";

export type GetPlacesRepositoryResponse = {
places: PlaceDTO[],
places: PlaceDTO[] | [],
nextPageToken?: string
}
69 changes: 55 additions & 14 deletions functions/src/data/place/get/places/GetPlacesRepository.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
import { PlaceDTO } from "../place/DTO/PlaceDTO"
import { AddressComponentsDTO } from "../place/DTO/AddressComponentsDTO"
import { GetPlacesRepositoryResponse } from "./DTO/GetPlacesRepositoryResponse"
import { GetPlacesRepositoryRequest } from "./DTO/GetPlacesRepositoryRequest"
import { locationRestrictionOfNZ } from "@shared/constants"
import { database } from "@data/firebase"

export const locationRestrictionOfNZ = {
rectangle: {
low: {
latitude: -47.0,
longitude: 166.0,
},
high: {
latitude: -34.0,
longitude: 178.0,
},
export function GetPlacesRepository(request: GetPlacesRepositoryRequest): Promise<GetPlacesRepositoryResponse>
export function GetPlacesRepository(placesIds: string[]): Promise<GetPlacesRepositoryResponse>

export async function GetPlacesRepository(param: GetPlacesRepositoryRequest | string[]): Promise<GetPlacesRepositoryResponse> {
if (Array.isArray(param)) {
return await _GetPlacesFromFirestore(param)
} else {
const request = param as GetPlacesRepositoryRequest
return await _GetPlacesFromGoogle(request.searchText, request.nextPageToken, request.userLocation)
}
}

async function _GetPlacesFromFirestore(
placesIds: string[]
): Promise<GetPlacesRepositoryResponse> {
try {
const placesSnapshot = await database
.collection('places')
.where("id", "in", placesIds)
.get();

if (placesSnapshot.empty) {
console.log("Places data are undefined for the given places IDs.");
return { places: [], nextPageToken: undefined };
} else {
const matchedPlaces = placesSnapshot.docs.map((doc) => {
const data = doc.data() as PlaceDTO;
return {
id: data.id,
displayName: {
text: data.displayName.text,
languageCode: data.displayName.languageCode,
},
addressComponents: data.addressComponents.map((component: AddressComponentsDTO) => {
return {
longText: component.longText,
shortText: component.shortText,
types: component.types,
languageCode: data.displayName.languageCode,
}
}),
location: null,
};
});

return { places: matchedPlaces, nextPageToken: undefined };
}
} catch (error) {
console.error("Error fetching places:", error);
throw error;
}
}

export async function GetPlacesRepository(
async function _GetPlacesFromGoogle(
searchText: string,
nextPageToken?: string,
userLocation?: { latitude: number, longitude: number }
Expand Down Expand Up @@ -86,9 +129,7 @@ export async function GetPlacesRepository(

}

const data = await response.json()

throw new Error(data)
throw new Error(response.statusText)

} catch (error) {
throw error
Expand Down
3 changes: 1 addition & 2 deletions functions/src/data/place/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export { PostPlaceRepositoryRequest } from './post/place/DTO/PostPlaceRepository
export { PlaceDTO } from './get/place/DTO/PlaceDTO'
export { GetPlacesRepository } from './get/places/GetPlacesRepository'
export { GetPlaceRepository } from './get/place/GetPlaceRepository'
export { PostPlaceRepository } from './post/place/PostPlaceRepository'
export { locationRestrictionOfNZ } from './get/places/GetPlacesRepository'
export { PostPlaceRepository } from './post/place/PostPlaceRepository'
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export interface PostPlaceRepositoryRequest {
id: string;
displayName: displayName,
addressComponents: AddressComponentsDTO[],
location: LatLngDTO
location: LatLngDTO | null
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { Timestamp } from "firebase-admin/firestore";
export enum SurchargeStatus {
REPORTED = 'REPORTED',
CONFIRMED = 'CONFIRMED',
UNKNOWN = 'UNKNOWN'
UNKNOWN = 'UNKNOWN',
AUTO_GENERATED = 'AUTO_GENERATED',
REJECTED = 'REJECTED'
}

export type GetSurchargesRepositoryResponse = {
id: string
image?: string
placeInformation: string
reportedDate: Timestamp
rate: number
Expand Down
41 changes: 24 additions & 17 deletions functions/src/data/surcharge/get/getSurchargesRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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[]>
Expand Down Expand Up @@ -36,7 +37,6 @@ async function _GetSurcharge(placeId: string): Promise<GetSurchargesRepositoryRe
surchargeAmount: surcharge.surchargeAmount,
surchargeStatus: surcharge.surchargeStatus
};
console.log("Surcharge fetched successfully:", result);
return result;
}
} catch (error) {
Expand Down Expand Up @@ -80,30 +80,37 @@ async function _GetSurcharges(placeIds: string[]): Promise<GetSurchargesReposito

async function _GetAllSurcharges(): Promise<GetSurchargesRepositoryResponse[] | {}> {
try {
const surcharges = await database
.collection('surcharges')
.get()
if (!surcharges){
console.log('There is no surcharges data yet')
return {}
} else{
const AllSurcharges = surcharges.docs.map((surcharge) => {
const data = surcharge.data()
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,
placeInformation: data.placeInformation, // Convert Firestore reference to string
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
}
surchargeStatus: data.surchargeStatus,
};
})
console.log("All surcharges fetched successfully:", AllSurcharges);
return AllSurcharges
}
);

console.log('All surcharges fetched successfully:');
return AllSurcharges;
} catch (error) {
console.error("Error fetching surcharges:", error);
console.error('Error fetching surcharges:', error);
throw error;
}
}
4 changes: 3 additions & 1 deletion functions/src/data/surcharge/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { GetSurchargesRepository } from './get/getSurchargesRepository'
export { PostSurchargeRepo } from "./post/postSurchargeRepository"
export {SurchargeStatus} from "./post/DTO/PostSurchargeRepositoryRequest"
export { PutSurchargeRepository } from "./put/PutSurchargeRepository"
export { GetSurchargesRepositoryResponse } from './get/DTO/GetSurchargesRepositoryResponse'
export { PostSurchargeRepositoryRequest } from './post/DTO/PostSurchargeRepositoryRequest'
export { PostSurchargeRepositoryRequest } from './post/DTO/PostSurchargeRepositoryRequest'
export { PutSurchargeRepositoryRequest } from './put/DTO/PutSurchargeRepositoryRequest'
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum SurchargeStatus {
REPORTED = 'REPORTED',
CONFIRMED = 'CONFIRMED',
UNKNOWN = 'UNKNOWN',
AUTO_GENERATED = 'AUTO_GENERATED',
REJECTED = 'REJECTED'
}

export type PutSurchargeRepositoryRequest = {
id: string,
rate?: number,
surchargeAmount?: number,
totalAmount?: number,
surchargeStatus: SurchargeStatus,
action: string
}
Loading