2023-02-25 09:12:03 -05:00
|
|
|
import { Inject } from '@nestjs/common';
|
2023-06-15 14:05:30 -04:00
|
|
|
import { DateTime } from 'luxon';
|
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-06-15 14:05:30 -04:00
|
|
|
import { MemoryLaneDto } from './dto';
|
2023-05-21 08:26:06 +02:00
|
|
|
import { MapMarkerDto } from './dto/map-marker.dto';
|
2023-06-15 14:05:30 -04:00
|
|
|
import { mapAsset, MapMarkerResponseDto } from './response-dto';
|
2023-06-14 20:47:18 -05:00
|
|
|
import { MemoryLaneResponseDto } from './response-dto/memory-lane-response.dto';
|
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
|
|
|
|
2023-06-15 14:05:30 -04:00
|
|
|
async getMemoryLane(authUser: AuthUserDto, dto: MemoryLaneDto): Promise<MemoryLaneResponseDto[]> {
|
|
|
|
|
const target = DateTime.fromJSDate(dto.timestamp);
|
2023-06-14 20:47:18 -05:00
|
|
|
|
2023-06-15 14:05:30 -04:00
|
|
|
const onRequest = async (yearsAgo: number): Promise<MemoryLaneResponseDto> => {
|
|
|
|
|
const assets = await this.assetRepository.getByDate(authUser.id, target.minus({ years: yearsAgo }).toJSDate());
|
|
|
|
|
return {
|
|
|
|
|
title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} since...`,
|
|
|
|
|
assets: assets.map((a) => mapAsset(a)),
|
|
|
|
|
};
|
|
|
|
|
};
|
2023-06-14 20:47:18 -05:00
|
|
|
|
2023-06-15 14:05:30 -04:00
|
|
|
const requests: Promise<MemoryLaneResponseDto>[] = [];
|
|
|
|
|
for (let i = 1; i <= dto.years; i++) {
|
|
|
|
|
requests.push(onRequest(i));
|
2023-06-14 20:47:18 -05:00
|
|
|
}
|
|
|
|
|
|
2023-06-15 14:05:30 -04:00
|
|
|
return Promise.all(requests).then((results) => results.filter((result) => result.assets.length > 0));
|
2023-06-14 20:47:18 -05:00
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|