|
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { OnEvent } from '@nestjs/event-emitter'; |
| 3 | +import { LOG_FINISHED_EVENT } from '../entities/log.entity'; |
| 4 | +import { Log } from '../schemas/log.schema'; |
| 5 | +import { InjectModel } from '@nestjs/mongoose'; |
| 6 | +import { Model, Types } from 'mongoose'; |
| 7 | +import { LogsService } from '../logs.service'; |
| 8 | +import { TYPE } from '../entities/type.entity'; |
| 9 | +import { HISTORY_TYPE } from '../entities/history-type.entity'; |
| 10 | +import { Station } from '../../stations/schemas/station.schema'; |
| 11 | +import { MissingStation } from '../../stations/schemas/missing-station.schema'; |
| 12 | + |
| 13 | +@Injectable() |
| 14 | +export class StationAssociationHandler { |
| 15 | + constructor( |
| 16 | + private readonly logsService: LogsService, |
| 17 | + @InjectModel(Station.name) private readonly stationModel: Model<Station>, |
| 18 | + @InjectModel(MissingStation.name) private readonly missingStationModel: Model<MissingStation>, |
| 19 | + @InjectModel(Log.name) private readonly logModel: Model<Log>, |
| 20 | + ) {} |
| 21 | + |
| 22 | + @OnEvent(LOG_FINISHED_EVENT) |
| 23 | + async handleFinished(log: Log) { |
| 24 | + try { |
| 25 | + if (log.type !== TYPE.CHARGE) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const history = await this.logsService.findOneWithHistory(log.akey, log._id.toString(), HISTORY_TYPE.LOCATION_DATA); |
| 30 | + |
| 31 | + const firstWithLocation = history.find((entry) => entry.latitude != null && entry.longitude != null); |
| 32 | + |
| 33 | + if (!firstWithLocation) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const latitude = firstWithLocation.latitude; |
| 38 | + const longitude = firstWithLocation.longitude; |
| 39 | + |
| 40 | + await this.stationModel.ensureIndexes(); |
| 41 | + |
| 42 | + const results = await this.stationModel.aggregate([ |
| 43 | + { |
| 44 | + $geoNear: { |
| 45 | + near: { type: 'Point', coordinates: [longitude, latitude] }, |
| 46 | + distanceField: 'distance', |
| 47 | + spherical: true, |
| 48 | + maxDistance: 200, |
| 49 | + key: 'location', |
| 50 | + }, |
| 51 | + }, |
| 52 | + { $sort: { distance: 1 } }, |
| 53 | + { $limit: 1 }, |
| 54 | + ]); |
| 55 | + |
| 56 | + if (results.length) { |
| 57 | + const stationDoc = results[0]; |
| 58 | + |
| 59 | + await this.logModel.updateOne({ _id: log._id, akey: log.akey }, { $set: { station: stationDoc._id } }); |
| 60 | + } else { |
| 61 | + await this.missingStationModel.ensureIndexes(); |
| 62 | + |
| 63 | + const msResults = await this.missingStationModel.aggregate([ |
| 64 | + { |
| 65 | + $geoNear: { |
| 66 | + near: { type: 'Point', coordinates: [longitude, latitude] }, |
| 67 | + distanceField: 'distance', |
| 68 | + spherical: true, |
| 69 | + maxDistance: 200, |
| 70 | + key: 'location', |
| 71 | + }, |
| 72 | + }, |
| 73 | + { $sort: { distance: 1 } }, |
| 74 | + { $limit: 1 }, |
| 75 | + ]); |
| 76 | + |
| 77 | + if (!msResults.length) { |
| 78 | + await this.missingStationModel.create({ |
| 79 | + akey: log.akey, |
| 80 | + logRef: log._id, |
| 81 | + location: { type: 'Point', coordinates: [longitude, latitude] }, |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | + } catch (error) { |
| 86 | + Logger.error('Error in StationAssociationHandler', error); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments