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
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
@@ -1,6 +1,6 @@
import { PlaceDTO } from "../../place/DTO/PlaceDTO";

export type GetPlacesRepositoryResponse = {
places: PlaceDTO[],
places: PlaceDTO[] | [],
nextPageToken?: string
}
51 changes: 40 additions & 11 deletions functions/src/data/place/get/places/GetPlacesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ 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 function GetPlacesRepository(request: GetPlacesRepositoryRequest): Promise<GetPlacesRepositoryResponse>
export function GetPlacesRepository(placesId: string[]): Promise<GetPlacesRepositoryResponse>
export function GetPlacesRepository(placesIds: string[]): Promise<GetPlacesRepositoryResponse>

export async function GetPlacesRepository(param: GetPlacesRepositoryRequest | string[]): Promise<GetPlacesRepositoryResponse> {
if (Array.isArray(param)) {
Expand All @@ -17,15 +17,44 @@ export async function GetPlacesRepository(param: GetPlacesRepositoryRequest | st
}
}

async function _GetPlacesFromFirestore(placeIds: string[]): Promise<GetPlacesRepositoryResponse> {

/*
Here you have to acess to Firestore and get the places by placeIds.
Return nextPageToken as undefined.
*/

return {
places: [],
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;
}
}

Expand Down
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 @@ -28,7 +28,10 @@ export type GetPlaceUsecaseResponse = {
displayName: displayName,
addressComponents: addressComponents[],
location?: LatLng,
image?: string,
rate?: number,
reportedDate?: Timestamp,
totalAmount?: number,
surchargeAmount?: number,
surchargeStatus?: SurchargeStatus
}
99 changes: 61 additions & 38 deletions functions/src/domain/place/get/places/GetPlacesUsecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,79 @@ import { GetPlacesUsecaseResponse } from "./entity/GetPlacesUsecaseResponse"
import { GetPlacesRepository } from "@data/place"
import { locationRestrictionOfNZ } from "@shared/constants"

export function GetPlacesUsecase(request: GetPlacesUsecaseRequest): Promise<GetPlacesUsecaseResponse>
export function GetPlacesUsecase(placeIds: string[]): Promise<GetPlacesUsecaseResponse>

export async function GetPlacesUsecase(param: GetPlacesUsecaseRequest | string[]): Promise<GetPlacesUsecaseResponse> {
export async function GetPlacesUsecase(request: GetPlacesUsecaseRequest | string[]): Promise<GetPlacesUsecaseResponse> {
let resultPlaces;

if (Array.isArray(request)) {

try {
const allSurcharges = await GetSurchargesRepository({})
const allSurchargesIds = allSurcharges.map((surcharge) => {
return surcharge.id
})
resultPlaces = await GetPlacesRepository(allSurchargesIds)

// const resultSurcharges = await GetSurchargesRepository(resultPlaceIds)

const placesWithSurcharges = resultPlaces.places.map((place) => {
return {
id: place.id,
displayName: {
text: place.displayName.text,
languageCode: place.displayName.languageCode,
},
addressComponents: place.addressComponents.map((component) => ({
longText: component.longText,
shortText: component.shortText,
types: component.types,
languageCode: component.languageCode,
})),
location: place.location
? {
latitude: place.location.latitude,
longitude: place.location.longitude,
}
: undefined,
image: allSurcharges.find((surcharge) => surcharge.id === place.id)?.image,
rate: allSurcharges.find((surcharge) => surcharge.id === place.id)?.rate,
totalAmount: allSurcharges.find((surcharge) => surcharge.id === place.id)?.totalAmount,
reportedDate: allSurcharges.find((surcharge) => surcharge.id === place.id)?.reportedDate,
surchargeAmount: allSurcharges.find((surcharge) => surcharge.id === place.id)?.surchargeAmount,
surchargeStatus: allSurcharges.find((surcharge) => surcharge.id === place.id)?.surchargeStatus as SurchargeStatus,
}
})

return {
places: placesWithSurcharges,
nextPageToken: undefined,
}

} catch (error) {
throw error
}

if (Array.isArray(param)) {
return await _GetPlacesByPlaceId(param)
} else {
return await _GetPlacesByRequest(param)
}

}

async function _GetPlacesByPlaceId(placeId: string[]): Promise<GetPlacesUsecaseResponse> {

/*
I think you can start here, and of course you need a repository for getting places by placeId
I made a snippet for you, and it can be found in GetPlacesRepository.ts
Return nextPageToken as undefined.
*/

return {
places: [],
nextPageToken: undefined,
}
}

async function _GetPlacesByRequest(request: GetPlacesUsecaseRequest): Promise<GetPlacesUsecaseResponse> {
if (request.userLocation) {
if (!isPointInRectangle(request.userLocation, locationRestrictionOfNZ)) {
throw new Error("User location is out of New Zealand")
if (request.userLocation) {
if (!isPointInRectangle(request.userLocation, locationRestrictionOfNZ)) {
throw new Error("User location is out of New Zealand")
}
}
resultPlaces = await GetPlacesRepository(
{
searchText: request.searchText,
nextPageToken: request.nextPageToken,
userLocation: request.userLocation,
}
)
}

const resultPlaces = await GetPlacesRepository(
{
searchText: request.searchText,
nextPageToken: request.nextPageToken,
userLocation: request.userLocation,
}
)

const resultPlaceIds = resultPlaces.places.map((place) => {
return place.id
})

try {

const resultSurcharges = await GetSurchargesRepository(resultPlaceIds)

const placesWithSurcharges = resultPlaces.places.map((place) => {
return {
id: place.id,
Expand Down
3 changes: 2 additions & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require('module-alias/register')

import express from "express";
import { onRequest } from "firebase-functions/v2/https";
import { getPlaceInterface, getPlacesInterface } from "@interface/place";
import { getPlaceInterface, getPlacesInterface, getPlacesWithSurchargesInterface } from "@interface/place";
import { getSurchargeInterface, postSurchargeInterface, getSurchargesInterface, putSurchargeInterface } from "@interface/surcharge";
import { getImageInterface } from "@interface/image";
import { AdminAuth } from "@shared/authentication";
Expand Down Expand Up @@ -36,6 +36,7 @@ admin.use(AdminAuth)
admin.get("/surcharges", getSurchargesInterface)
admin.put("/surcharge", putSurchargeInterface)
admin.get("/image", getImageInterface)
admin.get("/places", getPlacesWithSurchargesInterface);
exports.admin = onRequest(admin)

// For Mobile APIs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import express from "express"
import { GetPlacesUsecase } from "@domain/place"
import { Response } from "./model/GetPlacesInterfaceResponse"

export const getPlacesWithSurchargesInterface = async (request: express.Request, response: Response) => {

try {
const places = await GetPlacesUsecase([])

response.status(200).send({
places: places.places.map((place) => {
return {
id: place.id,
displayName: {
text: place.displayName.text,
languageCode: place.displayName.languageCode
},
addressComponents: place.addressComponents.map((component) => {
return {
longText: component.longText,
shortText: component.shortText,
types: component.types,
languageCode: component.languageCode
}
}),
location: place.location ? {
latitude: place.location.latitude,
longitude: place.location.longitude
} : undefined,
image: place.image,
surchargeStatus: place.surchargeStatus,
totalAmount: place.totalAmount,
surchargeAmount: place.surchargeAmount,
reportedDate: place.reportedDate,
rate: place.rate
}
}),
nextPageToken: places.nextPageToken
})

} catch (error: unknown) {
response.status(500).send({ message: error })
}
}
3 changes: 2 additions & 1 deletion functions/src/interface/place/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { getPlaceInterface } from "./get/place/getPlaceInterface"
export { getPlacesInterface } from "./get/places/getPlacesInterface"
export { getPlacesInterface } from "./get/places/getPlacesInterface"
export { getPlacesWithSurchargesInterface } from "./get/places/getPlacesWithSurchargesInterface"
2 changes: 1 addition & 1 deletion functions/src/shared/types/place/Place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export type Place = {
id: string,
displayName: LocalizedText,
addressComponents: AddressComponents[],
location?: LatLng
location?: LatLng | null
}