2023-02-25 09:12:03 -05:00
|
|
|
import { Inject } from '@nestjs/common';
|
2023-05-21 08:26:06 +02:00
|
|
|
import { AuthUserDto } from '../auth';
|
2023-03-02 21:47:08 -05:00
|
|
|
import { IAssetRepository } from './asset.repository';
|
2023-05-21 08:26:06 +02:00
|
|
|
import { MapMarkerDto } from './dto/map-marker.dto';
|
2023-06-14 20:47:18 -05:00
|
|
|
import { MapMarkerResponseDto, mapAsset } from './response-dto';
|
|
|
|
|
import { MemoryLaneResponseDto } from './response-dto/memory-lane-response.dto';
|
|
|
|
|
import { DateTime } from 'luxon';
|
2023-02-25 09:12:03 -05:00
|
|
|
|
|
|
|
|
export class AssetService {
|
2023-05-26 15:43:24 -04:00
|
|
|
constructor(@Inject(IAssetRepository) private assetRepository: IAssetRepository) {}
|
2023-05-21 08:26:06 +02:00
|
|
|
|
|
|
|
|
getMapMarkers(authUser: AuthUserDto, options: MapMarkerDto): Promise<MapMarkerResponseDto[]> {
|
|
|
|
|
return this.assetRepository.getMapMarkers(authUser.id, options);
|
|
|
|
|
}
|
2023-06-14 20:47:18 -05:00
|
|
|
|
|
|
|
|
async getMemoryLane(authUser: AuthUserDto, timezone: string): Promise<MemoryLaneResponseDto[]> {
|
|
|
|
|
const result: MemoryLaneResponseDto[] = [];
|
|
|
|
|
|
|
|
|
|
const luxonDate = DateTime.fromISO(new Date().toISOString(), { zone: timezone });
|
|
|
|
|
const today = new Date(luxonDate.year, luxonDate.month - 1, luxonDate.day);
|
|
|
|
|
|
|
|
|
|
const years = Array.from({ length: 30 }, (_, i) => {
|
|
|
|
|
const year = today.getFullYear() - i - 1;
|
|
|
|
|
return new Date(year, today.getMonth(), today.getDate());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const year of years) {
|
|
|
|
|
const assets = await this.assetRepository.getByDate(authUser.id, year);
|
|
|
|
|
|
|
|
|
|
if (assets.length > 0) {
|
|
|
|
|
const yearGap = today.getFullYear() - year.getFullYear();
|
|
|
|
|
const memory = new MemoryLaneResponseDto();
|
|
|
|
|
memory.title = `${yearGap} year${yearGap > 1 && 's'} since...`;
|
|
|
|
|
memory.assets = assets.map((a) => mapAsset(a));
|
|
|
|
|
|
|
|
|
|
result.push(memory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|