From f33b2d2c1fd97f1ab3b01499ecfdae342a2432c1 Mon Sep 17 00:00:00 2001 From: PetruccioU Date: Tue, 28 Jan 2025 22:32:43 +1300 Subject: [PATCH 1/5] GetPlacesUsecase updated, also PlaceDTO and Place type --- .../src/data/place/get/place/DTO/PlaceDTO.ts | 2 +- .../place/get/places/GetPlacesUsecase.ts | 97 +++++++++++-------- functions/src/shared/types/place/Place.ts | 2 +- 3 files changed, 61 insertions(+), 40 deletions(-) diff --git a/functions/src/data/place/get/place/DTO/PlaceDTO.ts b/functions/src/data/place/get/place/DTO/PlaceDTO.ts index 4f64caa..eb15602 100644 --- a/functions/src/data/place/get/place/DTO/PlaceDTO.ts +++ b/functions/src/data/place/get/place/DTO/PlaceDTO.ts @@ -6,5 +6,5 @@ export type PlaceDTO = { id: string, displayName: LocalizedTextDTO, addressComponents: AddressComponentsDTO[], - location: LatLngDTO + location: LatLngDTO | null } \ No newline at end of file diff --git a/functions/src/domain/place/get/places/GetPlacesUsecase.ts b/functions/src/domain/place/get/places/GetPlacesUsecase.ts index 74ff90b..ef96763 100644 --- a/functions/src/domain/place/get/places/GetPlacesUsecase.ts +++ b/functions/src/domain/place/get/places/GetPlacesUsecase.ts @@ -4,56 +4,77 @@ import { GetPlacesUsecaseResponse } from "./entity/GetPlacesUsecaseResponse" import { GetPlacesRepository } from "@data/place" import { locationRestrictionOfNZ } from "@shared/constants" -export function GetPlacesUsecase(request: GetPlacesUsecaseRequest): Promise -export function GetPlacesUsecase(placeIds: string[]): Promise - -export async function GetPlacesUsecase(param: GetPlacesUsecaseRequest | string[]): Promise { +export async function GetPlacesUsecase(request: GetPlacesUsecaseRequest | string[]): Promise { + 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, + rate: allSurcharges.find((surcharge) => surcharge.id === place.id)?.rate, + totalAmount: allSurcharges.find((surcharge) => surcharge.id === place.id)?.totalAmount, + 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 { - - /* - 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 { - 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, diff --git a/functions/src/shared/types/place/Place.ts b/functions/src/shared/types/place/Place.ts index 544f274..4c3599d 100644 --- a/functions/src/shared/types/place/Place.ts +++ b/functions/src/shared/types/place/Place.ts @@ -6,5 +6,5 @@ export type Place = { id: string, displayName: LocalizedText, addressComponents: AddressComponents[], - location?: LatLng + location?: LatLng | null } \ No newline at end of file From 7746fa8b13b3a82d2d87684d5d4841092aadfc6c Mon Sep 17 00:00:00 2001 From: PetruccioU Date: Tue, 28 Jan 2025 22:34:17 +1300 Subject: [PATCH 2/5] GetPlacesRepository updated along with GetPlacesRepositoryResponse and PostPlaceRepositoryRequest --- .../places/DTO/GetPlacesRepositoryResponse.ts | 2 +- .../place/get/places/GetPlacesRepository.ts | 51 +++++++++++++++---- .../place/DTO/PostPlaceRepositoryRequest.ts | 2 +- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/functions/src/data/place/get/places/DTO/GetPlacesRepositoryResponse.ts b/functions/src/data/place/get/places/DTO/GetPlacesRepositoryResponse.ts index bd6d781..be9eb55 100644 --- a/functions/src/data/place/get/places/DTO/GetPlacesRepositoryResponse.ts +++ b/functions/src/data/place/get/places/DTO/GetPlacesRepositoryResponse.ts @@ -1,6 +1,6 @@ import { PlaceDTO } from "../../place/DTO/PlaceDTO"; export type GetPlacesRepositoryResponse = { - places: PlaceDTO[], + places: PlaceDTO[] | [], nextPageToken?: string } \ No newline at end of file diff --git a/functions/src/data/place/get/places/GetPlacesRepository.ts b/functions/src/data/place/get/places/GetPlacesRepository.ts index 610713f..0ae0471 100644 --- a/functions/src/data/place/get/places/GetPlacesRepository.ts +++ b/functions/src/data/place/get/places/GetPlacesRepository.ts @@ -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 -export function GetPlacesRepository(placesId: string[]): Promise +export function GetPlacesRepository(placesIds: string[]): Promise export async function GetPlacesRepository(param: GetPlacesRepositoryRequest | string[]): Promise { if (Array.isArray(param)) { @@ -17,15 +17,44 @@ export async function GetPlacesRepository(param: GetPlacesRepositoryRequest | st } } -async function _GetPlacesFromFirestore(placeIds: string[]): Promise { - - /* - 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 { + 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; } } diff --git a/functions/src/data/place/post/place/DTO/PostPlaceRepositoryRequest.ts b/functions/src/data/place/post/place/DTO/PostPlaceRepositoryRequest.ts index 8413e1a..d596c18 100644 --- a/functions/src/data/place/post/place/DTO/PostPlaceRepositoryRequest.ts +++ b/functions/src/data/place/post/place/DTO/PostPlaceRepositoryRequest.ts @@ -19,5 +19,5 @@ export interface PostPlaceRepositoryRequest { id: string; displayName: displayName, addressComponents: AddressComponentsDTO[], - location: LatLngDTO + location: LatLngDTO | null } From ecd12ed0748522778a6a25906fc411c47785dcdc Mon Sep 17 00:00:00 2001 From: PetruccioU Date: Tue, 28 Jan 2025 22:55:09 +1300 Subject: [PATCH 3/5] getPlacesWithSurchargesInterface crated, introduced into index and exported --- functions/src/index.ts | 3 +- .../getPlacesWithSurchargesInterface.ts | 40 +++++++++++++++++++ functions/src/interface/place/index.ts | 3 +- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts diff --git a/functions/src/index.ts b/functions/src/index.ts index f400122..50d4c41 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -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"; @@ -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 diff --git a/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts b/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts new file mode 100644 index 0000000..92e238e --- /dev/null +++ b/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts @@ -0,0 +1,40 @@ +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, + surchargeStatus: place.surchargeStatus, + surchargeRate: place.rate + } + }), + nextPageToken: places.nextPageToken + }) + + } catch (error: unknown) { + response.status(500).send({ message: error }) + } +} \ No newline at end of file diff --git a/functions/src/interface/place/index.ts b/functions/src/interface/place/index.ts index 2ab68b5..f004a87 100644 --- a/functions/src/interface/place/index.ts +++ b/functions/src/interface/place/index.ts @@ -1,2 +1,3 @@ export { getPlaceInterface } from "./get/place/getPlaceInterface" -export { getPlacesInterface } from "./get/places/getPlacesInterface" \ No newline at end of file +export { getPlacesInterface } from "./get/places/getPlacesInterface" +export { getPlacesWithSurchargesInterface } from "./get/places/getPlacesWithSurchargesInterface" \ No newline at end of file From 6487fa24ce48d8c36cb0cc8cb4ceec1b7e24e638 Mon Sep 17 00:00:00 2001 From: PetruccioU Date: Tue, 28 Jan 2025 23:07:58 +1300 Subject: [PATCH 4/5] totalAmount, surchargeAmount and reportedDate added to getPlacesWithSurchargesInterface --- .../domain/place/get/place/entity/GetPlaceUsecaseResponse.ts | 2 ++ functions/src/domain/place/get/places/GetPlacesUsecase.ts | 1 + .../place/get/places/getPlacesWithSurchargesInterface.ts | 3 +++ 3 files changed, 6 insertions(+) diff --git a/functions/src/domain/place/get/place/entity/GetPlaceUsecaseResponse.ts b/functions/src/domain/place/get/place/entity/GetPlaceUsecaseResponse.ts index 2dfb8d9..a436396 100644 --- a/functions/src/domain/place/get/place/entity/GetPlaceUsecaseResponse.ts +++ b/functions/src/domain/place/get/place/entity/GetPlaceUsecaseResponse.ts @@ -30,5 +30,7 @@ export type GetPlaceUsecaseResponse = { location?: LatLng, rate?: number, reportedDate?: Timestamp, + totalAmount?: number, + surchargeAmount?: number, surchargeStatus?: SurchargeStatus } \ No newline at end of file diff --git a/functions/src/domain/place/get/places/GetPlacesUsecase.ts b/functions/src/domain/place/get/places/GetPlacesUsecase.ts index ef96763..9d51df5 100644 --- a/functions/src/domain/place/get/places/GetPlacesUsecase.ts +++ b/functions/src/domain/place/get/places/GetPlacesUsecase.ts @@ -39,6 +39,7 @@ export async function GetPlacesUsecase(request: GetPlacesUsecaseRequest | string : undefined, 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, } diff --git a/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts b/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts index 92e238e..183107b 100644 --- a/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts +++ b/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts @@ -28,6 +28,9 @@ export const getPlacesWithSurchargesInterface = async (request: express.Request, longitude: place.location.longitude } : undefined, surchargeStatus: place.surchargeStatus, + totalAmount: place.totalAmount, + surchargeAmount: place.surchargeAmount, + reportedDate: place.reportedDate, surchargeRate: place.rate } }), From b19288beced8b7bbfa49c6a0ec4e3f5b77d12c78 Mon Sep 17 00:00:00 2001 From: PetruccioU Date: Wed, 29 Jan 2025 08:56:20 +1300 Subject: [PATCH 5/5] GetPlaces api updated --- .../domain/place/get/place/entity/GetPlaceUsecaseResponse.ts | 1 + functions/src/domain/place/get/places/GetPlacesUsecase.ts | 1 + .../place/get/places/getPlacesWithSurchargesInterface.ts | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/functions/src/domain/place/get/place/entity/GetPlaceUsecaseResponse.ts b/functions/src/domain/place/get/place/entity/GetPlaceUsecaseResponse.ts index a436396..66e2b97 100644 --- a/functions/src/domain/place/get/place/entity/GetPlaceUsecaseResponse.ts +++ b/functions/src/domain/place/get/place/entity/GetPlaceUsecaseResponse.ts @@ -28,6 +28,7 @@ export type GetPlaceUsecaseResponse = { displayName: displayName, addressComponents: addressComponents[], location?: LatLng, + image?: string, rate?: number, reportedDate?: Timestamp, totalAmount?: number, diff --git a/functions/src/domain/place/get/places/GetPlacesUsecase.ts b/functions/src/domain/place/get/places/GetPlacesUsecase.ts index 9d51df5..878cc0a 100644 --- a/functions/src/domain/place/get/places/GetPlacesUsecase.ts +++ b/functions/src/domain/place/get/places/GetPlacesUsecase.ts @@ -37,6 +37,7 @@ export async function GetPlacesUsecase(request: GetPlacesUsecaseRequest | string 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, diff --git a/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts b/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts index 183107b..b6d83f6 100644 --- a/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts +++ b/functions/src/interface/place/get/places/getPlacesWithSurchargesInterface.ts @@ -27,11 +27,12 @@ export const getPlacesWithSurchargesInterface = async (request: express.Request, latitude: place.location.latitude, longitude: place.location.longitude } : undefined, + image: place.image, surchargeStatus: place.surchargeStatus, totalAmount: place.totalAmount, surchargeAmount: place.surchargeAmount, reportedDate: place.reportedDate, - surchargeRate: place.rate + rate: place.rate } }), nextPageToken: places.nextPageToken