diff --git a/server/src/queries/asset.repository.sql b/server/src/queries/asset.repository.sql index ce53bd1791..c0b778bb50 100644 --- a/server/src/queries/asset.repository.sql +++ b/server/src/queries/asset.repository.sql @@ -55,9 +55,10 @@ with inner join "exif" on "a"."id" = "exif"."assetId" ) select - ( - (now() at time zone 'UTC')::date - ("localDateTime" at time zone 'UTC')::date - ) / 365 as "yearsAgo", + date_part( + 'year', + ("localDateTime" at time zone 'UTC')::date + )::int as "year", json_agg("res") as "assets" from "res" diff --git a/server/src/repositories/asset.repository.ts b/server/src/repositories/asset.repository.ts index daefacef09..91597ed720 100644 --- a/server/src/repositories/asset.repository.ts +++ b/server/src/repositories/asset.repository.ts @@ -192,7 +192,7 @@ export class AssetRepository { } @GenerateSql({ params: [DummyValue.UUID, { day: 1, month: 1 }] }) - getByDayOfYear(ownerIds: string[], { day, month }: MonthDay): Promise { + getByDayOfYear(ownerIds: string[], { day, month }: MonthDay) { return this.db .with('res', (qb) => qb @@ -239,16 +239,12 @@ export class AssetRepository { .select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo')), ) .selectFrom('res') - .select( - sql`((now() at time zone 'UTC')::date - ("localDateTime" at time zone 'UTC')::date) / 365`.as( - 'yearsAgo', - ), - ) + .select(sql`date_part('year', ("localDateTime" at time zone 'UTC')::date)::int`.as('year')) .select((eb) => eb.fn.jsonAgg(eb.table('res')).as('assets')) .groupBy(sql`("localDateTime" at time zone 'UTC')::date`) .orderBy(sql`("localDateTime" at time zone 'UTC')::date`, 'desc') .limit(10) - .execute() as any as Promise; + .execute(); } @GenerateSql({ params: [[DummyValue.UUID]] }) diff --git a/server/src/services/asset.service.spec.ts b/server/src/services/asset.service.spec.ts index 336c3ac8f0..f91f600bb1 100755 --- a/server/src/services/asset.service.spec.ts +++ b/server/src/services/asset.service.spec.ts @@ -64,18 +64,18 @@ describe(AssetService.name, () => { mocks.partner.getAll.mockResolvedValue([]); mocks.asset.getByDayOfYear.mockResolvedValue([ { - yearsAgo: 1, + year: 2023, assets: [image1, image2], }, { - yearsAgo: 9, + year: 2015, assets: [image3], }, { - yearsAgo: 15, + year: 2009, assets: [image4], }, - ]); + ] as any); await expect(sut.getMemoryLane(authStub.admin, { day: 15, month: 1 })).resolves.toEqual([ { yearsAgo: 1, title: '1 year ago', assets: [mapAsset(image1), mapAsset(image2)] }, diff --git a/server/src/services/asset.service.ts b/server/src/services/asset.service.ts index a9b723c9f9..df66d405b7 100644 --- a/server/src/services/asset.service.ts +++ b/server/src/services/asset.service.ts @@ -38,12 +38,15 @@ export class AssetService extends BaseService { const userIds = [auth.user.id, ...partnerIds]; const groups = await this.assetRepository.getByDayOfYear(userIds, dto); - return groups.map(({ yearsAgo, assets }) => ({ - yearsAgo, - // TODO move this to clients - title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} ago`, - assets: assets.map((asset) => mapAsset(asset, { auth })), - })); + return groups.map(({ year, assets }) => { + const yearsAgo = DateTime.utc().year - year; + return { + yearsAgo, + // TODO move this to clients + title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} ago`, + assets: assets.map((asset) => mapAsset(asset as AssetEntity, { auth })), + }; + }); } async getStatistics(auth: AuthDto, dto: AssetStatsDto) { diff --git a/server/src/services/memory.service.ts b/server/src/services/memory.service.ts index be4d6dfc76..8a46b289c3 100644 --- a/server/src/services/memory.service.ts +++ b/server/src/services/memory.service.ts @@ -45,18 +45,18 @@ export class MemoryService extends BaseService { for (const [userId, userIds] of Object.entries(userMap)) { const memories = await this.assetRepository.getByDayOfYear(userIds, target); - for (const memory of memories) { - const data: OnThisDayData = { year: target.year - memory.yearsAgo }; + for (const { year, assets } of memories) { + const data: OnThisDayData = { year }; await this.memoryRepository.create( { ownerId: userId, type: MemoryType.ON_THIS_DAY, data, - memoryAt: target.minus({ years: memory.yearsAgo }).toISO(), + memoryAt: target.set({ year }).toISO(), showAt, hideAt, }, - new Set(memory.assets.map(({ id }) => id)), + new Set(assets.map(({ id }) => id)), ); } }