From 40a3a4f6eff0ea5d0a65a9163095ddbe25173a18 Mon Sep 17 00:00:00 2001 From: Robert Adams Date: Thu, 4 Feb 2021 14:30:51 -0800 Subject: [PATCH] Add optional domain parameter to get attendance functions to eliminate refetch of domain. --- src/Entities/Places.ts | 14 +++++++++----- src/route-tools/Util.ts | 6 +++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Entities/Places.ts b/src/Entities/Places.ts index ee92fc50..52cdd787 100755 --- a/src/Entities/Places.ts +++ b/src/Entities/Places.ts @@ -35,6 +35,7 @@ import { createObject, getObject, getObjects, updateObjectFields, deleteOne, del import { GenUUID, IsNullOrEmpty, IsNotNullOrEmpty, genRandomString } from '@Tools/Misc'; import { VKeyedCollection } from '@Tools/vTypes'; import { Logger } from '@Tools/Logging'; +import { DomainEntity } from './DomainEntity'; export let placeCollection = 'places'; @@ -119,7 +120,7 @@ export const Places = { return updateObjectFields(placeCollection, new GenericFilter({ 'id': pEntity.id }), pFields); }, - async getCurrentAttendance(pPlace: PlaceEntity): Promise { + async getCurrentAttendance(pPlace: PlaceEntity, pDomain?: DomainEntity): Promise { // Attendance is either reported by a beacon script or defaults to the domain's numbers // If the last current update is stale (older than a few minutes), the domain's number is used let attendance: number = 0; @@ -132,12 +133,15 @@ export const Places = { }; if (useDomain) { // There isn't current attendance info. Default to domain's numbers - if (IsNullOrEmpty(pPlace.domainId)) { - const aDomain = await Domains.getDomainWithId(pPlace.domainId); - if (IsNotNullOrEmpty(aDomain)) { - attendance = (aDomain.numUsers ?? 0) + (aDomain.anonUsers ?? 0); + let aDomain = pDomain; + if (IsNullOrEmpty(aDomain)) { + if (IsNotNullOrEmpty(pPlace.domainId)) { + aDomain = await Domains.getDomainWithId(pPlace.domainId); }; }; + if (IsNotNullOrEmpty(aDomain)) { + attendance = (aDomain.numUsers ?? 0) + (aDomain.anonUsers ?? 0); + }; }; return attendance; diff --git a/src/route-tools/Util.ts b/src/route-tools/Util.ts index 09cb1774..97efa7a8 100755 --- a/src/route-tools/Util.ts +++ b/src/route-tools/Util.ts @@ -251,7 +251,7 @@ export async function buildAccountProfile(pReq: Request, pAccount: AccountEntity // Return an object with the formatted place information // Pass the PlaceEntity and the place's domain if known. export async function buildPlaceInfo(pPlace: PlaceEntity, pDomain?: DomainEntity): Promise { - const ret = await buildPlaceInfoSmall(pPlace); + const ret = await buildPlaceInfoSmall(pPlace, pDomain); // if the place points to a domain, add that information also if (IsNotNullOrEmpty(pPlace.domainId)) { @@ -263,7 +263,7 @@ export async function buildPlaceInfo(pPlace: PlaceEntity, pDomain?: DomainEntity return ret; }; // Return the basic information block for a Place -export async function buildPlaceInfoSmall(pPlace: PlaceEntity): Promise { +export async function buildPlaceInfoSmall(pPlace: PlaceEntity, pDomain?: DomainEntity): Promise { const ret: VKeyedCollection = { 'placeId': pPlace.id, 'id': pPlace.id, @@ -275,7 +275,7 @@ export async function buildPlaceInfoSmall(pPlace: PlaceEntity): Promise { 'tags': pPlace.tags, 'thumbnail': pPlace.thumbnail, 'images': pPlace.images, - 'current_attendance': await Places.getCurrentAttendance(pPlace), + 'current_attendance': await Places.getCurrentAttendance(pPlace, pDomain), 'current_images': pPlace.currentImages, 'current_info': pPlace.currentInfo, 'current_last_update_time': pPlace.currentLastUpdateTime