From a4912e2869134a3ad69c8e2c0dfa414a82d1816d Mon Sep 17 00:00:00 2001 From: Austen Stone Date: Mon, 13 Jan 2025 10:14:02 -0500 Subject: [PATCH] Update octokit version and refactor test imports; improve settings service return types --- backend/package-lock.json | 2 +- backend/package.json | 2 +- .../services/combinedServiceTests.ts | 8 ++++---- .../services/metricsServiceStandalone.ts | 6 ++++-- .../services/seatServiceInsertStandalone.ts | 2 +- .../src/controllers/adoption.controller.ts | 2 +- .../src/controllers/settings.controller.ts | 3 +-- backend/src/controllers/survey.controller.ts | 4 ++-- backend/src/database.ts | 7 +++---- backend/src/routes/index.ts | 2 +- backend/src/services/copilot.seats.service.ts | 6 +++--- backend/src/services/settings.service.ts | 9 +++++---- backend/tsconfig.json | 19 ++++++++++++------- 13 files changed, 39 insertions(+), 33 deletions(-) diff --git a/backend/package-lock.json b/backend/package-lock.json index 4ad4586..71e7b59 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -19,7 +19,7 @@ "express-rate-limit": "^7.4.1", "mongoose": "^8.8.4", "mysql2": "^3.11.4", - "octokit": "^4.0.2", + "octokit": "4.1", "smee-client": "^2.0.4", "update-dotenv": "^1.1.1", "why-is-node-running": "^3.2.1" diff --git a/backend/package.json b/backend/package.json index 49259cd..221d932 100644 --- a/backend/package.json +++ b/backend/package.json @@ -24,7 +24,7 @@ "express-rate-limit": "^7.4.1", "mongoose": "^8.8.4", "mysql2": "^3.11.4", - "octokit": "^4.0.2", + "octokit": "4.1", "smee-client": "^2.0.4", "update-dotenv": "^1.1.1", "why-is-node-running": "^3.2.1" diff --git a/backend/src/__tests__/services/combinedServiceTests.ts b/backend/src/__tests__/services/combinedServiceTests.ts index 1e4661a..6d573e2 100644 --- a/backend/src/__tests__/services/combinedServiceTests.ts +++ b/backend/src/__tests__/services/combinedServiceTests.ts @@ -6,10 +6,9 @@ import { generateSurveys } from '../__mock__/survey-gen/runSurveyGenerator.js'; import { MockSeatsGenerator } from '../__mock__/seats-gen/mockSeatsGenerator.js'; import { MockMetricsGenerator } from '../__mock__/metrics-gen/mockGenerator.js'; import type { MockConfig, SeatsMockConfig } from '../__mock__/types.js'; -import { MetricDailyResponseType } from '../../models/metrics.model.js'; import Database from '../../database.js'; import 'dotenv/config'; -import seatsExample from '../__mock__/seats-gen/seatsExample.json'; type: 'json'; +import fs from 'fs'; if (!process.env.MONGODB_URI) throw new Error('MONGODB_URI is not defined'); const database = new Database(process.env.MONGODB_URI); @@ -132,6 +131,7 @@ async function runSeatTest() { const queryAt = new Date(); const seats = generateExampleSeatsData(); console.log('Generated seats:', seats.length); + const seatsExample = JSON.parse(fs.readFileSync('../__mock__/seats-gen/seatsExample.json', 'utf8')); await SeatService.insertSeats(org, queryAt, seatsExample.seats); // const insertedSeats = await SeatService.getAllSeats(org); // if (!insertedSeats || insertedSeats.length === 0) { @@ -145,10 +145,10 @@ async function runSeatTest() { async function runMetricsTest() { console.log('Running Metrics Test...'); - const exampleData: MetricDailyResponseType[] = generateExampleMetricsData(); + const exampleData: any = generateExampleMetricsData(); // : MetricDailyResponseType[] console.log('Generated metrics:', exampleData.length); for (const metric of exampleData) { - await metricsService.insertMetrics("octodemo", [metric], null); + await metricsService.insertMetrics("octodemo", [metric]); } } diff --git a/backend/src/__tests__/services/metricsServiceStandalone.ts b/backend/src/__tests__/services/metricsServiceStandalone.ts index 7dc211c..7c0e57e 100644 --- a/backend/src/__tests__/services/metricsServiceStandalone.ts +++ b/backend/src/__tests__/services/metricsServiceStandalone.ts @@ -81,9 +81,11 @@ const exampleConfig: MockConfig = { repositories: ['demo/repo1', 'demo/repo2'] }; +const org = "octodemo"; + function generateExampleData() { const mockGenerator = new MockMetricsGenerator(exampleConfig); - return mockGenerator.generateMetrics(); + return mockGenerator.generateMetrics(exampleConfig); // pass config? } if (!process.env.MONGODB_URI) throw new Error('MONGODB_URI is not defined'); @@ -98,7 +100,7 @@ async function runTest() { console.log(JSON.stringify(exampleData, null, 2)); // Verify the insertion - const insertedMetrics = await metricsService.getMetrics("octodemo"); + const insertedMetrics = await metricsService.getMetrics(org); // Assertions if (!insertedMetrics || insertedMetrics.length === 0) { diff --git a/backend/src/__tests__/services/seatServiceInsertStandalone.ts b/backend/src/__tests__/services/seatServiceInsertStandalone.ts index 84eb6ea..5874e22 100644 --- a/backend/src/__tests__/services/seatServiceInsertStandalone.ts +++ b/backend/src/__tests__/services/seatServiceInsertStandalone.ts @@ -18,7 +18,7 @@ async function runTest() { //loop through each seat and if the last_activity_at is "false" print the seat object to console. - seats.seats.forEach((seat) => { + seats.seats.forEach((seat: any) => { if (seat.last_activity_at === "false") { console.log("a false last activity was found", seat); } diff --git a/backend/src/controllers/adoption.controller.ts b/backend/src/controllers/adoption.controller.ts index a7cb0c3..36d9110 100644 --- a/backend/src/controllers/adoption.controller.ts +++ b/backend/src/controllers/adoption.controller.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; import SeatsService from '../services/copilot.seats.service.js'; -import adoptionService from 'services/adoption.service.js'; +import adoptionService from '../services/adoption.service.js'; class AdoptionController { async getAdoptions(req: Request, res: Response): Promise { diff --git a/backend/src/controllers/settings.controller.ts b/backend/src/controllers/settings.controller.ts index 62d1a93..ae2c025 100644 --- a/backend/src/controllers/settings.controller.ts +++ b/backend/src/controllers/settings.controller.ts @@ -8,8 +8,7 @@ class SettingsController { if (!settings) { return res.status(404).json({ error: 'Settings not found' }); } - const settingsRsp = Object.fromEntries(settings.map(setting => [setting.dataValues.name, setting.dataValues.value])); - res.json(settingsRsp); + res.json(settings); } catch (error) { res.status(500).json(error); } diff --git a/backend/src/controllers/survey.controller.ts b/backend/src/controllers/survey.controller.ts index e76568c..57be16a 100644 --- a/backend/src/controllers/survey.controller.ts +++ b/backend/src/controllers/survey.controller.ts @@ -52,8 +52,8 @@ class SurveyController { async createSurvey(req: Request, res: Response): Promise { try { - // const Survey = mongoose.model('Survey'); - // const survey = await Survey.create(req.body); + const Survey = mongoose.model('Survey'); + const survey = await Survey.create(req.body); surveyService.createSurvey(req.body); console.log('Creating survey (controller):', survey); res.status(201).json(survey); diff --git a/backend/src/database.ts b/backend/src/database.ts index edd9d79..23b9d99 100644 --- a/backend/src/database.ts +++ b/backend/src/database.ts @@ -23,13 +23,12 @@ class Database { serverSelectionTimeoutMS: 30000, // Server selection timeout }); // this.mongoose.set('debug', false); - this.mongoose.set('toJSON', (collectionName, methodName, ...methodArgs) => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const msgMapper = (m: any) => { + mongoose.set('debug', (collectionName: string, methodName: string, ...methodArgs: unknown[]) => { + const msgMapper = (m: unknown) => { return util.inspect(m, false, 10, true) .replace(/\n/g, '').replace(/\s{2,}/g, ' '); }; - logger.debug(`\x1B[0;36mMongoose:\x1B[0m: ${collectionName}.${methodName}` + `(${methodArgs.map(msgMapper).join(', ')})`) + logger.debug(`\x1B[0;36mMongoose:\x1B[0m: ${collectionName}.${methodName}` + `(${methodArgs.map(msgMapper).join(', ')})`); }); //logger.info('Database setup completed successfully'); } catch (error) { diff --git a/backend/src/routes/index.ts b/backend/src/routes/index.ts index 53c4ecf..e8b0ea0 100644 --- a/backend/src/routes/index.ts +++ b/backend/src/routes/index.ts @@ -7,7 +7,7 @@ import SeatsController from '../controllers/seats.controller.js'; import metricsController from '../controllers/metrics.controller.js'; import teamsController from '../controllers/teams.controller.js'; import targetValuesController from '../controllers/target-values.controller.js'; -import adoptionController from 'controllers/adoption.controller.js'; +import adoptionController from '../controllers/adoption.controller.js'; const router = Router(); diff --git a/backend/src/services/copilot.seats.service.ts b/backend/src/services/copilot.seats.service.ts index 629cc21..3f9ae50 100644 --- a/backend/src/services/copilot.seats.service.ts +++ b/backend/src/services/copilot.seats.service.ts @@ -5,9 +5,9 @@ import mongoose from 'mongoose'; import { MemberActivityType, MemberType } from 'models/teams.model.js'; import fs from 'fs'; -type _Seat = NonNullable[0]; +type _Seat = any;// NonNullable[0]; export interface SeatEntry extends _Seat { - plan_type: string; + plan_type: "business" | "enterprise" | "unknown"; assignee: components['schemas']['simple-user']; } @@ -170,7 +170,7 @@ class SeatsService { precision?: 'hour' | 'day' | 'minute'; since?: string; until?: string; - } = {}): any { // Promise { + } = {}): Promise { // Promise { console.log('getMembersActivity', params); const Seats = mongoose.model('Seats'); // const Member = mongoose.model('Member'); diff --git a/backend/src/services/settings.service.ts b/backend/src/services/settings.service.ts index d6aa4fb..f9796df 100644 --- a/backend/src/services/settings.service.ts +++ b/backend/src/services/settings.service.ts @@ -37,10 +37,13 @@ class SettingsService { return this.settings; } - async getAllSettings(): Promise<{ [key: string]: string; } | null> { + async getAllSettings() { try { const Setting = mongoose.model('Settings'); - return await Setting.findOne({}); + return await Setting.find<{ + name: string; + value: string; + }>({}); } catch (error) { console.error('Failed to get all settings:', error); throw error; @@ -63,7 +66,6 @@ class SettingsService { async updateSetting(name: keyof SettingsType, value: string) { try { const Setting = mongoose.model('Settings'); - const setting = await Setting.findOneAndUpdate( { name }, { value }, @@ -72,7 +74,6 @@ class SettingsService { upsert: true, } ); - return setting.value; } catch (error) { console.error('Failed to update setting:', error); diff --git a/backend/tsconfig.json b/backend/tsconfig.json index 937aa62..48bcc32 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -14,11 +14,16 @@ "inlineSources": true, "resolveJsonModule": true }, - "include": ["src/**/*.ts"], - "exclude": ["node_modules", "**/*.spec.ts"], - -"ts-node": { - "esm": true, - "experimentalSpecifiers": true -} + "include": [ + "src/**/*.ts" + ], + "exclude": [ + "node_modules", + "src/__tests__/**", // temp + "**/*.spec.ts" + ], + "ts-node": { + "esm": true, + "experimentalSpecifiers": true + } } \ No newline at end of file