Skip to content

Commit dd987d7

Browse files
committed
fix: memories off by one
1 parent 5c0538e commit dd987d7

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

server/src/queries/asset.repository.sql

+4-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ with
5555
inner join "exif" on "a"."id" = "exif"."assetId"
5656
)
5757
select
58-
(
59-
(now() at time zone 'UTC')::date - ("localDateTime" at time zone 'UTC')::date
60-
) / 365 as "yearsAgo",
58+
date_part(
59+
'year',
60+
("localDateTime" at time zone 'UTC')::date
61+
)::int as "year",
6162
json_agg("res") as "assets"
6263
from
6364
"res"

server/src/repositories/asset.repository.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class AssetRepository {
192192
}
193193

194194
@GenerateSql({ params: [DummyValue.UUID, { day: 1, month: 1 }] })
195-
getByDayOfYear(ownerIds: string[], { day, month }: MonthDay): Promise<DayOfYearAssets[]> {
195+
getByDayOfYear(ownerIds: string[], { day, month }: MonthDay) {
196196
return this.db
197197
.with('res', (qb) =>
198198
qb
@@ -239,16 +239,12 @@ export class AssetRepository {
239239
.select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo')),
240240
)
241241
.selectFrom('res')
242-
.select(
243-
sql<number>`((now() at time zone 'UTC')::date - ("localDateTime" at time zone 'UTC')::date) / 365`.as(
244-
'yearsAgo',
245-
),
246-
)
242+
.select(sql<number>`date_part('year', ("localDateTime" at time zone 'UTC')::date)::int`.as('year'))
247243
.select((eb) => eb.fn.jsonAgg(eb.table('res')).as('assets'))
248244
.groupBy(sql`("localDateTime" at time zone 'UTC')::date`)
249245
.orderBy(sql`("localDateTime" at time zone 'UTC')::date`, 'desc')
250246
.limit(10)
251-
.execute() as any as Promise<DayOfYearAssets[]>;
247+
.execute();
252248
}
253249

254250
@GenerateSql({ params: [[DummyValue.UUID]] })

server/src/services/asset.service.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ export class AssetService extends BaseService {
3838
const userIds = [auth.user.id, ...partnerIds];
3939

4040
const groups = await this.assetRepository.getByDayOfYear(userIds, dto);
41-
return groups.map(({ yearsAgo, assets }) => ({
42-
yearsAgo,
43-
// TODO move this to clients
44-
title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} ago`,
45-
assets: assets.map((asset) => mapAsset(asset, { auth })),
46-
}));
41+
return groups.map(({ year, assets }) => {
42+
const yearsAgo = DateTime.utc().year - year;
43+
return {
44+
yearsAgo,
45+
// TODO move this to clients
46+
title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} ago`,
47+
assets: assets.map((asset) => mapAsset(asset as AssetEntity, { auth })),
48+
};
49+
});
4750
}
4851

4952
async getStatistics(auth: AuthDto, dto: AssetStatsDto) {

server/src/services/memory.service.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ export class MemoryService extends BaseService {
4545
for (const [userId, userIds] of Object.entries(userMap)) {
4646
const memories = await this.assetRepository.getByDayOfYear(userIds, target);
4747

48-
for (const memory of memories) {
49-
const data: OnThisDayData = { year: target.year - memory.yearsAgo };
48+
for (const { year, assets } of memories) {
49+
const data: OnThisDayData = { year };
5050
await this.memoryRepository.create(
5151
{
5252
ownerId: userId,
5353
type: MemoryType.ON_THIS_DAY,
5454
data,
55-
memoryAt: target.minus({ years: memory.yearsAgo }).toISO(),
55+
memoryAt: target.set({ year }).toISO(),
5656
showAt,
5757
hideAt,
5858
},
59-
new Set(memory.assets.map(({ id }) => id)),
59+
new Set(assets.map(({ id }) => id)),
6060
);
6161
}
6262
}

0 commit comments

Comments
 (0)