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.
This commit is contained in:
Maurice
2026-11-30 12:55:07 +01:00
parent 09ae971cc8
commit e90380e2b4
4 changed files with 41 additions and 8 deletions
@@ -71,14 +71,17 @@ export class CollectionPlaceRatingsRepository extends TrekRepository<CollectionP
/**
* CL68 (`copyToTrip`, loop, TX) — `SELECT user_id, rating FROM
* collection_place_ratings WHERE collection_place_id=?`. `user_id` is a
* collection_place_ratings WHERE collection_place_id=?`, with each vote's
* `changed_at` (last change, else cast) so a copy keeps its age. `user_id` is a
* `persist(false)` relation mirror — Kysely, the program-wide "bare
* mirror column silently dropped" trap (3d/3e ledgers).
*/
async listForPlace(collectionPlaceId: number): Promise<{ user_id: number; rating: number }[]> {
async listForPlace(
collectionPlaceId: number,
): Promise<{ user_id: number; rating: number; changed_at: string | null }[]> {
return await this.kysely<Pick<DB, 'collection_place_ratings'>>()
.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();
}
@@ -95,10 +95,10 @@ export class PlaceRatingsRepository extends TrekRepository<PlaceRatings> {
* 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<Pick<DB, 'place_ratings'>>()
.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();
}
@@ -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 });
}
+30
View File
@@ -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', () => {