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, ) {} async callsignKnown(callsign: string): Promise { return this.inboundModel .findOne({ callsign }) .then((inbound) => inbound !== null); } async add(inbound: Inbound): Promise { 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 { return this.callsignKnown(callsign).then(async (known) => { if (!known) return false; await this.inboundModel.deleteOne({ callsign }); return true; }); } async update(inbound: Inbound): Promise { 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 { return this.inboundModel.find({ destination: icao }); } }