diff --git a/src/services/flight/flights.payload.from.flights.stat.ts b/src/services/flight/flights.payload.from.flights.stat.ts index 9647b1e..b539060 100644 --- a/src/services/flight/flights.payload.from.flights.stat.ts +++ b/src/services/flight/flights.payload.from.flights.stat.ts @@ -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: { diff --git a/src/services/flight/get.flights.ts b/src/services/flight/get.flights.ts index f17287e..c88553e 100644 --- a/src/services/flight/get.flights.ts +++ b/src/services/flight/get.flights.ts @@ -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!'); } diff --git a/src/services/flight/get.random.flight.ts b/src/services/flight/get.random.flight.ts index 22fe59d..34453c0 100644 --- a/src/services/flight/get.random.flight.ts +++ b/src/services/flight/get.random.flight.ts @@ -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'; @@ -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 @@ -50,18 +48,28 @@ export async function getRandomFlight(): Promise { 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) { diff --git a/src/services/flight/populate.flights.ts b/src/services/flight/populate.flights.ts index bf5a5fb..f941467 100644 --- a/src/services/flight/populate.flights.ts +++ b/src/services/flight/populate.flights.ts @@ -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( diff --git a/src/services/user/sync.user.ts b/src/services/user/sync.user.ts index d5e46d4..b5fc3fb 100644 --- a/src/services/user/sync.user.ts +++ b/src/services/user/sync.user.ts @@ -51,7 +51,6 @@ export async function syncAuthProviderForUser(userID: string) { where: { id: entry.id }, })), }, - isAnonymous, lastSignInAt: firebaseUser.metadata.lastSignInTime ? moment(firebaseUser.metadata.lastSignInTime).toDate()