-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement getting stats from airtable for daily/weekly/yearly stats
- Loading branch information
1 parent
6cac430
commit fb0416c
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |