Skip to content

Commit

Permalink
Quickfix for place find endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
namark committed Sep 14, 2022
1 parent eebb400 commit 4400b55
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 126 deletions.
111 changes: 30 additions & 81 deletions vircadia_metaverse_v2_api/src/services/place/place.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,11 @@ export class Place extends DatabaseService {
if (IsNotNullOrEmpty(maturity)) {
filterQuery.maturity = maturity;
}

if (IsNotNullOrEmpty(search)) {
filterQuery.name = search;
}

if (IsNotNullOrEmpty(tag)) {
filterQuery.tag = { $in: tag };
}
Expand All @@ -333,7 +335,31 @@ export class Place extends DatabaseService {
};
}

// const places: any[] = [];
const allDomains = await this.findDataToArray(config.dbCollections.domains, {
query: {
networkAddr: { $exists: true },
$limit: 1000
}
});

const myDomains = await this.findDataToArray(config.dbCollections.domains, {
query: {
networkAddr: { $exists: true },
sponsorAccountId: loginUser.id
$limit: 1000
}
});
const myDomainIds = myDomains.filter(domain => domain.networkAddr).map(domain => domain.id);

if (!asAdmin)
{
filterQuery.$or = [
{ visibility: { $exists: false } }, // if 'visibility' is not specified, assume "OPEN"
{ visibility: Visibility.OPEN },
{ visibility: Visibility.PRIVATE, domainId: { $in: myDomainIds } }
];
}

const allPlaces = await this.findData(config.dbCollections.places, {
query: {
...filterQuery,
Expand All @@ -345,40 +371,9 @@ export class Place extends DatabaseService {
const placesData: PlaceInterface[] =
allPlaces.data as Array<PlaceInterface>;

const domainIds = (placesData as Array<PlaceInterface>)
?.map((item) => item.domainId)
.filter(
(value, index, self) =>
self.indexOf(value) === index && value !== undefined
);

var domains = await this.findDataToArray(config.dbCollections.domains, {
query: { id: { $in: domainIds } },
});
const places: any[] = [];

for await (const place of placesData) {
let DomainInterface: DomainInterface | undefined;
for await (const domain of domains) {
if (domain && domain.id === place.domainId) {
DomainInterface = domain;
if (
await this.criteriaTestAsync(
place,
domain,
asAdmin,
loginUser
)
) {
places.push(
await buildPlaceInfo(this, place, DomainInterface)
);
break;
}
// }
}
}
}
const places = await Promise.all(placesData.map(data =>
buildPlaceInfo(this, data, allDomains.find(domain => domain.id === data.domainId))
));

const data = {
places: places,
Expand All @@ -394,50 +389,4 @@ export class Place extends DatabaseService {
)
);
}

async criteriaTestAsync(
place: any,
domain: any,
asAdmin: boolean,
loginUser: any
): Promise<any> {
let ret = asAdmin;
if (!ret) {
if (domain.networkAddr) {
if (place.hasOwnProperty('visibility')) {
switch (place.visibility) {
case Visibility.OPEN:
ret = true;
break;
case Visibility.PRIVATE:
if (loginUser && place.hasOwnProperty('domainId')) {
const aDomain =
domain ??
(await this.getData(
config.dbCollections.domains,
place.domainId
));
if (aDomain) {
ret =
aDomain.sponsorAccountId ===
loginUser.id;
}
}
break;
default:
ret = false;
return;
}
} else {
// if 'visibility' is not specified, it's assumed "OPEN"
ret = true;
}
} else {
ret = false;
}
return ret;
} else {
return ret;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function (app: Application): void {
const options = {
paginate: app.get('paginate'),
id: 'id',
whitelist: ['$exists']
};

// Initialize our service with any options it requires
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,62 +70,44 @@ export class PlacesFeild extends DatabaseService {
const page = parseInt(params?.query?.page) || 1;
const skip = (page - 1) * perPage;

const myDomains = await this.findDataToArray(config.dbCollections.domains, {
query: {
sponsorAccountId: loginUser.id,
$limit: 1000
},
});

const myDomainIds = myDomains.map(domain => domain.id);

// const places: any[] = [];
const allPlaces = await this.findData(config.dbCollections.places, {
const myPlaces = await this.findData(config.dbCollections.places, {
query: {
domainId: { $in: myDomainIds },
$skip: skip,
$limit: perPage,
},
});

const placesData: PlaceInterface[] =
allPlaces.data as Array<PlaceInterface>;

const domainIds = (placesData as Array<PlaceInterface>)
?.map((item) => item.domainId)
.filter(
(value, index, self) =>
self.indexOf(value) === index && value !== undefined
);

const domains = await this.findDataToArray(
config.dbCollections.domains,
{
query: {
id: { $in: domainIds },
sponsorAccountId: loginUser.id,
},
}
);

const places: any[] = [];
const placesData: PlaceInterface[] =
myPlaces.data as Array<PlaceInterface>;

(placesData as Array<PlaceInterface>)?.forEach(async (element) => {
let DomainInterface: DomainInterface | undefined;
for (const domain of domains) {
if (domain && domain.id === element.domainId) {
DomainInterface = domain;
break;
}
}
if (DomainInterface) {
places.push(
await buildPlaceInfo(this, element, DomainInterface)
);
}
});
const places = await Promise.all(placesData.map(data =>
buildPlaceInfo(this, data, myDomains.find(domain => domain.id === data.domainId))
));

const data = {
places: places,
'maturity-categories': Maturity.MaturityCategories,
};

return Promise.resolve(
buildPaginationResponse(
data,
page,
perPage,
Math.ceil(allPlaces.total / perPage),
allPlaces.total
Math.ceil(myPlaces.total / perPage),
myPlaces.total
)
);
}
Expand Down Expand Up @@ -290,17 +272,17 @@ export class PlacesFeild extends DatabaseService {
);
return Promise.resolve(buildSimpleResponse(place));
} else {
throw new BadRequest("Invalid place name");
}
throw new BadRequest("Invalid place name");
}
} else {
throw new NotAuthenticated(messages.common_messages_unauthorized);
}
throw new NotAuthenticated(messages.common_messages_unauthorized);
}
} else {
throw new BadRequest(messages.common_domainId_notFound);
}
throw new BadRequest(messages.common_domainId_notFound);
}
} else {
throw new BadRequest(messages.common_messages_parameter_missing);
}
throw new BadRequest(messages.common_messages_parameter_missing);
}
} else {
throw new NotAuthenticated(messages.common_messages_unauthorized);
}
Expand Down

0 comments on commit 4400b55

Please sign in to comment.