diff --git a/package.json b/package.json index ab46766..97f8cd1 100644 --- a/package.json +++ b/package.json @@ -40,8 +40,8 @@ "typescript": "^5.2.2" }, "peerDependencies": { - "typescript": "^5.2.2", - "class-validator": "^0.14.0" + "class-validator": "^0.14.0", + "typescript": "^5.2.2" }, "dependencies": { "@apollo/server": "^4.9.5", diff --git a/prisma/migrations/20231119220003_add_date_format_pref/migration.sql b/prisma/migrations/20231119220003_add_date_format_pref/migration.sql deleted file mode 100644 index 806af0e..0000000 --- a/prisma/migrations/20231119220003_add_date_format_pref/migration.sql +++ /dev/null @@ -1,5 +0,0 @@ --- CreateEnum -CREATE TYPE "DateFormatType" AS ENUM ('AMERICAN', 'WORLD'); - --- AlterTable -ALTER TABLE "UserPreference" ADD COLUMN "dateFormat" "DateFormatType" NOT NULL DEFAULT 'AMERICAN'; diff --git a/prisma/migrations/20231112181835_init/migration.sql b/prisma/migrations/20231126021752_/migration.sql similarity index 99% rename from prisma/migrations/20231112181835_init/migration.sql rename to prisma/migrations/20231126021752_/migration.sql index 6a22c98..e59ec7e 100644 --- a/prisma/migrations/20231112181835_init/migration.sql +++ b/prisma/migrations/20231126021752_/migration.sql @@ -19,6 +19,9 @@ CREATE TYPE "FlightStatus" AS ENUM ('SCHEDULED', 'DEPARTED', 'DELAYED', 'ARRIVED -- CreateEnum CREATE TYPE "MeasurementType" AS ENUM ('AMERICAN', 'METRIC', 'IMPERIAL'); +-- CreateEnum +CREATE TYPE "DateFormatType" AS ENUM ('AMERICAN', 'WORLD'); + -- CreateTable CREATE TABLE "Airline" ( "id" TEXT NOT NULL, @@ -319,6 +322,7 @@ CREATE TABLE "UserPreference" ( "id" TEXT NOT NULL, "userID" TEXT NOT NULL, "measurement" "MeasurementType" NOT NULL DEFAULT 'AMERICAN', + "dateFormat" "DateFormatType" NOT NULL DEFAULT 'AMERICAN', CONSTRAINT "UserPreference_pkey" PRIMARY KEY ("id") ); diff --git a/src/@generated/graphql/models/FlightEvent.ts b/src/@generated/graphql/models/FlightEvent.ts index 0b97e65..4d1f16a 100644 --- a/src/@generated/graphql/models/FlightEvent.ts +++ b/src/@generated/graphql/models/FlightEvent.ts @@ -3,6 +3,7 @@ import { GQL_ValueType } from '../enums/ValueType'; import { GQL_ChangeType } from '../enums/ChangeType'; import { Prisma } from '@prisma/client'; import * as GraphQLScalars from 'graphql-scalars'; +import GraphQLScalars from 'graphql-scalars'; @ObjectType('FlightEvent') export class GQL_FlightEvent { diff --git a/src/@generated/graphql/models/JsonCache.ts b/src/@generated/graphql/models/JsonCache.ts index 4ee1b33..bb87342 100644 --- a/src/@generated/graphql/models/JsonCache.ts +++ b/src/@generated/graphql/models/JsonCache.ts @@ -1,6 +1,7 @@ import { Field, ObjectType, ID } from 'type-graphql'; import { Prisma } from '@prisma/client'; import * as GraphQLScalars from 'graphql-scalars'; +import GraphQLScalars from 'graphql-scalars'; @ObjectType('JsonCache') export class GQL_JsonCache { diff --git a/src/api/graphql/resolvers/user.flights.resolver.ts b/src/api/graphql/resolvers/user.flights.resolver.ts index 60dadab..64e6c95 100644 --- a/src/api/graphql/resolvers/user.flights.resolver.ts +++ b/src/api/graphql/resolvers/user.flights.resolver.ts @@ -1,4 +1,3 @@ -import { FlightStatus } from '@prisma/client'; import { Arg, Authorized, @@ -12,6 +11,9 @@ import { import { prisma } from '@app/prisma'; import { GQL_Flight } from '@app/@generated/graphql/models/Flight'; import { GQL_UserFlight } from '@app/@generated/graphql/models/UserFlight'; +import { saveFlightToUser } from '@app/services/user.flight/save.flight.to.user'; +import { getUserActiveFlights } from '@app/services/user.flight/get.user.active.flights'; +import { getUserArchivedFlights } from '@app/services/user.flight/get.user.archived.flights'; import { CurrentUserID } from '../_decorators/current.user.id.decorator'; @@ -36,20 +38,7 @@ export class UserFlightResolver { @CurrentUserID() userID: string, @Arg('flightID') flightID: string, ): Promise { - const record = await prisma.userFlight.upsert({ - create: { - flightID, - userID, - }, - update: {}, - where: { - flightID_userID: { - flightID, - userID, - }, - }, - }); - + const record = await saveFlightToUser(flightID, userID); return record.id; } @@ -74,45 +63,15 @@ export class UserFlightResolver { @Authorized() @Query(() => [GQL_UserFlight]) async userActiveFlights(@CurrentUserID() userID: string) { - const result = await prisma.userFlight.findMany({ - orderBy: { - Flight: { - estimatedGateDeparture: 'asc', - }, - }, - where: { - Flight: { - status: { - notIn: [FlightStatus.ARCHIVED, FlightStatus.CANCELED], - }, - }, - userID, - }, - }); - - return result; + const flights = await getUserActiveFlights(userID); + return flights; } @Authorized() @Query(() => [GQL_UserFlight]) async userArchivedFlights(@CurrentUserID() userID: string) { - const result = await prisma.userFlight.findMany({ - orderBy: { - Flight: { - estimatedGateDeparture: 'desc', - }, - }, - where: { - Flight: { - status: { - in: [FlightStatus.ARCHIVED, FlightStatus.CANCELED], - }, - }, - userID, - }, - }); - - return result; + const flights = await getUserArchivedFlights(userID); + return flights; } @Authorized() diff --git a/src/firebase.ts b/src/firebase.ts index 259b697..5de9a4b 100644 --- a/src/firebase.ts +++ b/src/firebase.ts @@ -14,4 +14,8 @@ export const firebase = firebaseAdmin.initializeApp({ credential, }); -Logger.getSubLogger({ name: 'Firebase' }).debug('initialized'); +Logger.getSubLogger({ name: 'Firebase' }).debug( + 'initialized projectID=%s clientEmail=%s', + ENV.FIREBASE_PROJECT_ID, + ENV.FIREBASE_CLIENT_EMAIL, +); diff --git a/src/prisma.ts b/src/prisma.ts index ef039f1..b81ec3f 100644 --- a/src/prisma.ts +++ b/src/prisma.ts @@ -1,5 +1,6 @@ import { PrismaClient } from '@prisma/client'; +import { ENV } from './env'; import { Logger } from './lib/logger'; class _prisma extends PrismaClient { @@ -26,6 +27,9 @@ class _prisma extends PrismaClient { this.$connect() .then(() => { this.logger.debug('Connected to Prisma'); + if (ENV.NODE_ENV === 'development') { + this.logger.warn('Prisma database url=%s', ENV.DATABASE_URL); + } }) .catch(error => { this.logger.error('Unable to connect to Prisma', error); diff --git a/src/scheduler/defined.jobs/plane.sync.job.ts b/src/scheduler/defined.jobs/plane.sync.job.ts index 5878fb0..735b3ba 100644 --- a/src/scheduler/defined.jobs/plane.sync.job.ts +++ b/src/scheduler/defined.jobs/plane.sync.job.ts @@ -11,6 +11,11 @@ import { Job } from '../job'; export class SyncActivePlaneLocationJob extends Job { override cronTime = CronTime.every(7).minutes(); + constructor() { + super(); + this.onProcess(); + } + private async getTailNumbers() { const ceil = moment().add(10, 'hours').toDate(); const floor = moment().subtract(12, 'hours').toDate(); diff --git a/src/services/aircraft/update.aircraft.position.ts b/src/services/aircraft/update.aircraft.position.ts index 7e647cf..9d3b39d 100644 --- a/src/services/aircraft/update.aircraft.position.ts +++ b/src/services/aircraft/update.aircraft.position.ts @@ -9,7 +9,12 @@ export async function updateAircraftPosition( ) { const position = await RadarBox.getAircraft(aircraft.tailNumber); - Logger.debug('Plane position', position); + Logger.debug( + 'Plane tailNumber=%s airlineIata=%s position=%o', + aircraft.tailNumber, + aircraft.airlineIata, + position, + ); if (!position || !position?.destinationIata || !position.originIata) { Logger.debug( diff --git a/src/services/user.flight/get.user.active.flights.ts b/src/services/user.flight/get.user.active.flights.ts new file mode 100644 index 0000000..6e7669f --- /dev/null +++ b/src/services/user.flight/get.user.active.flights.ts @@ -0,0 +1,26 @@ +import { FlightStatus, User } from '@prisma/client'; + +import { prisma } from '@app/prisma'; +import { Logger } from '@app/lib/logger'; + +export async function getUserActiveFlights(userID: User['id']) { + Logger.debug('Getting user=%s active flights', userID); + const flights = await prisma.userFlight.findMany({ + orderBy: { + Flight: { + estimatedGateDeparture: 'asc', + }, + }, + where: { + Flight: { + status: { + notIn: [FlightStatus.ARCHIVED, FlightStatus.CANCELED], + }, + }, + userID, + }, + }); + + Logger.debug('Got user=%s active flights=%s', userID, flights.length); + return flights; +} diff --git a/src/services/user.flight/get.user.archived.flights.ts b/src/services/user.flight/get.user.archived.flights.ts new file mode 100644 index 0000000..5c06af1 --- /dev/null +++ b/src/services/user.flight/get.user.archived.flights.ts @@ -0,0 +1,26 @@ +import { FlightStatus, User } from '@prisma/client'; + +import { prisma } from '@app/prisma'; +import { Logger } from '@app/lib/logger'; + +export async function getUserArchivedFlights(userID: User['id']) { + Logger.debug('Getting user=%s archived flights', userID); + const flights = await prisma.userFlight.findMany({ + orderBy: { + Flight: { + estimatedGateDeparture: 'desc', + }, + }, + where: { + Flight: { + status: { + in: [FlightStatus.ARCHIVED, FlightStatus.CANCELED], + }, + }, + userID, + }, + }); + + Logger.debug('Got user=%s archived flights=%s', userID, flights.length); + return flights; +} diff --git a/src/services/user.flight/save.flight.to.user.ts b/src/services/user.flight/save.flight.to.user.ts new file mode 100644 index 0000000..d2b8c66 --- /dev/null +++ b/src/services/user.flight/save.flight.to.user.ts @@ -0,0 +1,27 @@ +import { Flight, User } from '@prisma/client'; + +import { prisma } from '@app/prisma'; +import { Logger } from '@app/lib/logger'; + +export async function saveFlightToUser( + flightID: Flight['id'], + userID: User['id'], +) { + Logger.debug('Adding flight[%s] to user[%s]', flightID, userID); + + const record = await prisma.userFlight.upsert({ + create: { + flightID, + userID, + }, + update: {}, + where: { + flightID_userID: { + flightID, + userID, + }, + }, + }); + + return record; +} diff --git a/src/vendors/aircraft/radar.box/index.ts b/src/vendors/aircraft/radar.box/index.ts index a497ba6..3ebc91c 100644 --- a/src/vendors/aircraft/radar.box/index.ts +++ b/src/vendors/aircraft/radar.box/index.ts @@ -37,7 +37,7 @@ export class RadarBox { const html = await request.text(); const data = this.crawlAircraftHtml(html); this.logger.debug( - 'RadarBox request[%s/%s] status[%s]', + 'RadarBox request[%s] status[%s]', request.url, request.statusText, );