Skip to content

Commit 81c0949

Browse files
authored
50 overload getplacesusecase getplacesrepository (#55)
* GetPlacesUsecase updated, also PlaceDTO and Place type * GetPlacesRepository updated along with GetPlacesRepositoryResponse and PostPlaceRepositoryRequest * getPlacesWithSurchargesInterface crated, introduced into index and exported * totalAmount, surchargeAmount and reportedDate added to getPlacesWithSurchargesInterface * GetPlaces api updated
1 parent 4d8e788 commit 81c0949

File tree

10 files changed

+156
-55
lines changed

10 files changed

+156
-55
lines changed

functions/src/data/place/get/place/DTO/PlaceDTO.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ export type PlaceDTO = {
66
id: string,
77
displayName: LocalizedTextDTO,
88
addressComponents: AddressComponentsDTO[],
9-
location: LatLngDTO
9+
location: LatLngDTO | null
1010
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PlaceDTO } from "../../place/DTO/PlaceDTO";
22

33
export type GetPlacesRepositoryResponse = {
4-
places: PlaceDTO[],
4+
places: PlaceDTO[] | [],
55
nextPageToken?: string
66
}

functions/src/data/place/get/places/GetPlacesRepository.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { AddressComponentsDTO } from "../place/DTO/AddressComponentsDTO"
33
import { GetPlacesRepositoryResponse } from "./DTO/GetPlacesRepositoryResponse"
44
import { GetPlacesRepositoryRequest } from "./DTO/GetPlacesRepositoryRequest"
55
import { locationRestrictionOfNZ } from "@shared/constants"
6-
6+
import { database } from "@data/firebase"
77

88
export function GetPlacesRepository(request: GetPlacesRepositoryRequest): Promise<GetPlacesRepositoryResponse>
9-
export function GetPlacesRepository(placesId: string[]): Promise<GetPlacesRepositoryResponse>
9+
export function GetPlacesRepository(placesIds: string[]): Promise<GetPlacesRepositoryResponse>
1010

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

20-
async function _GetPlacesFromFirestore(placeIds: string[]): Promise<GetPlacesRepositoryResponse> {
21-
22-
/*
23-
Here you have to acess to Firestore and get the places by placeIds.
24-
Return nextPageToken as undefined.
25-
*/
26-
27-
return {
28-
places: [],
20+
async function _GetPlacesFromFirestore(
21+
placesIds: string[]
22+
): Promise<GetPlacesRepositoryResponse> {
23+
try {
24+
const placesSnapshot = await database
25+
.collection('places')
26+
.where("id", "in", placesIds)
27+
.get();
28+
29+
if (placesSnapshot.empty) {
30+
console.log("Places data are undefined for the given places IDs.");
31+
return { places: [], nextPageToken: undefined };
32+
} else {
33+
const matchedPlaces = placesSnapshot.docs.map((doc) => {
34+
const data = doc.data() as PlaceDTO;
35+
return {
36+
id: data.id,
37+
displayName: {
38+
text: data.displayName.text,
39+
languageCode: data.displayName.languageCode,
40+
},
41+
addressComponents: data.addressComponents.map((component: AddressComponentsDTO) => {
42+
return {
43+
longText: component.longText,
44+
shortText: component.shortText,
45+
types: component.types,
46+
languageCode: data.displayName.languageCode,
47+
}
48+
}),
49+
location: null,
50+
};
51+
});
52+
53+
return { places: matchedPlaces, nextPageToken: undefined };
54+
}
55+
} catch (error) {
56+
console.error("Error fetching places:", error);
57+
throw error;
2958
}
3059
}
3160

functions/src/data/place/post/place/DTO/PostPlaceRepositoryRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ export interface PostPlaceRepositoryRequest {
1919
id: string;
2020
displayName: displayName,
2121
addressComponents: AddressComponentsDTO[],
22-
location: LatLngDTO
22+
location: LatLngDTO | null
2323
}

functions/src/domain/place/get/place/entity/GetPlaceUsecaseResponse.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export type GetPlaceUsecaseResponse = {
2828
displayName: displayName,
2929
addressComponents: addressComponents[],
3030
location?: LatLng,
31+
image?: string,
3132
rate?: number,
3233
reportedDate?: Timestamp,
34+
totalAmount?: number,
35+
surchargeAmount?: number,
3336
surchargeStatus?: SurchargeStatus
3437
}

functions/src/domain/place/get/places/GetPlacesUsecase.ts

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,79 @@ import { GetPlacesUsecaseResponse } from "./entity/GetPlacesUsecaseResponse"
44
import { GetPlacesRepository } from "@data/place"
55
import { locationRestrictionOfNZ } from "@shared/constants"
66

7-
export function GetPlacesUsecase(request: GetPlacesUsecaseRequest): Promise<GetPlacesUsecaseResponse>
8-
export function GetPlacesUsecase(placeIds: string[]): Promise<GetPlacesUsecaseResponse>
9-
10-
export async function GetPlacesUsecase(param: GetPlacesUsecaseRequest | string[]): Promise<GetPlacesUsecaseResponse> {
7+
export async function GetPlacesUsecase(request: GetPlacesUsecaseRequest | string[]): Promise<GetPlacesUsecaseResponse> {
8+
let resultPlaces;
9+
10+
if (Array.isArray(request)) {
11+
12+
try {
13+
const allSurcharges = await GetSurchargesRepository({})
14+
const allSurchargesIds = allSurcharges.map((surcharge) => {
15+
return surcharge.id
16+
})
17+
resultPlaces = await GetPlacesRepository(allSurchargesIds)
18+
19+
// const resultSurcharges = await GetSurchargesRepository(resultPlaceIds)
20+
21+
const placesWithSurcharges = resultPlaces.places.map((place) => {
22+
return {
23+
id: place.id,
24+
displayName: {
25+
text: place.displayName.text,
26+
languageCode: place.displayName.languageCode,
27+
},
28+
addressComponents: place.addressComponents.map((component) => ({
29+
longText: component.longText,
30+
shortText: component.shortText,
31+
types: component.types,
32+
languageCode: component.languageCode,
33+
})),
34+
location: place.location
35+
? {
36+
latitude: place.location.latitude,
37+
longitude: place.location.longitude,
38+
}
39+
: undefined,
40+
image: allSurcharges.find((surcharge) => surcharge.id === place.id)?.image,
41+
rate: allSurcharges.find((surcharge) => surcharge.id === place.id)?.rate,
42+
totalAmount: allSurcharges.find((surcharge) => surcharge.id === place.id)?.totalAmount,
43+
reportedDate: allSurcharges.find((surcharge) => surcharge.id === place.id)?.reportedDate,
44+
surchargeAmount: allSurcharges.find((surcharge) => surcharge.id === place.id)?.surchargeAmount,
45+
surchargeStatus: allSurcharges.find((surcharge) => surcharge.id === place.id)?.surchargeStatus as SurchargeStatus,
46+
}
47+
})
48+
49+
return {
50+
places: placesWithSurcharges,
51+
nextPageToken: undefined,
52+
}
53+
54+
} catch (error) {
55+
throw error
56+
}
1157

12-
if (Array.isArray(param)) {
13-
return await _GetPlacesByPlaceId(param)
1458
} else {
15-
return await _GetPlacesByRequest(param)
16-
}
1759

18-
}
19-
20-
async function _GetPlacesByPlaceId(placeId: string[]): Promise<GetPlacesUsecaseResponse> {
21-
22-
/*
23-
I think you can start here, and of course you need a repository for getting places by placeId
24-
I made a snippet for you, and it can be found in GetPlacesRepository.ts
25-
Return nextPageToken as undefined.
26-
*/
27-
28-
return {
29-
places: [],
30-
nextPageToken: undefined,
31-
}
32-
}
33-
34-
async function _GetPlacesByRequest(request: GetPlacesUsecaseRequest): Promise<GetPlacesUsecaseResponse> {
35-
if (request.userLocation) {
36-
if (!isPointInRectangle(request.userLocation, locationRestrictionOfNZ)) {
37-
throw new Error("User location is out of New Zealand")
60+
if (request.userLocation) {
61+
if (!isPointInRectangle(request.userLocation, locationRestrictionOfNZ)) {
62+
throw new Error("User location is out of New Zealand")
63+
}
3864
}
65+
resultPlaces = await GetPlacesRepository(
66+
{
67+
searchText: request.searchText,
68+
nextPageToken: request.nextPageToken,
69+
userLocation: request.userLocation,
70+
}
71+
)
3972
}
4073

41-
const resultPlaces = await GetPlacesRepository(
42-
{
43-
searchText: request.searchText,
44-
nextPageToken: request.nextPageToken,
45-
userLocation: request.userLocation,
46-
}
47-
)
48-
4974
const resultPlaceIds = resultPlaces.places.map((place) => {
5075
return place.id
5176
})
5277

5378
try {
54-
5579
const resultSurcharges = await GetSurchargesRepository(resultPlaceIds)
56-
5780
const placesWithSurcharges = resultPlaces.places.map((place) => {
5881
return {
5982
id: place.id,

functions/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require('module-alias/register')
22

33
import express from "express";
44
import { onRequest } from "firebase-functions/v2/https";
5-
import { getPlaceInterface, getPlacesInterface } from "@interface/place";
5+
import { getPlaceInterface, getPlacesInterface, getPlacesWithSurchargesInterface } from "@interface/place";
66
import { getSurchargeInterface, postSurchargeInterface, getSurchargesInterface, putSurchargeInterface } from "@interface/surcharge";
77
import { getImageInterface } from "@interface/image";
88
import { AdminAuth } from "@shared/authentication";
@@ -36,6 +36,7 @@ admin.use(AdminAuth)
3636
admin.get("/surcharges", getSurchargesInterface)
3737
admin.put("/surcharge", putSurchargeInterface)
3838
admin.get("/image", getImageInterface)
39+
admin.get("/places", getPlacesWithSurchargesInterface);
3940
exports.admin = onRequest(admin)
4041

4142
// For Mobile APIs
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import express from "express"
2+
import { GetPlacesUsecase } from "@domain/place"
3+
import { Response } from "./model/GetPlacesInterfaceResponse"
4+
5+
export const getPlacesWithSurchargesInterface = async (request: express.Request, response: Response) => {
6+
7+
try {
8+
const places = await GetPlacesUsecase([])
9+
10+
response.status(200).send({
11+
places: places.places.map((place) => {
12+
return {
13+
id: place.id,
14+
displayName: {
15+
text: place.displayName.text,
16+
languageCode: place.displayName.languageCode
17+
},
18+
addressComponents: place.addressComponents.map((component) => {
19+
return {
20+
longText: component.longText,
21+
shortText: component.shortText,
22+
types: component.types,
23+
languageCode: component.languageCode
24+
}
25+
}),
26+
location: place.location ? {
27+
latitude: place.location.latitude,
28+
longitude: place.location.longitude
29+
} : undefined,
30+
image: place.image,
31+
surchargeStatus: place.surchargeStatus,
32+
totalAmount: place.totalAmount,
33+
surchargeAmount: place.surchargeAmount,
34+
reportedDate: place.reportedDate,
35+
rate: place.rate
36+
}
37+
}),
38+
nextPageToken: places.nextPageToken
39+
})
40+
41+
} catch (error: unknown) {
42+
response.status(500).send({ message: error })
43+
}
44+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { getPlaceInterface } from "./get/place/getPlaceInterface"
2-
export { getPlacesInterface } from "./get/places/getPlacesInterface"
2+
export { getPlacesInterface } from "./get/places/getPlacesInterface"
3+
export { getPlacesWithSurchargesInterface } from "./get/places/getPlacesWithSurchargesInterface"

functions/src/shared/types/place/Place.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ export type Place = {
66
id: string,
77
displayName: LocalizedText,
88
addressComponents: AddressComponents[],
9-
location?: LatLng
9+
location?: LatLng | null
1010
}

0 commit comments

Comments
 (0)