From e90380e2b46802296c955e6f4c70d3594a28383c Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 30 Nov 2026 12:55:07 +0100 Subject: [PATCH] fix(places): votes copied with a place keep when they were last changed Saving a trip place into a list and copying a saved place into a trip carried the votes with no change time, so each copy read as cast now and could outrank a newer vote on another copy of the same place, moving the entry's own vote, average, filter and sort. Both copies now carry the source vote's time, as the vote carry-over already does. --- .../CollectionPlaceRatings.repository.ts | 9 ++++-- .../repositories/PlaceRatings.repository.ts | 4 +-- .../nest/collections/collections.service.ts | 6 ++-- server/tests/e2e/place-votes.e2e.test.ts | 30 +++++++++++++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/server/src/db/repositories/CollectionPlaceRatings.repository.ts b/server/src/db/repositories/CollectionPlaceRatings.repository.ts index aac04b52e..91ec3c2a3 100644 --- a/server/src/db/repositories/CollectionPlaceRatings.repository.ts +++ b/server/src/db/repositories/CollectionPlaceRatings.repository.ts @@ -71,14 +71,17 @@ export class CollectionPlaceRatingsRepository extends TrekRepository { + async listForPlace( + collectionPlaceId: number, + ): Promise<{ user_id: number; rating: number; changed_at: string | null }[]> { return await this.kysely>() .selectFrom('collection_place_ratings') - .select(['user_id', 'rating']) + .select(['user_id', 'rating', (eb) => eb.fn.coalesce('updated_at', 'created_at').as('changed_at')]) .where('collection_place_id', '=', collectionPlaceId) .execute(); } diff --git a/server/src/db/repositories/PlaceRatings.repository.ts b/server/src/db/repositories/PlaceRatings.repository.ts index 5b1013253..dc4f16d5b 100644 --- a/server/src/db/repositories/PlaceRatings.repository.ts +++ b/server/src/db/repositories/PlaceRatings.repository.ts @@ -95,10 +95,10 @@ export class PlaceRatingsRepository extends TrekRepository { * ledgers; `listForPlaces`'s own docstring above documents the joined * variant of the same finding). */ - async listVotesForPlace(place_id: number): Promise<{ user_id: number; rating: number }[]> { + async listVotesForPlace(place_id: number): Promise<{ user_id: number; rating: number; changed_at: string | null }[]> { return await this.kysely>() .selectFrom('place_ratings') - .select(['user_id', 'rating']) + .select(['user_id', 'rating', (eb) => eb.fn.coalesce('updated_at', 'created_at').as('changed_at')]) .where('place_id', '=', place_id) .execute(); } diff --git a/server/src/nest/collections/collections.service.ts b/server/src/nest/collections/collections.service.ts index e306dd531..95acaca9e 100644 --- a/server/src/nest/collections/collections.service.ts +++ b/server/src/nest/collections/collections.service.ts @@ -679,7 +679,7 @@ export class CollectionsService { const eligible = await this.collectionMemberIds(collectionId); const rows = await this.tripPlaceRatings.listVotesForPlace(sourcePlaceId); for (const r of rows) { - if (eligible.has(r.user_id)) await this.collectionPlaceRatings.insertIgnoreRating(collectionPlaceId, r.user_id, r.rating); + if (eligible.has(r.user_id)) await this.collectionPlaceRatings.insertIgnoreRating(collectionPlaceId, r.user_id, r.rating, r.changed_at); } } @@ -1076,9 +1076,9 @@ export class CollectionsService { const tagIds = await tripTagIdsOf(carried?.[s.id] ?? [], tripTags, async (name, color) => (await this.tags.createTag({ user_id: userId, trip_id: body.trip_id, name, color })).id); await this.tags.insertIgnore(newPlaceId, tagIds); - // Ratings of trip members travel along (#1435); nothing is mirrored back. + // Ratings of trip members travel along (#1435) with their age; nothing is mirrored back. const votes = await this.collectionPlaceRatings.listForPlace(s.id); - for (const v of votes) if (tripMemberIds.has(v.user_id)) await this.tripPlaceRatings.insertIgnore(newPlaceId, v.user_id, v.rating); + for (const v of votes) if (tripMemberIds.has(v.user_id)) await this.tripPlaceRatings.insertIgnore(newPlaceId, v.user_id, v.rating, v.changed_at); trackInsertedInDedupSet(s, dedup); created.push({ id: newPlaceId, source_id: s.id }); } diff --git a/server/tests/e2e/place-votes.e2e.test.ts b/server/tests/e2e/place-votes.e2e.test.ts index 829dda061..f69604c45 100644 --- a/server/tests/e2e/place-votes.e2e.test.ts +++ b/server/tests/e2e/place-votes.e2e.test.ts @@ -258,6 +258,36 @@ describe('a list or trip shows only its own members their votes', () => { expect(tripVote?.rating).toBe(4); expect(tripVote?.updated_at).not.toBeNull(); }); + + it('a vote copied along with a place keeps when it was last changed, into a list and back into a trip', async () => { + const s = await seed(); + const changed = '2025-06-01 10:00:00'; + await insertRow(orm, PlaceRatings, { + place: s.cafe.id, + user: s.me.id, + rating: 4, + created_at: '2025-05-01 10:00:00', + updated_at: changed, + }); + const coffee = await makeCollection(orm, s.me.id, { name: 'Coffee' }); + const saved = await request(server) + .post(`${COLLECTIONS}/places/from-trip`) + .set(as(s.me.id)) + .send({ collection_id: coffee.id, source_trip_id: s.alps.id, source_place_id: s.cafe.id }); + expect(saved.status).toBe(200); + const [listVote] = await findRows(orm, CollectionPlaceRatings, { collectionPlace: saved.body.place.id }); + expect(listVote).toMatchObject({ rating: 4, updated_at: changed }); + + const coast = await makeTrip(orm, s.me.id, { title: 'Coast' }); + const copied = await request(server) + .post(`${COLLECTIONS}/copy-to-trip`) + .set(as(s.me.id)) + .send({ trip_id: coast.id, place_ids: [saved.body.place.id] }); + expect(copied.status).toBe(200); + const onCoast = (await findRows(orm, PlaceRatings, { user: s.me.id })).filter((v) => v.place_id !== s.cafe.id); + expect(onCoast).toHaveLength(1); + expect(onCoast[0]).toMatchObject({ rating: 4, updated_at: changed }); + }); }); describe('the vote by entry key', () => {