Skip to content

Commit

Permalink
implement getting stats from airtable for daily/weekly/yearly stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophiahooley committed Mar 26, 2024
1 parent 6cac430 commit fb0416c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 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' });
}
};
4 changes: 4 additions & 0 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { pathParameterExample } from '../controllers/TestControllers/pathParamet
import { bodyParameterExample } from '../controllers/TestControllers/bodyParameterExample';
import { retrievePassengers } from '../controllers/TestControllers/retrievePassengers';
import { createUser } from '../controllers/User.controller';
import { getDashboardStats } from '../controllers/Dashboard.controller';
import type { Express, Request, Response } from 'express';

const routes = (app: Express) => {
Expand Down Expand Up @@ -44,6 +45,9 @@ const routes = (app: Express) => {
app.post('/requests/', createFlightRequest);
app.put('/requests/:id', updateFlightRequest);

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

// 404
app.use((_: Request, res: Response) => {
res.status(404).send('404: Page not found');
Expand Down
65 changes: 65 additions & 0 deletions src/tests/Dashboard.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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(12000, () => {
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.equal('1');
console.log(res.body);
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.equal('1');
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.equal('7');
done();
});
});
});

0 comments on commit fb0416c

Please sign in to comment.