Skip to content

Instantly share code, notes, and snippets.

@SergioLarios
Created January 29, 2019 14:50
Show Gist options
  • Save SergioLarios/3b7116c41a4f7aa62779f63126b8441c to your computer and use it in GitHub Desktop.
Save SergioLarios/3b7116c41a4f7aa62779f63126b8441c to your computer and use it in GitHub Desktop.
// assignPairingToCrew
await this.contentService.updateWithQuery({ filledCategories: pairing.filledCategories }, PairingStructure, { id: pairing.id });
// Remember to update pending cert and crew every on create/update
if (!flight.pendingCert || Object.keys(flight.pendingCert).length <= 0 ||
!flight.pendingCrew || Object.keys(flight.pendingCrew).length <= 0) {
const pendingCrew = await this.flightHelper.calculateCrewCfg(flight);
const pendingCert = await this.flightHelper.calculateCertificationCfg(flight);
await this.tlModificationHelper.addTlMod(<TlModificationStructure>{
user: 'Admin',
type: TlModificationType.updatedFlight,
payload: <ITlModUpdate<FlightStructure>>{
newAttributes: { pendingCrew, pendingCert },
previousVersion: flight
}
});
flight = await this.contentService.findOne<FlightStructure>(
FlightStructure, { flightId: req.flightId });
}
if (!req.flight.crewConfiguration) {
const plane = await this.contentService.findOne<AircraftStructure>(AircraftStructure, {
registration: req.flight.aircraft
});
req.flight.crewConfiguration = plane.defaultCrewConfigurationId;
req.flight.certificationConfig = plane.defaultCertificationConfigurationId;
}
// P^rev add flight, comprobar rango
const req: FlightApi.AddSIMFlightReq = { flight: jsonBody, simulationId: params.simulationId };
const modifiedDepartureByKey = `modifiedBy.${req.simulationId}.departureDate`;
const modifiedArrivalByKey = `modifiedBy.${req.simulationId}.arrivalDate`;
const matchingFlights = await this.contentService.groupBy(FlightStructure,
{
$addFields: {
depDate: { $cond: [`$${modifiedDepartureByKey}`, `$${modifiedDepartureByKey}`, '$departureDate'] },
arrDate: { $cond: [`$${modifiedArrivalByKey}`, `$${modifiedArrivalByKey}`, '$arrivalDate'] }
}
},
{
$match: {
$or: [
{ depDate: { $gte: req.flight.departureDate, $lte: req.flight.arrivalDate } },
{ arrDate: { $gte: req.flight.departureDate, $lte: req.flight.arrivalDate } },
{ depDate: { $lte: req.flight.departureDate }, arrDate: { $gte: req.flight.arrivalDate } }
],
aircraft: req.flight.aircraft
}
}
);
// Cuando vayamos a crear un vuelo siempre hay que poner los pendingcrew o cret y sus configuraciones
// EDITAR O ELIMINAR CUALQUIER COSA DEL PASADO NO TE PUEDE DEJAR
flight.stdDate < this.dateInternalService.newDateNow().getTime()
return {
success: false,
errorCode: ErrorEnum.FlightError.FlightPassed,
errorMsg: 'You cant remove crew members to past flight ' + flightId
};
// Update flight
if (req.autoCrew || req.autoCert) {
const calculated = await that.flightHelper.calculateCfg(flight.flightId);
const calculatedCert = await that.flightHelper.calculateCfgCert(flight.flightId);
flight.crewConfiguration = req.autoCrew ? calculated : flight.crewConfiguration;
flight.certificationConfig = req.autoCert ? calculatedCert : flight.certificationConfig;
const pendingCrew = await that.flightHelper.calculateCrewCfg(flight);
const pendingCert = await that.flightHelper.calculateCertificationCfg(flight);
this.tlModificationHelper.addTlMod(<TlModificationStructure>{
type: TlModificationType.updatedFlight,
typeId: flight.flightId,
user: 'Admin',
payload: <ITlModUpdate<FlightStructure>>{
previousVersion: flight,
newAttributes: {
autoCrewConfiguration: req.autoCrew, autoCertificationConfig: req.autoCert,
crewConfiguration: flight.crewConfiguration,
certificationConfiguration: flight.autoCertificationConfig,
pendingCrew,
pendingCert
}
}
});
res = {
success: true,
content: {
idCrew: flight.crewConfiguration,
idCert: flight.certificationConfig,
pendingCrew,
pendingCert
}
};
}
// TO check on flight helper
public async calculateCfg(flightId: string) {
let res;
const flight = await this.contentService.findOne<FlightStructure>(FlightStructure, { flightId });
const aircraft = await this.contentService.findOne<AircraftStructure>(AircraftStructure, { registration: flight.aircraft });
flight.crewConfiguration = aircraft.defaultCrewConfigurationId;
const crewConfiguration: any = (await this.contentService.findOne<CrewConfigurationStructure>(CrewConfigurationStructure,
{ id: flight.crewConfiguration })).crewCategories;
flight.pendingCrew = await this.calculateCrewCfg(flight);
res = Object.keys(flight.pendingCrew).filter(key => flight.pendingCrew[key] < 0 &&
Object.keys(crewConfiguration).includes(key));
return res.length > 0 ? aircraft.reinforcedCrewConfiguration : aircraft.defaultCrewConfigurationId;
}
public async calculateCfgCert(flightId: string) {
let res;
const flight = await this.contentService.findOne<FlightStructure>(FlightStructure, { flightId });
const aircraft = await this.contentService.findOne<AircraftStructure>(AircraftStructure, { registration: flight.aircraft });
flight.certificationConfig = aircraft.defaultCertificationConfigurationId;
const certificationConfiguration: any = (
await this.contentService.findOne<CertificationConfigurationStructure>(
CertificationConfigurationStructure, { id: flight.certificationConfig })
).certifications;
flight.pendingCert = await this.calculateCertificationCfg(flight);
res = Object.keys(flight.pendingCert).filter(key => flight.pendingCert[key] < 0 &&
Object.keys(certificationConfiguration).includes(key));
return res.length > 0 ? aircraft.reinforcedCertificationConfiguration : aircraft.defaultCertificationConfigurationId;
}
public async calculateCrewCfgs(result: (FlightStructure)[]) {
const that = this;
const fligtPromises = result.map(flight => {
return that.flightHelper.calculateCrewCfg(flight).then(pending => {
flight.pendingCrew = pending;
});
});
await Promise.all(fligtPromises);
}
public async calculateCertCfgs(result: (FlightStructure)[]) {
const that = this;
const fligtPromises = result.map(flight => {
return that.flightHelper.calculateCertificationCfg(flight).then(pending => {
flight.pendingCert = pending;
});
});
await Promise.all(fligtPromises);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment