introduce the database and controller connections for all inbounds

This commit is contained in:
Sven Czarnian
2022-10-23 16:58:00 +02:00
parent 7a41dd0fb2
commit f69e365623
19 changed files with 849 additions and 4 deletions

View File

@@ -0,0 +1,61 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Inbound, InboundDocument } from './models/inbound.model';
@Injectable()
export class InboundService {
constructor(
@InjectModel('inbound')
private readonly inboundModel: Model<InboundDocument>,
) {}
async callsignKnown(callsign: string): Promise<boolean> {
return this.inboundModel
.findOne({ callsign })
.then((inbound) => inbound !== null);
}
async add(inbound: Inbound): Promise<boolean> {
return this.callsignKnown(inbound.callsign).then(async (known) => {
if (known) return false;
// modify the inbound data
inbound.plan = undefined;
inbound.controllerData.reportedTime = new Date().toUTCString();
await this.inboundModel.create(inbound);
return true;
});
}
async remove(callsign: string): Promise<boolean> {
return this.callsignKnown(callsign).then(async (known) => {
if (!known) return false;
await this.inboundModel.deleteOne({ callsign });
return true;
});
}
async update(inbound: Inbound): Promise<boolean> {
return this.callsignKnown(inbound.callsign).then(async (known) => {
if (!known) return false;
// modify the inbound data
inbound.plan = undefined;
inbound.controllerData.reportedTime = new Date().toUTCString();
await this.inboundModel.findOneAndUpdate(
{ callsign: inbound.callsign },
inbound,
);
return true;
});
}
async airportInbounds(icao: string): Promise<Inbound[]> {
return this.inboundModel.find({ destination: icao });
}
}