Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memories off by one #16434

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions server/src/queries/asset.repository.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 3 additions & 7 deletions server/src/repositories/asset.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class AssetRepository {
}

@GenerateSql({ params: [DummyValue.UUID, { day: 1, month: 1 }] })
getByDayOfYear(ownerIds: string[], { day, month }: MonthDay): Promise<DayOfYearAssets[]> {
getByDayOfYear(ownerIds: string[], { day, month }: MonthDay) {
return this.db
.with('res', (qb) =>
qb
Expand Down Expand Up @@ -239,16 +239,12 @@ export class AssetRepository {
.select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo')),
)
.selectFrom('res')
.select(
sql<number>`((now() at time zone 'UTC')::date - ("localDateTime" at time zone 'UTC')::date) / 365`.as(
'yearsAgo',
),
)
.select(sql<number>`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<DayOfYearAssets[]>;
.execute();
}

@GenerateSql({ params: [[DummyValue.UUID]] })
Expand Down
8 changes: 4 additions & 4 deletions server/src/services/asset.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)] },
Expand Down
15 changes: 9 additions & 6 deletions server/src/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions server/src/services/memory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);
}
}
Expand Down
Loading