Skip to content

Commit

Permalink
Merge pull request #15 from ChangePlusPlusVandy/APP-82
Browse files Browse the repository at this point in the history
implement getting stats from airtable for daily/weekly/yearly stats
  • Loading branch information
jacoblurie29 authored Apr 12, 2024
2 parents 5522f56 + 3f57d7f commit 72fab48
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 40 deletions.
44 changes: 44 additions & 0 deletions src/controllers/Dashboard.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logger from '../util/logger';
import Airtable from 'airtable';
import type { Request, Response } from 'express';

/**
* This function gets all stats from Airtable Data table 'Data Table'
*
* @param req - the request object
* @param res - the response object
*/
export const getDashboardStats = async (req: Request, res: Response) => {
// use Joi to validate the request body
// ...

const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY || '' }).base(
'appwPsfAb6U8CV3mf'
);

try {
await base('Data Table')
.select({
view: 'Grid view',
})
.firstPage(function (err, records) {
if (err) {
return res.status(400).json({ error: 'No record found' });
}
let data = {};
records?.forEach(function (record) {
data = {
...data,
[(record.get('Name') as string) || 'Unknown']: record.get(
'Count'
) as number,
};
});

res.status(200).send(data);
});
} catch (err: any) {
logger.error(err);
return res.status(500).json({ error: 'Error fetching record' });
}
};
18 changes: 11 additions & 7 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import {
createPassenger,
updatePassenger,
} from '../controllers/Passenger.controller';
import {
createUser,
linkUserToAirtableRecord,
} from '../controllers/User.controller';
import { getDashboardStats } from '../controllers/Dashboard.controller';
import validateAuth from '../middleware/validateAuth';
import {
getAllFlightRequestsForUser,
getFlightRequestById,
getFlightLegsById,
createFlightRequest,
updateFlightRequest,
getFlightLegsById,
} from '../controllers/FlightRequest.controller';
import {
createUser,
linkUserToAirtableRecord,
} from '../controllers/User.controller';
import validateAuth from '../middleware/validateAuth';
import express from 'express';
import type { LooseAuthProp } from '@clerk/clerk-sdk-node';
import type { Request, Response } from 'express';
import type { LooseAuthProp } from '@clerk/clerk-sdk-node';

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
Expand All @@ -43,6 +44,9 @@ router.get('/passenger/:id', validateAuth, getPassengerById);
router.post('/passenger/:id', validateAuth, createPassenger);
router.put('/passenger/:id', validateAuth, updatePassenger);

/* Dashboard Controller Routes */
router.get('/dashboard/', getDashboardStats);

/* Flight Request Controller Routes */
router.get('/requests', validateAuth, getAllFlightRequestsForUser);
router.get('/requests/:id', validateAuth, getFlightRequestById);
Expand Down
78 changes: 78 additions & 0 deletions src/tests/Dashboard.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { configureServer } from '../config/server.config';
import chaiHttp from 'chai-http';
import dotenv from 'dotenv';
import chai, { expect } from 'chai';
import type { Server } from 'http';
dotenv.config();

// set up chai
chai.use(chaiHttp);
chai.should();

// set up mock server
const app = configureServer();
let server: Server;

// start mock server
before(done => {
server = app.listen(1200, () => {
done();
});
});

// close mock server
after(done => {
server.close();
done();
});

// Test case
describe('DASHBOARD dashboard/getDashboardStats', () => {
it('should return a 200 response', done => {
chai
.request(app)
.get('/dashboard')
.send({ name: 'Test flights this week' })
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body['Flights This Week']).to.be.oneOf(['0', '1', '2', '3']);
done();
});
});
it('should return a 200 response', done => {
chai
.request(app)
.get('/dashboard')
.send({ name: 'Test flights today' })
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body['Flights Today']).to.be.oneOf(['0', '1', '2', '3']);
done();
});
});
it('should return a 200 response', done => {
chai
.request(app)
.get('/dashboard')
.send({ name: 'Test flights this year' })
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body['All Total Flights']).to.be.oneOf([
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
]);
done();
});
});
});
33 changes: 0 additions & 33 deletions src/tests/FlightRequest.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ describe('GET /requests/', () => {
'2022-12-08 | In Progress | Gilchrist, Stormie | 2014-06-21'
);

// Assuming flightLegs is an array and we're testing the first flight leg for simplicity
const firstFlightLeg = firstRequest.flightLegs[0];
expect(firstFlightLeg)
.to.have.nested.property('Departure Date/Time')
.that.equals('2023-02-06');

done();
});
});
Expand Down Expand Up @@ -103,30 +97,3 @@ describe('GET /requests', () => {
});
});
});

// Test getFlightLegsById
describe('GET /requests/:id/legs', () => {
it('should return the correct passenger names, leg IDs, and Airtable record IDs for the flight legs', done => {
chai
.request(app)
.get('/requests/recp5zrIaW8EZhJtu/legs')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.an('array').that.is.not.empty;

expect(res.body[0]).to.have.property('fields').that.is.an('object').that
.is.not.empty;
const firstFlightLegFields = res.body[0].fields;
expect(firstFlightLegFields['Passenger Names']).to.equal(
'Stormie Gilchrist, Jessie Gilchrist '
);
expect(firstFlightLegFields['Leg ID']).to.equal(
'2023-02-06 | Pending | Departure SEA > JFK'
);
expect(firstFlightLegFields['AirTable Record ID']).to.equal(
'rec0uwOR2O7cvLbDA'
);
done();
});
});
});

0 comments on commit 72fab48

Please sign in to comment.