Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Fix flight creation
Browse files Browse the repository at this point in the history
  • Loading branch information
anduong96 committed Nov 21, 2023
1 parent f6b15a3 commit 7239b76
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 25 deletions.
8 changes: 5 additions & 3 deletions src/services/flight/flights.payload.from.flights.stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ export function flightStatFlightToFlightPayload(
arrivalAirport.timeZoneRegionName,
);

const flightTimelinePayload = getFlightTimelinePayload(flightID, flight).map(
entry => omit(entry, ['flightID']),
);

return {
FlightTimeline: {
create: getFlightTimelinePayload(flightID, flight).map(entry =>
omit(entry, ['flightID']),
),
create: flightTimelinePayload,
},
FlightVendorConnection: {
create: {
Expand Down
1 change: 1 addition & 0 deletions src/services/flight/get.flights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function getFlights(
Logger.debug('%s Flights found for param[%o]', flights.length, param);
return flights;
} else if (isEmpty(flights) && throwIfNotFound) {
Logger.error('No flights found! param=%o', param);
throw new Error('Flight(s) not found!');
}

Expand Down
30 changes: 19 additions & 11 deletions src/services/flight/get.random.flight.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { omit } from 'lodash';
import { Flight } from '@prisma/client';
import { Flight, Prisma } from '@prisma/client';

import { prisma } from '@app/prisma';
import { Logger } from '@app/lib/logger';
Expand All @@ -9,7 +8,6 @@ import { FlightCreatedTopic } from '@app/topics/defined.topics/flight.created.to

import { getFlightEmissions } from './get.flight.emissions';
import { flightStatFlightToFlightPayload } from './flights.payload.from.flights.stat';
import { getFlightTimelinePayload } from '../flight.timeline/get.flight.timeline.payload';

/**
* The function `getRandomFlight` retrieves a random flight from FlightStats API, checks if it already
Expand Down Expand Up @@ -50,18 +48,28 @@ export async function getRandomFlight(): Promise<Flight> {

try {
const data = flightStatFlightToFlightPayload(remoteFlight);
const timeline = getFlightTimelinePayload(data.id!, remoteFlight);
const timelineData = timeline.map(entry => omit(entry, ['flightID']));
const payload: Prisma.FlightUncheckedCreateInput = Object.assign(
data,
emission,
);

const payload = Object.assign(data, emission, {
FlightTimeline: {
createMany: {
data: timelineData,
},
const flight = await prisma.flight.upsert({
create: payload,
update: {},
where: {
airlineIata_flightNumber_originIata_destinationIata_flightYear_flightMonth_flightDate:
{
airlineIata: payload.airlineIata,
destinationIata: payload.destinationIata,
flightDate: payload.flightDate,
flightMonth: payload.flightMonth,
flightNumber: payload.flightNumber,
flightYear: payload.flightYear,
originIata: payload.originIata,
},
},
});

const flight = await prisma.flight.create({ data: payload });
TopicPublisher.broadcastAll([new FlightCreatedTopic(flight.id)]);
return flight;
} catch (error) {
Expand Down
39 changes: 29 additions & 10 deletions src/services/flight/populate.flights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,51 @@ export async function populateFlights(params: FlightQueryParam) {
]);

if (isEmpty(flights)) {
Logger.error('No flights found! %o', { params });
throw new Error('Flight(s) not found!');
}

try {
Logger.debug('Creating flights for param[%o]', params);
const data = flights.map(flight => Object.assign(flight, emissions));
// create many is having issue with validation
const result = await prisma.$transaction(
data.map(entry =>
prisma.flight.create({
data: entry,
data.map(entry => {
Logger.debug(
'Creating flight=%s%s on date=%s-%s-%s',
entry.airlineIata,
entry.flightNumber,
entry.flightYear,
entry.flightMonth,
entry.flightDate,
);

return prisma.flight.upsert({
create: entry,
select: {
id: true,
},
}),
),
update: {},
where: {
airlineIata_flightNumber_originIata_destinationIata_flightYear_flightMonth_flightDate:
{
airlineIata: entry.airlineIata,
destinationIata: entry.destinationIata,
flightDate: entry.flightDate,
flightMonth: entry.flightMonth,
flightNumber: entry.flightNumber,
flightYear: entry.flightYear,
originIata: entry.originIata,
},
},
});
}),
);

Logger.warn(
'Created %d for Flight[%s%s] on %s/%s/%s',
'Created %d for Flight[%s%s]',
result.length,
params.airlineIata,
params.flightNumber,
params.flightYear,
params.flightMonth,
params.flightDate,
);

TopicPublisher.broadcastAll(
Expand Down
1 change: 0 additions & 1 deletion src/services/user/sync.user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export async function syncAuthProviderForUser(userID: string) {
where: { id: entry.id },
})),
},

isAnonymous,
lastSignInAt: firebaseUser.metadata.lastSignInTime
? moment(firebaseUser.metadata.lastSignInTime).toDate()
Expand Down

0 comments on commit 7239b76

Please sign in to comment.