Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Mar 24, 2024
1 parent ed7c5ed commit c390a9d
Show file tree
Hide file tree
Showing 28 changed files with 120 additions and 102 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@
"@typescript-eslint/comma-dangle": "off",
"@typescript-eslint/naming-convention": "off",
"n/prefer-global/process": "off",
"unicorn/no-await-expression-member": "off"
"unicorn/no-await-expression-member": "off",
"@typescript-eslint/no-unsafe-argument": "off"
}
},
"prisma": {
Expand Down
1 change: 1 addition & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ seed()
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
})
// eslint-disable-next-line unicorn/prefer-top-level-await
.finally(async () => {
await prisma.$disconnect();
});
4 changes: 3 additions & 1 deletion src/buildings/buildings.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Controller, Get, Header, Injectable} from '@nestjs/common';
import {
Controller, Get, Header, Injectable
} from '@nestjs/common';
import * as db from 'zapatos/db';
import {PoolService} from '~/pool/pool.service';

Expand Down
2 changes: 1 addition & 1 deletion src/cache/cache.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Module} from '@nestjs/common';
import {CacheModule as Cache} from "@nestjs/cache-manager"
import {CacheModule as Cache} from '@nestjs/cache-manager';

@Module({
imports: [
Expand Down
6 changes: 4 additions & 2 deletions src/courses/courses.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {Controller, Get, Injectable, Query, Res, UseInterceptors} from '@nestjs/common';
import {
Controller, Get, Injectable, Query, Res, UseInterceptors
} from '@nestjs/common';
import {FastifyReply} from 'fastify';
import {NoCacheUpdatedSinceInterceptor} from 'src/interceptors/no-cache-updated-since';
import {CacheKey} from '@nestjs/cache-manager';
import {CoursesService} from './courses.service';
import {GetCoursesParameters, GetUniqueCoursesParameters, FindFirstCourseParameters} from './types';
import {streamSqlQuery} from '~/lib/stream-sql-query';
import {PoolService} from '~/pool/pool.service';
import { CacheKey } from '@nestjs/cache-manager';

@Controller('courses')
@UseInterceptors(NoCacheUpdatedSinceInterceptor)
Expand Down
4 changes: 3 additions & 1 deletion src/courses/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {IsDate, IsIn, IsNumber, IsOptional} from 'class-validator';
import {
IsDate, IsIn, IsNumber, IsOptional
} from 'class-validator';
import {Type} from 'class-transformer';
import {ApiProperty} from '@nestjs/swagger';
import {Semester} from '@prisma/client';
Expand Down
6 changes: 4 additions & 2 deletions src/instructors/instructors.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {Controller, Get, Header, Injectable, Query, UseInterceptors} from '@nestjs/common';
import {
Controller, Get, Header, Injectable, Query, UseInterceptors
} from '@nestjs/common';
import {Thumbor} from '@mtucourses/thumbor';
import {NoCacheUpdatedSinceInterceptor} from 'src/interceptors/no-cache-updated-since';
import * as db from 'zapatos/db';
import {CacheInterceptor} from '@nestjs/cache-manager';
import {GetInstructorsParameters} from './types';
import {PoolService} from '~/pool/pool.service';
import { CacheInterceptor } from '@nestjs/cache-manager';

@Controller('instructors')
@UseInterceptors(CacheInterceptor, NoCacheUpdatedSinceInterceptor)
Expand Down
6 changes: 3 additions & 3 deletions src/interceptors/no-cache-updated-since.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {CallHandler, ExecutionContext, NestInterceptor} from '@nestjs/common';
import {Injectable} from '@nestjs/common';
import {
Injectable, type CallHandler, type ExecutionContext, type NestInterceptor
} from '@nestjs/common';
// NestJS bug: https://stackoverflow.com/a/63984129/12638523

import type {FastifyReply} from 'fastify';

@Injectable()
Expand Down
13 changes: 8 additions & 5 deletions src/lib/convert-semester-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import type {Semester} from 'zapatos/schema';

export const fetcherSemesterToDatabaseSemester = (semester: ESemester): Semester => {
switch (semester) {
case ESemester.fall:
case ESemester.fall: {
return 'FALL';
case ESemester.spring:
}

case ESemester.spring: {
return 'SPRING';
case ESemester.summer:
}

case ESemester.summer: {
return 'SUMMER';
default:
throw new Error('Invalid semester');
}
}
};
12 changes: 6 additions & 6 deletions src/lib/db-utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type * as schema from 'zapatos/schema';
import * as db from 'zapatos/db';

export const mapWithSeparator = <TIn, TSep, TOut>(
export const mapWithSeparator = <TIn, TSeparator, TOut>(
array: TIn[],
separator: TSep,
cb: (x: TIn, i: number, a: TIn[]) => TOut
): Array<TOut | TSep> => {
const result: Array<TOut | TSep> = [];
separator: TSeparator,
callback: (x: TIn, i: number, a: TIn[]) => TOut
): Array<TOut | TSeparator> => {
const result: Array<TOut | TSeparator> = [];
for (let i = 0, length = array.length; i < length; i++) {
if (i > 0) {
result.push(separator);
}

result.push(cb(array[i], i, array));
result.push(callback(array[i], i, array));
}

return result;
Expand Down
11 changes: 5 additions & 6 deletions src/lib/parse-location.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {Building, Section} from '@prisma/client';
import {LocationType} from '@prisma/client';
import {LocationType, type Building, type Section} from '@prisma/client';

const numbersRegex = new RegExp(/(\d)\w+/);
const numbersRegex = /(\d)\w+/;

const getMappedBuildingName = (buildingName: string, buildings: Building[]) => {
const building = buildings.find(b => b.name === buildingName);
Expand Down Expand Up @@ -49,13 +48,13 @@ const parseLocation = (from: string, buildings: Building[]): Pick<Section, 'loca
};
}

const lastWord = fragments[fragments.length - 1];
const lastWord = fragments.at(-1);

if (numbersRegex.test(lastWord)) {
if (numbersRegex.test(lastWord!)) {
return {
locationType: LocationType.PHYSICAL,
buildingName: getMappedBuildingName(fragments.splice(0, fragments.length - 1).join(' '), buildings),
room: lastWord
room: lastWord!
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// eslint-disable-next-line import/no-unassigned-import
// eslint-disable-next-line import/no-unassigned-import, import/order
import './sentry';
import {NestFactory} from '@nestjs/core';
import {ValidationPipe} from '@nestjs/common';
import type {
NestFastifyApplication
} from '@nestjs/platform-fastify';
import {
FastifyAdapter,
FastifyAdapter, type
NestFastifyApplication

} from '@nestjs/platform-fastify';
import {DocumentBuilder, SwaggerModule} from '@nestjs/swagger';
import {WorkerService} from 'nestjs-graphile-worker';
Expand Down Expand Up @@ -37,4 +36,5 @@ async function bootstrap() {
void app.get(WorkerService).run();
}

// eslint-disable-next-line unicorn/prefer-top-level-await
void bootstrap();
17 changes: 9 additions & 8 deletions src/passfaildrop/passfaildrop.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {Body, Controller, Get, Injectable, Put, UseInterceptors, Headers, Header, Query} from '@nestjs/common';
import {
Body, Controller, Get, Injectable, Put, UseInterceptors, Headers, Header, Query
} from '@nestjs/common';
import checkAuthHeader from 'src/lib/check-auth-header';
import * as db from 'zapatos/db';
import type {WhereableForTable} from 'zapatos/schema';
import type {PutDto} from './types';
import {GetAllParameters} from './types';
import {CacheInterceptor} from '@nestjs/cache-manager';
import {GetAllParameters, type PutDto} from './types';
import {PoolService} from '~/pool/pool.service';
import { CacheInterceptor } from '@nestjs/cache-manager';

@Controller('passfaildrop')
@UseInterceptors(CacheInterceptor)
Expand Down Expand Up @@ -39,10 +40,10 @@ export class PassFailDropController {

// Todo: move this into the query
// eslint-disable-next-line unicorn/no-array-reduce
return result.reduce<Record<string, unknown[]>>((acc, row) => {
return result.reduce<Record<string, unknown[]>>((accumulator, row) => {
const key = `${row.courseSubject}${row.courseCrse}`;
acc[key] = [
...(acc[key] ?? []),
accumulator[key] = [
...(accumulator[key] ?? []),
{
semester: row.semester,
year: row.year,
Expand All @@ -52,7 +53,7 @@ export class PassFailDropController {
}
];

return acc;
return accumulator;
}, {});
}

Expand Down
9 changes: 4 additions & 5 deletions src/pool/pool.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {OnApplicationShutdown} from '@nestjs/common';
import {Module} from '@nestjs/common';
import {Module, type OnApplicationShutdown} from '@nestjs/common';
import {ModuleRef} from '@nestjs/core';
import {PoolService} from './pool.service';

Expand All @@ -10,15 +9,15 @@ import {PoolService} from './pool.service';
exports: [PoolService]
})
export class PoolModule implements OnApplicationShutdown {
constructor(private readonly moduleRef: ModuleRef) {
moduleRef.get(PoolService).on('error', error => {
constructor(private readonly moduleReference: ModuleRef) {
moduleReference.get(PoolService).on('error', error => {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
});
}

async onApplicationShutdown() {
const pool = this.moduleRef.get(PoolService);
const pool = this.moduleReference.get(PoolService);
await pool.end();
}
}
4 changes: 3 additions & 1 deletion src/sections/sections.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Controller, Get, Injectable, Query, UseInterceptors, Res} from '@nestjs/common';
import {
Controller, Get, Injectable, Query, UseInterceptors, Res
} from '@nestjs/common';
import {FastifyReply} from 'fastify';
import {NoCacheUpdatedSinceInterceptor} from 'src/interceptors/no-cache-updated-since';
import {GetSectionsParameters, FindFirstSectionParamters} from './types';
Expand Down
4 changes: 3 additions & 1 deletion src/sections/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {IsDate, IsIn, IsNumber, IsOptional} from 'class-validator';
import {
IsDate, IsIn, IsNumber, IsOptional
} from 'class-validator';
import {Type} from 'class-transformer';
import {ApiProperty} from '@nestjs/swagger';
import {Semester} from '@prisma/client';
Expand Down
4 changes: 3 additions & 1 deletion src/semesters/semesters.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Controller, Get, Header, Injectable} from '@nestjs/common';
import {
Controller, Get, Header, Injectable
} from '@nestjs/common';
import * as db from 'zapatos/db';
import {PoolService} from '~/pool/pool.service';

Expand Down
2 changes: 1 addition & 1 deletion src/tasks/scrape-ratemyprofessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ScrapeRateMyProfessorsTask {
})(async (instructor: schema.JSONSelectableForTable<'Instructor'>) => {
const nameFragments = instructor.fullName.split(' ');
const firstName = nameFragments[0];
const lastName = nameFragments[nameFragments.length - 1];
const lastName = nameFragments.at(-1);

const results = await this.fetcher.rateMyProfessors.searchTeacher(`${firstName} ${lastName}`, schools[0].id);

Expand Down
6 changes: 3 additions & 3 deletions src/tasks/scrape-section-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ export class ScrapeSectionDetailsTask {

const scrapedSectionDetailsWithNulls = await Promise.all(sections.map(async section => {
try {
const extScrapedDetails = await this.throttledGetSectionDetails({
const extensionScrapedDetails = await this.throttledGetSectionDetails({
subject: section.course.subject,
crse: section.course.crse,
crn: section.crn,
term: termToDate({year: section.course.year, semester: section.course.semester})
});

return {
extScrapedDetails,
extScrapedDetails: extensionScrapedDetails,
section
};
} catch (error: unknown) {
Expand Down Expand Up @@ -222,7 +222,7 @@ export class ScrapeSectionDetailsTask {
const fragments = name.split(' ');
return {
firstName: fragments[0],
lastName: fragments[fragments.length - 1],
lastName: fragments.at(-1),
fullName: name
};
});
Expand Down
23 changes: 11 additions & 12 deletions src/tasks/scrape-sections.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Injectable, Logger} from '@nestjs/common';
import pThrottle from 'p-throttle';
import type {ICourseOverview, ISection} from '@mtucourses/scraper';
import type {IRuleOptions} from 'src/lib/rschedule';
import {Schedule} from 'src/lib/rschedule';
import {Schedule, type IRuleOptions} from 'src/lib/rschedule';
import {calculateDiffInTime, dateToTerm, mapDayCharToRRScheduleString} from 'src/lib/dates';
import getTermsToProcess from 'src/lib/get-terms-to-process';
import {Task, TaskHandler} from 'nestjs-graphile-worker';
Expand Down Expand Up @@ -30,9 +29,9 @@ export class ScrapeSectionsTask {

private async processTerm(term: Date) {
const {semester, year} = dateToTerm(term);
let extCourses: ICourseOverview[] = [];
let extensionCourses: ICourseOverview[] = [];
try {
extCourses = await this.fetcher.getAllSections(term);
extensionCourses = await this.fetcher.getAllSections(term);
} catch (error: unknown) {
if (error instanceof Error && error.message === 'Banner services are currently down.') {
this.logger.warn('Banner services are currently down. Skipping scraping instructors.');
Expand All @@ -56,15 +55,15 @@ export class ScrapeSectionsTask {
}).run(trx);

// Upsert courses
await db.upsert('Course', extCourses.map(extCourse => {
const [minCredits, maxCredits] = this.getCreditsRangeFromCourse(extCourse);
await db.upsert('Course', extensionCourses.map(extensionCourse => {
const [minCredits, maxCredits] = this.getCreditsRangeFromCourse(extensionCourse);

return {
year,
semester,
subject: extCourse.subject,
crse: extCourse.crse,
title: extCourse.title,
subject: extensionCourse.subject,
crse: extensionCourse.crse,
title: extensionCourse.title,
minCredits,
maxCredits,
};
Expand All @@ -77,12 +76,12 @@ export class ScrapeSectionsTask {
}).run(trx);

// Upsert sections
const sectionsUpsertInput: Array<InsertableForTable<'Section'>> = extCourses.flatMap(extCourse => extCourse.sections.map(extSection => {
const section = this.reshapeSectionFromScraperToDatabase(extSection, year);
const sectionsUpsertInput: Array<InsertableForTable<'Section'>> = extensionCourses.flatMap(extensionCourse => extensionCourse.sections.map(extensionSection => {
const section = this.reshapeSectionFromScraperToDatabase(extensionSection, year);

return {
...section,
courseId: db.sql`(SELECT ${'id'} FROM ${'Course'} WHERE ${'year'} = ${db.param(year)} AND ${'semester'} = ${db.param(semester)} AND ${'subject'} = ${db.param(extCourse.subject)} AND ${'crse'} = ${db.param(extCourse.crse)})`
courseId: db.sql`(SELECT ${'id'} FROM ${'Course'} WHERE ${'year'} = ${db.param(year)} AND ${'semester'} = ${db.param(semester)} AND ${'subject'} = ${db.param(extensionCourse.subject)} AND ${'crse'} = ${db.param(extensionCourse.crse)})`
};
}));

Expand Down
Loading

0 comments on commit c390a9d

Please sign in to comment.