Skip to content

Commit a447920

Browse files
committed
fix: nearby still triggered if old location (e.g. 3 weeks ago)
1 parent ab6b9ce commit a447920

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

backend/src/entities/user/user.repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
EIntention,
88
EVerificationStatus,
99
} from "@/types/user.types";
10+
import { getNearbyMaxLocationAge } from "@/utils/date.utils";
1011
import { getTypedCoordinatesFromPoint } from "@/utils/location.utils";
1112
import { getAgeRangeParsed } from "@/utils/misc.utils";
1213
import { Injectable, Logger } from "@nestjs/common";
@@ -197,13 +198,12 @@ export class UserRepository extends Repository<User> {
197198
}
198199

199200
private withRecentLocationsOnly(): this {
200-
const h24HoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
201201
this.queryBuilder.andWhere(
202202
`
203203
user.locationLastTimeUpdated IS NOT NULL
204204
AND user.locationLastTimeUpdated >= :h24HoursAgo
205205
`,
206-
{ h24HoursAgo },
206+
{ h24HoursAgo: getNearbyMaxLocationAge() },
207207
);
208208
return this;
209209
}

backend/src/entities/user/user.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
ELanguage,
2525
EVerificationStatus,
2626
} from "@/types/user.types";
27+
import { getNearbyMaxLocationAge } from "@/utils/date.utils";
2728
import {
2829
API_VERSION,
2930
BE_ENDPOINT,
@@ -751,6 +752,13 @@ export class UserService {
751752
)
752753
.andWhere("user.location IS NOT NULL")
753754
.andWhere("user.isActive = :isActive", { isActive: true })
755+
.andWhere(
756+
`
757+
user.locationLastTimeUpdated IS NOT NULL
758+
AND user.locationLastTimeUpdated >= :h24HoursAgo
759+
`,
760+
{ h24HoursAgo: getNearbyMaxLocationAge() },
761+
)
754762
.getRawMany();
755763

756764
return result.map((row) => row.user_id);

backend/src/utils/date.utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { ELanguage } from "@/types/user.types";
22

3+
/** @dev Get current dateTime - 24h right now to define users as nearby right now */
4+
export const getNearbyMaxLocationAge = () =>
5+
new Date(Date.now() - 24 * 60 * 60 * 1000);
6+
37
export const getAge = (birthday: Date | string): number => {
48
const birthdayDate = new Date(birthday);
59
const today = new Date();

backend/test/integration/services/user.service.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { goBackInTimeFor } from "@/cronjobs/cronjobs.types";
12
import { AppStatistic } from "@/entities/app-stats/app-stat.entity";
23
import { EAPP_STAT_KEY } from "@/entities/app-stats/app-stats.types";
34
import { Encounter } from "@/entities/encounter/encounter.entity";
@@ -462,6 +463,75 @@ describe("UserService", () => {
462463
}, 10000);
463464
});
464465

466+
describe("is nearby logic", () => {
467+
it("should only define a user as nearby if the lastLocationDateTimeUpdated is not older than 24h", async () => {
468+
const user1 = await userFactory.persistNewTestUser({
469+
dateMode: EDateMode.LIVE,
470+
location: new PointBuilder().build(600, 600),
471+
locationLastTimeUpdated: goBackInTimeFor(23, "hours"),
472+
gender: EGender.WOMAN,
473+
genderDesire: [EGender.MAN],
474+
intentions: [EIntention.RELATIONSHIP],
475+
approachChoice: EApproachChoice.BOTH,
476+
});
477+
const user2 = await userFactory.persistNewTestUser({
478+
dateMode: EDateMode.LIVE,
479+
location: new PointBuilder().build(0, 0),
480+
locationLastTimeUpdated: goBackInTimeFor(23, "hours"),
481+
gender: EGender.WOMAN,
482+
genderDesire: [EGender.MAN],
483+
intentions: [EIntention.RELATIONSHIP],
484+
approachChoice: EApproachChoice.BOTH,
485+
});
486+
const user3 = await userFactory.persistNewTestUser({
487+
dateMode: EDateMode.LIVE,
488+
location: new PointBuilder().build(0, 0),
489+
locationLastTimeUpdated: goBackInTimeFor(25, "hours"),
490+
gender: EGender.WOMAN,
491+
genderDesire: [EGender.MAN],
492+
intentions: [EIntention.RELATIONSHIP],
493+
approachChoice: EApproachChoice.BOTH,
494+
});
495+
const user4 = await userFactory.persistNewTestUser({
496+
dateMode: EDateMode.LIVE,
497+
location: new PointBuilder().build(0, 0),
498+
locationLastTimeUpdated: goBackInTimeFor(23, "hours"),
499+
isActive: false,
500+
gender: EGender.WOMAN,
501+
genderDesire: [EGender.MAN],
502+
intentions: [EIntention.RELATIONSHIP],
503+
approachChoice: EApproachChoice.BOTH,
504+
});
505+
const user5 = await userFactory.persistNewTestUser({
506+
dateMode: EDateMode.LIVE,
507+
location: null,
508+
locationLastTimeUpdated: goBackInTimeFor(23, "hours"),
509+
gender: EGender.WOMAN,
510+
genderDesire: [EGender.MAN],
511+
intentions: [EIntention.RELATIONSHIP],
512+
approachChoice: EApproachChoice.BOTH,
513+
});
514+
515+
const userIds = [user1.id, user2.id, user3.id, user4.id, user5.id];
516+
expect(userIds.length).toEqual(
517+
(await userService.findAll()).length,
518+
);
519+
520+
const nearbyUserIds = await userService.findUsersNearbyByUserIds(
521+
userIds,
522+
new PointBuilder().build(0, 0),
523+
1500,
524+
);
525+
expect(nearbyUserIds.find((id) => id === user1.id)).toBeUndefined(); // @dev location wise not nearby
526+
expect(nearbyUserIds.find((id) => id === user2.id)).toEqual(
527+
user2.id,
528+
); // @dev location and time wise nearby
529+
expect(nearbyUserIds.find((id) => id === user3.id)).toBeUndefined(); // @dev location wise nearby but location too old
530+
expect(nearbyUserIds.find((id) => id === user4.id)).toBeUndefined(); // @dev nearby but account inactive
531+
expect(nearbyUserIds.find((id) => id === user5.id)).toBeUndefined(); // @dev nearby but no location available
532+
});
533+
});
534+
465535
describe("encounter lookup", function () {
466536
it("should not find matches if 2 people already met", async () => {
467537
const mainUser = await userFactory.persistNewTestUser({

0 commit comments

Comments
 (0)